wwwoffle cached images icons

Kragen Javier Sitaker kragen at pobox.com
Tue Mar 20 14:11:09 EDT 2007


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?  :)

> [rest of cleaner version omitted]

I agree --- that is considerably easier to read!

> 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.  I suppose it would eventually
have dawned on me that you intended the stuff going into @ARGV to get
used for something eventually, and that the "while <>" wouldn't do any
good if it was trying to open and read the directory
/var/cache/wwwoffle/http.

> But the punchline is the implementation of this same thing in
> terms of the File::Find::Rule module from the CPAN:
>     #!/usr/bin/perl
>     use strict;
>     use warnings;
>     use File::Find::Rule;
>     # perl this-script /var/cache/wwwoffle/http > tmp.html
>     @ARGV = File::Find::Rule
>         ->name( 'D*' )
>         ->file
>         ->grep( qr/ \A Content-Type: [ ]* image/jpeg \r\n /ix )
>         ->in( @ARGV );
>     s[ \A (.*) /D ]{ $1 . '/U' }ex for @ARGV;
>     print qq(<img src="$_" width="128" height="128" />\n) while <>;
> You can see that F::F::R is the bee’s knees.

Now that, that is quite remarkable.



More information about the Kragen-discuss mailing list