Created
February 14, 2012 16:43
-
-
Save anazawa/1828017 to your computer and use it in GitHub Desktop.
Blosxom::Header::Prototype
This file contains hidden or 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 Blosxom::Header::Prototype; | |
| use strict; | |
| use warnings; | |
| use Carp; | |
| use Scalar::Util qw(refaddr); | |
| { | |
| my %prototype_of; | |
| sub can { | |
| my $self = shift; | |
| my $method = shift; | |
| my $id = refaddr( $self ); | |
| return exists $prototype_of{ $id }{ $method }; | |
| } | |
| sub new { | |
| my $class = shift; | |
| my %method = @_; | |
| my $self = bless \do{ my $anon_scalar }, $class; | |
| my $id = refaddr( $self ); | |
| $prototype_of{ $id } = \%method; | |
| return $self; | |
| } | |
| sub AUTOLOAD { | |
| my $self = shift; | |
| my @args = @_; | |
| my $id = refaddr( $self ); | |
| ( my $method = our $AUTOLOAD ) =~ s{.*::}{}o; | |
| if ( $self->can( $method ) ) { | |
| my $code_ref = $prototype_of{ $id }{ $method }; | |
| return $code_ref->( @args ); | |
| } | |
| croak qq{Can't locate object method "$method" via package } . | |
| __PACKAGE__; | |
| } | |
| sub DESTROY { | |
| my $self = shift; | |
| my $id = refaddr( $self ); | |
| delete $prototype_of{ $id }; | |
| return; | |
| } | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment