introduction to perl
play

Introduction to Perl Scott Hazelhurst - PowerPoint PPT Presentation

Introduction to Perl Introduction to Perl Scott Hazelhurst http://www.bioinf.wits.ac.za/~scott/perl.pdf August 2013 Introduction to Perl Introduction and Motivation Introduction and Motivation Practical Extraction and Report Language


  1. Introduction to Perl File operations Files in general ◮ open(DATAF, "figs.dat") read ◮ open(DATAF, "<figs.dat") read ◮ open(DATAF, ">figs.dat") write ◮ open(DATAF, ">>figs.dat") append ◮ open(DATAF, "| output-pipe-cmd"); set up output filter ◮ open(DATAF, "input-pipe-cmd| "); set up input filter

  2. Introduction to Perl File operations Files in general ◮ open(DATAF, "figs.dat") read ◮ open(DATAF, "<figs.dat") read ◮ open(DATAF, ">figs.dat") write ◮ open(DATAF, ">>figs.dat") append ◮ open(DATAF, "| output-pipe-cmd"); set up output filter ◮ open(DATAF, "input-pipe-cmd| "); set up input filter To print to a file that is open for writing, use the related file handle in the print statement.

  3. Introduction to Perl File operations Input from files Input from files Suppose that INP is a file handle. A reference to <INP> does the following: ◮ Returns the next line of input; ◮ Consumes the input – in the case of a file, advances the file pointer to the next line of the file. ◮ NB: end-of-line marker read in.

  4. Introduction to Perl File operations Input from files for($i=0; $i <3; $i++) { $x=<INP >; print "**$x##" } Assuming that file contains apple , banana , cherry .

  5. Introduction to Perl File operations Input from files for($i=0; $i <3; $i++) { $x=<INP >; print "**$x##" } Assuming that file contains apple , banana , cherry . **apple ##**banana ##**cherry ##

  6. Introduction to Perl File operations Input from files To add up the numbers in a file with three numbers. open(INP ,"nums.txt"); $x1 = <INP >; $x2 = <INP >; $x3 = <INP >; print "Answer is " . $x1 + $x2 + $x3; or open(INP ,"nums.txt"); print "Answer is " . (<INP > + <INP > + <INP >);

  7. Introduction to Perl File operations Input from files What’s the difference between print "Answer is " . (<INP > + <INP > + <INP >); and print "Answer is . (<INP > + <INP > + <INP >) ";

  8. Introduction to Perl File operations Input from files Add up odd numbers in file print "Enter the file name: "; $namef = <STDIN >; open(DATAF , $namef ); while($x = <DATAF >) { if ($x % 2) { $sum = $sum + $x; } } close(DATAF ); print "The sum is $sum\n";

  9. Introduction to Perl File operations Input from files A common Perl idiom is open(DATAF ,$namef) or die "Can’t open

  10. Introduction to Perl File operations Implicit operands Implicit Operands Perl uses implicit operands extensively. ◮ We are told that this is a feature. ◮ The main culprit: $_ or $ARG Set (among other places) by reference to a file handle. Many operators use $ARG if not given explicit operator explicitly. ◮ The following prints out the contents of a file: while (<DATAF>) {print};

  11. Introduction to Perl File operations Implicit operands Exercise Write a Perl program that reads integers from a file called nums.dat and counts how many numbers are ◮ less than zero ◮ between zero and 10 ◮ greater than or equal to 11 If there is no such file, your program should print an error message and halt.

  12. Introduction to Perl Arrays/lists Arrays/lists Array variables are prefixed by @ my @days; @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "F ◮ Arrays indexed from 0

  13. Introduction to Perl Arrays/lists Arrays/lists Array variables are prefixed by @ my @days; @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "F ◮ Arrays indexed from 0 ◮ Individual element are scalars: so $days[0]

  14. Introduction to Perl Arrays/lists Common to use foreach @days = ("Sun", "Mon", "Tue", "Wed", " foreach $d (@days) { print "Day $d\n"; }

  15. Introduction to Perl Arrays/lists Quote words Short hand for arrays of words @days = qw(Sun Mon Tue Wed Thu Fri Sat

  16. Introduction to Perl Arrays/lists @ARGV – program’s arguments @ARGV : Predefined variable array ◮ Contains the program’s arguments – values passed by the caller. ◮ If the program is run as follows: ./example.pl apple pear 1 2 3 array @ARGV contains those values ◮ $ARGV[0] is apple ; ◮ $ARGV[3] is 2 ;

  17. Introduction to Perl Arrays/lists Array semantics Expressions are evaluated in scalar or list context. ◮ Similar or identical expressions can evaluate to different things in different contexts.

  18. Introduction to Perl Arrays/lists Array semantics Expressions are evaluated in scalar or list context. ◮ Similar or identical expressions can evaluate to different things in different contexts. Context determined by operation: ◮ LHS of assignment determines context ◮ Operators or functions may determine context

  19. Introduction to Perl Arrays/lists Array context ◮ print @days yields SunMonTueWedThuFriSat ◮ @weekdays = @days[1..5] array $weekdays set to Mon .. Fri ◮ @days[1] is a one element array

  20. Introduction to Perl Arrays/lists Array context ◮ print @days yields SunMonTueWedThuFriSat ◮ @weekdays = @days[1..5] array $weekdays set to Mon .. Fri ◮ @days[1] is a one element array Scalar context ◮ $numdays = @days; sets $numdays to 7

  21. Introduction to Perl Arrays/lists Array context ◮ print @days yields SunMonTueWedThuFriSat ◮ @weekdays = @days[1..5] array $weekdays set to Mon .. Fri ◮ @days[1] is a one element array Scalar context ◮ $numdays = @days; sets $numdays to 7 ◮ $y = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat") sets $y to Sat

  22. Introduction to Perl Arrays/lists Array context ◮ print @days yields SunMonTueWedThuFriSat ◮ @weekdays = @days[1..5] array $weekdays set to Mon .. Fri ◮ @days[1] is a one element array Scalar context ◮ $numdays = @days; sets $numdays to 7 ◮ $y = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat") sets $y to Sat ◮ $weekdays = $days[1..5] sets $weekdays to 5 ◮ $#days is 6 – the highest index of @days

  23. Introduction to Perl Arrays/lists Useful functions ◮ push : adds something to the right of the array ◮ pop : removes the rightmost element of the array. ◮ shift/unshift ◮ sort: returns a sorted list by default string order ascending, but can be changed.

  24. Introduction to Perl Arrays/lists Arrays are inherently 1D ◮ Need references to handle multi-dimensional arrays

  25. Introduction to Perl Arrays/lists Asides Asides on strings ◮ splitting @x = split ";", "1;2;3"; foreach $s (@x) { print "$s\n"; } ◮ chop , chomp . ◮ see regexes later

  26. Introduction to Perl Hash tables Hash tables Unordered set of scalars which allows fast information retrieval: ◮ Elements accessed/indexed by a string value associated with it ◮ Variables prefixed by a %, e.g. %currency ◮ Individual elements are scalar: $currency["South Africa"]

  27. Introduction to Perl Hash tables $currency["South Africa"]="ZAR"; $currency["Britain"] = "GBP";

  28. Introduction to Perl Hash tables Example %day2num = ("Sun",0, "Mon",1, "Tue",2, "Wed",3, "Thu",4, "Fri",5, "Sat",6) Better is %day2num= ("Sun"=>0, "Mon"=>1, "Tue"=>2, "Wed"=>3, "Thu"=>4, "Fri"=>5, "Sat"=>6) Referred to as $day2num{"Wed"}

  29. Introduction to Perl Hash tables Useful hash operations ◮ keys returns list of keys in hash table. Order non-deterministic.

  30. Introduction to Perl Hash tables Useful hash operations ◮ keys returns list of keys in hash table. Order non-deterministic. ◮ values returns list values stored in the hash table. Order of no significance.

  31. Introduction to Perl Hash tables Useful hash operations ◮ keys returns list of keys in hash table. Order non-deterministic. ◮ values returns list values stored in the hash table. Order of no significance. ◮ sort keys %table lists the keys in lexicographic order

  32. Introduction to Perl Hash tables Example # read in from file while ($name = <INP>) { $reg = <INP>; $carreg{$name} = $reg; } # print out in order of name foreach $n (sort keys %carreg) { print "Car reg of $n is $carreg{$n} \n"; }

  33. Introduction to Perl Hash tables Can also tell sort how to sort: ◮ sort {$a <=> $b} list sorts the list in numeric order. ◮ sort {$table{$a} cmp $table{$b}} keys %table sorts the keys so that corresponding hash table elements are in order

  34. Introduction to Perl Hash tables #print out in order of car reg foreach $n (sort {$carreg{$a} cmp $carreg{$b}} keys %carreg) { print "$carreg{$n} owned by $n\n"; }

  35. Introduction to Perl Procedures Procedures Perl has procedures but its parameter passing mechanism is poor. ◮ Suppose we have a procedure plus that adds up two numbers. Called: $x = &plus($a, $b); where $a and $b are the parameters. Call by value semantics

  36. Introduction to Perl Procedures Declaring a subroutine To declare a subroutine: sub NAME BLOCK e.g. sub printhead { print "Name Age Number Balance\n"; } ... &printhead();

  37. Introduction to Perl Procedures Parameters Procedures have one formal parameter: @_ ◮ The values of the actual parameter are given to the formal parameter (call-by-value). ◮ @_ : a list local to the procedure. sub plus { sub plus { $x = $_[0]; my ($x,$y) = @_; $y = $_[1]; return $x+$y; return $x + $y; } }

  38. Introduction to Perl Procedures A common idiom uses shift sub plus { $x = shift @_; $y = shift @_; return $x + $y; }

  39. Introduction to Perl Procedures Example A procedure to add up a list: sub addlist {$sum = 0; foreach $num (@_) {$sum += $num}; return $sum;} sub proclist { my ($f1,$f2,@nums) = @_; foreach $n (@nums) {$sum = $sum+$n; } return ($f1*$f2*$sum); } $x = &addlist(1,2,3,5,6,9); $y = &proclist(3,4,1,1,0,2);

  40. Introduction to Perl Procedures The following does not work (s sub dotprod { (@a,@b) = @_; ... ... } @x = (1,2,3); @y = (4,5,6); &dotprod(@x,@y);

  41. Introduction to Perl Procedures ◮ Formal parameter is an unstructured list (possible danger in passing multiple lists as parameters) ◮ Forward declarations: sub addlist; Telling Perl that addlist is a procedure which will be declared elsewhere. ◮ Can have anonymous procedures that get assigned to variables. ◮ Weakness in parameter system can be overcome by using references. ◮ Default: all variables are global wherever defined. This is even recognised by the Perl community to be a Bad Thing Declare variables local inside procedures. Best way : prefix first use of variable in a procedure with with my. Lexical scoping.

  42. Introduction to Perl References References References are mechanisms for a variable to refer to something, e.g. (1) another variable; (2) a piece of data; (3); a function Symbolic references Can turn a string into a variable $x = 123; $y = "x"; $$y = 5; print $x;

  43. Introduction to Perl References Hard references Can use the \ operator to dereference a variable, and -> to dereference. @a = (10,20,30,40,50); $x = \@a; for $e ( @{$x} ) { print "$e\n"; } print $x->[0];

  44. Introduction to Perl References Call by reference sub count { my ($fname , $rcount) = @_; chomp $fname; unless (-T $fname) { return; } open(FINP ,$fname ); while (<FINP >) { $$rcount ++ } close(FINP ); }

  45. Introduction to Perl References @files = ‘ls -1 $ARGV [0]‘; $n = 0; for $f (@files) { &count($f ,\$n); } print "Total number of lines is $n\n";

  46. Introduction to Perl References Passing multiple arrays sub dotprod { ($a,$b)=@_; .... } @x = (1,2,3); @y = (4,5,6); &dotprod(\@x,\@y); Note that only scalars are passed.

  47. Introduction to Perl References Refs to anonymous arrays and hashes Square brackets creates an array – returns a reference $a = [ 10, 20, 30, 40]; ◮ Error: @a = [ 10, 20, 30, 40]; Curly braces creates a hash – returns a reference $day = {"sun"=>0, "mon"=>1, "tues"=> .....}

  48. Introduction to Perl Modules Modules A module is a collection of code, data structures that can be used as a library ◮ Often see it in OO programming Magic word is use use IO; ◮ To access something is in a module use :: . So, module::thing ◮ Modules can be nested.

  49. Introduction to Perl Modules Example use IO:: Compress :: Bzip2; IO:: Compress :: Bzip2 :: bzip2 ("do.pl", "do.pl.bz

  50. Introduction to Perl Modules Example use IO:: Compress :: Bzip2; IO:: Compress :: Bzip2 :: bzip2 ("do.pl", "do.pl.bz Can also import specific things use IO:: Compress :: Bzip2 qw(bzip2 $Bzip2Error ); unless (bzip2 ("do.plx", "do.pl.bz2")) { warn "Compression failed returning : $Bzip2 }

  51. Introduction to Perl Objects Objects Data structures which ◮ contain data, know functions that can apply to them; ◮ anonymous ◮ accessed through references ◮ typically organised in classes

  52. Introduction to Perl Objects use Bio::Seq; $seqio = Bio::SeqIO ->new(’-format ’ => ’embl ’ , -file $seqobj = $seqio ->next_seq (); $seqstr = $seqobj ->seq (); $seqstr = $seqobj ->subseq (10 ,50); @features = $seqobj -> get_SeqFeatures (); foreach my $feat ( @features ) { print "Feature ",$feat ->primary_tag , " starts ",$feat ->start , " ends ", $feat ->end ," strand ",$feat ->strand ,"\n"; }

  53. Introduction to Perl Regular expressions Regular expressions, matching, and more Perl’s regular expression support powerful ◮ concise way of describing a set of strings. Typical: a command uses a regular expression to process some argument. ◮ Example: split uses a regex to split a string $line = "Gauteng;Johannesburg GP 7"; @info = split("[ ;]", $line); ($prov, $capital,$reg, $pop) = split("[ ;] ", $line);

  54. Introduction to Perl Regular expressions Specifying Regular Expressions ◮ Most characters stand for themselves: F Fred 6312

  55. Introduction to Perl Regular expressions Specifying Regular Expressions ◮ Most characters stand for themselves: F Fred 6312 ◮ \ | ( ) [ { ^ $ * + ? . are metacharacters (have special meaning) ◮ escape with a backslash for the chars: Fred stands for the string (Fred)

  56. Introduction to Perl Regular expressions Specifying Regular Expressions ◮ Most characters stand for themselves: F Fred 6312 ◮ \ | ( ) [ { ^ $ * + ? . are metacharacters (have special meaning) ◮ escape with a backslash for the chars: Fred stands for the string (Fred) ◮ To group things together, use parentheses.

  57. Introduction to Perl Regular expressions Specifying Regular Expressions ◮ Most characters stand for themselves: F Fred 6312 ◮ \ | ( ) [ { ^ $ * + ? . are metacharacters (have special meaning) ◮ escape with a backslash for the chars: Fred stands for the string (Fred) ◮ To group things together, use parentheses. ◮ To specify alternatives, use | (green|red) apples stands for green apples or red apples

  58. Introduction to Perl Regular expressions Specifying Regular Expressions ◮ Most characters stand for themselves: F Fred 6312 ◮ \ | ( ) [ { ^ $ * + ? . are metacharacters (have special meaning) ◮ escape with a backslash for the chars: Fred stands for the string (Fred) ◮ To group things together, use parentheses. ◮ To specify alternatives, use | (green|red) apples stands for green apples or red apples

  59. Introduction to Perl Regular expressions Specifying Regular Expressions ◮ A list of characters in square brackets matches any of the characters. ◮ [YyNn] matches any of an upper or lower case “y” or “n”. ◮ [A-Za-z0-9] is all the alphanumeric characters

  60. Introduction to Perl Regular expressions ◮ \n new line; \t tab; \s a whitespace;

  61. Introduction to Perl Regular expressions ◮ \n new line; \t tab; \s a whitespace; ◮ \d digit; \D non-digit;

  62. Introduction to Perl Regular expressions ◮ \n new line; \t tab; \s a whitespace; ◮ \d digit; \D non-digit; ◮ \w a word charater, \W a non-word character

  63. Introduction to Perl Regular expressions ◮ \n new line; \t tab; \s a whitespace; ◮ \d digit; \D non-digit; ◮ \w a word charater, \W a non-word character ◮ . anything but a \n

Recommend


More recommend