1 / 125

Perl

Perl. Introduction. Why Perl?. Widely used scripting language Powerful text manipulation capabilities Relatively easy to use Has a wide range of libraries available Fast Good support for file and process operations. Less suiteable for:. Building large and complex applications

jhardwick
Télécharger la présentation

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

  2. Why Perl? • Widely used scripting language • Powerful text manipulation capabilities • Relatively easy to use • Has a wide range of libraries available • Fast • Good support for file and process operations

  3. Less suiteable for: • Building large and complex applications • Java, C\C++, C# • Applications with a GUI • Java, C\C++, C# • High performance/memory efficient applications • Java, C\C++, C#, Fortran • Statistics • R

  4. Learning to script Knowledge + Skills

  5. Exercise Determine the percentage GC-content of the human chromosome 22

  6. open file read lines per line: skip if header line count Cs and Gs count all nucleotides report percentage Cs and Gs

  7. Hello World

  8. Hello World…. Simple line of Perl code: print "Hello World"; Run from a terminal: perl -e 'print "Hello World";' Now try this and notice the difference: perl -e 'print "Hello World\n";'

  9. \n “backslash-n” newline character 'Enter'key

  10. \t “backslash-t” 'Tab' key

  11. Hello World (cont) To create a text file with this line of Perl code: echo 'print "Hello World\n";' > HelloWorld.pl perl HelloWorld.pl In the terminal window, type kate HelloWorld.pl and then hit the enter key. Now you can edit the Perl code.

  12. Pythagoras' theorem a2 + b2 = c2 32 + 42 = 52

  13. Pythagoras.pl $a = 3; $b = 4; $a2 = $a * $a; $b2 = $b * $b; $c2 = $a2 + $b2; $c = sqrt($c2); print $c;

  14. $a a single value or scalarvariable starts with a $ followed by its name

  15. Pythagoras.pl $a = 3; $b = 4; $a2 = $a * $a; $b2 = $b * $b; $c2 = $a2 + $b2; $c = sqrt($c2); print $c;

  16. 5

  17. Perl scripts Add these lines at the top of each Perl script: #!/usr/bin/perl # author: # description: use strict; use warnings;

  18. perl Pythagoras.pl Global symbol "$a2" requires explicit package name at Pythagoras.pl line 8. Global symbol "$b2" requires explicit package name at Pythagoras.pl line 9. Global symbol "$c2" requires explicit package name at Pythagoras.pl line 10. Global symbol "$a2" requires explicit package name at Pythagoras.pl line 10. Global symbol "$b2" requires explicit package name at Pythagoras.pl line 10. Global symbol "$c" requires explicit package name at Pythagoras.pl line 11. Global symbol "$c2" requires explicit package name at Pythagoras.pl line 11. Global symbol "$c" requires explicit package name at Pythagoras.pl line 12. Execution of Pythagoras.pl aborted due to compilation errors.

  19. Pythagoras.pl $a = 3; $b = 4; $a2 = $a * $a; $b2 = $b * $b; $c2 = $a2 + $b2; $c = sqrt($c2); print $c;

  20. Pythagoras.pl my $a = 3; my $b = 4; my $a2 = $a * $a; my $b2 = $b * $b; my $c2 = $a2 + $b2; my $c = sqrt($c2); print $c;

  21. my The first time a variable appears in the script, it should be claimed using ‘my’. Only the first time...

  22. Pythagoras.pl my($a,$b,$c,$a2,$b2,$c2); $a = 3; $b = 4; $a2 = $a * $a; $b2 = $b * $b; $c2 = $a2 + $b2; $c = sqrt($c2); print $c;

  23. Pythagoras.pl $a = 3; $b = 4; $a2 = $a * $a; $b2 = $b * $b; $c2 = $a3 + $b2; $c = sqrt($c2); print $c;

  24. 4

  25. Pythagoras.pl $a = 3; $b = 4; $a2 = $a * $a; $b2 = $b * $b; $c2 = $a3 + $b2; $c = sqrt($c2); print $c;

  26. Pythagoras.pl my $a = 3; my $b = 4; my $a2 = $a * $a; my $b2 = $b * $b; my $c2 = $a3 + $b2; my $c = sqrt($c2); print $c;

  27. perl Pythagoras.pl Global symbol "$a3" requires explicit package name at Pythagoras.pl line 10. Execution of Pythagoras.pl aborted due to compilation errors.

  28. Text or number Variables can contain text (strings) or numbers my $var1 = 1; my $var2 = "2"; my $var3 = "three"; Try these four statements: print $var1 + $var2; print $var2 + $var3; print $var1.$var2; print $var2.$var3;

  29. Text or number Variables can contain text (strings) or numbers my $var1 = 1; my $var2 = "2"; my $var3 = "three"; Try these four statements: print $var1 + $var2; => 3 print $var2 + $var3; => 2 print $var1.$var2; => 12 print $var2.$var3; => 2three

  30. variables can be added, subtracted, multiplied, divided and modulo’d with: + - * / % variables can be concatenated with: .

  31. sequence.pl print "Please type a DNA sequence: "; #this is a comment line #Read a line from the standard input (keyboard) my $DNAseq = <STDIN>; #Remove the newline (Enter) from the typed text chomp($DNAseq); #Get the length of the text(DNA sequence) my $length = length($DNAseq); print "It has $length nucleotides\n";

  32. Program flow is top - down sequence.pl print "Please type a DNA sequence: "; #this is a comment line #Read a line from the standard input (keyboard) my $DNAseq = <STDIN>; #Remove the newline (Enter) from the typed text chomp($DNAseq); #Get the length of the text(DNA sequence) my $length = length($DNAseq); print "It has $length nucleotides\n";

  33. <STDIN> read characters that are typed on the keyboard. Stop after the Enter key is pressed

  34. <> same, STDIN is the default and can be left out. This is a recurring and confusing theme in Perl...

  35. sequence.pl print "Please type a DNA sequence: "; #this is a comment line #Read a line from the standard input (keyboard) my $DNAseq = <>; #Remove the newline (Enter) from the typed text chomp($DNAseq); #Get the length of the text(DNA sequence) my $length = length($DNAseq); print "It has $length nucleotides\n";

  36. $output = function($input) input and output can be left out parentheses are optional

  37. $coffee = function($beans,$water)

  38. sequence2.pl print "Please type a DNA sequence: "; my $DNAseq = <>; chomp($DNAseq); #Get the first three characters of $DNAseq my $first3bases = substr($DNAseq,0,3); print "The first 3 bases: $first3bases\n";

  39. $frag = substr($text, $start, $num) Extract a fragment of string $text starting at $start and with $num characters. The first letter is at position 0!

  40. perldoc perldoc -f substr substr EXPR,OFFSET,LENGTH,REPLACEMENT substr EXPR,OFFSET,LENGTH substr EXPR,OFFSET Extracts a substring out of EXPR and returns it. First character is at offset 0, .....

  41. print perldoc -f print print FILEHANDLE LIST print LIST print Prints a string or a list of strings. If you leave out the FILEHANDLE, STDOUT is the destination: your terminal window.

  42. print In Perl items in a list are separated by commas print "Hello World","\n"; Is the same as: print "Hello World\n";

  43. sequence3.pl print "Please type a DNA sequence: "; my $DNAseq = <>; chomp($DNAseq); #Get the second codon of $DNAseq my $codon2 = substr($DNAseq,3,3); print "The second codon: $codon2\n";

  44. if, else, unless

  45. sequence4.pl print "Please type a DNA sequence: "; my $DNAseq = <>; chomp($DNAseq); #Get the first three characters of $DNAseq my $codon = substr($DNAseq,0,3); if($codon eq "ATG") { print "Found a start codon\n"; }

  46. Conditional execution if ( condition ) { do something } if ( condition ) { do something } else { do something else }

  47. Conditional execution if ( $number > 10 ) { print "larger than 10"; } elsif ( $number < 10 ) { print "smaller less than 10"; } else { print "number equals 10"; } unless ( $door eq "locked" ) { openDoor(); }

  48. Conditions are true or false 1 < 10 : true 21 < 10 : false

  49. Comparison operators

  50. Examples if ( 1 == 1 ) { # TRUE if ( 1 == 2 ) { # FALSE if ( 1 != 2 ) { # TRUE if ( -1 > 10 ) { # FALSE if ( "hi" eq "dag" ) { # FALSE if ( "hi" gt "dag" ) { # TRUE if ( "hi" == "dag" ) { # TRUE !!! The last example may surprise you, as "hi" is not equal to "dag" and therefore should evaluate to FALSE. But for a numerical comparison they are both 0.

More Related