1 / 20

Perl Programming for Biology Exercise 1

Perl Programming for Biology Exercise 1. The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman and Dudu Burstein. Running Perl at the DOS command prompt.

alena
Télécharger la présentation

Perl Programming for Biology Exercise 1

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 Programming for BiologyExercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman and Dudu Burstein

  2. Running Perl at the DOS command prompt Traditionally, Perl scripts are run from a command prompt (a DOS window). (Start it by clicking: Start  Accessories  Command Promptor:Start  Run…  cmd ) Running a Perl scriptperl -w YOUR_SCRIPT_NAME (To check if Perl is installed in your computer use the ‘perl -v’ command)

  3. Running Perl at the DOS command prompt Common DOS commands: d:change to other drive (d in this case)md my_dirmake a new directorycd my_dirchange directorycd .. move one directory updirlist files (dir /p to view it page by page)helplist all dos commandshelp dirget help on a dos command

  4. A first Perl script print "Hello world!"; A Perl statement must end with a semicolon “;”The printfunction outputs some information to the terminal screen • Try it yourself! • Use Notepad to write the script in a file named “hello.pl” (Save it in D:\perl_ex) • Run it! • Click StartProgramsAccessoriesCommand Prompt (opens a DOS prompt) • Change to the right drive ("D:") and change directory to the directory that holds the Perl script ("cd perl_ex"). • Type perl -w script_name.pl(replace script_name.pl with the name of the script)

  5. Scalar Data

  6. Scalar values A scalar is either a string or a number. Numerical values 3 -20 3.14152965 1.3e4(= 1.3 × 104 = 1,300)‏ 6.35e-14( = 6.35 × 10-14)‏

  7. Scalar values Strings Single-quoted strings print 'hello world';hello world Double-quoted strings print "hello world";hello world print "hello\tworld";hello world print 'a backslash-t: \t ';a backslash-t: \t print "a backslash: \\ ";a backslash: \ print "a double quote: \" ";a double quote: " Backslash is an “escape” character that gives the next character a special meaning:

  8. Operators Anoperator takes some values (operands), operates on them, and produces a new value. Numerical operators:+ - * / ** (exponentiation) ++ -- (autoincrement)‏ print 1+1;2 print ((1+1)**3);8

  9. Operators An operator takes some values (operands), operates on them, and produces a new value. String operators:.(concatenate)x(replicate)‏ e.g. print ('swiss'.'prot'); swissprot print (('swiss'.'prot')x3);swissprotswissprotswissprot

  10. String or number? Perl decides the type of a value depending on itscontext: (9+5).'a' 14.'a' '14'.'a' '14a' Warning:When you use parentheses in print make sure to put one pair of parantheses around the WHOLE expression: print (9+5).'a'; #wrong print ((9+5).'a'); #right You will know that you have such a problem if you see this warning: print (...) interpreted as function at ex1.pl line 3. (9x2)+1 ('9'x2)+1 '99'+1 99+1 100

  11. Class exercise 1 Write a Perl script that prints the following lines: • The string “hello world! hello Perl!” • Use the operator “.” to concatenate the words “apple”, “orange” and “banana”‏ 3*. Produce the line: “666:666:666:god help us!”without any 6 and with only one : in your script! Like so: hello world! hello Perl! apple orange banana 666:666:666:god help us!

  12. Variables Scalarvariables can store scalar values. Variabledeclarationmy $priority; Numericalassignment $priority = 1; Stringassignment $priority = 'high'; Assign the value of variable $b to $a $a = $b; Note:Here we make a copy of$b in$a.

  13. Variables - notes and tips • Tips: • Give meaningful names to variables: e.g.$studentNameis better than$n • Always use an explicit declaration of the variables using themyfunction • Note: Variable names in Perl arecase-sensitive. This means that the following variables are different (i.e. they refer to different values): $varname = 1; $VarName = 2; $VARNAME = 3; • Note: Perl has a long list of scalarspecial variables($_, $1, $2,…) So please don’t use them!

  14. Variables - always use strict! • Always include the line: • use strict; • as the first line of every script. • “Strict” mode forces you to declare all variables bymy. • This will help you avoid very annoying bugs, such as spelling mistakes in the names of variables.

  15. Interpolating variables into strings $a = 9.5;print "a is $a!\n"; a is 9.5! Reminder: print 'a is $a!\n'; a is $a!\n

  16. Reading input Use the chomp function to remove the “new-line” from the end of the string (if there is any): print "What is your name?\n"; my $name = <STDIN>; chomp $name; # Remove the new-line print "Hello $name!"; Here is a test run: What is your name? Shmulik Hello Shmulik! $name: "Shmulik\n" $name: "Shmulik"

  17. Built-in Perl functions:The lengthfunction The length function returns the length of a string: print length("hi you"); 6

  18. The substr function The substr function extracts a substring out of a string. It receives 3 arguments: substr(EXPR,OFFSET,LENGTH) For example: $str = "university"; $sub = substr ($str, 3, 5); $sub is now "versi", and $str remains unchanged. Note: If length is omitted, everything to the end of the string is returned. You can use variables as the offset and length parameters. The substr function can do a lot more, google it and you will see…

  19. Documentation of perl functions A good place to start is the list of All basic Perl functions in the Perl documentation site: http://perldoc.perl.org/ Click the link “Functions” on the left.

  20. Home exercise 1 – submit by email until next class Install Perl on your computer. Use Notepad to write scripts, or optionally - install the Perl Express editor (this you should do at home). Write a script that prints "I will submit my homework on time" 100 times. Write a script that assigns your e-mail address into the variable $email and then prints it. Write a script that reads a line and prints the length of it. Write a script that reads a line and prints the first 3 characters. 6*. Write a script that reads a line and three numbers, and then prints the letters between the positions given by the first two numbers, duplicated as many times as indicated by the third number. * Kohavit questions are a little tougher, and are not mandatory

More Related