Introduction to perl
description
Transcript of Introduction to perl
Introduction to perlIntroduction to perl
Research Computing/Larry MasonFebruary 17, 2009
its.unc.edu 2
Class MaterialClass 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)
its.unc.edu 3
ssh to linux hostssh 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
its.unc.edu 4
Bring in class filesBring 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
its.unc.edu 5
Running perl scriptsRunning 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
its.unc.edu 6
#!/afs/isis/pkg/isis/bin/perl –wprint STDOUT “Hello, world.”;
print STDOUT “Hello, world.\n”;
print STDOUT ‘Hello, world.\n’;
print STDOUT “\n\n”;
1) Hello, World!1) Hello, World!
its.unc.edu 7
2) Various Variables2) 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”;
its.unc.edu 8
3) pragmas3) pragmas
#!/afs/isis/pkg/isis/bin/perl –w
use strict;
my $string = “/tmp/fileout.$$”;
my @number;
…
# Note the error messages.
its.unc.edu 9
4) Variable flexibility4) 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;
…
its.unc.edu 10
5) String or Number5) 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”;
its.unc.edu 11
6) Files: in and out6) 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”;
its.unc.edu 12
7) Loops: while7) Loops: while
#!/afs/isis/pkg/isis/bin/perl –w
. . .
open SAM, “<$string”;
while (<SAM>) {
print STDOUT “$_”;
}
close SAM;
its.unc.edu 13
8) chop and chomp8) 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
its.unc.edu 14
9)Loops: for9)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
its.unc.edu 15
10) Next10) 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”;
its.unc.edu 16
11) Last11) 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”;
its.unc.edu 17
12) hashes12) 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”;
its.unc.edu 18
13)loops: foreach13)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
its.unc.edu 19
Functions: subFunctions: 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.
its.unc.edu 20
14) sorting14) 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?
its.unc.edu 21
15) sorting by Value15) sorting by Value
#!/afs/isis/pkg/isis/bin/perl –w
…
sub byvalue { $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
# sub byvalue { $number{$b} <=> $number{$a}; }
# sub byvalue { $number{$a} cmp $number{$b}; }
its.unc.edu 22
16) Command Input16) 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;
…
its.unc.edu 23
17) More Command Input
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
its.unc.edu 24
18) Parsing Input18) 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;
its.unc.edu 25
19) More Parsing19) 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;
its.unc.edu 26
Functions:Pattern Matching
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
its.unc.edu 27
20) Regular Expressions II
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”;
its.unc.edu 28
Functions: dieFunctions: 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.
its.unc.edu 29
21) Functions: getpwent
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
its.unc.edu 30
22) Functions: index , length
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”;
its.unc.edu 31
23) Functions: time23) 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.
its.unc.edu 32
Various functionsVarious 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.
its.unc.edu 33
Modules: exampleModules: 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.
its.unc.edu
The EndThe End