340 likes | 555 Views
Introduction to perl. Research Computing/Larry Mason February 17, 2009. Class Material. Point web browser to: http://its.unc.edu/Research Click on “ Training ” (in left column) Click on “ ITS Research Computing Training Presentations ” (Bottom paragraph)
E N D
Introduction to perl Research Computing/Larry Mason February 17, 2009
Class Material Point web browser to: http://its.unc.edu/Research Click on “Training” (in left column) Click on “ITS Research Computing Training Presentations” (Bottom paragraph) Click on “Getting Started on Perl” (under General Computing)
ssh to linux host To start ssh using SecureCRT in Windows: Start Programs [Remote Services] SecureCRT Click the Quick Connect icon at the top. (Second icon from left?) Hostname: emerald.isis.unc.edu Login with your ONYEN and password Create a directory for this course mkdir perl_intro [or other name of your choice] cd perl_intro
Bring in class files On the linux host (emerald?) in perl_intro: cp /afs/isis/depts/its/public_html/divisions/rc/training/scientific/perl_Intro/examples/* . ls –l
Running perl scripts ls –l 001hello.pl Shows permissions in left column -rwxr-xr-x means the script can be run by everybody (the “x” means “executable”) chmod 700 *.pl To make all perl scripts runable / executable by you, the owner
1) Hello, World! #!/afs/isis/pkg/isis/bin/perl –w print STDOUT “Hello, world.”; print STDOUT “Hello, world.\n”; print STDOUT ‘Hello, world.\n’; print STDOUT “\n\n”;
2) Various Variables #!/afs/isis/pkg/isis/bin/perl –w $string = “Hello,” . “ world.\n”; # concatenation print STDOUT “$string”; $number = 2009; print STDOUT “the number is $number\n”; $number[0] = “one”; $number[1] = “2”; $number[2] = “three”; print STDOUT “@number\n”;
3) pragmas #!/afs/isis/pkg/isis/bin/perl –w use strict; my $string = “/tmp/fileout.$$”; my @number; … # Note the error messages.
4) Variable flexibility #!/afs/isis/pkg/isis/bin/perl –w use strict; my $file = “/tmp/fileout.$$”; my $string = “Hello, world”; … $number[0] = 14; print STDOUT “$number[0]\n”; $number[0] = substr($string,0,5); print STDOUT “$number[0]\n”; exit; …
5) String or Number #!/afs/isis/pkg/isis/bin/perl –w … my $string = “123456789”; … $number[0] = substr($string,-5,3); print STDOUT “$number[0]\n”; print STDOUT “$string\n”; $number[0] = $string * 3; print STDOUT “$number[0]\n”; $string = “abc”; $number[0] = $string * 3; print STDOUT “$number[0]\n”;
6) Files: in and out #!/afs/isis/pkg/isis/bin/perl –w my $string = “/tmp/fileout.$$”; my $number = 2009; my $record = “”; my @number = (“one”, “2”, “three”); open OUTFILE, “>$string”; print OUTFILE “$number\nnumber[1] is $number[1]\nnumber[2] is $number[2]\n\nOUTFILE is $string”; close OUTFILE; open SAM, “<$string”; $record = <SAM>; print STDOUT “$record”;
7) Loops: while #!/afs/isis/pkg/isis/bin/perl –w . . . open SAM, “<$string”; while (<SAM>) { print STDOUT “$_”; } close SAM;
8) chop and chomp #!/afs/isis/pkg/isis/bin/perl –w . . . open SAM, “<$string”; while (<SAM>) { print STDOUT “$_”; chomp $_; print STDOUT “$_”; chop $_; print STDOUT “$_”; } # close of SAM unnecessary here because program ends here
9)Loops: for #!/afs/isis/pkg/isis/bin/perl –w ... my $last = $#number; for ($i = 0; $i<= $last; $i++) { print OUTFILE “$number[$i]\n”; } # end for each element of the @number array close OUTFILE; open SAM,”<$string”; while (<SAM>) { print STDOUT “$_”; } # end while records from file SAM
10) Next #!/afs/isis/pkg/isis/bin/perl –w my $count = 0; … close ANYFILE; open ANYFILE, “<$file”; while (<ANYFILE>) { chomp $_; $count++; if ( $_ gt “one” ) { next; } print STDOUT “now count is $count\n”; } # end while ANYFILE records print STDOUT “Final count is $count\n”;
11) Last #!/afs/isis/pkg/isis/bin/perl –w … CASE: while (<ANYFILE>) { chomp $_; $count++ if ( $_ eq “two” ) { next CASE;} elsif ( $_ ne “one” ) { print STDOUT “What\’s $_ doing here?\n”; last; } else { print STDOUT “It\’s only $_\n”; } # end else found a one print STDOUT “Record number $count is $_\n”; } # end while ANYFILE records print STDOUT “Final count is $count\n”;
12) hashes #!/afs/isis/pkg/isis/bin/perl –w my $var = “rosebud”; my %number; $number{one} = “one”; $number{‘two’} = “something”; $number{“molly”} = 3; $number{$var} = “4”; print STDOUT “%number\n”; print STDOUT “$number{molly} and $number{rosebud}\n”;
13)loops: foreach #!/afs/isis/pkg/isis/bin/perl –w … my $var = “”; my %number = (‘one’,1,’two’,2,’molly’,3,’rosebud’,4); open ANYFILE, “>$string”; foreach $var ( keys %number ) { print ANYFILE “$number{$var}\n”; } # end foreach index key of %number print STDOUT “$string\n”; # check the contents of the output file $string
Functions: sub $result = &mysub ($param); sub mysub { my $something = $_[0]; return; } Could use “return $something;” with the same result. Value of last expression evaluated is returned by default. Parameters are passed to the subroutine in the array @_ Changing $_[0] would change the value of $param . Subroutine has access to global variables.
14) sorting #!/afs/isis/pkg/isis/bin/perl –w … open ANYFILE, “>$string”; foreach $var ( sort keys %number ) { print ANYFILE “$number{$var}\n”; } # end foreach sorted index key of %number print STDOUT “$string\n”; # what did it sort by?
15) sorting by Value #!/afs/isis/pkg/isis/bin/perl –w … subbyvalue { $number{$a} <=> $number{$b}; } open ANYFILE, “$string”; foreach $var ( sort byvalue keys %number ) { print ANYFILE “$number{$var}\n”; } # end foreach index, sorted by value, keys of %number print STDOUT “$string\n”; # Now check the sequence of records in $string # subbyvalue { $number{$b} <=> $number{$a}; } # subbyvalue { $number{$a} cmp $number{$b}; }
16) Command Input #!/afs/isis/pkg/isis/bin/perl –w … $file = “/tmp/fileout\*”; $string = `/usr/bin/wc $file`; # all in one string @number = `/usr/bin/wc $file`; # each record one element print STDOUT “STRING is $string\n”; print STDOUT “NUMBER STUFF\n@number\n”; exit; …
17) More Command Input #!/afs/isis/pkg/isis/bin/perl –w ... my $var = “”; my $file = “/tmp/fileout\*”; $var = “/bin/ls $file”; open LS, “$var |”; while (<LS>) { print STDOUT “$_”; } # end while ls command output
18) Parsing Input #!/afs/isis/pkg/isis/bin/perl –w … my @F; @number = `/usr/bin/wc $file`; print STDOUT “INPUT $number[0]\n”; @F = split(‘‘,$number[$count]); print STDOUT “ALL F[0] $F[0] F[2] $F[2] F[4] $F[4]\n”; @F = split(‘ ‘,$number[$count],3); print STDOUT “THREE F[0] $F[0] F[2] $F[2]\n”; $#F = 1; print STDOUT “Array of two F[0] $F[0] F[2] $F[2]\n”; exit;
19) More Parsing #!/afs/isis/pkg/isis/bin/perl –w … my $this = my $that = my $trash =0; print STDOUT “INPUT $number[0]\n”; $#F = -1; print STDOUT “F[0] $F[0]\n”; @F = split(/\//,$number[0]); print STDOUT “F[0] $F[0] F[1] $F[1] F[2] $F[2]\n”; ($this, $that, $trash) = split(/tmp/,$number[0],3); print STDOUT “this $this that $that trash $trash\n”; exit;
Functions:Pattern Matching m/PATTERN/gimosx m#PATTERN#gimosx metacharacters \ | ( ) [ { ^ $ * + ? . “^” matches beginning of string “$” matches end of string “\n” newline character “\t” tab character “\d” digit (0-9) “\D” non-digit “\s” whitespace character “\S” non-whitespace chatacter
20) Regular Expressions II #!/afs/isis/pkg/isis/bin/perl –w my $one = my $five = my $fifteen = 0; ($one, $five, $fifteen) = (`/usr/bin/uptime` =~ /(\d+\.\d+)/g); print STDOUT “one is $one, five is $five, fifteen is $fifteen\n”; my $paragraph = “”; my $sentences = 0; $/ = “”; while ($paragraph = <>) { while ($paragraph =~ /[a-z][‘”)]*[.!?]+[‘”)]*\s/g) { $sentences++; } } print STDOUT “$sentences\n”;
Functions: die open OUTFILE, “>$file” or die; open OUTFILE, “>$file” or die “Could not open $file, stopped “; Perl will add “at [pgmname] line [N].” to the die output.
21) Functions: getpwent #!/afs/isis/pkg/isis/bin/perl –w my $name = my $passwd = my $uid = $gid = “”; my $quota = my $comment = my $gcos = “”; my $dir = my $shell = “”; my %uid; ($name, $passwd, $unid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwent; print STDOUT “name = $name and shell = $shell\n”; while (($name, $passwd, $uid) = getpwent) { $uid{$name} = $uid; # add to hash } # end while records in /etc/passwd
22) Functions: index , length #!/afs/isis/pkg/isis/bin/perl –w my $here = my $position = 0; my $string = “abcdefghi”; my $substr = “def”; $here = index ($string, $substr, $position); print STDOUT “$substr is at position $here in $string\n”; $here = length ($string); print STDOUT “The string is $here in length.\n”;
23) Functions: time my $second = my $min = my $hour = my $mday = my $mon = 0; my $year = my $wday = my $yday = my $isdst = 0; ($second, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time); January is month “0”. The year needs 1900 added to it. Sunday is wday “0”. Is Daylight Savings Time is zero for EST. “time” returns the seconds since the Epoch.
Various functions sleep 60; # wait for 60 seconds before going on. system “$command”; # executes the string in $command # in a forked bourne shell and waits for it to finish. tr/[searchlist/replacementlist/cds; $result =~ tr/A-Z/a-z/; # Translates the string in $result # uppercase letters changed to lower case. # output stored in $result. # “c” after “///” means to compliment the searchlist # “d” means delete characters not replaced # “s” means print only one of a sequence all the same undef @F; undef %H; undef $string; # undefines things.
Modules: example use strict; use Getopt::Std; use vars qw($opt_u, $opt_r, $opt_s, $opt_g); getopts(‘u:rsg:’); # colon means parameters “u” and “g” have arguments. # each causes a variable with name “$opt_” prefix. # If r is used the $opt_r variable will be “1”, else “0”. # The “use vars” is a “pragma” that allows this to work with # use strict. # the “qw” is “quote words” and is a way to present a list.