1 / 21

PERL

PERL. The Scripting Programming Language. #!/usr/bin/perl. 1. WHAT IS PERL ?. Perl is a programming language. Unlike programming languages such as C and Fortran, Perl programs are scripts which direct the execution of the program “perl”.

fuller
Download Presentation

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 The Scripting Programming Language

  2. #!/usr/bin/perl 1 WHAT IS PERL ? • Perl is a programming language. • Unlike programming languages such as C and Fortran, Perl programs are scripts which direct the execution of the program “perl”. • Perl execution is similar in form to shell script execution. • Look at the introduction: • http://perldoc.perl.org/perlintro.html

  3. #!/usr/bin/perl 2 WHY USE PERL ? • Perl is quick and easy to write. • Perl is versatile: • File/Sequence Management • Command Line Execution • Database • GUI • Webpage • Easy manipulation of sequences in genetic sequence analysis

  4. #!/usr/bin/perl 3 BASIC SCRIPT • The script “Example.pl” is: • #!/usr/bin/perl • $x = 1; • $y = 1; • $z = $x + $y; • print “$x + $y = $z”; • The execution of Example.pl is: > chmod +x Example.pl > ./Example.pl 1 + 1 = 2

  5. #!/usr/bin/perl 4 WHAT ARE PERL VARIABLES ? • Three main perl variables: • Scalar : $name • Array : @name • Hash : %name

  6. #!/usr/bin/perl 5 PERL SCALAR • Scalar variables are common and versatile. • Scalar variables hold either numbers or strings • $number = 12.4; • $string = “Value is: “; • print “$string$number\n”; • $string = $string . “number 12”; • print “$string\n”; • Generates: • Value is: 12.4 • Value is: number 12

  7. #!/usr/bin/perl 6 PERL ARRAY • Array holds a list of values • $value = “there”; • @array = (“Hello”,$value,1); • print “$arr[0] $arr[1] number $arr[2]\n”; • $array[2] = 2; • $array[3] = “again”; • print “$arr[0] $arr[3] number “, $arr[2] – 1, “\n”; • $size = @array; • print “Array has size $size\n”; • Generates: • Hello there number 1 • Hello again number 1 • Array has size 4

  8. #!/usr/bin/perl 7 PERL HASH • Hash holds references to values • %hash = ( “number” => 1, 34 => “Hello there” ); • print $hash{34}, “ number “, $hash{“number”}, “\n”; • @array = keys %hash; • foreach $ref (@array){ • print “Key: $ref = $hash{$ref}\n”; • } • Generates: • Hello there number 1 • Key number = 1 • Key 34 = Hello there

  9. #!/usr/bin/perl 8 LINE CONTROL • Perl controls line execution with • if – elsif – else • while • for • foreach • With comparisons: • For numbers: • < (less than), > (greater than) • <= (less than or equal), >= (greater than or equal) • == (equal) • For strings: • eq (equal strings) • ne (not equal strings)

  10. #!/usr/bin/perl 9 IF – ELSIF - ELSE • For example: • if( $val < 2 ){ • print “Value is less than 2\n”; • } • elsif( $val == 2 ){ • print “Value is equal to 2\n”; • } • else{ • print “Value is greater than 2\n”; • } • if( $stringA eq $stringB ){ • print “Both strings are the same\n”; • } • else{ • print “Both strings are different\n”; • }

  11. #!/usr/bin/perl 10 WHILE, FOR LOOPS • For example: • # while( condition ) • while( $i < 20 ){ • print “The value $i is less than 20\n”; • $i++; • } • # for( initial operation; condition; loop action ) • for( $i = 0; $i < 20; $i++ ){ • print “The value $i is greater or equal to 0\n”; • print “The value $i is less than 20\n”; • }

  12. #!/usr/bin/perl 11 FOREACH LOOP • For example: • # foreach $value ( @array ) • foreach $value ( @array ){ • print “The value $value is in the array\n”; • } • foreach $value ( keys %hash ){ • print “$value has value $hash{$value}\n”; • } • Note that foreach loops traverse the array from low index to high index.

  13. #!/usr/bin/perl 12 READING IN DATA • Array @ARGV holds command line arguments • To open and read in a file: • open(IN,”$fileName”); • $firstLine = <IN>; • while(<IN>){ • print $_; • } • close(IN); • To write to a file: • open(OUT,”> $fileName”); • print OUT “Write this into $fileName\n”; • close(OUT);

  14. #!/usr/bin/perl 13 STANDARD STREAMS • To read standard input: • print “Write something: “; • $value = <STDIN>; • print “User wrote $value\n”; • To write to standard output: • print STDOUT “You see this on the command line\n”; • To write to standard error: • print STDERR “This is written as an error\n”;

  15. #!/usr/bin/perl 14 REGULAR EXPRESIONS • Regular expressions are a powerful tool for locating and modifying data • The basic evaluation of regular expression are written as: • $variable =~ /regular expression/ • which returns true if the variable holds to the regular expression and false otherwise.

  16. #!/usr/bin/perl 15 REG EXP EXAMPLES • Example: • if( $string =~ /Jonathan/ ){ • print “The string contains Jonathan”; • print “as a substring.\n”; • } • if( $string !~ /Myers/ ){ • print “The string does not contain”; • print “Myers as a substring”; • }

  17. #!/usr/bin/perl 16 REG EXP Variables • Basic elements in regular expressions: • /./ = Any single character • /\w/ = Any character a-z, A-Z, 0-9, and ‘_’ • /\d/ = Any digit 0-9 • /\s/ = space, tab, or enter • /\./ = The character ‘.’ • /\\/ = The character ‘\’ • Definition of groups: • /[xyz]/ = Occurrence of either x, y or z • /[^xyz]/ = Occurrence of anything but x, y, or z • Location of occurrence: • /^x/ = Occurrence of x as the first character • /x$/ = Occurrence of x as the last character

  18. #!/usr/bin/perl 17 REG EXP Counts • Number of occurrences: • /x*/ = 0 or more copies of character ‘x’ in a row • /x+/ = 1 or more copies of character ‘x’ in a row • /x{3}/ = 3 copies of character ‘x’ in a row • /(str)*/ = 0 or more copies of string ‘str’ in a row • /$var*/ = 0 or more copies of the string $var in a row • Example: • $str = “a string with too much”; • if( $str =~ /o{2}/ ){ • print “True!\n”; • } • if( $str =~ /(crazy)*/ ){ • print “True!\n”; • }

  19. #!/usr/bin/perl 18 Substitutions • Substitutions are regular expressions that define replacements within strings. • $str =~ s/(regular expression)/(replacement)/ • Examples: • $str = “aabbccddeeff”; • $str =~ s/c/1/; • print “$str\n”; • $str =~ s/.$/2/; • print “$str\n”; • $str =~ s/^\w{3}/start /; • print “$str\n”; • Generates: • aabb1cddeeff • aabb1cddeef2 • start b1cddeef2

  20. #!/usr/bin/perl 19 SPLIT FUNCTION • Split function breaks a string into an array of strings • @array = split(/(regular expression)/,$string) • Example: • $str = “A set of strings”; • @array = split(/\s+/,$str); • for( $i = 0; $i < @array; $i++ ){ • print “$array[$i]\n”; • } • Generates: • A • set • of • strings

  21. #!/usr/bin/perl 20 REMEMBER PRACTICE!!!! • We learn by doing !!!! • Look at these sites!!! • http://perldoc.perl.org/perlintro.html • http://www.perl.com/pub/a/2000/10/begperl1.html • http://www.comp.leeds.ac.uk/Perl/start.html

More Related