Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Scripting Programming Language

Similar presentations


Presentation on theme: "The Scripting Programming Language"— Presentation transcript:

1 The Scripting Programming Language
PERL The Scripting Programming Language

2 Perl is a programming language.
#!/usr/bin/perl 1 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:

3 Perl is quick and easy to write. Perl is versatile:
#!/usr/bin/perl 2 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

4 The script “Example.pl” is:
#!/usr/bin/perl 3 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

5 WHAT ARE PERL VARIABLES ?
#!/usr/bin/perl 4 WHAT ARE PERL VARIABLES ? Three main perl variables: Scalar : $name Array Hash : %name

6 Scalar variables are common and versatile.
#!/usr/bin/perl 5 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

7 Array holds a list of values
#!/usr/bin/perl 6 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 print “Array has size $size\n”; Generates: Hello there number 1 Hello again number 1 Array has size 4

8 Hash holds references to values
#!/usr/bin/perl 7 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 print “Key: $ref = $hash{$ref}\n”; } Generates: Hello there number 1 Key number = 1 Key 34 = Hello there

9 Perl controls line execution with
#!/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)

10 IF – ELSIF - ELSE For example: #!/usr/bin/perl 9 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”; print “Both strings are different\n”;

11 WHILE, FOR LOOPS For example: #!/usr/bin/perl 10 # 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”;

12 FOREACH LOOP For example:
#!/usr/bin/perl 11 FOREACH LOOP For example: # foreach $value ) foreach $value ){ 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.

13 Array @ARGV holds command line arguments To open and read in a file:
#!/usr/bin/perl 12 READING IN DATA 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);

14 To read standard input:
#!/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”;

15 The basic evaluation of regular expression are written as:
#!/usr/bin/perl 14 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.

16 REG EXP EXAMPLES Example: #!/usr/bin/perl 15
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”;

17 Basic elements in regular expressions:
#!/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

18 Number of occurrences:
#!/usr/bin/perl 17 REG EXP Counts Number of occurrences: /x*/ = 0 or more copies of character ‘x’ in a row /x+/ = 1 or more copies of character ‘x’ in a row /x{3}/ = 3 copies of character ‘x’ in a row /(str)*/ = 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)*/ ){

19 #!/usr/bin/perl 18 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/; $str =~ s/^\w{3}/start /; Generates: aabb1cddeeff aabb1cddeef2 start b1cddeef2

20 Split function breaks a string into an array of strings
#!/usr/bin/perl 19 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 $i++ ){ print “$array[$i]\n”; } Generates: A set of strings

21 REMEMBER PRACTICE!!!! We learn by doing !!!! Look at these sites!!!
#!/usr/bin/perl 20 REMEMBER PRACTICE!!!! We learn by doing !!!! Look at these sites!!!


Download ppt "The Scripting Programming Language"

Similar presentations


Ads by Google