Venky Iyer
 

Hello, World and other short stories

Introduction to Perl: 1

Session 1.2



Topics:

You had me at Hello, World...

Virtues of a good programmer


Pseudocode


PCR


  1. Get template, primers, nucleotides, buffers, enzyme, water
  2. Dilute to appropriate concentrations
  3. Mix in specific quantities and defined order
  4. Modulate temperature in specific ways, for specific number of iterations and amount of time

operations


inputs


dilute


just a wrapper around mix?

dilute = mix with stuff, water as (ingredients)

Pseudocode for PCR


  1. get : Call on each of template, primers, nucleotides, buffers, enzyme, water
  2. dilute : For each, with appropriate stock concentrations
  3. mix : Reaction mix in specific order and under mixing conditions
  4. do the following 30 times:
    1. modulate_temp with some values
    2. modulate_temp with some values
    3. ....
  5. modulate_temp at 4 degrees C

The Elements of a Program


What is Perl




Perl, Meet World



#!/usr/bin/perl

use strict;
use warnings;

print "Hello, World";

__END__

The Shebang



#!/usr/bin/perl

use strict;
use warnings;

print "Hello, World";

__END__

Structure


@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


Comments and documentation



# This is a comment
# So is this
my $but_this = 'is not';


perldoc and POD


$> perldoc
$> perldoc perldoc


print "Something";

=head2 This is a POD section

This is POD content

=cut

# POD ended


use



use strict;
use warnings;
use Statistics::Descriptive;


strict



#!/usr/bin/perl

use strict;
use warnings;

print "Hello, World";

__END__
$> perldoc strict


warnings



#!/usr/bin/perl

use strict;
use warnings;

print "Hello, World";

__END__
$> perldoc warnings


Digression: A reminder about STDIN, STDOUT, STDERR


http://www.matisse.net/OSX/intro_unix/standard_in_and_out.gif

Examples

$> cat input.tsv | perl foo.pl
 
$> cat input.tsv | perl foo.pl > output.tsv
 
$> cat input.tsv | perl foo.pl | output.tsv
 
$> cat input.tsv > perl foo.pl
 
$> cat input.tsv | perl fool.pl | perl bar.pl | most
 
$> perl foo.pl < input.tsv
 
$> perl foo.pl < input.tsv > output.tsv
 
$> perl foo.pl < input.tsv | sort | most

Redirection, continued

More Examples

$> perl foo.pl 2> errors.txt
 
$> perl foo.pl 1> output.tsv
 
$> perl foo.pl > output.tsv
 
$> perl foo.pl 2>&1 | most
 
$> perl foo.pl &> both.txt
 
$> perl bar.pl >> output.tsv
 
$> perl bar.pl 2>> errors.txt

warnings, continued

diagnostics

$> perl badfoo.pl 2> warnings.txt
 
$> splain -h
Unknown option: h
Usage: /home/sapo/software/bin/splain [-v] [-p] [-f splainpod]
 
$> perldoc splain
 
$> cat warnings.txt | splain -p
 
$> splain -p < warnings.txt
 
$> perl badfoo.pl 2>&1 | splain -p
 

print

$> perldoc -f print

print "Hello, World";

#  :-
#  Hello, World$>

print STDOUT "Hello, World";

#  :-
#  Hello, World$>

print STDERR "Hello, World";

#  :-
#  Hello, World$>

Newlines


print "Hello, World
";

#  :-
#  Hello, World
#  $>

print "Hello, World\n";

#  :-
#  Hello, World
#  $>

print "Hello,", "World", "\n";

#  :-
#  Hello,World
#  $>

print "Hello, ", "World", "\n";

#  :-
#  Hello, World
#  $>

The tab


print "Hello,\tWorld\n";

#  :-
#  Hello,    World
#  $>

Quotes




print "Hello, World\n";

#  :-
#  Hello, World
#  $>

print 'Hello, World\n';

#  :-
#  Hello, World\n$>

More Quotes




print "Hello, " Venky "\n";

#  :-
#  #%@#%$#@%$
#  $>

print "Hello, \"Venky\"\n";

#  :-
#  Hello, "Venky"
#  $>

print "Hello, \'Venky\'\n";

#  :-
#  Hello, 'Venky'
#  $>

print qq{Hello, "Venky"\n};

#  :-
#  Hello, "Venky"
#  $>

print qq/Hello, {Venky}\n/;

#  :-
#  Hello, {Venky}
#  $>

The __END__ marker


Some Arithmetic



#!/usr/bin/perl

use strict;
use warnings;

# declare our variables
my $first_number  = 1;
my $second_number = 12;

