[Phoenix-pm] autobox fun

Brock awwaiid at thelackthereof.org
Tue Mar 14 08:03:38 PST 2006


As an added bonus (besides being prettier), you could later actually
override the hash with real methods, eh? Make conf return an object
instead of a hash, that is.

--Brock

On 2006.03.14.07.59, Scott Walters wrote:
| 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
| 
| _______________________________________________
| Phoenix-pm mailing list
| Phoenix-pm at pm.org
| http://mail.pm.org/mailman/listinfo/phoenix-pm


More information about the Phoenix-pm mailing list