PERL Programming Basic

106
INTRODUCTION TO PERL Prepared with the help internet, Google and countless contributors to this PPT Thanks to all of them Presented By Viswanatha Yarasi [email protected] (9849743894)

Transcript of PERL Programming Basic

Page 1: PERL Programming Basic

INTRODUCTION TO PERL

Prepared with the help internet, Google and countless contributors to this PPT

Thanks to all of them

Presented ByViswanatha Yarasi

[email protected](9849743894)

Page 2: PERL Programming Basic

PERL Programming

• Objectives– Introduction to PERL Programming.– Where to get perl.– Writing a perl script– How to execute the perl intrepreter.– Variables

• Scalars• Arrays• Hashes

Page 3: PERL Programming Basic

Objectives Contd.– Using strict.– Built in help – perldoc.– Conditional and looping statements– Built in functions.– Regular expressions.– File/Directory handling.– Input/Output– Functions

Page 4: PERL Programming Basic

What Is Perl

– Practical Extraction and Report language– Pathologically Eclectic Rubbish Lister?

– Perl is a High-level Scripting language

– Released in 1987 by Larry Wall

– Faster than sh or csh, slower than C

– More powerful than C, and easier to use

– No need for sed, awk, tr, wc, cut, …

Page 5: PERL Programming Basic

Larry Wall

Page 6: PERL Programming Basic

Larry Wall Quotes

• “Even if you aren't in doubt, consider the mental welfare of the person who has to maintain the code after you, and who will probably put parens in the wrong place. “

• "It is easier to port a shell than a shell script." • “The three chief virtues of a programmer are:

Laziness, Impatience and Hubris”• “And don't tell me there isn't one bit of difference

between null and space, because that's exactly how much difference there is. :-) “

Page 7: PERL Programming Basic

What Is Perl (Contd.)– Compiles at run-time

– Available for Unix, PC, Mac

– Best Regular Expressions on Earth

– Originally designed to address the short comings of Unix Scripts.

– The Swiss Army chainsaw of scripting languages

– Open source and free.

Page 8: PERL Programming Basic

What Is Perl (Contd.)– Thousands of modules available from

http://www.cpan.org– Originally designed to address the short

comings of Unix Scripts.– “More than one way to skin a cat”– Can be used in conjunction with other

languages like C,Python, Java etc.– Can be difficult to read.

Page 9: PERL Programming Basic

What Is Perl Good For?– Quick scripts, complex scripts– Parsing & restructuring data files– CGI-BIN scripts– Biotechnology– Prototypes– System Administration– QA– High-level programming

• Networking libraries• Graphics libraries• Database interface libraries

Page 10: PERL Programming Basic

Strengths of Perl

• Text processing (pattern matching)

• List processing

• Database access

• System language

Page 11: PERL Programming Basic

What Is Perl Bad For?– Compute-intensive applications (use C)– Hardware interfacing (device drivers…)

Page 12: PERL Programming Basic

Where To Get Perl– Latest release is 5.8.5– Most used is 5.6.1– Download from http://www.cpan.org/ports/

for the OS being used– Easy installation of Perl on Windows from

• http://www.cygwin.com

– For Linux/Windows/Solaris• http://www.activestate.com/Products/ActivePerl

/• http://www.perl.com/download.csp

Page 13: PERL Programming Basic

Perl is reasonably well documented!

• Programming Perl– Wall&Schwartz; O’Reilly/Nutshell– the “camel book”

• Programming Perl– Wall, Christiansen,&Schwartz; O’Reilly– the other camel book

• www-cgi.cs.cmu/cgi-bin/perl-man– html-based manual

Page 14: PERL Programming Basic

Perl is an interpreted language

• Program is text file

• Perl loads it, compiles into internal form

• Executes the intermediate code

Page 15: PERL Programming Basic

Perl scripts– Writing a perl script

#!/usr/bin/perl -w

Statements(;)

Comments(#)

– Editing a perl script

vi script1.pl

#!/usr/bin/perl -w

#print some text

print “Hello World\n”;

Page 16: PERL Programming Basic

Command Line Options• From the shell: /usr/bin/perl [optional

parameters]• As a shell script: #!/usr/bin/perl [optional

parameters]• -c - check syntax without doing execution• -d - run the Perl debugger• -e - specify one line of script (like sed)• -v - print minimal version information• -V - print verbose version information• -w - prints VERY useful syntax and runtime

warnings; everybody should make a habit of testing their scripts with this!

Page 17: PERL Programming Basic

Perl scripts (Contd.)

• Running a perl script

