Skip to content

Instantly share code, notes, and snippets.

@clkao
Created January 19, 2010 10:33
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 clkao/280833 to your computer and use it in GitHub Desktop.
Save clkao/280833 to your computer and use it in GitHub Desktop.
package Sub::Chained;
use Moose;
sub chained {
__PACKAGE__->new();
}
sub AUTOLOAD {
my $self = shift;
my $name = our $AUTOLOAD;
$name =~ s/^.*://;
@{$self->{context}} = $self->execute( $self->find_code($name),
@{$self->{context}}, @_ );
return $self;
}
sub find_code {
my ($self, $name) = @_;
sub { warn "dummy $name: ".join(',',@_); return 1 };
}
sub execute {
my $self = shift;
my $code = shift;
$code->(@_);
}
__PACKAGE__->meta->make_immutable;
no Moose;
1;
package Sub::Chained::Method;
use Moose;
extends 'Sub::Chained';
has object => (is => "ro");
sub find_code {
my ($self, $name) = @_;
warn $name;
$self->object->can($name);
}
sub execute {
my $self = shift;
my $code = shift;
$self->object->$code(@_);
}
__PACKAGE__->meta->make_immutable;
no Moose;
1;
package main;
my $cnt = 0;
sub foo {
warn join(',',@_);
return ++$cnt;
}
Sub::Chained::chained()->foo->bar;
Sub::Chained::Method->new(object => bless {}, 'main')->foo()->foo()->foo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment