Charles Engelke's Blog

July 8, 2003

Tricks of the Wizards (second half)

Filed under: OSCON 2003 — Charles Engelke @ 7:14 pm

Once I decided not to go back to the Struts tutorial, I looked around to
see if I could sneak in to something else. I saw that
Mark Jason Dominus was giving his
Tricks of the Wizards
Perl talk. I’ve seen it before, but he keeps doing new things. And,
he’s probably the best presenter I’ve ever seen, so I know it will be
interesting.

I came in during some tied hash examples. He does some neat things with
this concept. Tied hashes look like hash variables (which in turn are like
arrays except that the “index” can be any string), but every time you use
them you really end up running code you write. This means you can put a lot
of processing being what seems to be a simple variable name.

The next major topic is autoloading. This is really neat. If a
program calls a function that doesn’t exist in the module, the module can
set things up so that a function called AUTOLOAD will be invoked, rather
than having a program error. AUTOLOAD can figure out what to do and then
try to do it. For a trivial case, this could be used to make calls to
functions in your module all case insensitive.

Dominus didn’t spend much
time on this because attendees wanted to hear about source filtering.
That’s a new topic to me, so I’ll learn something new. A Perl source filter
can process your program source code before Perl deals with it. You could
create your own syntax and have a source filter translate it to valid Perl
syntax before the Perl parser sees it. You could encrypt your program file,
and have the source filter decrypt it so that Perl can run it.

Filters are tricky, because Perl doesn’t parse simply. He even gave an
example of two statements that appear nearly identical, but aren’t:

$x = time / 3 ;   #comment /;
$x = sin  / 3 ;   #comment /;

In the first line #comment and everything following it is a comment. Not
in the second line. That’s because sin is a Perl function, so the
parser figures / 3 ; #comment/ must be a regular expression to apply
to the default variable $_, and after applying the expression, pass the
result to the sin function. He then gave another example of a single line
that treated the second half as a comment depending on the phase of the moon!

Advertisements

Create a free website or blog at WordPress.com.

%d bloggers like this: