1 / 78

Intermediate Perl

Intermediate Perl. by Benjamin J. Lynch blynch@msi.umn.edu. Introduction. Perl is a powerful interpreted language that takes very little knowledge to get started. It can be used to automate many research tasks with little effort.

sharona
Download Presentation

Intermediate 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. Intermediate Perl by Benjamin J. Lynch blynch@msi.umn.edu

  2. Introduction • Perl is a powerful interpreted language that takes very little knowledge to get started. It can be used to automate many research tasks with little effort. • The greatest strength and weakness of Perl is the ability to accomplish the same task using two very different codes.

  3. Outline • Review of Perl • Variable types • Context • Operators • Control structure • Pattern Matching • Subroutines • Context • References • grep • map • modules

  4. When should I use Perl? • Perl stands for: Practical Export Report Language • Perl is most useful for: • parsing files to extract desired data • Doing almost anything you can do in a shell script • cgi scripts to generate HTML for web pages • updating or retrieving information from databases • acting as in interface between programs

  5. Programming Style • Questions you should ask: • Who else might look at the code? • Co-workers? • Complete strangers? • How often will the code be modified? • Remember your target audience • There is no substitute for comments

  6. An Interpreted Language • Perl programs are also called Perl scripts because Perl is an interpreted language. • When you execute a Perl script, the script is compiled into a set of instructions for the Perl interpreter • This set of instructions (or parse tree) is sent to the Perl interpreter • The Perl interpreter shares many similarities to the virtual machine in Java • There is no need to compile a Perl script as a separate preliminary step, making Perl scripts similar to shell scripts (at least on the exterior).

  7. A Simple Perl Script #!/usr/bin/perl print “Hello world! \n”; blynch@msi[~] % chmod +x hello.pl blynch@msi[~] % ./hello.pl Hello world! blynch@msi[~] % \n is a new line. The routine print will print the item or list of items that follows.

  8. Variable Types • Scalar • Reference (scalar pointer to another variable) • List (array) • Hash (associative array)

  9. Scalars • Examples: $var = ‘3’; $name = “Larry”; $float = 1.1235813; $sum = $a + 1.2;

  10. A scalar is a single value. $number = 1; $text = ‘Hello world!’; $a = 1.2; $b = 1.3; $sum = $a + $b; print “$sum \n”; 2.5 The scalar can be: integer 64-bit floating point string reference The way that the data is stored (integer, floating point ,…) does not need to be specified. The Perl interpreter will determine it automatically

  11. Lists • A list (or array) of values can be specified like: @number_list = (1,1,2,3,5,8,13,21); @grocery_list = (‘apples’,’chicken’,’canned soup’); • A list always starts with a @

  12. Lists (arrays) @mylist = (1,2,2,3,4,4,4); @names = (‘Larry’, ‘ Moe’); push(@names, ‘ Curly’); print @names; Larry Moe Curly Adds an item to the end of a list

  13. Lists A list (or array) of values @grocery_list = (‘apples’,’chicken’,’canned soup’); print $grocery_list[2]; canned soup Note the numbering of elements A ‘$’ is used in the print statement because of the context. We only want print to handle a single value from the array and so we use $ to denote the scalar context.

  14. Hashes (associative arrays) • A Hash is an associative array • Instead of using an integer index, a hash uses a key to access elements of the hash %lunch = (‘monday’ => ‘pizza’, ‘tuesday’ => ‘burritos’, ‘wednesday’ => ‘sandwich’, ‘thursday’ => ‘fish’); print “on Tuesday I’ll eat $lunch{‘tuesday’}”; on Tuesday I’ll eat burritos

  15. Hashes (associative arrays) • A Hash can be created with a list of key/value pairs. • Each key has one value associated with it. %hash = (‘Larry’ => 1, ‘Moe’ => 2, ‘Curly’ => 3); %hash = (‘Larry’ , 1, ‘Moe’ , 2, ‘Curly’ , 3); Either of these work to specify a hash

  16. Variable Context @number_list = (1,1,2,3,5,8,13,21); @grocery_list = (‘apples’,’chicken’,’canned soup’); print @grocery_list; appleschickencanned soup If we use the array (or list) context, the print command will print out all elements from the array.

  17. Variable Context @number_list = (1,1,2,3,5,8,13,21); @grocery_list = (‘apples’,’chicken’,’canned soup’); print $grocery_list[1]; chicken If we use the scalar context, we must specify the element we want to print from the list.

  18. Variable Context @grocery_list = (‘apples’,’chicken’,’canned soup’); $var = @grocery_list; print $var; 3 If we request a scalar from a list, the list will return it’s length.

  19. Perl Operators $mass*$height ; Multiplication $a + $b Addition $a - $b Subtraction $a / $b division $str1.$str2 Concatenate $count++ Increment $count by 1 $missing-- decrease $missing by 1 $total+= $subtotal increase $total by $subtotal $interest*= $factor set $interest to interest*$factor $string.= $more append $more to the end of $string

  20. rand Perl rand($num) returns a random, double-precision floating-point number between 0 and $num. $var = rand(4);

  21. Control structure #!/usr/bin/perl @my_grocery_list = (‘apples’,’chicken’,’canned soup’); foreach $item (@my_grocery_list){ &purchase ($item); } while ( some condition is true ){ &do_this }

  22. Control structure Two ways to if/then if ($condition) {print “It is true \n”} print “It is true \n” if $condition;

  23. Retrieving a random element from a list • @greeting = (‘Hello’,’Greetings’,’Hola’,’Howdy’) • print $greeting[rand @greeting]; • print $greeting[rand 4]; • print $greeting[2.59196266661263168]; • print $greeting[2]; • print ‘Hola’;

  24. Subroutines • Defined like this: sub my_sub_name { do something } • Used like this: &mysubroutine(variables passed) ;

  25. Subroutines • Variables passed to a subroutine enter the routine as a single list @list1 = (‘a ’,’b ’,’d ’) ; $scalar = 42 ; &mysub(@list1, $scalar) ; sub mysub{ print @_ } a b d 42

  26. Returning values from subroutines • Subroutines return whatever is returned by the return statement or else the last item evaluated in the subroutine. @list1 = (2,3) ; print &mymult(@list1) ; sub mymult{ $product = $_[0]*$_[1]; return $product; } 6

  27. Pattern Matching Perl uses a very robust pattern matching syntax The most basic pattern match looks like: $string =~ /some pattern/ $string = ‘ 1 2 three’; if ($string =~ /2/) { print “the number 2 is in the string\n” } In Perl, anything but ‘’ and 0 are considered TRUE

  28. Pattern Matching $, = “\n”; $string = “1 2 hello 2 5”; $matching = ($string =~ /\d/); print $matching 1 @matches = ($string =~ /\d/g); print @matches; 1 2 2 5 g is for global. This will allow the pattern to be matched multiple times. \d will match any single digit

  29. Pattern Matching $,=“\n”; $string = ‘1.45 1.482 1.938 other text 10.2849’; print (string = ~ /\d.\d+/g); 1.45 1.482 1.938 0.2849

  30. Pattern matching /pattern/ /(sub-expression1)(sub-expression2)/ \d number \s whitespace \S non-whitespace pattern{2} will match pattern exactly twice [character list] defined character class [abcDEF] [^a] NOT ‘b’ | OR statement - it will match pattern on either side

  31. /(bb|[^b]{2})/ This is written on a T-Shirt I own

  32. /(bb|[^b]{2})/ bb We want 2 of them New character class NOT ‘b’ OR statement

  33. Pattern Matching ------------------------------------------------ Charge Models 2 and 3 (CM2 and CM3) and Solvation Model SM5.42 GAMESSPLUS version 4.3 ------------------------------------------------ Gas-phase ------------------------------------------------ Center Atomic CM3 RLPA Lowdin Number Number Charge Charge Charge ------------------------------------------------ 1 3 .218 -1.090 -.938 Gas-phase dipole moment (Debye) ------------------------------------------------ X Y Z Total CM3 -.718 -.592 -1.748 1.980 RLPA -.327 1.122 -.840 1.440 Lowdin -.116 1.662 -.761 1.832 ------------------------------------------------

  34. Pattern Matching if (/ CM3\s+([-]?\d*\.\d+)\s*([-]?\d*\.\d+)\s*([-]?\d*\.\d+)\s*([-]?\d*\.\d+)\s*/) { $amsol[9]=$4; if (/ CM3\s+([-]?\d*\.\d+\s*){3}([-]?\d*\.\d+)\s*/) { $amsol[9]=$2; if (/ CM3\s+(-?\d*\.\d+\s*){3}(-?\d*\.\d+)\s*/) { $amsol[9]=$2; if (/ CM3(\s+\S+){3}\s+(\S+)/) { $amsol[9]=$2;

  35. Pattern Matching if (/ CM3\s+(-?\d*\.\d+\s*){3}(-?\d*\.\d+)\s*/) { $amsol[9]=$2; if (/ CM3(\s+\S+){3}\s+(\S+)/) { $amsol[9]=$2;

  36. Substitutions s/search pattern/replace/ $string = ‘words9words383words’; $string =~ s/\d+/, /g; print $string words, words, words

  37. Special Variables • $1, $2, $3, … • Holds the contents of the most recent sub-patterns matched if ($string =~ /(Larry) (Moe) Curly/){ print $2 } Moe

  38. Special Variables • $[ • Determines which index in a list is the first, the default is 0. my @mylist = (Larry, Moe, Curly); print $mylist[1]; $[ = 1; print $mylist[1]; Moe Larry

  39. Special Variables • $& • Entire pattern from most recent match

  40. Special Variables • $/ • Input record separator, default is \n undef $/; open(FILE,<input.txt); $buffer=<INFILE>; • $buffer contains the entire file

  41. Special Variables • $. • Current line number

  42. Special Variables • $, • Default separator used when a list is printed, default is ‘’ • $,=‘ ‘; will add a space between each item if you print out a list. • $\ • Default record separator, default is ‘’ • $\ = “\n”; will add a blank line after each print statement.

  43. Special Variables • $^T time the perl program was executed • $| autoflush • $$ process ID number for Perl • $0 name of perl script executed • %ENV hash containing environmental variables.

  44. Special Variables • @ARGV is a list that old all the arguments passed to the Perl script. • @_ is a list of all the variables passed to the current subroutine

  45. Special Variables $_ is a variable that hold the current topic. e.g.; while (<FILE1>){ print “line $. $_” }

  46. Special Variables $_ is a variable that hold the current topic. e.g.; while (<FILE1>){ print “line $. $_” } This is the current line number This is the current line being processed in FILE1

  47. References • A reference is a scalar • Instead of number or string, a reference holds a memory location for another variable or subroutine. $myref = \$variable; $subref= \&subroutine;

  48. Dereferencing the Reference • To retrieve the value stored in a reference, you must dereference it. $name = ‘Larry’; $ref_name = \$name; print $ref_name , “\n”; print $$ref_name, “\n”; SCALAR(0x60000000000218a0) Larry

  49. Dereferencing the Reference • Modifying a dereferenced reference to a variable is the same as modifying the variable. $name = ‘Larry’; $ref_name = \$name; $$ref_name .= ‘, Moe, and Curly’; print $$ref_name , “\n”; print $name, “\n”; Larry, Moe, and Curly Larry, Moe, and Curly

  50. Where do we want to use a reference? • References are very useful when passing lists to a subroutine. @mylist = (‘Larry’, ‘Moe’, ‘Curly’); $list_ref = \@mylist; &mysub($list_ref ); sub mysub { my $ref = $_[0]; my @list = @$ref; print $list[2], “\n”; }

More Related