| 1 | package Object::Prototype; | ||||||||||
| 2 | |||||||||||
| 3 | # $Id: Prototype.pm,v 0.2 2007/04/07 01:57:59 dankogai Exp dankogai $ | ||||||||||
| 4 | 3 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Scalar::Util qw(refaddr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Storable (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; | |||||||||
| 12 | 3 | our $DEBUG = 0; | |||||||||
| 13 | 3 | my %constructor_of; | |||||||||
| 14 | my %prototype_of; | ||||||||||
| 15 | |||||||||||
| 16 | 17 | ▶ | sub new($$;$) { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 17 | 17 | my $class = shift; | |||||||||
| 18 | 17 | my $constructor = shift; | |||||||||
| 19 | 17 | my $self = Storable::dclone($constructor); | |||||||||
| 20 | 17 | my $id = refaddr $self; | |||||||||
| 21 | 17 | $constructor_of{$id} = $constructor; | |||||||||
| 22 | 17 | $prototype_of{$id} = {}; | |||||||||
| 23 | 17 | bless $self => $class; | |||||||||
| 24 | 17 | for my $method ( keys %{ $_[0] } ) { | |||||||||
| 25 | 24 | $self->prototype( $method, $_[0]->{$method} ); | |||||||||
| 26 | } | ||||||||||
| 27 | 17 | return $self; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 29 | ▶ | sub prototype($$;$) { | ||||||||
|
Subroutine
prototype: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 31 | 29 | my $self = shift; | |||||||||
| 32 | 29 | my $id = refaddr $self; | |||||||||
| 33 | 29 | my $method = shift; | |||||||||
| 34 | 29 | ▶ | $prototype_of{$id}{$method} = shift if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 29 | return $prototype_of{$id}{$method}; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 2 | ▶ | sub constructor($) { | ||||||||
|
Subroutine
constructor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 39 | 2 | my $self = shift; | |||||||||
| 40 | 2 | my $id = refaddr $self; | |||||||||
| 41 | 2 | return $constructor_of{$id} | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 17 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.0
| |||||||||||
| 45 | 17 | my $id = refaddr shift; | |||||||||
| 46 | 17 | ▶ | carp "DESTROY: $id" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 17 | delete $constructor_of{$id}; | |||||||||
| 48 | 17 | delete $prototype_of{$id}; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 54 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=8 SCAR=21.6
| |||||||||||
| 52 | 54 | my $self = shift; | |||||||||
| 53 | 54 | my $method = our $AUTOLOAD; | |||||||||
| 54 | 54 | $method =~ s/.*:://o; | |||||||||
| 55 | 54 | my $id = refaddr $self; | |||||||||
| 56 | 54 | ▶ | warn "$id -> $method" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 54 | my $code = $prototype_of{$id}{$method}; | |||||||||
| 58 | 54 | ▶ | return $self->$code(@_) if $code; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 26 | ▶ | while ( my $constructor = $constructor_of{$id} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 79 | ▶ | warn "$constructor -> $method" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 79 | $id = refaddr $constructor; | |||||||||
| 62 | $code = ref $constructor eq ref $self | ||||||||||
| 63 | 79 | ▶ | ? $prototype_of{$id}{$method} # Prototypal | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | : $constructor->can($method); # Classical Perl Obj. | ||||||||||
| 65 | 77 | ▶ | return $self->$code(@_) if $code; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | } | ||||||||||
| 67 | 0 | ▶ | confess "unknown method : $method" if !$code; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 3 | 1; | |||||||||
| 71 | __END__ | ||||||||||