Perl courseparti


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • -Larry wall is a linguist-what does the Perl interpreter do? It compiles the program (source code) internally intobytecode and then executes it immediately. Perl is commonly known as an interpreted language, but this is not strictly true. Since the interpreter actually does convert the program into byte code before executing it, it is sometimes called an interpreter/compiler , if anything at all. [ 1 ] Although the compiled form is not stored as a file, release 5.005 of Perl includes a working version of a standalone Perl compiler.[1] So do you call something a Perl "script" or a Perl "program"? Typically, the word "program" is used to describe something that needs to be compiled into assembler or byte code before executing, as in the C language, and the word "script" is used to describe something that runs through an interpreter, as in the Bourne shell. For Perl, you can use either phrase and not worry about offending anyone.What does all this brouhaha mean for you? When you write a Perl program, you can just give it a correct #! line at the top of the script, make it executable with chmod +x , and run it. For 95% of Perl programmers in this world, that's all you'll care about.
  • (official documentation) (Perl wiki)
  • -#! (hash-bang or shebang) tells the shell where to look for perl-The print built-in function is one of the most frequently used parts of Perl. You use it to display things on the screen or to send information to a file.-Perl program consists of statements, each of which ends with a semicolon.-”.pl” extension is optional but is commonly used-”-w” switches on warnings: is not required but it is advisable
  • -Perl identifier is what follows the dollar sign-Choosing Good Variable Names. You should generally select variable names that mean something regarding the purpose of the variable. For example, $r is probably not very descriptive but $line_length is.
  • Perl courseparti

    1. 1. Introduction to Perl progammingPart I<br />16/12/2010<br />Ernesto Lowy<br />CRG Bioinformatics core<br />
    2. 2. What is Perl?<br />Perl is a programming language extensively used in bioinformatics<br />Created by Larry Wall in 1987<br />Provides powerful text processing facilities, facilitating easy manipulation of text files<br />Perl is an interpreted language (no compiling is needed)<br />Perl is quite portable <br />Programs can be written in many different ways (advantage?)<br />Perl slogan is "There's more than one way to do it”<br />Rapid prototyping (solve a problem with fewer lines of code than Java or C)<br />
    3. 3. Installing Perl<br />Perl comes by default on Linux and MacOSX<br />On windows you have to install it:<br /> (100% open source)<br /> (commercial distribution-but free!)<br />Latest version is Perl 5.12.0<br /> To check if Perl is working and version<br /> $perl –v<br />
    4. 4. Perl resources <br />Web sites<br /><br /><br /><br /><br /><ul><li>Books
    5. 5. Learning Perl (good for begginers)
    6. 6. Beginning Perl for Bioinformatics
    7. 7. Programming Perl (Camel book)
    8. 8. Perl cookbook</li></li></ul><li>Ex1. First program…<br />Open a terminal<br />Enter which perl<br />Open your favourite text editor and enter<br />#!/../path/to/perl –w<br /> #prints Hello world in the screen<br /> print “Hello world!n”;<br />4) Save it as<br />5) Execute it with<br /><br />
    9. 9. 1000 #integer<br />1.25 #floating-point<br />1.2e30 #1.2 times 10 to the 30th power<br />-1<br />-1.2<br />Only important thing to remember is that you never insert commas or spaces into numbers in Perl. So in a Perl program you never will find:<br />10 000<br />10,000<br />Perl basic data typesNumbers<br />
    10. 10. A string is a collection of characters in either single or double quotes:<br />“This is the CRG.”<br />‘CRG is in Barcelona!’<br />Difference between single and double quotes is:<br />print “Hello!nMy name is Erneston”; #Interprete contents<br />Will display:<br />>Hello!<br />>My name is Ernesto<br />print ‘Hello!nMy name is Erneston’; #contents should be taken literally<br />Will display:<br />>Hello!nMy name is Erneston<br />Perl basic data typesStrings <br />
    11. 11. Scalar variables<br />Variable is a name for a container that holds one or more values.<br />Scalar variable (contains a single number or string):<br />$a=1; <br />$codon=“ATG”;<br />$a_single_peptide=“GMLLKKKI”;<br />(valid Perl identifiers are letter,words,underscore,digits)<br />Important! Scalar variables cannot start with a digit<br />Important! Uppercase and Lowercase letters are distinct ($Maria and $maria)<br />Example (Assignment operator):<br />$codon=“ATG”;<br />print “$codon codes for Methioninen”;<br />Will display:<br />ATG codes for Methionine<br />
    12. 12. Numerical operators<br />Perl provides the typical operators. For example:<br />5+3 #5 plus 3, or 5<br />3.1-1.2 #3.1 minus 1.2, or 1.9<br />4*4 # 4 times 4 = 16<br />6/2 # 6 divided by 2, or 3<br />Using variables<br />$a=1;<br />$b=2;<br />$c=$a+$b;<br />print “$cn”;<br />Will print:<br />3<br />
    13. 13. Special numerical operators <br />$a++; #same than<br /> $a=$a+1;<br />$b--; #same than<br /> $b=$b-1;<br />$c +=10; #same than<br /> $c=$c+10;<br />
    14. 14. String manipulation <br />Concatenate strings with the dot operator<br /> “ATG”.”TCA” # same as “ATGTCA”<br />String repetition operator (x)<br /> “ATC” x 3 # same as “ATCATCATC”<br />Length() get the length of a string<br /> $dna=“acgtggggtttttt”;<br /> print “This sequence has “.length($dna).” nucleotidesn”;<br />Will print:<br /> This sequence has 10 nucleotides<br />convert to upper case<br /> $aa=uc($aa);<br />convert to lower case<br /> $aa=lc($aa);<br />
    15. 15. Conditional statements(if/else)<br />Determine a particular course of action in the program.<br />Conditional statements make use of the comparison operators to compare numbers or strings. These operators always return true/false as a result of the comparison<br />
    16. 16. Comparison operators(Numbers)<br />Examples:<br />35 == 35 # true<br />35 != 35 # false<br />35 != 32 # ????<br />35 == 32+3 # ????<br />
    17. 17. Comparison operators(Strings)<br />Examples:<br />‘hello’ eq ‘hello’ # true<br />‘hello’ ne ‘bye’ # true<br />‘35’ eq ‘35.0’ # ????<br />
    18. 18. If/else statement<br />Allows to control the execution of the program<br />Example:<br />$a=4;<br />$b=10;<br />If ($a>$b) {<br /> print “$a is greater than $bn”;<br />} else {<br /> print “$b is greater then $an”;<br />}<br />
    19. 19. elsif clause<br />To check a number of conditional expressions, one after another to see which one is true<br /><ul><li>Game of rolling a dice. Player wins if it gets an even number</li></ul>$outcome=6; #enter here the result from rolling a dice<br />if ($outcome==6) {<br /> print “Congrats! You win!n”;<br />} elsif ($outcome==4) {<br /> print “Congrats! You win!n”;<br />} elsif ($outcome==2) {<br /> print “Congrars! You win!n”;<br />} else {<br /> print “Sorry, try again!n”;<br />} <br />
    20. 20. Logical operators <br />Used to combine conditional expressions<br />|| (OR)<br />
    21. 21. Logical operators<br />Example:<br />$day=“Saturday”;<br />if ($day eq “Saturday” || $day eq “Sunday”) {<br /> print “Hooray! It’s weekend!n”;<br />}<br />Will print:<br />>Hooray! It’s weekend!<br />
    22. 22. Logical operators<br />&& (AND)<br />Example:<br />$hour=12;<br />if ($hour >=9 && $hour <=18) {<br /> “You are supposed to be at work!n”;<br />}<br />Will print:<br />>You are supposed to be at work!<br />
    23. 23. Boolean values <br />Perl does not have the Boolean data type. So how Perl knows if a given variable is true or false?<br />If the value is a number then 0 means false; all other numbers mean true<br />Example:<br />$a=15;<br />$is_bigger=$a>10; # $is_bigger will be 1<br />If ($is_bigger) {….}; # this block will be executed<br />
    24. 24. Boolean values <br />If a certain value is a string. Then the empty string (‘’) means false; all other strings mean true<br />$day=“”;<br />#evaluates to false, so this block will not be executed<br />if($day) { <br /> print $day contains a string<br />}<br />
    25. 25. Boolean values<br />Get the opposite of a boolean value (! Operator)<br />Example (A program that expects a filename from the user):<br />print “Enter file name, pleasen”;<br />$file=<>;<br />chomp($file); #remove n from input<br />If (!$file) { #if $file is false (empty string)<br /> print “I need an input file to proceedn”;<br />}<br />#try to process the file<br />
    26. 26. die() function<br />Raises an exception, which means that throws an error message and stops the execution of the program.<br />So previous example revisited:<br />print “Enter file name, pleasen”;<br />$file=<>;<br />chomp($file); #remove n from input<br />if (!$file) { #if $file is false (empty string)<br />die(“I need an input file to proceedn”);<br />}<br />#process the file only if $file is defined<br />
    27. 27. Ex 2. Using conditional expressions <br />TODO: Write a program to get an exam score from the keyboard and prints out a message to the student.<br />Hint: To read input from keyboard enter in your program<br />print "Enter the score of a student: ";<br />$score = <>; <br />
    28. 28. Solution<br />#! /usr/bin/perl<br />print "Enter the score of a student: ";<br />$score = <>; <br />if($score>=90) { <br /> print "Excellent Performance!n";<br />} elsif ($score>=70 && $score<90) {<br /> print "Good Performance!n”;<br />} elsif ($score>=50 && $score<70) {<br /> print "Uuff! That was close!n”;<br />} else {<br /> print "Sorry, try harder!n";<br />}<br />