autobox example to subscript hashrefs like $x-<y-<z

scrottie on 2006-03-09T21:02:42

perl -e 'use connect; use autobox; use autoboxglue; print config->path->templates, "\n"; '

Explanation: I've been writing config->{path}->{templates}, but I've been working around a Python programmer, and I've been trying to make my code as spiffy as possible, among other things to show off. I decided that this construct was ugly, and it would be much nicer if I could just write config->path->templates instead. After all, since config is a sub, why couldn't path and templates be methods? With autobox, you can call methods in non-blessed references, so this was easy as cake.

Somewhere in the bowels of connect.pm:

*{$caller.'::config'} = sub { return { path => { # ... templates => '/home/improv/templates', }, }; };

autoboxglue.pm:

package HASH;

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

1;