Perl Scripting II Conditionals, Logical operators, Loops, and File ...

27 downloads 25 Views 147KB Size Report
Control structures allow you to tell Perl the order in which you want the interpreter ... What does this print if $i=2 and $j=2? ... What is TRUE (in Perl). 1. The string ...
Perl Scripting II Conditionals, Logical operators, Loops, and File handles

Suzi Lewis Genome Informatics

Control Structures  Control structures allow you to tell Perl the order in which you want the interpreter to execute statements.  You can create alternative branches in which different sets of statements are executed depending on circumstances  You can create various types of repetitive loops.

Conditional Blocks $i = 1; $j = 2; if ($i == $j) {

# Curly braces define a “block”

print "i equals j\n"; $i += 1; } unless ($i == $j) { print "i does not equal j\n"; $i += 2; } print "\$i is $i\n";

Single line Conditionals  You can also use the operators if and unless at the end of a single statement. This executes that one statement conditionally. print "i equals j\n"

if $i == $j;

print "i is twice j\n" if $i == $j * 2; print "i does not equal j\n" unless $i == $j;

If-Else Statements  Use else blocks for either/or constructions. if ($i == $j) { print "i equals j\n"; $i += $j; } else { print "i does not equal j\n"; die "Operation aborted!"; }

 What does this print if $i=2 and $j=2?

If-Else Statements  You can perform multiple tests in a series using elsif: if ($i > 100) { print "i is too large\n"; } elsif ($i < 0) { print "i is too small\n"; } elsif ($i == 50) { print "i is too average\n"; } else { print "i is just right!\n"; }

 What does this print if $i=50? If $i=51?

Use == to Compare Two Numbers for Equality $i =

4 == 4;

# TRUE

$i =

4 == 2 + 2;

# TRUE

$i =

4 == $j;

# depends on what $j is

 Do not confuse == with =  == is for numeric comparison.  = is for assignment.

Use != to Compare Two numbers for Non-Equality $i =

4 != 4;

# FALSE

$i =

4 != 2 + 2;

# FALSE

$i =

4 != $j;

# depends on what $j is

Use > and < for "Greater than", "Less than" $i =

4 > 3;

# TRUE

$i =

4 < 3;

# FALSE

$i =

4 > $j;

# depends on what $j is

Use >= and = 3;

# TRUE

$i =

4 >= 4;

# TRUE

$i =

4 0) { print "a is the right size\n"; } else { die "out of bounds error, operation aborted!"; } if ($i >= 100 || $i 0); print "a is too small\n" if not $ok; # same as this: print "a is too small\n" unless $ok; # and this: print "a is too small\n" if !$ok;

and versus &&, or versus ||  Precedence  && higher than = which is higher than and.  || higher than = which is higher than or.

 This is an issue in assignments  Example 1 $ok = $i < 100 and $i > 0; # This doesn't mean: $ok = ($i < 100 and $i > 0); # but: ($ok = $i < 100) and $i > 0;

 Example 2 $ok = $i < 100 && $i > 0; # This does mean $ok = ($i < 100 && $i > 0);

 When in doubt, use parentheses.

The or and || operators do no more than necessary.  If what is on the left is true, then what is on the right is never evaluated, because it doesn't need to be. $i = 10; $j = 99; # $j comparison never evaluated $i < 100 or $j < 100;

The "or die" Idiom  The die() function aborts execution with an error message  You Combine “or die” and truth statements idiomatically like this ($i < 100 and $i > 0) or die "\$i is the wrong size";

File Tests  A set of operators are used to check whether files exist, directories exist, files are readable, etc.  -e # file exists  -r # file is readable  -x # file is executable  -w # file is writable  -d # filename is a directory  Examples (-w "./fasta.out”)or die "Can't write to file"; print "This file is executable\n" if -x "/usr/bin/perl";

Loops  Loops let you execute the same piece of code over and over again.  A while loop  Has a condition at the top.  The code within the block will execute until the condition becomes false. while ( CONDITION ) { # Code to execute }

While loop: Print "below 5" until the number is not below 5  Code:

 Output of: 51% counter.pl

#!/usr/bin/perl

0 is less than 5

# file: counter.pl

1 is less than 5

$number = 0;

2 is less than 5

while ( $number < 5 ) {

3 is less than 5

print "$number is less than 5\n"; $number = $number + 1; }

4 is less than 5

foreach Loops  foreach will process each element of an array or list:

foreach $list_item ( @array ) { Do something with $list_item; }

for Loops  The for loop is the most general form of loop: for ( initialization; test; update ) { # Do something }

 The first time the loop is entered, the code at initialization is executed.  Each time through the loop, the test is reevaluated and the loop stops if it returns false.  After the execution of each loop, the code at update is performed.

A simple for loop for ( $i = 1; $i < 5; $i++ ) { print $i,"\n"; } 

This will print out the numbers 1 to 4:  From command line: 52% loop.pl

1 2 3 4 

This is equivalent to the previous while example.



There are ways to leave loops outside of the conditional, but this practice is discouraged.

Basic Input & Output (I/O) Getting computer programs to talk to the rest of the world.

The STDIN, STDOUT and STDERR File handles  Every Perl scripts starts out with three connections to the outside world:  STDIN (Standard input)  Used to read input. By default connected to the keyboard, but can be changed from shell using redirection (cosmids.fasta"



For appending open FILEHANDLE,">>cosmids.fasta"



For reading and writing open FILEHANDLE,"+