[Phoenix-pm] autobox fun

Scott Walters scott at illogics.org
Mon Mar 13 23:59:40 PST 2006


Hi all,

Just had to share this stupid Perl trick.  

I've been doing some Perl along side a Python programmer and so I've been
trying to think of ways to get his goat... by making Perl both elegant
looking and also by showing off it's expressive power.  The autobox
module no longer requires a patch to the Perl interpreter (yay!), so I
started using it in production.  One thing that bugged me was how site
config data was accessed... I had been writing conf->{path}->{webroot}
and stuff like that.  That's ugly.  conf is exported to the module and
is a sub that returns a hashref.  I'd rather write conf->path->webroot.
Here's some glue to make that work:

(in autoboxglue.pm:)

package HASH;

sub AUTOLOAD :lvalue {
    my $method = $AUTOLOAD; $method =~ s/.*:://;
    return if $method eq 'DESTROY';
    my $hashref = shift;
    $hashref->{$method};
}

1;

It's very simple... it takes method calls on hashrefs (that aren't otherwise
blessed), extracts the method name, evaluates to the result of that subscripting
that hashref with the method name called on it.

I posted this to my blog too, so sorry to dup it.

Cheers,
-scott



More information about the Phoenix-pm mailing list