[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Perl tutorial (was Re: Apache and Frontpage)




Mark Bishop said:
> On Tue, 28 Jul 1998, Steven Pritchard wrote:
> > BTW, for those of you trying to convince people to move away from IIS,
> > I noticed that there is a mod_perl module that implements ASP for
> > Apache/mod_perl.
> 
> I bet it is faster.

Most likely.

> There just something wrong about that. Plain wrong.

:-)

> I've got to learn perl. 

Today is your lucky day.  I'm in the mood to write a short perl
tutorial.  Here goes...

First of all, perl syntax is much like C, C++, and Java.  If you know
any of those languages, perl is the easiest thing in the world to pick
up.  It also borrows a lot from the shells, i.e. "#" is the comment
character, and `/some/command` works more-or-less like you'd expect.

One difference between perl and most other languages is how it
implements typing of variables.  Basically, perl only knows three
types of variables:  scalars, lists (AKA arrays), and hashes (AKA
associative arrays).

In perl, a scalar is the catch-all name for strings, integers, etc.
Scalars are represented as "$" plus the name of the variable, i.e.
$foo.  (This is essentially the same as in the shells.)  Perl
auto-converts between strings and numbers, so the following code all
has equivalent output:

    $foo="10";       $foo=10;         $foo="10a";
    print ++$foo;    print ++$foo;    print ++$foo;

(Of course, all of the above would print "11".)

The next type of variable is the list, which is simply an array of
hashes.  Lists are represented as "@" plus the name of the variable,
i.e. @foo.  Individual elements of @foo are represented as $foo[0] to
$foo[$#foo] (where $#foo gives you the number of the highest element).
Lists can also be represented as a list of scalars in parentheses.
The following code would print "bar" (followed by a linefeed):

    @foo=("foo","bar","baz");
    print "$foo[1]\n";

Hashes are simply arrays with arbitrary strings as indexes.  They are
represented as "%" plus the name of the variable, i.e. %foo.
Individual elements of %foo can be referenced as $foo{'any text'}.

Perl does a lot of work by using special variables.  Many of these are
the same (more-or-less) as in the shells.  (For example, $0, $$, and $?
have the same meaning as in the shells.)  All of these special
variables are documented in the perlvar(1) man page.

One very useful special variable is "$_".  $_ is kind of hard to
explain.  Basically it tends to magically be the piece of data that
you are working on when in a loop, and tends to be the default scalar
that functions operate on.  As a simple example, here is the utility
cat in perl:

    #!/usr/bin/perl

    while (<>) # <foo> is a filehandle.  The null filehandle is magic.
    {
	print; # Prints $_.  The loop reads a line and sets $_ to it.
    }

It is also trivial to implement grep in perl using perl's regular
expression handling.

    #!/usr/bin/perl

    # Get the search pattern from $ARGV[0], the first command line argument.
    $pattern=shift; # shift() works on @ARGV by default

    while (<>) # Magic.  Handles filename arguments, "-", and stdin for free.
    {
	if (/$pattern/)
	{
	    print;
	}
	# The above could be represented as "print if (/$pattern/);",
	# but not as "if (/$pattern/) print;".
    }

There.  That's the basics.  If you want to learn more, get one of the
O'Reilly perl books, or read the (excellent) man pages ("man perl" to
start).

Steve
-- 
steve@silug.org           | Linux Users of Central Illinois
(217)698-1694             | Meetings the 4th Tuesday of every month
Steven Pritchard          | http://www.luci.org/ for more info

--
To unsubscribe, send email to majordomo@silug.org with
"unsubscribe silug-discuss" in the body.