./script1.pl

• Make your file executable (chmod u+x)!!

• chmod u+x script1.pl

or

• Chmod 755 script1.pl

Page 18: PERL Programming Basic

Hello World$a = 123;$b = 55;$c = $a + $b;$d = "kakukk\n";$d = 'kakukk\n' if $c == 178;if( $d eq "kakukk\n" ){ print "Hello World!\n"; }else{ print "This is not a good day!\n";}OUTPUT:This is not a good day!

Page 19: PERL Programming Basic

Variables

• Variables are containers to hold values

• Values can change within a script

• Types– Scalars – single pieces of information– Arrays – lists of information– Hashes – 'look-up' table of information– Special variables, like $_ or $/

Page 20: PERL Programming Basic

Scalars

• Contain single pieces of info• Naming conventions-• Preceded by '$'• No spaces (use '_')• Usually lowercase• e.g. $test_scalar• Store various types including strings and

numbers

Page 21: PERL Programming Basic

Scalars

– Scalar values

$days # the simple scalar value "days"

$days[28] # the 29th element of array @days

$days{'Feb'} # the 'Feb' value from hash %days

$#days # the last index of array @days

Page 22: PERL Programming Basic

Scalars

– Scalar values$abc = 12345;$abc = 12345.67;$abc = 0xffff; # hexadecimal$abc = 0377; # octal$abc = 'a simple string\n';$abc = "a string with a newline\n";

Page 23: PERL Programming Basic

Scalars

• Variables start with $

• There is nothing like reserved words

• $else $if $while are good variables

$a = 123;$b = 55;$c = $a + $b;$d = "kakukk\n";$d = 'kakukk\n' if $c == 178;if( $d eq "kakukk\n" ){ print "Hello World!\n"; }else{ print "This is not a good day!\n";}

Page 24: PERL Programming Basic

Scalars

• ”kakukk\n” is interpolated string• ’kakukk\n’ is NOT interpolated, 8

characters

$a = 123;$b = 55;$c = $a + $b;$d = "kakukk\n";$d = 'kakukk\n' if $c == 178;if( $d eq "kakukk\n" ){ print "Hello World!\n"; }else{ print "This is not a good day!\n";}

Page 25: PERL Programming Basic

Scalars

$n = 1234; # decimal integer$n = 0b1110011; # binary integer$n = 01234; # octal integer$n = 0x1234; # hexadecimalinteger $n = 12.34e-56; # exponential notation

$n = "-12.34e56"; # number specified as a string

$n = "1234"; # number specified as a string

Page 26: PERL Programming Basic

Scalars Example

./script2.pl

• $bit_of_text assigned value with '='

#!/usr/bin/perl -w

#create variable with value of 'Hello World'

$bit_of_text = “Hello World\n”;

print $bit_of text;

Page 27: PERL Programming Basic

Simple Arithmetic

• #!usr/bin/perl -w

$number1 = 6;

$number2 = 3;

$add = $number1 + $number2;

print "Addition answer is $add\n";

$subtract = $number1 - $number2;

print "Subtraction answer is $subtract\n";

Page 28: PERL Programming Basic

Simple Arithmetic

$multiply = $number1 * $number2;print "Multiplication answer is $multiply\n";$divide = $number1/$number2;print “Division answer is $divide";$modulus = $number1 % $number2;print “Modulus answer is $modulus\n”;$exponent = $number1 ** $number2;Print “Exponent is $exponent\n”;

Page 29: PERL Programming Basic

Simple Arithmetic

• Incrementing

• Numbers can be incremented with '++'

$number1 = 3;

$number1++;

print “$number1\n”; # prints 4

Page 30: PERL Programming Basic

Simple Arithmetic

• Decrementing

• Numbers can be decremented with '--'

$number1 = 3;

$number1--;

print “$number1\n”; # prints 2

Page 31: PERL Programming Basic

Expression (1)

• Expression is just as in other programming language

• + - / * arithmetic operators• . string concatenation• == equality numeric comparison• != non equal, <=, <, >, >= numeric

comparison• eq, ne, lt, le, gt, ge string

comparison

Page 32: PERL Programming Basic

Expression (2)

• Precedence as usual

• Use ( and ) to group sub-expressions• condition ? true-exp:false-exp

• , comma operator• = assignment operator• op= operator assignment operators +=, -=, *=, /=, =~

Page 33: PERL Programming Basic

Operating on Assignments

• Sometimes you see code like:– chop ($name = $_);

• Perl is following these rules:– process parenthesized stuff first– the "value" of an assignment statement is the thing on

