Perl courseparti

  • 636 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
636
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
4
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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.
  • http://perldoc.perl.org/ (official documentation)https://www.socialtext.net/perl5/index.cgi (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.

Transcript

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