Download PERL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
PERL
The Scripting Programming
Language
#!/usr/bin/perl
WHAT IS PERL ?
• Perl is a programming language.
• Unlike programming languages such
as C and Fortran, Perl programs are
scripts which direct the execution of the
program “perl”.
• Perl execution is similar in form to shell
script execution.
• Look at the introduction:
• http://perldoc.perl.org/perlintro.html
1
#!/usr/bin/perl
WHY USE PERL ?
• Perl is quick and easy to write.
• Perl is versatile:
• File/Sequence Management
• Command Line Execution
• Database
• GUI
• Webpage
• Easy manipulation of sequences in
genetic sequence analysis
2
3
#!/usr/bin/perl
BASIC SCRIPT
• The script “Example.pl” is:
#!/usr/bin/perl
$x = 1;
$y = 1;
$z = $x + $y;
print “$x + $y = $z”;
• The execution of Example.pl is:
> chmod +x Example.pl
> ./Example.pl
1 + 1 = 2
#!/usr/bin/perl
WHAT ARE PERL
VARIABLES ?
• Three main perl variables:
• Scalar : $name
• Array : @name
• Hash : %name
4
#!/usr/bin/perl
PERL SCALAR
• Scalar variables are common and
versatile.
• Scalar variables hold either numbers
or strings
$number = 12.4;
$string = “Value is: “;
print “$string$number\n”;
$string = $string . “number 12”;
print “$string\n”;
• Generates:
Value is: 12.4
Value is: number 12
5
6
#!/usr/bin/perl
PERL ARRAY
• Array holds a list of values
$value = “there”;
@array = (“Hello”,$value,1);
print “$arr[0] $arr[1] number $arr[2]\n”;
$array[2] = 2;
$array[3] = “again”;
print “$arr[0] $arr[3] number “, $arr[2] – 1, “\n”;
$size = @array;
print “Array has size $size\n”;
• Generates:
Hello there number 1
Hello again number 1
Array has size 4
7
#!/usr/bin/perl
PERL HASH
• Hash holds references to values
%hash = ( “number” => 1, 34 => “Hello there” );
print $hash{34}, “ number “, $hash{“number”}, “\n”;
@array = keys %hash;
foreach $ref (@array){
print “Key: $ref = $hash{$ref}\n”;
}
• Generates:
Hello there number 1
Key number = 1
Key 34 = Hello there
#!/usr/bin/perl
8
LINE CONTROL
• Perl controls line execution with
•
•
•
•
if – elsif – else
while
for
foreach
• With comparisons:
• For numbers:
< (less than), > (greater than)
<= (less than or equal), >= (greater than or equal)
== (equal)
• For strings:
eq (equal strings)
ne (not equal strings)
#!/usr/bin/perl
IF – ELSIF - ELSE
• For example:
if( $val < 2 ){
print “Value is less than 2\n”;
}
elsif( $val == 2 ){
print “Value is equal to 2\n”;
}
else{
print “Value is greater than 2\n”;
}
if( $stringA eq $stringB ){
print “Both strings are the same\n”;
}
else{
print “Both strings are different\n”;
}
9
#!/usr/bin/perl
10
WHILE, FOR LOOPS
• For example:
# while( condition )
while( $i < 20 ){
print “The value $i is less than 20\n”;
$i++;
}
# for( initial operation; condition; loop action )
for( $i = 0; $i < 20; $i++ ){
print “The value $i is greater or equal to 0\n”;
print “The value $i is less than 20\n”;
}
#!/usr/bin/perl
11
FOREACH LOOP
• For example:
# foreach $value ( @array )
foreach $value ( @array ){
print “The value $value is in the array\n”;
}
foreach $value ( keys %hash ){
print “$value has value $hash{$value}\n”;
}
• Note that foreach loops traverse the array
from low index to high index.
#!/usr/bin/perl
12
READING IN DATA
• Array @ARGV holds command line arguments
• To open and read in a file:
open(IN,”$fileName”);
$firstLine = <IN>;
while(<IN>){
print $_;
}
close(IN);
• To write to a file:
open(OUT,”> $fileName”);
print OUT “Write this into $fileName\n”;
close(OUT);
#!/usr/bin/perl
13
STANDARD STREAMS
• To read standard input:
print “Write something: “;
$value = <STDIN>;
print “User wrote $value\n”;
• To write to standard output:
print STDOUT “You see this on the command line\n”;
• To write to standard error:
print STDERR “This is written as an error\n”;
14
#!/usr/bin/perl
REGULAR
EXPRESIONS
• Regular expressions are a powerful
tool for locating and modifying data
• The basic evaluation of regular
expression are written as:
$variable =~ /regular expression/
which returns true if the variable holds
to the regular expression and false
otherwise.
#!/usr/bin/perl
REG EXP EXAMPLES
• Example:
if( $string =~ /Jonathan/ ){
print “The string contains Jonathan”;
print “as a substring.\n”;
}
if( $string !~ /Myers/ ){
print “The string does not contain”;
print “Myers as a substring”;
}
15
#!/usr/bin/perl
16
REG EXP Variables
• Basic elements in regular expressions:
/./ = Any single character
/\w/ = Any character a-z, A-Z, 0-9, and ‘_’
/\d/ = Any digit 0-9
/\s/ = space, tab, or enter
/\./ = The character ‘.’
/\\/ = The character ‘\’
• Definition of groups:
/[xyz]/ = Occurrence of either x, y or z
/[^xyz]/ = Occurrence of anything but x, y, or z
• Location of occurrence:
/^x/ = Occurrence of x as the first character
/x$/ = Occurrence of x as the last character
#!/usr/bin/perl
17
REG EXP Counts
• Number of occurrences:
/x*/ = 0
/x+/ = 1
/x{3}/ =
/(str)*/
or more copies of character ‘x’ in a row
or more copies of character ‘x’ in a row
3 copies of character ‘x’ in a row
= 0 or more copies of string ‘str’ in a
row
/$var*/ = 0 or more copies of the string $var in
a row
• Example:
$str = “a string with too much”;
if( $str =~ /o{2}/ ){
print “True!\n”;
}
if( $str =~ /(crazy)*/ ){
print “True!\n”;
}
#!/usr/bin/perl
Substitutions
• Substitutions are regular expressions
that define replacements within strings.
$str =~ s/(regular expression)/(replacement)/
• Examples:
$str = “aabbccddeeff”;
$str =~ s/c/1/;
print “$str\n”;
$str =~ s/.$/2/;
print “$str\n”;
$str =~ s/^\w{3}/start /;
print “$str\n”;
• Generates:
aabb1cddeeff
aabb1cddeef2
start b1cddeef2
18
#!/usr/bin/perl
SPLIT FUNCTION
• Split function breaks a string into an
array of strings
@array = split(/(regular expression)/,$string)
• Example:
$str = “A set of strings”;
@array = split(/\s+/,$str);
for( $i = 0; $i < @array; $i++ ){
print “$array[$i]\n”;
}
• Generates:
A
set
of
strings
19
20
#!/usr/bin/perl
REMEMBER
PRACTICE!!!!
• We learn by doing !!!!
• Look at these sites!!!
• http://perldoc.perl.org/perlintro.html
• http://www.perl.com/pub/a/2000/10/begperl1.html
• http://www.comp.leeds.ac.uk/Perl/start.html