Skip to content

Instantly share code, notes, and snippets.

@jesboat
Created August 28, 2012 22:10
Show Gist options
  • Save jesboat/3504775 to your computer and use it in GitHub Desktop.
Save jesboat/3504775 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
{
package Base;
sub scan_stash {
my ($for, $stash, $seen) = @_;
if (my $glob = $stash->{$for}) {
if (my $sub = *$glob{CODE}) {
return $sub;
}
}
$seen->{$stash}++
and return;
while (my ($name, $glob) = each %$stash) {
if ($name =~ /::$/
and my $nested = *$glob{HASH})
{
my $found = scan_stash($for, $nested, $seen);
return $found if $found;
}
}
return;
}
sub AUTOLOAD {
our $AUTOLOAD;
$AUTOLOAD =~ s/.*:://;
my $sub = scan_stash($AUTOLOAD, \%::, \my %seen);
$sub or die "$AUTOLOAD: no method by that name!\n";
goto &$sub;
}
}
{
package Foo::Bar;
sub hello {
print "hello: @_\n";
}
}
push @UNIVERSAL::ISA, 'Base';
my $baz = Baz->new;
$baz->hello;
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment