Download presentation
Presentation is loading. Please wait.
Published byKaylee Walton Modified over 11 years ago
1
A primer on Perl programming First structures (with examples) http://www.shlomifish.org/lecture/Perl/Newbies/lecture1/
2
A primer on Perl programming Thanks for using the blog
3
A primer on Perl programming Thanks for using the blog
4
A primer on Perl programming Perl is popular among bioinformaticists because its easy, has a huge amount of modules and was born to parse text. A program takes an input and returns some elaborated output. We need to: test conditions, and iterate blocks of instructions.
5
A primer on Perl programming To reverse complement a multifasta file we need to: Input: filename Open the file and parse it (headers, sequences) E.g.: IF line starts with > its a header (one line), else concatenate lines… Output: Print header as is, reverse and complement the sequence
6
Recall: variables $scalar contains a single value (number or string @array is an ordered list of scalars %hash is a unordered list of values with a name (key). %age = ('Andrea' => 29, 'Cesare' => 80); $age{'Carlo'} = 39; print $name is $age{$name} old.\n;
7
The foreach cycle Performs a block of instruction for each element of an array @names = ('Aldo', 'John', 'Jack'); foreach $name (@names) { $count++; print Hello $name!\n; } print There were $count people in the array!\n;
8
The for cycle When you have to repeat a set of instruction a defined number of times. for (initial condition; until; ending instuction ) {...block... } for ($i = 1; $i<=10; $i++) { print \$i now is $i\n; }
9
Iterations #print numbers 1-10 in three different ways $i = 1; while ($i<=10) { print "$i\n"; $i++; } for ($i=1; $i<=10; $i++) { print "$i\n"; } foreach $i (1,2,3,4,5,6,7,8,9,10) { print "$i\n"; }
10
Logical operators They are different for numbers and strings!!! Equal: == or eq $a == 10 $a eq 'hello' Not equal: != or ne Greater than: >, >= Less than: <, <=
11
Operators Same precedence as in math +, -, *, / Modulus (division remainder): % Power: ** Concatenate strings:. $seqlength = 18; print There are.$seqlength**4. combinations\n;
12
Math functions Some functions: abs($x): absolute value int($x): integer part rand($x): random number up to $x sqrt($x): square root $percentage = int(100*$done/$todo); $dice = int(1+rand(6));
13
String functions Simple as: $length = length(Put a string here); Less simple as: $portion = substr($string, $start, $length); @pieces = split(/;/, $string); A world apart: Pattern matching $string=~/TATA/;
14
If statement Executes a block of code if a condition is TRUE. You can use boolean logic (and, or). Quite simple: if (length($a) == 10) { print Yes, its ten characters long!\n; } else { print No, \$a is not as long as I want.\n; }
15
IF… ELSE… Only one condition: if (condition) { block… } else { block… } Multiple conditions: elsif (other condition)
16
Talking to scripts Parameters passed from command line are stored in @ARGV (uppercase!) Eg: $ perl myscript.pl 100 Genomics print First parameter: $ARGV[0]\n; print Last one: GUESS\n; Can you write a script that prints the number of parameters passed by the users plus a list of them?
17
Reading a file open(NAME, Filename) || die Unable to read\n; while ( ) { print $_; } Dont be scared!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.