# compute some stuff
my $sum        = $first_number + $second_number;
my $difference = $first_number - $second_number;
my $product    = $first_number * $second_number;
my $power      = $first_number**$second_number;
my $reminder   = $first_number % $second_number;

# print the operands
print "First Number = $first_number\n";
print "Second Number = $second_number\n";

# print some values
print "sum = $sum, difference = $difference\n";
print "product = $product, ratio = ", $first_number / $second_number, "\n";
print "power = $power, reminder = $first_number % $second_number\n";

__END__

Operators


+ - % * / ** x


print 2 + 5, ',', 2 - 5, ',', 2 * 5, ',', 2 / 5, ',', 5 % 2, ',', 2**5, 2 x 5, "\n";

#  :-
#  7,-3,10,0.4,1,32,22222
#  $>

Precedence


$> perldoc perl
 
$> perldoc perlop



print( ( 2 + 2 ) / 2 ) * 2;

Scalar Variables



my $foo = 5;
print "The value of the 'foo' variable is $foo\n";

#  :-
#  The value of the 'foo' variable is 5
#  $>

Operator Operator

# post-increment
my $foo = 1;
print $foo, ',', $foo++, ',', $foo, "\n";

#  :-
#  1,1,2
#  $>

# pre-increment
my $foo = 1;
print $foo, ',', ++$foo, ',', $foo, "\n";

#  :-
#  1,2,2
#  $>

my $foo = 1;

# $foo = $foo + 3
$foo += 3;    #4

# $foo = $foo - 1
$foo -= 1;    #3
$foo *= 2;    #6
$foo /= 2;    #3
$foo %= 2;    #1

Dynamic typing and the values of scalars




my $bar;    #undef
my $foo = 'tim5';     #initialized
my $baz = '5toady';

print $bar;

#  :-
#  #^$%@#$#$
#  $>

my $sum = $bar + 3;

#  :-
#  #$%@#%#@$%@#
#  $>

print $foo + 3, "\n";    # numerical value of $foo is 0

#  :-
#  3
#  $>

print $foo + $baz, "\n";    # numerical value of $baz is 5

#  :-
#  5
#  $>

my declarations and scoping



Example


#!/usr/bin/perl

use strict;
use warnings;

my $foo = 5;
print "foo outside the scopes is $foo\n";

{
    print "foo at the beginning of the first scope is $foo\n";

    my $foo = 10;
    print "foo in the first scope is $foo\n";

    {
        print "foo at the beginning of the second scope is $foo\n";

        # will throw error! $bar doesn't exist here!
        # print "bar at the end of the first scope is $bar\n";

        my $bar = 20;
        print "bar in the second scope is $bar\n";
    }

    print "foo at the end of the first scope is $foo\n";

    # will throw error! $bar doesn't exist here!
    # print "bar at the end of the first scope is $bar\n";
}
print "foo outside the end of the scopes is $foo\n";

__END__
#  :-
#  foo outside the scopes is 5
#  foo at the beginning of the first scope is 5
#  foo in the first scope is 10
#  foo at the beginning of the second scope is 10
#  bar in the second scope is 20
#  foo at the end of the first scope is 10
#  foo outside the end of the scopes is 5
#  $>

Reading from STDIN



#!/usr/bin/perl

use strict;
use warnings;

print "Enter something\n";
my $foo = <STDIN>;

print "You entered '$foo'\n";

__END__

#  :-
#  Enter something
#  ->My name is Venky
#  You entered 'My name is Venky
#  '
#  $>


chomp



#!/usr/bin/perl

use strict;
use warnings;

print "Enter something\n";
my $foo = <STDIN>;
chomp $foo;

print "You entered '$foo'\n";

__END__

#  :-
#  Enter something
#  ->My name is Venky
#  You entered 'My name is Venky'
#  $>


Invoking functions



chomp( my $foo = <STDIN> );

# another
my $foo = <STDIN>;
chomp $foo;

# another
my $foo = <STDIN>;
print "You entered '", chomp $foo, "'\n";

Arithmetic revisited


#!/usr/bin/perl

use strict;
use warnings;

# declare our variables
chomp( my $first_number  = <STDIN> );
chomp( my $second_number = <STDIN> );

# compute some stuff
my $sum        = $first_number + $second_number;
my $difference = $first_number - $second_number;
my $product    = $first_number * $second_number;
my $power      = $first_number**$second_number;
my $reminder   = $first_number % $second_number;

# print the operands
print "First Number = $first_number\n";
print "Second Number = $second_number\n";

# print some values
print "sum = $sum, difference = $difference\n";
print "product = $product, ratio = ", $first_number / $second_number, "\n";
print "power = $power, reminder = $first_number % $second_number\n";

__END__