![Page 1: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/1.jpg)
A primer on Perl programming
First structures (with examples)
http://www.shlomifish.org/lecture/Perl/Newbies/lecture1/
![Page 2: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/2.jpg)
A primer on Perl programming
Thanks for using the blog
![Page 3: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/3.jpg)
A primer on Perl programming
Thanks for using the blog
![Page 4: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/4.jpg)
A primer on Perl programming
• Perl is popular among bioinformaticists because it’s 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.
![Page 5: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/5.jpg)
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 ‘>’ it’s a header (one line), else concatenate lines…
•Output: Print header as is, reverse and complement the sequence
![Page 6: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/6.jpg)
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”;
![Page 7: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/7.jpg)
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”;
![Page 8: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/8.jpg)
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”;
}
![Page 9: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/9.jpg)
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";
}
![Page 10: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/10.jpg)
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: <, <=
![Page 11: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/11.jpg)
Operators
• Same precedence as in math
• +, -, *, /
• Modulus (division remainder): %
• Power: **
• Concatenate strings: .
$seqlength = 18;
print ‘There are ’.$seqlength**4.‘ combinations\n’;
![Page 12: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/12.jpg)
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));
![Page 13: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/13.jpg)
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/;
![Page 14: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/14.jpg)
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”;
}
![Page 15: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/15.jpg)
IF… ELSE…
Only one condition:if (condition) {
block…
} else {
block…
}
Multiple conditions: elsif (other condition)
![Page 16: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/16.jpg)
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?
![Page 17: A primer on Perl programming First structures (with examples)](https://reader033.vdocuments.us/reader033/viewer/2022061304/5513deea5503463a298b5794/html5/thumbnails/17.jpg)
Reading a file
open(NAME, “Filename”) || die “Unable to read\n”;
while (<NAME>) {
print “$_”;
}
Don’t be scared!