1 / 76

An Introduction to Perl

An Introduction to Perl. PERL: P ractical E xtraction and R eport L anguage. Developed in 1986 by Larry Wall Perl is very portable, very friendly to the operating system Perl has gained wide distribution and is readily available.

yakov
Télécharger la présentation

An Introduction to Perl

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. An Introduction to Perl

  2. PERL: Practical Extraction and Report Language Developed in 1986 by Larry Wall Perl is very portable, very friendly to the operating system Perl has gained wide distribution and is readily available. Perl programs syntax are taken from shells scripts, awk and C programs.

  3. PERL: syntax • A bunch of statements and declarations like in a shell script (no main( ) like in C); • Each statement ends with a semi-colon (;) • #!/usr/bin/perl -w (w for warnings) at the beginning of your program. • (like sed and awk) PERL, completely parses and compiles the program into an internal format before executing any of it. • (unlike sed and awk) PERL executes sequences of statements exactly once.

  4. PERL: Features • rich collection of built-in operators / functions • regular expressions capabilities • no arbitrary limits on • array sizes • length or content of strings • comes with a symbolic debugger

  5. PERL Features (cont’) • at first used as pattern -scanning and report generating language • now used for • system administration • client-server programming • CGI programming for WWW • database access • other tasks • many modules available: examples • CGI, LWP, DBI, Tk

  6. PERL: Example: #!/usr/bin/perl -w # -w issues `warnings print “What is your favorite color? “; $color = <STDIN>; chomp($color); if ($color eq ‘blue’) { print “That is my favorite! \n”; } elsif ($color =~ /black/) { print “ I do like $color for my shoes\n”; } else { print “$color is a nice choice \n”; }

  7. Running Perl in UNIX • Explicit invocation of a program $perl options program arguments • Using the #! Directive in program $program arguments • Debugging mode $perl -d program args • Interactive debugging to test statements $perl -de 0

  8. Perl Data Structures • Three data structures • scalars • arrays • associative arrays, known as “hashes” • arrays are indexed by numbers, starting with 0 • hashes are indexed by string

  9. Perl Data structures • Scalars: • $str = “The world is round” ; #string var • $num = 134.99; #numeric val • Arrays • @students =(“Mike, “Lisa”, “John”); • print $students[0]; #print Mike • $students[3]= “Julie”; #add a new element • @students = (); #empty array

  10. Perl data structures (cont’) • Hashes • %emp =(“Julie”, “President”, “Mary”, “VP”); • print $emp {“Julie”}; #print president • print $emp{Julie} • $emp{John} = “controller”; • %emp =(); # empty hash

  11. Scalars: STRINGS and NUMBERS • Perl supports strings and numbers as the simplest kind of data. • A scalar: is either a number or a string of characters. • A scalar value can be acted upon with operators (like + or concatenation) yielding a scalar result.

  12. Perl data structuresreference: Perl data structures (MIT) • Scalar values are named with $, even when referring to a scalar that is part of an array • it works like the English word “the” $days #simple scalar value days $days[28] #the 29th element of array @days $days{‘Feb’} # the ‘Feb’ value from hash %days $#days #the last index of array @days

  13. Perl data structuresreference http://www.mit.edu/perl/perldata.htm • Entire array and array slice are denoted by ‘@’, which works like “these” or “those” @days#$days[0], $days[1],…$days[n]) @days[3,4,5]#same as @days[3..5] @days{‘a’,’c’}#same as ($days{‘a’},$days{‘b’}) Entire hashes are denoted by % %days # (key1, val1, key2, val2 …)

  14. Perl: namespace reference: Perl data structures (MIT) • Each variable type has its own namespace • you can use, with fear of conflict, same name for a scalar variable, array, hash, subroutine. Example: $foo and @foo are two different variables • Context: • scalar and list • certain operations return list values in contexts wanting a list, and scalar otherwise

  15. PERL: Operators for numbers • Addition, subtraction, multiplication , division, etc.. • For integers, a range ..operator to specify a range of integers. • Example : print 1. . 9"\n" • 1 2 3 4 5 6 7 8 9

  16. PERL: Operators for strings: • Concatenation operator "." • Example: print "hello" . "world" ; helloworld • String repetitor operator x • string x num makes as many concatenated copies of the string as indicated by num • "fred" x 3 # is "fredfredfred"

  17. PERL: Numeric and String comparison operators Comparison Numeric String • equal = = eq • not equal != ne • less than < lt • greater than > gt • less than or equal to <= le • greater than or equal to >= ge

  18. PERL:SCALAR VARIABLES • Scalar variables are like those of the shell: • A scalar variable holds a single scalar value; • Variables can be assigned any type of scalar value and Perl keeps track of the type for you. • $ is always used to denote the variable and also to expand the value of the variable

  19. PERL: Scalar variables(see Perl mit) • $var = value; = is the assignment operator • print "$var \n"; prints the value of var • $car = "buick"; • $count = 1;

  20. More on scalars • Not required to declare or initialize • pattern matching • if ($var =~ m/pattern/) {….} • if ($var !~ m/pattern/) { …} • substitution • $var1 =~ s/pattern/replacement/;

  21. FUNCTIONS ON SCALAR VARIABLES • chop($var) takes a single argument, a scalar variable, and removes the last character from the string value of that variable. • $doc = "the Perl document" • chop($doc); $doc is now "the Perl documen" • chomp($var) removes only a newline character at the end if there is one: • $a = "hello world\n"; • chomp($a); # is now "hello world" • chomp($a); # nothing happens

  22. <STDIN> as a scalar value • when <STDIN> is used where a scalar value is expected, • Perl reads the next complete text line from standard input (up to the first newline) and uses that string as the value of <STDIN> $a = <STDIN>; # get the text chomp($a); # get rid of that pesky newline same as: chomp($a = <STDIN>);

  23. PERL: ARRAYS • A list is an ordered scalar data. • An array is a variable that holds that list. • arrays are dynamically allocated • An array is denoted by an "@" sign: • example: • @arr = (1, 2 , 3, 4, 5);

  24. PERL: ARRAY OPERATORS AND FUNCTIONS • Assignment: @fred = (1, 2, 3); @barney = @fred; #copies @fred in @barney • if an array variable is assigned to a scalar variable, the number assigned is the length of the array (i.e., number of elements) • $length = @arr ;

  25. PERL: ARRAY OPERATORS AND FUNCTIONS • access a single element with index number in brackets. Note that the @ on the array name becomes a $ on the element reference • example: print $arr[0], "\n"; # prints first element of @arr which is 1

  26. ARRAYS (cont') • undef is returned when accessing an array element beyond the end of the current array. • $#arrname is used to get the index of the last element of array @arrname.

  27. ARRAY FUNCTIONS • push and pop functions • when arrays are used a stack of information, where new values are added to and removed from the right hand side of the list. • Example: push(@mylist, $newvalue); like @mylist = (@mylist, $newvalue); $oldvalue = pop(@mylist); removes last element of my list.

  28. ARRAY FUNCTIONS(cont') • shift and unshift functions • Perform similar action to push and pop on the "left" side of a list. • Example: @fred = (5, 6, 7); unshift(@fred, $a) ; like @fred = ($a, @fred); $x = shift(@fred); x gets 5, @fred is now (6, 7) • reverse function • reverses the order of the elements of its argument, returning the resulting list • @a = (1, 2, 3); • @b = reverse(@a); # means @b = (3, 2, 1);

  29. ARRAY FUNCTIONS (CONT') • sort function • returns a sorted list in ascending ASCII order without changing original list • reverse function • reverses the order of the elements of its argument, returning the resulting list @a = (1, 2, 3); @b = reverse(@a); # means @b = (3, 2, 1);

  30. ARRAY FUNCTIONS (CONT') • sort function • returns a sorted list in ascending ASCII order without changing original list • chomp function • @stuff = ("hello\n", "hi\n", "ola\n"); • chomp(@stuff); # @stuff is now ("hello", "hi", "ola")

  31. Filtering a list • Grep function may be used to select from list • @toys =qw(car racecar truck bluecar train); • @cars = grep(/car/, @toys); • print “@cars\n”; • car racecar bluecar • grep from any list • @daemons = grep /daemon/, `ps-ef`;

  32. Iterating through an array • C-style for loop for( $I = 0; $I < @myarray; $I++) { print “$myarray[$I]\n”; } • foreach loop foreach $element (@myarray) { print “$element\n”; }

  33. <STDIN> as an array • in a list context, <STDIN> returns all remaining lines up to the end of a file each line is returned as a separate element of the list. • @a = <STDIN> # read standard input in a list context.

  34. @ARGV Array • @ARGV array contains command line arguments • @ARGV does not include program name, • $0 contains program name • use in scalar context to determine argument count if (@ARGV !=2) { die: “usage: $0 infile argument\n”; }

  35. PERL: HASHES • A hash is a collection of scalar data with individual elements selected by some index. • Index values are arbitrary scalars called keys; They are used to retrieve values from the array. • Elements of a hash have no special order • A hash is denoted by "%" sign • Elements of a hash are referenced by $hashname{key}

  36. HASH: Examples • Example: %carcolor = ("buick", "yellow"); • $mycolor = $carcolor{ "buick"}; • $mycolor is now "yellow" • %copy = %original; # copy from %original to %copy

  37. More Hash examples • Using the => operator • %carcolor = { buick => ‘yellow’ ge => ‘red’ nissan => ‘blue’ }

  38. HASH FUNCTIONS • keys functions: • keys(%hashname) yields a list of the current keys in the hash %hashname. • Example: keys(%hashname) = keys %hashname; # once for each key of % fred foreach $key (keys (%fred)) { print "at $key we have $fred{$key} \n"; # show key and value

  39. Hash Keys • Order of keys is unpredictable • special %ENV hash stores environment variables • print $ENV{“SHELL”} • SHELL =/bin/csh

  40. HASH FUNCTIONS (CONT') • values function: • values(%hashname) yields a list of the current values of %hashname in the same order as keys returned by keys(%hashname) • %lastname = ("barney", "flinstone", "gerry", "smith"); • @lastname = values(%lastname); #grab the values

  41. HASH FUNCTIONS (each) • each function: • each(%hashname) returns a key-value pair as a two element list. • Used to iterate over an entire hash (examining every element of ). Example: while (($first, $last)) = each(%lastname)) { print "the last name of $first is $last\n"; }

  42. Hash function: delete • removes hash elements, takes a hash reference as argument • delete $lastname{"barney"}; #lastname has only one key-value pair now.

  43. Checking hash keys or values • Checking for a true/false value if ($emp{John}) { …} • checking for an exsiting key if (exists $emp{john}) {…} • checking for an existing key and a defined value if (defined $emp{John}) {..}

  44. Checking hash keys or values (cont’) • Undefining a value undef $emp{john}; • deleting a key-value pair from hash delete $emp{john} • checking to see if hash contains anything if (%emp) {..}

  45. CONTROL STRUCTURES • Perl supports "if", "for" while" similar than those in C. • "foreach" constructs is from the C shell • foreach example: • If the list we are iterating over is made of real variables rather than some functions returning a list value, then the variable being used for iteration is in fact an alias for each variable in the list instead of being a merely copy of the values

  46. Foreach example • Example: @numbers = ( 3, 5, 7, 9) foreach $one (@numbers ) { $one*=3; } #@numbers is now (9,15,21,27)

  47. BASIC I/O • Input from STDIN • Perl uses the variable $_ to contain the line read from STDIN. • $a = <STDIN>; #reads the next line • @a = <STDIN>; # reads all lines until control ^D • typically while (defined ($line = <STDIN>) { # process $line here } when no more lines read. <STDIN> returns undef.

  48. using the diamond operator <> • <> operates like <STDIN>, but gets data from file or files • specified on the command line that invoked the PERL program. • <> looks at the @ARGV array #!/usr/bin/perl while (<>) { print $_; }

  49. Output to STDOUT • print for normal output • printf for formatted output

  50. REGULAR EXPRESSIONS • PERL supports the same regular expressions as in SED. • =~ match operator • It takes a regular expression operator on the right side and changes the target of the operator to some value. • The target of the =~ operator can be any expression that yields some scalar string value. Example: if (<STDIN> = ~ /^[yY]/) { print" what can I do for you? ";

More Related