To run a Perl program:
Step 1: Create a file name.pl with the program in the example
Step 2: Type: perl name.pl
#!/usr/local/bin/perl if (2 < 3) {print "2 < 3 \n"; print "done \n"; } if (2 > 3) {
print "2 > 3 \n"; print "finished \n"; } print "Now I am really done \n";
Example 1 Explanation:
This is standard C.
Example 2 - if-then-else
#!/usr/local/bin/perl if (2 < 3) { print "2 < 3 \n"; print "done \n"; } if (2 > 3) { print "2 > 3 \n"; print "finished \n"; } print "Now I am really done \n"; if (3 < 2) {print "3 < 2 \n"; print "not done \n"; } elsif (3 == 2) { print "3 = 2 \n"; print "not done \n"; } else { print "3 > 2 \n"; print "done again \n"; }
Example 2 Explanation
Again, this is standard C.
Example 3 For Loop
#!/usr/local/bin/perl print "The first 10 integers are: \n"; for ($count = 1; $count <= 10; $count++) { print "$count\n"; } print "And that's all \n";
Example 3 Explanation
Same as C again
Example 4 - While Loop
print "The first 10 numbers are: \n"; $count = 1; while ($count <= 10) { print "$count \n"; $count++; } print "All done \n";
Example 4 - While Explanation
Same as C.
Example 5 File I/O
First Create a file called ex4.dat containing several lines. Here's mine:
This is just a junky file that has a lot of junk in itNow type in and run the following program
open (MYFILE, "ex4.dat"); #1 while (<MYFILE>) #2 { print "$_\n"; #3 } close (MYFILE); #4 print "file done \n";
Example 5 - File I/O Explanation
#1 The file ex4.dat is given the logical name MYFILE and opened
#2 The While loop reads until and EOF is found. Each time through, the next line up to (and including) the carriage return is read and the characters are assigned to the Perl variable $_.
#3 The value of $_ is printed
#4 The file ex4.dat is closed.
EXERCISE (pass this in). Did you notice that an extra <CR> was inserted between each line. Use the chop command (from last week's lab) to remove the <CR> from the line after it is read in.
Did you lose the last letter on the last line? If so, why?
Example 6 - File I/O from <STDIN>
print "What is your name?"; chop($name = <STDIN>); #1 printf "Hi, $name \n";
Example 6 - <STDIN> Explanation
#1 A line up to and including the carriage return is read from the keyboard and assigned to the variable $name
Example 7 - DIE
First Run this first without a file named ex7.names
Then make an indexed file called ex7.names of names like:
------ This is my ex7.names ------------ 1 Karen A. Lemone 2 Michael J. Smith 3 Nobody ---- End of my ex7.names -------------- # DIE gives an error message when an open fails open (NAMES, "ex7.names") || die "Can't open ex7.names: $!\n"; #1 while (<NAMES>){ #2 ($num, $name) = split(' ',$_); #3 $realid[$num]=$name; #4 print "$realid[$num]\n"; #5 } close NAMES; #6
Example 7 Explanation
#1 File ex7.names is opened and assigned the logical name NAMES. if there is no such file the message between quotes is printed
#2 The file is read line by line. Each line is assigned to $_
#3 The line is split at the first space: the part up to the space is assigned to $num; the rest is assigned to $name
#4 An array $realid is created, indexed by $num with value $name
#5 The current entry of $realid is printed
#6 The file ex7.names is closed
Example 8 - Your turn!
EXERCISE
Add statements to the code in Example 7 that will (1) prompt the user for a number (2) print out the value of $realid for that number. You needn't worry about values out of range.
Example 9 - A useful, but dangerous program!
I wrote the following conversion program, ed2np.pl to copy all the .html files in my course directory beginning with ED to files with the same name, but with ED replaced by NP. Thus, if I had a file called EDassets.html, after running ed2np.html, I also had the same file with the name NPassets.html.
Before you begin this example,
######################## Variables to be fed to this routine eventually $ncd = "NP"; # New course designator $ocd = "ED"; # Old course designator ###################### # Cycle through the files in the directory looking for files = "ocd*.html" opendir(DIR, "."); #Opens the current directory while ($myfile=readdir(DIR) ) #loops through the directory { if ($myfile =~ ".html" && $myfile =~ $ocd) #file = old-course-designator.stuff.html #change this! check for $ocd . at beginning #not just as part of the name! { # Input filename is "ocd.stuff.html" # Output filename is "ncd.stuff.html" @filename = split ($ocd,$myfile); # break off ocd from filename open(NETIN, "$myfile"); $outfile = join($ncd,@filename); open (NETOUT, "> $outfile"); ############### while (<NETIN>){ {print NETOUT $_ ;} }; close NETIN; chmod (755, NETOUT); # System commands can be read (and piped) close NETOUT; } };
Example 10 - My Perl program which posts the "Thanks for your Homework" page.
To understand this, you must also look at the Source for the Homework Form page and the page that is referenced in the "Location:" operator below.
#!/usr/local/bin/perl require 'cgi-lib.pl'; &ReadParse; # This statement reads and parses your homework # When you press "submit" print "Location: http://cs.wpi.edu/~kal/elecdoc/hwresponse.html\n\n"; $email = $in{"email"}; $answer = $in{"Answers"}; open (MYMAILTO, "|mail \"karenl\@evitech.fi\n\""); print MYMAILTO "homework from $email \n" ; print MYMAILTO "$answer\n" ; close (MYMAILTO); # END!!!
Example 10 Explanation It is important that you understand this example. Please ask about anything you don't understand.
Check out directions for creating a guest book. These are essentially the same directions you followed for Lab 2. If you didn't create a guestbook then, try again now.
Now look and perhaps try at the examples on the following pages:
or
Send questions and comments to: Karen Lemone