Introduction to Perl 
| 1. What Is Perl?
2. Course Requisites and Goals 3. Perl References & Resources |
4. State of Perl
5. Taste of Perl 6. Storing & Running Perl Programs |
By completing this course and the associated lab work, you should be able to:
www.cclabs.missouri.edu/things/instruction/perl
www.cis.ufl.edu/perl/
news:comp.lang.perl.announce,
news:comp.lang.perl.misc.
perl.oreilly.com.
www.idgbooks.com.
wdvl.internet.com.
http://language.perl.com/all_about/perltoot.html
http://www.stonehenge.com/merlyn/UnixReview/col13.html
Larry Wall, Perl's creator, chief architect, implementer, and maintainer, released Perl 5 in October 1994. Perl 5 brings object-oriented capabilities, more data structures, new standard interfaces to system and database functions, and handy new functions. Extensions to Perl for windowing, graphics, and database work are being developed in Perl 5. Full Perl 5 documentation with lots of examples is freely available on-line! See below.
The Perl code in this document works under both Perl 4.036 (the last version of Perl 4) and Perl 5.003 (the latest version of Perl 5 at this writing). The Reference Guide identifies with a double-dagger (as in ++) new features of Perl 5.
For major general-purpose Perl applications, particularly CGI scripts
and client or server applications, first check the 'Net for Perl modules
that you can exploit! See the FAQ "Perl 5 Module List" regularly posted
to the Usenet group comp.lang.perl.announce.
Example: Amazing Perl One-Liner That Substitutes Text In Multiple Files
perl -e 's/gopher/World Wide Web/gi' -p -i.bak *.htmlThis command, issued at the Unix prompt, executes the short Perl program specified in single quotes. This program consists of one perl operation; it substitutes for original word "gopher" the phrase "World Wide Web", (globally, ignoring case). The remainder of the Unix command indicates that the perl program should run for each file ending in ".html" in the current directory. If any file "blah.html" needs changing, a backup of the original is made as file "blah.html.bak". Programming Perl lists additional handy one-liners.
For those accustomed to "classic" procedural programming, the "amazing one-liner" above can be expanded in Perl in a style more like C or Pascal:
Example: Global Substitution, The Scenic Route
#!/usr/local/bin/perl -w
# File: go2www
# This Perl program in classic programming style changes
# the string "gopher" to "World Wide Web" in all files
# specified on the command line.
# 19950926 gkj
$original='gopher';
$replacement="World Wide Web";
$nchanges = 0;
# The input record separator is defined by Perl global
# variable $/. It can be anything, including multiple
# characters. Normally it is "\n", newline. Here, we
# say there is no record separator, so the whole file
# is read as one long record, newlines included.
undefine $/;
# Suppose this program was invoked with the command
# go2www ax.html big.basket.html candle.html
# Then builtin list @ARGV would contain three elments
# ('ax.html', 'big.basket.html', 'candle.html')
# These could be accessed as $ARGV[0] $ARGV[1] $ARGV[2]
foreach $file (@ARGV) {
$file = $ARGV[$i];
if (! open(INPUT,"<$file") ) {
print STDERR "Can't open input file $bakfile\n";
next;
}
# Read input file as one long record.
$data=<INPUT>;
close INPUT;
if ($data =~ s/$original/$replacement/gi) {
$bakfile = "$file.bak";
# Abort if can't backup original or output.
if (! rename($file,$bakfile)) {
die "Can't rename $file $!";
}
if (! open(OUTPUT,">$file") ) {
die "Can't open output file $file\n";
}
print OUTPUT $data;
close OUTPUT;
print STDERR "$file changed\n";
$nchanges++;
}
else { print STDERR "$file not changed\n"; }
}
print STDERR "$nchanges files changed.\n";
exit(0);
Questions:
if (! open(OUTPUT,">$file") ) {
die "Can't open output file $file\n";
}
if ($x == 0) {$y = 10;} else {$y = 20;}
$y = $x==0 ? 10 : 20;
$y = 20; $y = 10 if $x==0;
unless ($x == 0) {$y=20} else {$y=10}
if ($x) {$y=20} else {$y=10}
$y = (10,20)[$x != 0];
#!/usr/local/bin/perl -w
if ($#ARGV >= 0) { $who = join(' ', @ARGV); }
else { $who = 'World'; }
print "Hello, $who!\n";
~/labs/hello ~/labs/hello Citizens of Earth hello (If you're in the ~/labs directory.)If you expect to use this program a lot and want to execute it as a command, then you need to do two things.
1. The first line of the program should, after a "#!", specify the location of the perl command, typically as #!/usr/local/bin/perl, as illustrated in the preceding example program. This line can also give command options, like -w (warn of possible inconsistencies). It is advisable to perform a syntax check first by using the command option -cw, i.e.:
perl -cw ~/labs/hello
And then run the program with the following line: ^M
perl ~/labs/hello
2. Set the execute permissions of the program file. To make the file executable (and readable and writable) by only yourself, use a Unix command like:
chmod 700 ~/labs/helloTo make it executable and readable by all enter a Unix command like the following:
chmod a+rx ~/labs/helloYou may also need to use chmod a+x on the directories ~ and ~/labs. See "man chmod" for details and the security implications.