Perl Basics ============ Quick Review ============ Why Perl for data processing & bioinformatics - Fast text processing - Regular expressions - Extensive module libraries for pre-written tools - Large number of users in community of bioinformatics - Scripts are often faster to write than full compiled programs Every perl script needs to contain the following lines: #!/usr/bin/perl use warnings; use strict; Print "hello world". Type this into your text editor and save it. Give the script a .pl extension. #!/usr/bin/perl use warnings; print "hello world\n"; Run it: on the command line type perl your_script_name.pl Now try rewriting your script so that it doesnot have a new line character (\n). Run it. Using variables: $scalar A scalar contains a value, can be a string or a number. @array An array contains a list of scalars. %hash A hash contains paired values (key/value pairs) Using a scalar to print: #!/usr/bin/perl use warnings; use strict; my $phrase = "hello world"; print "$phrase\n"; # This is a comment # A print statement can combine variables and strings print "This is my phrase $phrase\n"; # this is another way to print the same as above # the print function takes a list of arguments # in this example "This is my phrase $phrase" is the first argument # and "\n" is the second print "This is my phrase $phrase" , "\n"; ---- note about scope ---- Scope: (we will go over this concept in more detail at a later time) but in summary: use strict; #!!!!! this is important when you 'use strict' you have to use my when declaring a variable. This means when you use a variable for the first time use the term 'my'; --------------------------- Something Advanced but fun tr/// #!/usr/bin/perl use warnings; use strict; my $pet = "cat"; print $pet, "\n"; $pet =~ tr/cat/dog/; print $pet , "\n"; $pet =~ tr/o/O/; print $pet, "\n"; dOg $pet =~ tr/gd/wc/; print $pet, "\n"; ---- getting help ------- Documentation of perl scripts and modules Perldoc online http://perldoc.perl.org/ Or on your computer - type 'perldoc' For functions use -f 'perldoc -f sprintf' For modules just the name 'perldoc List::Util' http://www.cpan.org =========== Problem Set =========== 1. Create a script called "add.pl" script to sum two $scalar variables: (hint: use + ) 2. Try other mathmatical operators. Create a new scripts that adds, subtracts, multiplies and divides values. 3. Test your knowledge of precedence. Create a script that uses may operators together. Do you get the answer that you expect? 4. Create a script to produce the reverse complement of a sequence (hint, use the reverse and tr/// functions) % reversec.pl GAGAGAGAGAGTTTTTTTTT output: AAAAAAAAACTCTCTCTCTC