Youll find a large collection of excellent Perl tutorials on the Web at:
http://www.programmingtutorials.com/
The following tutorial requires that you have a basic understanding of the Unix operating system, in particular the ability to use a Unix text editor, like VI or PICO.
perl -e 'print "Hello World\n";'
who | perl -n -e 'print if (/ist/);'
#!/usr/bin/perl print "Hello World\n";
#!/usr/bin/perl $priority = 9; print "Priority is $priority\n";Note that variables are interpolated inside double quotes; not so inside single quotes.
$a = 1 + 2 #Add 1 and 2 and store in $a $a = 3 4 #Subtract 4 from 3 and store in $a $a = 5 * 6 #Multiply 5 and 6 and store in $a $a = 7 / 8 #Divide & by 8 and store in $a $a = 9 ** 10 #Nine to the power of 10 $a = 5 % 2 #Remainder of 5 divided by ++$a #Increment $a and then return it $a++ #Return $a and then increment it --$a #Decrement $a and then return it $a-- #Return $a and then decrement it $a = $b #Assign $b to $a $a += $b #Add $b to $a $a -= $b #Subtract $b from $a
$a = $b . $c #Concatenate $b and $c $a = $b x $c #$b repeated $c times $a .= $b #Append $b onto $a
#!/usr/bin/perl @colours = ('red', 'green', 'blue'); for ($i = 0; $i <= $#colours; $i++) { print "$colours[$i]\n"; }
#!/usr/bin/perl %fruitColours = ('apple','red','banana','yellow','line','green'); foreach $fruit ( keys(%fruitColours) ) { print "$fruit $fruitColours{$fruit}\n"; }
#!/usr/bin/perl %fruitColours = ('apple','red','banana','yellow','line','green'); foreach $fruit ( keys(%fruitColours) ) { print "$fruit $fruitColours{$fruit}"; if ( $fruitColours{$fruit} eq 'red' ) { print "- it\'s my favorite\!\n"; last; } elsif ( $fruitColours{$fruit} eq 'green' ) { print "- not my favorite\n"; } else { print "- I dislike it\n"; } }note the use of last to break out of a loop.
== equal and != not equal > greater than and >= greater than or equal < less than and <= less than or equal
$x=10; print "\$x > 9 \n" if ( $x > 9 );
eq equal and ne not equal to gt greater than and lt less thanAdd the following to your test program
print "\$x lt 9\n" if ( $x lt 9 );
print "\$x=11\n" if ( $x = 11 );In an expression, $x was successfully assigned the value 11. This success was signaled by a true condition.
#!/usr/bin/perl while(<>) { if(/ist/) { print $_; } };
Who |./whoist.pl
#!/usr/bin/perl open(IST, ">istlogons"); while(<>) { if(/ist/) { print IST $_; }; } close IST;If we had used >> instead of a single > we would be opening a file for appending.
#!/usr/bin/perl open(IST, "<istlogons") or die "Cannot open input file istlogons"; @input=<IST>; print @input; close IST;Note the use of the or in the open statement. If the open fails, the or will force the evaluation of the right hand side of the statement.
@input = `cat istlogons`;Change your program to use this technique.
#!/usr/bin/perl $name = "SamSlate\@ist.uwaterloo.ca"; # simple pattern if ( $name =~ /Sam/ ) { print "Name contains Sam\n"};Note that the pattern operator =~ assigns the target to the search.
# case insensitive if ( $name =~ /sam/i ) { print "Name contains sam\n"}; # alternative characters if ( $name =~ /[Ss]am/i ) { print "Name contains Sam or sam\n"}; # starts with if ( $name =~ /^S/ ) { print "Name begins with an S\n"}; # return matching value if ( $name !~ /^S/ ) { print "Name does not begin with an S\n"}; # return matching value # Brackets may be used to return values that match the enclosed patterns. if ( $name =~ /(.*)\@(.*)/ ) { print "Name is $1 domain $2\n"}; # Note the dot . matches any character and the * repeats the match 0 or # more times. Use + for 1 or more times. # We can also match alternate patterns using the or operator |. # If $name starts with either Sam or Jack, then: if ( $name =~ /(SamSlate|Jack.*)\@(.*)/ ) { print "Name is $1 domain $2\n"};
#!/usr/bin/perl $_ = "pattern matching is a black art"; s/black art/confusing/; print "$_ \n";Note that the substitution operator is operating on the default variable $_.
#!/usr/bin/perl $sentence = "pattern matching is a black art"; $sentence =~ s/black art/confusing/; print "$sentence \n";
$sentence =~ s/at/rat/g;
#!/usr/bin/perl $_ = "Pattern matching is a Black Art"; tr/A-Z/a-z/; print "$_ \n";Run it and see what happens.
while (<>) { chop; # avoid \n on last field @array = split(/,/); ... }
#!/usr/bin/perl while ( <> ) { &GetWord; print "$firstword \n"; } sub GetWord { ($firstword, $otherwords) = split(/ /,$_); return; }
mkdir public_html cd public_html
<html> <body> <h1>This is My Test Form</h1> <FORM METHOD=GET ACTION="cgi-bin/firstform.pl"> What is your name? <INPUT TYPE="text" NAME="name"><BR> <INPUT TYPE="submit" VALUE="Submit"> <INPUT TYPE="reset" VALUE="Reset"> </FORM> </body> </html>
chmod o+x firstform.html
mkdir cgi-bin chmod o+x cgi-bin
SetHandler cgi-script
#!/usr/bin/perl push (@INC, "/software/www_server/data/cgi-bin"); require "cgi-lib.pl"; &ReadParse(*input); print "Content-type: text/html\n\n"; print "Hello, ", $input{"name"}, "\n";
push (@INC, "/software/www_server/data/cgi-bin"); - Add the cgi-bin directory to the set of libraries that Perl searches. require "cgi-lib.pl"; - include the routines found in this library &ReadParse(*input); - execute the ReadParse routine, creating an array called input of values that have been passed to the program. print "Content-type: text/html\n\n"; - print the standard html header followed by a null line. print "Hello, ", $input{"name"}, "\n"; - print the value associated with the "name" parameter.
<html> <body> <h1>Course Survey</h1> <FORM METHOD=GET ACTION="cgi-bin/survey.pl"> What is your name? <INPUT TYPE="text" NAME="name"><BR> What was your evaluation of the course? <SELECT NAME="evaluation"> <OPTION SELECTED>good <OPTION>satisfactory <OPTION>poor </SELECT><BR> <INPUT TYPE="submit" VALUE="Submit"> <INPUT TYPE="reset" VALUE="Reset"> </FORM>
#!/usr/bin/perl push (@INC, "/software/www_server/data/cgi-bin"); require "cgi-lib.pl"; &ReadParse(*input); print "Content-type: text/html\n\n"; $recipient = "ist01\@watserv1"; $mailprog = '/usr/bin/mail'; open (MAIL, "|$mailprog $recipient"); print MAIL "Subject: Survey Using PERL to Process Your Web Form\n"; print MAIL "---------------------------------------------------\n"; foreach $var ( keys(%input) ) { print MAIL "$var : $input{$var} \n"; } close MAIL;Notice that the program will pick up whatever fields are defined on the form. Program is available at /u/ist01/public_html/cgi-bin