the left hand side.• Another example you might see:

– …read into the $email variable…($user = $email) =~ s/@sedona\.intel\.com//;

– This only modifies the variable $user• first an assignment• then a substitution

Page 34: PERL Programming Basic

A note on equality

• When we use control structures, we generally compare one thing to another.

• What we are looking for in gneralised terms is "do X if A=B" or "do Y if A=C".

• When comparing scalars you can compare them in a numerical context or a string context

• Equals:$integer == 1$string eq "perl"

• Not equals:$integer != 1$string ne "perl"

Page 35: PERL Programming Basic

Reading Input

• Read input from the Diamond Operator print “Enter a string\n”;

$a = <STDIN>; # read the next line print “You entered:$a\n”;

while(defined($_ = <STDIN>)) { chomp($_); # other operations with $_ here}

Page 36: PERL Programming Basic

IF/ELSE

• A control expression that IF the condition is true, one statement block isexecuted, ELSE a different statement block is exected (ifelse.pl).if (control_expression is TRUE) {

do this;and this;

} else {

do that;and that;

}

Page 37: PERL Programming Basic

ELSIF• if/else is great for yes/no decisions. If you want to test mutltiple

statements you can combine else and if to make 'elsif' (elsif.pl).

if (condition 1 is TRUE) {do this;

} elsif (condition 2 is TRUE) {

do that;} elsif (condition 3 is TRUE) {

do the other;} else { #all tests are failed

do whatever;}

Page 38: PERL Programming Basic

WHILE

• Lets say you want to do a series of actions whilst a certain condition is true (while.pl):

while (expression is true) {

do this;

do that;

do the other; #until no longer true

}

Page 39: PERL Programming Basic

FOR

• The statement people remember from BASIC (or C!)

• An initial expression is set up ($init_val), a test expression is then set up which is tested at each iteration ($init_val < 10). Finally the initial expression is changed with each loop iteration ($init_val++).

for ($init_val; $init_val < 10; $init_val++) {print "$init_val\n";

}

Page 40: PERL Programming Basic

Other Control Structures

• unless/else - like if/else - but unless (false) rather than if(true).

• do/while and do/until - "does" a statement block "while" a condition is evaluated or "does" a statement block "until" expression is evaluated.

• last - allows you to get out of a loop early - e.g. instead of loop finishing when loop conditions are met - a loop can end when conditions internal to theloop are met. See also "next" "redo" and read up on "labelled blocks" for more info.

Page 41: PERL Programming Basic

Arrays

• Using array as an whole array– @a = ( 1, 2, 3, )

• Using array element– $a[0] is scalar 1– (unless $[ is assigned different from 0)

• Arrays are one dimensional– You will learn later how to emulate multi-dim arrays– Arrays are integer-indexed

Page 42: PERL Programming Basic

Arrays

@foo = (1,2,3);

print $foo[1,2],"\n",@foo[1,2],"\n";

OUTPUT:

3

23

$foo[1,2] is the same as $foo[2] (comma operator)

@foo[1,2] is an array slice, is the same as ($foo[1],$foo[2])

You can also use @foo[0..2]

Page 43: PERL Programming Basic

Arrays

• List of information

• Each member of list is an element

• Assign values using '=' and list ()@test_array = (8,”hello”,5.6,$something)

• Refer to elements as$test_array[element number]

Page 44: PERL Programming Basic

Arrays

$test_array[1] is “hello”

• Arrays start at 0!$test_array[0] is 8

• Take care with variable naming!$test_array[0]

is unrelated to.....

$test_array

Page 45: PERL Programming Basic

Assign to Array Slice

@a = ( 1,2,3,4,5,6);print @a,"\n";@a[0..2] = (3,2,1,0);print @a,"\n";@a[0..2] = (0,0);print @a,"\n";OUTPUT:12345632145600456

You can assign value to a slice of an array.

Extra elements are ignored.If there are less elements the array gets shorter.- “ITZ WRONG”. Instead of getting reduced that array element will hold NULL Value.

Page 46: PERL Programming Basic

FOREACH

• Takes a list of values and assigns them to a scalar variable, which executes a block of code (foreach.pl).foreach $element (@list) {

do this;

do that;

do the_other; #until no more $element's

}

Page 47: PERL Programming Basic

FOREACH(2)

• There's a few things missing from this code snippet compared to the previous one (foreach2.pl).

• No specification of $element this time. And yet it still works! Why?foreach (@list) {

do this;

do that;

do the_other; #until no more implied $_'s

}

Page 48: PERL Programming Basic

FOREACH(3)• There is an implied scalar on the previous slide - $_

• The $_ is a special scalar variable - almost like a scratchpad - its a container for information (foreach3.pl). Notice it works both for the foreach AND the print statement.

• Perl knows that if you use foreach (@list) that it is going to assign each element to a scalar - so it will use $_ by default.

foreach $_ (@list) {do this;do that;do the_other; #until no more $_'s

}

Page 49: PERL Programming Basic

FOREACH(4)%hash = (Gabor => 123, Peter => 78, Adam => 10);

# returns an unordered list of the keys@list_of_keys = keys %hash;

# returns an unordered list of the values # useful only if you don't care about the keys

# use it like thisforeach $key (sort keys %hash) {

print "$key $hash{$key}\n"; }

@list_of_values = values %hash;

Page 50: PERL Programming Basic

FOREACH(5)# iterates through the hash# do not change the keys here !!

while (($key, $value) = each(%hash)) {print "$key $value\n";

}

#EXAMPLE %h = (Gabor => 123, Peter => 78, Adam => 10);

foreach $key (sort keys %h) { print "$key $h{$key}\n";

}

Output:Adam 10 Gabor 123 Peter 78

Page 51: PERL Programming Basic

Array Functions

• pop – remove from right hand side

• push – add to right hand side

• shift – remove from left hand side

• unshift – add to left hand side

Page 52: PERL Programming Basic

Array Functions

• Script4 -pop and push• #create an array@an_array = (8,54,78,2,5,6,4)

• POP into variable (variable=4)#pop the last value$pop_test = pop (@an_array);

• PUSH variable back onto array• #push $pop_test back on• push (@an_array,$pop_test);

8 54 78 2 5 6 4

0 1 2 3 4 5 6

8 54 78 2 5 6

0 1 2 3 4 5

8 54 78 2 5 6 4

0 1 2 3 4 5 6

Page 53: PERL Programming Basic

Array Functions• Script4 –shift and unshift#create an array@an_array = (8,54,78,2,5,6,4)

• SHIFT into variable (variable=8)

#shift the first value$shift_test = shift (@an_array);

• UNSHIFT variable back onto array

#unshift $shift_test back onunshift (@an_array,$shift_test);

8 54 78 2 5 6 4

0 1 2 3 4 5 6

8 54 78 2 5 6 4

0 1 2 3 4 5 6

54 78 2 5 6 4

0 1 2 3 4 5

Page 54: PERL Programming Basic

Hashes

• Look-up table of 'Keys' with associated

• 'Values'

• e.g. Hash called 'car'

• KEY VALUE

• COLOUR BLUE

• SIZE BIG

• SPEED REALLY FAST

Page 55: PERL Programming Basic

Hashes

• Keys are arbitrary scalars

• Preceded by %, e.g. %car

• Use keys to retrieve values:

$test_hash{key}=value

$car{colour}=”blue”

Page 56: PERL Programming Basic

What is hash

• Hash is associative array

• Index can be anything not only number• %hash = (1, 2, ”b”, 4); has 2

elements– $hash{1}=2 and $hash{”b”}=4 but also

can be written as

• %hash = ( 1 => 2, ”b” => 4 );

Page 57: PERL Programming Basic

Array Slices from hash

%foo = (1=>2,3=>4,'apple'=>'peach');

print @foo{1,3},"\n";

• OUTPUT:

• 24

@foo{1,3} is a hash slice, is the same as ($foo{1},$foo{2})

It starts with @ and not %

Page 58: PERL Programming Basic

Hash Tables & Key Lookups• Arrays are integer-indexed.• Hashes are string-indexed.• When you don't know what your data is, it's

probably a string.• Array lookups are quick (multiply, add)

– INDEX VALUE0 Banana----1 Chips-----2 Salsa-----3 Wheelchair

– 4 records, 10 chars each = 40 bytes– lookup: byte = (index * 10) + char;

Page 59: PERL Programming Basic

Hash Tables & Key Lookups

• Same lookup time for index 1 or 100000

• But what if the index you want to use is a string like someone's name?– storing names in an array and "searching" is not a good solution

• A really good solution is a Hash Table.

Page 60: PERL Programming Basic

Hash Tables & Key Lookups

• Terminology– The thing you look up with is a key.– The thing you get back is a value.– Together they're a key/value pair.– Sometimes we use hash tables on numeric-

looking things…• Social Security numbers 555-61-6542• Credit Card numbers• Phone Numbers

Page 61: PERL Programming Basic

Hash Tables & Key Lookups

• Example Hash Table– KEY VALUE

602-917-1111 Fred Flintstone602-822-2222 Barney Rubble520-779-5555 George Jetson

• Look up by phone number only now.

• To look up by name, need a separate hash table with names as keys.

Page 62: PERL Programming Basic

Hashes Example

#Hash example - codon translation

use strict;

my %translate;

$translate{'atg'} = 'M';

$translate{'taa'} = '*';

$translate{'ctt'} = 'L';

print "$translate{'atg'}\n";

Page 63: PERL Programming Basic

Hashes Examples

• Building Hash Tables– my(%phonebook) = (

"602-917-1111" => "Fred Flintstone", "602-822-2222" => "Barney Rubble", "520-779-5555" => "George Jetson",);

– Give me a list of all the keys– @phonenums = keys(%phonebook);

• Look up a phone number– $person = $phonebook{"602-822-2222"};

Page 64: PERL Programming Basic

Hashes Examples

• Adding to a Hash– $phonebook{"602-102-2001"} = "Daffy Duck";

• Deleting from a Hash– delete $phonebook{"602-102-2001"};

• Checking for existence of an element– if (exists $phonebook{"602-102-2001"}) …

Page 65: PERL Programming Basic

Hash Functions

• keys

e.g. @codons = keys(%translate)

• values

e.g. @aas = values(%translate)

Page 66: PERL Programming Basic

Printing Hash Contents

my(%phonebook) = ( "602-917-1111" => "Fred

Flintstone", "602-822-2222" => "Barney Rubble", "520-779-5555" => "George Jetson",);

while (($phonenum, $name) = each(%phonebook)) { print "phonenum:$phonenum, name:$name\n";}print "\n";

Page 67: PERL Programming Basic

Strings

• Interpolated and non-interpolated strings

$a = 'apple';print "$a\n";

print '$a\n'; OUTPUT:apple$a\n

Page 68: PERL Programming Basic

Multi-line strings$a = 'zero';

$b = <<END;

this is ba: $a

END

print $b;

$c = <<'END';

this is ca: $a

END

print $c;

OUTPUT:

this is ba: zero

this is ca: $a

Play with the interpolated strings putting expressions into it and experience what is

interpolated and what is not!

Page 69: PERL Programming Basic

Simple string handling operators

• Concatenate strings:$a = ”apple” . ”peach”;

• Automatic conversion$a .= 555;

OUTPUT:

applepeach555

Page 70: PERL Programming Basic

String Concatenation

• Strings can be concatenated with '.'

$string1 = “This”;

$string2 = “ is”;

$string3 = “ easy”;

$string4 = “ so far”;

print $string1.$string2.$string3.$string4;

# prints This is easy so far

Page 71: PERL Programming Basic

Changing Case on Strings

• Applications– when comparing two strings, compare case-

insensitively• force the case, then compare the strings.

– keyword recognition in configuration files– usernames, email addrs, …

• wrong: if ($email eq "pab\@sedona.intel.com")• better: $email =~ tr/A-Z/a-z/;

if ($email eq "pab\@sedona.intel.com")

Page 72: PERL Programming Basic

Changing Case on Strings

• Well written programs observe this rule:– If humans might try it,

your program ought to understand it.• ignore case where it should be ignored• respect case where it should be respected

– output to the user– rewriting config files

Page 73: PERL Programming Basic

Don’t program dangerous!

• $variable

• @variable

• %variable

• Are three different variables!

• And still you can have subroutine with the name.

Page 74: PERL Programming Basic

Use strict

'use strict;'

• Forces you to declare variables before

using them

• Good for when scripts get bigger

• Declarations start with 'my'

e.g. my %translate;

Page 75: PERL Programming Basic

local or my?$my = 'global';

$local = 'global';

&subi;

&bubi;

sub subi {

my $my ='my';

local $local = 'local';

&bubi;

}

sub bubi {

print "$my\n$local\n";

}

OUTPUT:

global

local

global

global

my is really local.

local is actually global, but saves old value.

Page 76: PERL Programming Basic

Calling Subroutinessub name { command(s) }

• Arguments are put into the global array @_• You can $_[$i] or $v = shift• Return value is return expression or just the last expression• Local variables are created using keyword my or local

print printMsg("\nHello world passed");sub printMsg { my ($my_var); local ($local_var); $my_var = 1; print "\nIn sub printMsg, \$my_var:$my_var\n"; print "\nInput arguments to the sub, printMsg:@_\n"; print "Input arguments through implicit array ". $_[0],$_[1],$_[2] . "\n"; return "Hello world received from sub, printMsg";}

OUTPUT:• In sub printMsg, $my_var:1

• Input arguments to the sub, printMsg:• Hello world passed• Input arguments through implicit array• Hello world passed• Hello world received from sub, printMsg

Page 77: PERL Programming Basic

Optimizing Perl - Some Tips• Perl is fast, but not that fast...• Still need to take care with apparently simple things in 'hot' code

• Function/method calls have significant overheads per call.• Copying data also isn't cheap, especially long strings (allocate and copy)• Perl compiles to 'op codes' then executes them in a loop...• The more ops, the slower the code (all else being roughly equal).• Try to do more with fewer ops. Especially if you can move loops into ops.

• Key techniques include:• Caching at many levels, from common sub-expression elimination to web

caching• Functional programming: @result = map { … } grep { … } @data;

• But don't get carried away... only optimize hot code, and only if needed

• Don't optimize for performance at the cost of maintenance. Learn perl idioms.

• Beware "Compulsive Tuning Disorder" - Gaja Krishna Vaidyanatha • And remember that "Premature optimization is the root of all evil" - Donald

Knuth

Page 78: PERL Programming Basic

Built In Functions

• SPLIT• JOIN• LENGTH• SUBSTR• UC/LC• S///• REVERSE and TR• TR/REVERSE

Page 79: PERL Programming Basic

SPLIT

• split can take a scalar and chop it into bits, each individual bit then endsup in an array. The "recognition sequence" is user-defined but not retained (split.pl).

$dna_strand = “AGCTATCGATGCTTTAAACGGCTATCGAGTTTTTTTT";

print "My DNA strand is: $dna_strand\n";print "If we split this using TTTAAA we get thefollowing fragments:\n";@dna_fragments = split(/TTTAAA/,$dna_strand);foreach $fragment (@dna_fragments) {

print "$fragment\n";}

Page 80: PERL Programming Basic

JOIN

• join is the conceptual opposite of split. Lets think of it interms of a DNA ligation with a linker sequence (join.pl):

my ($ligated_fragments);my (@dna_fragments);@dna_fragments=("AGGCTT", "AGCCCAAATT",

"AGCCCCATTA");$ligated_fragments = join ("aaattt", @dna_fragments);print "The fragments have been ligated with an aaatttlinker:\n";print "$ligated_fragments\n";

Page 81: PERL Programming Basic

LENGTH

• length - finds the length of a scalar (or a bit of DNA!) (length.pl).

#!/usr/bin/perl -wuse strict;my ($genome, $genome_length);$genome ="AGATCATCGATCGATCGATCAGCATTCAGCTACTAGC

TAGCTGGGGGGATCATCTATC";$genome_length = length($genome);print "My genome sequence is:\n$genome\nand is$genome_length bases long\n"

Page 82: PERL Programming Basic

SUBSTR

• substr extracts a specified part of a scalar (substr.pl).• substr($scalar, $start_position, $length)

#!/usr/bin/perl -wuse strict;my ($dna_sequence, $substring);$dna_sequence ="AGCTATACGACTAGTCTGATCGATCATCGATGCTGA";$substring = substr ($dna_sequence, 0, 5);print "The first 5 bases of $dna_sequence are:\n$substring\

n";

Page 83: PERL Programming Basic

UC/LC

• uc (uppercase) and lc (lowercase) simply change the case of a scalar (uclc.pl).

#!/usr/bin/perl -wuse strict;my ($mixed_case, $uppercase, $lowercase);$mixed_case = "AgCtAAGggGTCaCAcAAAAaCCCcATTTgcCC";$uppercase = uc ($mixed_case);$lowercase = lc ($mixed_case);print "From $mixed_case we get:\n";print "UPPERCASE: $uppercase\n";print "lowercase: $lowercase\n";

Page 84: PERL Programming Basic

S/// - SUBSTITUTE

• This is proper Perl :-)• The obvious difference between DNA and RNA

is the replacement of T with U.• Lets mimic the transcription of DNA to RNA with

our new found Perl skills.• We can use the substitution operator 's'.• This can convert one element in a scalar to

another element.• This takes the form s/[one thing]/[for another

thing]/• Let's see it in action (transcription.pl).

Page 85: PERL Programming Basic

S/// - SUBSTITUTE (2)

#!/usr/bin/perl -wuse strict;my ($dna_molecule, $rna_molecule);$dna_molecule ="AGCTATCGATGCTTTCGATCACCGGCTATCGAGTTTTT

TTT";print "My DNA molecule is $dna_molecule\n";$rna_molecule = $dna_molecule;$rna_molecule =~ s/T/U/g;print "My RNA molecule is $rna_molecule\n";exit();

Page 86: PERL Programming Basic

=~

• What is that crazy =~ sign?• This is called the "=~ operator".• Allows you to specify the target of a pattern matching

operation (FYI the /[whatever]/ bit is a "matching operator").

• By default matching operators act on $_ ie. if you just saw s/T/U/g; in a program on its own it is acting on $_

• We have $rna_molecule =~ s/T/U/g; - which means perform the s/T/U/g on $rna_molecule. We have re-assigned the effect of the matching operator from $_ to $rna_molecule.

• If you want $rna_molecule to remain unchanged - but alter it in someway - assign it to another scalar first.

Page 87: PERL Programming Basic

REVERSE and TR

• So substitution allows you to change one thing ito another. This is great – we could use the same technique to get the complement of a DNA strand!

• All we have to do is change all the A's to T's, all the G's to C's, all the T's to A's and all the C's to G's.

• Then if we reverse it we get the reverse complement! Or do we? See wrong_revcom.pl.

• I guess the game is given away in the filename that there's something up with this.

• Look closely.• Think about what each line is going to do to the scalar

$DNA.• Tell me why the code is wrong.

Page 88: PERL Programming Basic

REVERSE and TR (2)#!/usr/bin/perl –w

$DNA = "AAAAGGGGCCCCTTTAGCTAGCT";$DNA_UNTOUCHED = $DNA;print "After no substitutions: DNA is : $DNA\n";#substitute all the A's to T's$DNA =~ s/A/T/g;print "After A-T substitution: DNA is : $DNA\n";#substiutute all the G's to C's$DNA =~ s/G/C/g;print "After G-C substitution: DNA is : $DNA\n";#substitute all the C's to G's$DNA =~ s/C/G/g;print "After C-G substitution: DNA is : $DNA\n";#subsitute all the T's to A's$DNA =~ s/T/A/g;print "After A-T substitution: DNA is : $DNA\n";$DNA = reverse ($DNA);print "$DNA_UNTOUCHED reverse complemented is:\n$DNA\n";

Page 89: PERL Programming Basic

REVERSE and TR (3)

The answer

• You can't use sequential substitutions!• WATCH YOUR PERL SYNTAX vs YOUR INTERNAL LOGIC! If

yourthinking is wrong, even if your Perl is correct – your output will be the result of your flawed logic! ie - WRONG!

• Ideally we want make all our substitutions in one statement that understands our needs.

• Come forth the tr operator.• tr is like s, but better for tasks like this• tr/ABCD/dcba would make AABBCCDD into ddccbbaa.• Don't believe me?• Look at revcomp.pl:

Page 90: PERL Programming Basic

TR/REVERSE

#!/usr/bin/perl -wuse strict;my ($DNA, $DNA_UNTOUCHED);$DNA = "AAAAGGGGCCCCTTTAGCTAGCT";$DNA_UNTOUCHED = $DNA;$DNA =~ tr/AGCT/TCGA/;$DNA = reverse ($DNA);print "$DNA_UNTOUCHED has a reverse complementof:\n$DNA\n";exit ();

Page 91: PERL Programming Basic

File Handling

• open(FILEHANDLE,”filename”)• close FILEHANDLE• < FILEHANDLE > read a record from file• print FILEHANDLE expressionlist• read, write, seek, truncate, flock, binmode

Page 92: PERL Programming Basic

File Opening and Closing

open(FH,”file.txt”); #to readopen(FH,”>file.txt”); #to write a new fileopen(FH,”>>f.txt”); #to appendopen(FH,”+<f.txt”); #read/writeopen(FH,”+>f.txt”); #read/write but first

truncateReturn value is non-zero or undef in case of error.

close FH; #closes the file

Page 93: PERL Programming Basic

Reading Records From File

• $/ specifies the record separator, \n by default

• <FH> reads a line (bytes until the next record separator)

• @a = <FH>; # gives all the lines• $/=undef; $file=<FH>; reads the

whole file

Page 94: PERL Programming Basic

Reading Into A Loop

open(FH,"test.pl");

# Returns undef when end of file

while(<FH>){

# By default the buffer is read into $_

print $_;

}

close FH;

Page 95: PERL Programming Basic

Printing To A File

• print FILEHANDLE expression list– print STDERR ”error output”– print STDOUT ”just output”– print ”to STDOUT default”– print FH ”a line into a file\n”

• STDERR, STDOUT are reserved words

Page 96: PERL Programming Basic

Removing New Line

open(FH,"test.pl");# <FH> reads the whole line including the new line at the end of the line.

while(<FH>){

# chomp chops off the new line safely if new line exists.

chomp;

print ”$_\n”;

}

close FH;

Page 97: PERL Programming Basic

Truncate, Seek, Flock$LOCK_SH = 1; # shared lock$LOCK_EX = 2; # exclusive lock$LOCK_NB = 4; # non blocking lock$LOCK_UN = 8; # unlockopen(FH,"+<counter.txt") or open(FH,">counter.txt");flock FH,$LOCK_EX;seek F,0,0;# seek to the start of the file$counter = <FH>;$counter++;# change here to --seek FH,0,0;# seek to the start of the file againtruncate FH,0;# comment this out when decrementingprint FH $counter;flock FH,$LOCK_UN;close FH;print $counter;

Page 98: PERL Programming Basic

Handling Directories

opendir(DIR,”dirname”);

readdir(DIR);

closedir DIR;

• You can not open a directory for writing

Page 99: PERL Programming Basic

Example: Getting the list of the files

opendir(DIR,'.');@filesanddirs = readdir(DIR);closedir DIR;@files = ();for ( @filesanddirs ){ push @files, $_ if -f $_;}for(@files){ print "$_\n";}

Page 100: PERL Programming Basic

Example: Getting all files recursively

$startDirectory = '.';opendir(DIR,$startDirectory);@files = readdir(DIR);closedir(DIR);@dirs = grep( (-d "$startDirectory/$_") && /^[^.]/ ,@files);

while( $#dirs > -1 ){ $cdir = pop @dirs;

opendir(DIR,“sStartDirectory/$cdir"); @f = readdir(DIR); closedir(DIR);

@d = grep( (-d "$startDirectory/$cdir/$_") && /^[^.]/,@f); for( @d ){ push @dirs, "$cdir/$_"; } for( @f ){ push @files, "$startDirectory/$cdir/$_"; } }

Page 101: PERL Programming Basic

Implied Variables

While ($a = <FOO>) { # Read FH $a =~ s/blah/quux/; print $a; }

$_ is implied for reads from FH's, regexp, and print

while (<FOO>) { # $_ instead of $a s/blah/quux/; print; }

Subs return value of last operation if no return (seesorting)Implied variables decrease clutter, but increasecomplexity – use with care!

Page 102: PERL Programming Basic

Global Special Variables

$| Turn off line buffering$_ The default input and pattern-searching space$. Input line number from files which are being read@_ Arguments passes to the function$$ $PID $PROCESS_ID

Process ID (read only)$< $UID $REAL_USER_ID

Real user ID of the process$> $EUID $EFFECTIVE_USER_ID

Effective user id$( $GID $REAL_GROUP_ID

The real group id of the process$) $EGID $EFFECTIVE_GROUP_ID

The effective group id of the process

Page 103: PERL Programming Basic

Global Special Variables (2)

@array = qw(hello world have a nice day);

# $_ is default input and pattern-searching spaceforeach $_ (@array) { print "Prinint default input, \$_:" . $_ . "\n";

if ($_ =~ "hello") { print "\nhello found\n\n"; }}

foreach ('hickory','dickory','doc') {print;

}

print "\n\$\$:Process ID:" . $$ . "\n"; # process ID

Page 104: PERL Programming Basic

Global Special Variables (3)

while (($var_name, $var_value) = each(%ENV)) { print "var_name:$var_name, var_value:$var_value\n";}

foreach (@INC) { # Include path print "Include path:$_\n";}

print "\nPrinting file name \__FILE\__ :" . __FILE__ . "\n";print "\nPrinting line number wich is useful in debugging \

__LINE\__ :" . __LINE__ . "\n";

Page 105: PERL Programming Basic

Global Special Variables (4)

open(MY_FILE,"myperlbits.txt");while( <MY_FILE> ) # print "Line number from $.{ print $., $_, "\n";}close (MY_FILE);

&printMsg("Hello world passed");sub printMsg { # input arguments from @_ print "\nInput arguments to the function, printMsg:@_\n";}

Page 106: PERL Programming Basic

Global Special Variables (5)

$a = "print \"1\\n\";\nwhat is this?";eval $a;print $a,"\n",$@;print "but we run fine\n";$a = "print \"1\\n\";";eval $a;print $a,"\n",$@;

OUTPUT:print "1\n";what is this?syntax error at (eval 1) line 3, at EOFbut we run fine1print "1\n";