Created
January 19, 2010 10:33
-
-
Save clkao/280833 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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