Skip to content

Instantly share code, notes, and snippets.

Created August 28, 2009 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/177313 to your computer and use it in GitHub Desktop.
Save anonymous/177313 to your computer and use it in GitHub Desktop.
{
package Object::MethodChain;
our $open_sesame = [];
AUTOLOAD {
my $self = shift;
my $class = ref($self) || $self;
my @chain = ref($self) ? &$self($open_sesame) : ();
our $AUTOLOAD =~ /.*::(.*)/s or
die "error: invalid method name";
push @chain, {"method", $1, "args", \@_};
bless sub {
if(ref($_[0]) && $open_sesame == $_[0]) {
@chain;
} else {
my $r = $_[0];
for my $pair (@chain) {
my($method, $args) = @$pair{"method", "args"};
$r = $r->$method(@$args);
}
$r;
}
}, $class;
}
DESTROY {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment