Notes for CSC447

Perl

00. Introduction


Perl Intro

Perl is surprisingly complex, lots of layers to learn over time.

Ports for various architectures exist, started on Unix but can run just about everywhere now.


Perl Cult(ure) & Philosophy

Large culture surrounds Perl. From earliest days it was very popular and has grown to do just about anything people want it to. Legions of zealots.

Online information is rampant, www.perl.com, www.perl.org, CPAN, newsgroups. Many Perl "gurus" hang out on the newsgroups and are willing to talk.

"There's More Than One Way To Do It" - Perl offers a variety of operators and flexible syntax, there is often more than one way to the solution to a problem. Perl doesn't want to force a particular way of thinking on you.

"Make the easy things easy, and the hard things possible" - optimized for common tasks, lots of shortcuts for text processing. Modules exist to make things that would be hard easier (networking, database access, CGI, etc).

Baby Talk is ok - Perl is complex enough to take a while to learn.

Practical Extraction and Report Language.

Pathologically Eclectic Rubbish Lister.

Adding to the language is encouraged. If you find something you are doing might be of general utility, code it up as a module to be added to Perl. Collected on CPAN, a few make it into the standard Perl distro.

Perl is the 500-pound language. If it finds a feature it likes, it absorbs it. This is why you can use Tk and Expect (originally from Tcl) in Perl now.

Unix-based language, but has been ported to PC/Mac as well as others. Lots of C-like system calls available, just thinly veiled calls to the real ones. You'll lose some of them on other machines, as well as some of the file/directory manipulation commands.


Perl Applications

Somewhere between Shell and C programming for any number of tasks.


Perl References

Lots of good Perl books out there.

See class web page for pointers to online references.


The Perl Interpreter

When you start a perl script, the interpreter first reads the entire script and parses it into a compiled internal form, then executes.

Syntax checking and other common compiler operations done to entire program before run begins, different than traditionally interpreted (like shell) which can can crash after half of a run due to syntax error midway.

Due to this compilation startup might be a touch slow, but generally not noticable, and scripts run much faster.

Possible to get access to this internal parsed version of code, this is how they developed the perl compiler.

Parrot and Perl 6.


Sample Script

#!/usr/bin/perl -w
# first line invokes Perl interpreter
# -w flag is important, warnings
# note comments like shell

$pattern = shift;        # shift first arg into a var,
                         # was in array @ARGV

while (<>) {             # while we have input line
   chomp;                # rip off newline
   next if /^#/;         # skip comments at start of line
   if (/$pattern/) {     # print if pattern found
      print "found: $_\n";
   }
}


Suffix

Perl scripts typically have their names end in ".pl"