1 / 124

Perl Tutorial

Perl Tutorial. 林光龍. The Goal of this Course. To know what is Perl programming language To know how to design program using Perl. 2. Course Outline. Perl Introduction How to work with perl on your PC Fundamental programming skill Advanced programming skill. 3. Perl Introduction.

reuben
Download Presentation

Perl Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Tutorial 林光龍

  2. The Goal of this Course • To know what is Perl programming language • To know how to design program using Perl 2

  3. Course Outline • Perl Introduction • How to work with perl on your PC • Fundamental programming skill • Advanced programming skill 3

  4. Perl Introduction • What is Perl? • Why Perl? 4

  5. What is Perl? • Perl is acronym for Practical Extraction and Report Language • High level scripting language • Originally developed by Larry Wall in 1987 for UNIX platform (it has been developed ever since as Open Source project) • Now Perl available for almost all platform • It has syntax like C and shell scripting • Useful to write CGI scripts (Web/Internet applications), database applications, network programming, biological data analysis and automating system administration task etc. 5

  6. Why Perl? • Easy to use and efficient scripting language • It is portable • It is free and runs on wide range of platform (UNIX/Linux/BSD/Windows etc) • Ready to use libraries available (called as modules) for different tasks such as network or database programming • Read Ten Perl Myths by Simon Cozens 6

  7. How to work with perl on your PC • Unix-like Platform • Windows Platform 7

  8. Unix-like Platform • Find out the pathname of the Perl file which would be executed in the your environment with which command $ which perl /usr/bin/perl 8

  9. Windows Platform (1/2) • To download and install the latest ActivePerl (ActivePerl-5.10.0.1005-MSWin32-x86-290470.msi) from http://www.activestate.com/activeperl/ • To download and install the PSPad, a freeware programmer’s editor, from http://www.pspad.com/en/

  10. Windows Platform (1/2)

  11. Fundamental Programming Skill • Your First Perl Program • Comments • Literal & Variable • String • Interpolated String • Non-interpolated String • Operation • Statement • Control Statement 11

  12. Advanced Programming Skill Special Variable Subroutine Modules (CPAN) Regular Expression File Operation 12

  13. Your First Perl Program (1/4) #!/usr/bin/perl # Your first Perl Program to display message # Program to display knowledge is power on screen print "Knowledge is power \n"; 13

  14. Your First Perl Program (2/4)

  15. Your First Perl Program (3/4) #!/usr/bin/perl use strict; # important pragma use warnings; # another important pragma print "What is your username? "; # print out the question my $username; # "declare" the variable $username = <STDIN>; # ask for the username chomp($username); # remove "new line" print "Hello, $username.\n"; # print out the greeting

  16. Your First Perl Program (4/4)

  17. Comments (1/2) • The comments are advantageous in the sense that they make the programmer feel convenient to grasp the logic of the program. • The comments are ignored by the Perl interpreter, they are included in the program for the convenience of the programmer to understand the code

  18. Comments(2/2) • Comment’s notations: • Single line comment: opening each line with # • Multi-line comments: begin with =head and ending with =cut # Everything after the # is ignored =head It is very important to place comments into your Perl programs. Comments will enable you to figure out the intent behind the mechanics of your program. =cut print "Knowledge is power \n";

  19. Literal • Aliteralis a value that is represented "as is" or hard-coded in your source code. • 45.5 refers to forty-five and a half • Perl uses four types of literals: • Numbers • Strings • Arrays • Associative Arrays

  20. Numeric Literals (Integer Value)

  21. Numeric Literals (Float Value) • Scientific notation print 0.000034; print "\n"; print 3.4e-5; print "\n"; print 3.4e+5; print "\n";

  22. String Literal • String Literals are groups of characters surrounded by quotes. • In Perl you can use single quotes ('), double quotes("), and back quotes (`). #!/usr/bin/perl use strict; use warnings; print 'Date\n'; print "Date\n"; print `Date`;

  23. 2 3 2 3 Variables • literals - values that don't change while your program runs because you represent them in your source code exactly as they should be used • variable - a name for a container that holds one or more values. Thename of the variable stays the same throughout the program, but thevalueorvalues contained in that variable typically change repeatedly throughout the execution of the program. $a $a $a = 2; $a = 2; $a = 3;

  24. Variable Types (1/2)

  25. Variable Types (2/2) • Perl is a dynamically typed language • There is no integer, string, character, or pointer type in Perl. Strings, characters, and numbers are all treated roughly interchangeably because they can be converted from one to the other internally by the Perl interpreter during runtime. • All of the numeric data types in C, pointers, and even strings, all correspond to a single data-type in Perl: the scalar. • Unlike C, which is statically typed, Perl is dynamically typed. What this means is that the datatype of any particular scalar variable is not known at compile-time.

  26. Pragma (1/2) • Perl tends to be a pretty permissive language. But maybe you want Perl to impose a little discipline; that can be arranged with the use strict pragma. • A pragma is a hint to a compiler, telling it something about the code. • The 'use strict' and 'use warnings' pragma tells Perl's internal compiler that it should enforce some good programming rules for the rest of this block or source file.

  27. Pragma (1/2) $bamm_bamm = 3; # Perl creates that variable automatically $bammbamm += 1; # Oops! print $bamm_bamm; use strict; # Enforce some good programming rules use warnings; # To warn as much as possible when you write code that might be questionable my $bamm_bamm = 3; # New lexical variable $bammbamm += 1; # No such variable: Compile time errors! print $bamm_bamm; $something = "Hello World"; # Create a variable out of thin air print $somehting; # automatically create a variable & be initialized to an empty string. use strict; use warnings; my $something = "Hello World"; # New lexical variable print $somehting; # No such variable: Compile time errors!

  28. 23 "Perl" $a $b Scalar Variables • Scalar variables are used to track single pieces of information. • All scalars begin with a $ # Assign a value of 23 to a variable called $numberOfRooms. $numberOfRooms = 23; #Add 5 to the $numberOfRooms variable. $numberOfRooms = $numberOfRooms + 5; # Assign a value of Perl by Example to a variable called $bookTitle. $bookTitle = "Perl by Example";

  29. $a $b 3 2 1 1 2 $a[2] $a[1] @a $a[0] Array Variables • Arrays are ordered, integer-indexed collections of any object. • Array indexing starts at 0. • Array variable names always begin with a @ character. ($a, $b) = (1, 2); @a = (1, 2, 3);

  30. $a[1] $a[0] @a @a $a2 $a1 @a = (1, 2, 3); ($a1, $a2) = @a; 2 3 1 100 Cat 2 1 $a[2] $a[1] @a $a[0] @a = (100, "Cat"); @a = ();

  31. $a4 $a3 $a2 $a1 2 1 1 2 3 1 2 3 3 $a[2] $a[2] $a[-3] $a[-2] $a[-1] $a[1] $a[1] @a @a $a[0] $a[0] @a = (1, 2, 3); ($a1, $a2, $a3, $a4) = @a; @a = (1, 2, 3);

  32. $b $b 2 2 1 3 2 3 3 1 $a[2] $a[2] $a[1] $a[1] @a @a $a[0] $a[0] @a = (1, 2, 3); $b = @a; @a = (1, 2, 3); $b = $#a;

  33. $a[10] $a[2] $a[2] $a[1] $a[1] @a @a $a[0] $a[0] $b 8 3 2 1 11 7 6 11 … @a = (1, 2, 3, 4); $a[10] = 11; $b = @a; print $b; @a = (1, 2, 3, 4, 5); @a = (6, 7, 8);

  34. @a = (); print "Number of elements:"; print @a . "\n"; $a[5] = 10; print "Number of elements:"; print @a . "\n"; @a = (1, 2, 3); print "Number of elements:"; print @a . "\n"; @emptyArray = (); @numberArray = (12, 014, 0x0c, 34.34, 23.3E-3); @stringArray = ("This", "is", 'an', "array", 'of', "strings"); @mixedArray = ("This", 30, "is", 'a', "mixed array", 'of', 0x08, "items"); print "Here is an empty array:" . @emptyArray . "<-- Nothing there!\n"; print @numberArray; print "\n"; print @stringArray; print "\n"; print @mixedArray; print "\n";

  35. @smallArrayOne = (5..10); @smallArrayTwo = (1..5); @largeArray = (@smallArrayOne, @smallArrayTwo); print @largeArray; #Create an array with five elements. @array = (1..5); #Print the array. print @array; print "\n"; #Print each element of the array. print $array[0]; print "\n"; print $array[1]; print "\n"; print $array[2]; print "\n"; print $array[3]; print "\n"; print $array[4]; print "\n"; $index = 2; @array = (1..5); print $array[$index]; print "\n";

  36. Interpolated String (1/5) • In Perl, any string that is built with double quotes will be interpolated. That is, any variable or escaped char that appears within the string will be replaced with the value of that variable.

  37. Interpolated String (2/5) #!/usr/bin/perl use strict; use warnings; my $apples = 4; print "I have $apples apples\n"; #!/usr/bin/perl use strict; use warnings; my @friends = ('Margaret', 'Richard', 'Carolyn', 'Rohan', 'Cathy', 'Yukiko'); print "Friends: @friends\n";

  38. Interpolated String (3/5) • The function qq() works just like double quotes, but makes it easy to put double quotes in your string: #!/usr/bin/perl use strict; use warnings; my $apples = 4; print qq(I "have" $apples apples\n);

  39. Interpolated String (4/5) • Perl offers a convenient way of printing multiple lines of output through an interesting feature known as a "here" document. • A here document works like this: • The first line of your command will include the two characters<< followed by a "special" identifier string, followed by a semi-colon. • Next, just enter all of the lines of output that you want to print. • When you are ready to terminate the output, put your special identifier string on a line by itself to end the output.

  40. Interpolated String (5/5) my $a = "Hello"; my $str = "There you go."; my $true = "False"; print <<"END"; The value of \$a is: "$a" The value of \$str is: "$str" The value of true is: "$true" END

  41. Escape Character • There are some characters that can't be written directly in a string. The backslash ('\') character precedes any of these special characters. • For example, if a string contains a double quotes, put a backslash ('\') in front of each internal double quote, eg "abc\"def\"ghi".

  42. Table of Escape Sequences • Escape sequence is a character constant of special characters.

  43. Non-interpolated String (1/3) • Single quoted strings do not interpolate variables or most escaped values, such as \n, (however \' is the exception). Single quotes are helpful when you want to include a $ or a % or any other special character in your output. #!/usr/bin/perl use strict; use warnings; print 'Those strawberries cost $2.50';

  44. Non-interpolated String (2/3) • The function q() works the same as single quotes, except it makes it easier to include a single quote in your data. #!/usr/bin/perl use strict; use warnings; print q(Bob's strawberries cost $2.50);

  45. Non-interpolated String (3/3) • Variables in here documents, where the end token is surrounded with single quotes, are not interpolated. my $apples = 4; my $oranges = 7; my $pears = 3; print <<'EOT'; My fruit list: $apples apples $oranges oranges $pears pears EOT

  46. Back-Quoted Strings • Perl uses back-quoted strings to execute system commands. When Perl sees a back-quoted string, it passes the contents to Windows, UNIX, or whatever operating system you are using. print `dir *.txt`;

  47. Type of Operation • Bitwise operation • Arithmetic operation • String operation • Range operation • Assignment operation • Comparison operation • Logical operation • File test operation

  48. Operators of Bitwise Operation

  49. Operators of Arithmetic Operation

  50. Operators of Increment/Decrement

More Related