wwwoffle cached images icons

A. Pagaltzis pagaltzis at gmx.de
Mon Mar 26 08:10:05 EDT 2007


* Kragen Javier Sitaker <kragen at pobox.com> [2007-03-20 19:15]:
> Aristotle Pagaltzis writes:
> > * Kragen Javier Sitaker <kragen at pobox.com> [2006-11-11 09:37]:
> > > # Generate a really big page of small inline JPEG images from a
> > > [fugly code omitted]
> > A cleaner version that crawls the filesystem without external aid:
> > [most of cleaner version omitted too]
> >         open my $fh, '<', $filename or die "Can't open $filename: $!";
> 
> You don't like use Fatal?  :)

So-so. I think its output format is pretty awful (and generating
that doubles the otherwise small module’s code!), though it is
tolerable. So I do use Fatal when it saves me substantial work,
but not for just one or two `open`s.

> > This code uses a minor trick: the <> operator will open and
> > read all files listed in @ARGV sequentially, so the code
> > stuffs the names of the data files of interest into that
> > array, then turns them into URL file names, then uses the
> > diamond operator to read them.
> 
> Yeah, I think that part would have taken me a while to figure
> out without the explanation --- although I've used while (<>) a
> thousand times, I've never used it to read a bunch of filenames
> the Perl program itself had stuffed into @ARGV.

Interesting; I picked it up from the community-accepted canonical
idiomatic way to write a `slurp` function:

    sub slurp {
        my ( $filename ) = @_;
        local ( @ARGV, $/ ) = $filename;
        return <>;
    }

Here you end up with a single-entry `@ARGV` and an undefined `$/`
for the duration of the function, which causes `<>` to gobble up
the file whose name was just put into `@ARGV`. (Sidenote since we
mentioned Fatal previously: when using `<>`, Perl will supply its
own (though non-fatal) error messages.)

(Actually, though, that is unsafe: the list of variables to
localise should  read `@ARGV, $ARGV, ARGV, $/`. Alternatively, in
Perl 5.8 and newer, you can localise the entire ARGV glob:

    local ( *ARGV, $/ ) = [ $filename ];

But globs are an ugly wart in Perl 5, and I like to pretend they
don’t exist wherever possible; not to mention how much subtlety
is involved in how/why that line work on top of the idioms in the
rest of the snippet. (I’m not going to explain it here.) So I
prefer the longer version, which has the bonus that it will run
on old perls.)

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>


More information about the Kragen-discuss mailing list