| 1 | package Class::LazyObject; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Carp qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use vars '$AUTOLOAD'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | #We want to inflate calls to methods defined in UNIVERSAL, but we implicitly inherit from UNIVERSAL. | ||||||||||||||||||||||||||||
| 15 | #As long as we predeclare the methods here, they will override those in UNIVERSAL, but since they are never defined, AUTOLOAD will be called: | ||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use subs grep {defined UNIVERSAL::can(__PACKAGE__, $_)} keys %UNIVERSAL::; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub AUTOLOAD | ||||||||||||||||||||||||||||
| 19 | 108 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||
| 20 | 108 | my $self = $_[0]; #don't shift it, since we will need to access this directly later. | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 108 | $AUTOLOAD =~ /.*::(\w+)/; | |||||||||||||||||||||||||||
| 23 | 108 | my $method = $1; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 108 | ▶ | my $class_method = (ref($self) || $self).'::Methods';#call all class methods on this. | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 108 | ▶ | if (($method eq 'new') && !ref($self)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method eq "new" and not ref $self
MC/DC50%1/2
$method eq "new" and not ref $self
$method eq "new"
not ref $self
Truth table66%2/3
A and not(B)
A= $method eq "new"B= not ref $self
| |||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | #new was called on a class, rather than an object, so we should actually construct ourselves, rather than passing this to whatever we're proxying. | ||||||||||||||||||||||||||||
| 30 | 40 | return $class_method->new(@_); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 68 | ▶ | ref($self) or return $class_method->$method(@_[ 1 .. $#_ ]); #If this was called as a class method, pass it on to ::Methods but don't pass OUR package name. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 50 | ▶ | print "Lazy...\n" if $class_method->get_classdata('debug'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 50 | my $object; #this is the object we will eventually replace ourselves with. | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 50 | ▶ | if ( ref($$self) && UNIVERSAL::isa($$self, $class_method->get_classdata('class')) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $$self and UNIVERSAL::isa($$self, $class_method->get_classdata("class"))
MC/DC50%1/2
ref $$self and UNIVERSAL::isa($$self, $class_method->get_classdata("class"))
ref $$self
UNIVERSAL::isa($$self, $class_method->get_classdata("class"))
Truth table66%2/3
A and B
A= ref $$selfB= UNIVERSAL::isa($$self, $class_method->get_classdata("class"))
| |||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | 10 | $object = $$self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else | ||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | 40 | $object = $class_method->get_classdata('inflate')->($class_method->get_classdata('class'), $$self); | |||||||||||||||||||||||||||
| 46 | 40 | $$self = $object; #don't create this again. | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 50 | $_[0] = $object; #replace ourselves with the object. | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | goto ( | ||||||||||||||||||||||||||||
| 52 | 50 | ▶ | UNIVERSAL::can($_[0], $method) || | ||||||||||||||||||||||||||
|
Condition100%5/5
UNIVERSAL::can($_[0], $method) || $class_method->_prepareAUTOLOADRef($_[0], ref($_[0]) . '::' . $method) || Carp::croak(sprintf(q[Can't locate object method "%s" via package "%s" ], $method, ref $_[0]))
UNIVERSAL::can($_[0], $method) or $class_method->_prepareAUTOLOADRef($_[0], ref($_[0]) . '::' . $method)
MC/DC100%2/2
UNIVERSAL::can($_[0], $method) || $class_method->_prepareAUTOLOADRef($_[0], ref($_[0]) . '::' . $method) || Carp::croak(sprintf(q[Can't locate object method "%s" via package "%s" ], $method, ref $_[0]))
UNIVERSAL::can($_[0], $method)
$class_method->_prepareAUTOLOADRef($_[0], ref($_[0]) . '::' . $method)
Truth table100%3/3
A or B
A= UNIVERSAL::can($_[0], $method)B= $class_method->_prepareAUTOLOADRef($_[0], ref($_[0]) . '::' . $method)
| |||||||||||||||||||||||||||||
| 53 | $class_method->_prepareAUTOLOADRef($_[0], ref($_[0]).'::'.$method) || #UNIVERSAL::can can't detect if a method is AUTOLOADed, so we have to. | ||||||||||||||||||||||||||||
| 54 | Carp::croak(sprintf qq{Can\'t locate object method "%s" via package "%s" }, $method, ref $_[0] )#Error message stolen from Class::WhiteHole | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub DESTROY #You won't AUTOLOAD this! Muahahaha! | ||||||||||||||||||||||||||||
| 59 | 40 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 40 | undef; #This is here because certain perl versions can't handle completely emtpy subs. | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #class method to see whether something is lazy? | ||||||||||||||||||||||||||||
| 65 | #class methods for original isa and can | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #--------- | ||||||||||||||||||||||||||||
| 68 | package Class::LazyObject::Methods; | ||||||||||||||||||||||||||||
| 69 | #stick all of our class methods here so we don't pollute Class::LazyObject's namespace. | ||||||||||||||||||||||||||||
| 70 | #everything in this class should be called as class methods, NOT object methods. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | ▶ | use Carp::Clan '^Class::LazyObject(::|$)'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 6 | ▶ | use Class::Data::TIN qw(get_classdata); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 6 | ▶ | use Class::ISA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub _findAUTOLOADPackage | ||||||||||||||||||||||||||||
| 77 | 16 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_findAUTOLOADPackage: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 78 | #Takes 1 argument, either an object or the name of a package. | ||||||||||||||||||||||||||||
| 79 | #Returns the name of the package containing the sub AUTOLOAD that would be called when $first_arg->AUTOLOAD was called | ||||||||||||||||||||||||||||
| 80 | #In other words, it traverses the inheritance hierarchy the same way Perl does until it finds an AUTOLOAD, and returns the name of the package containing the AUTOLOAD. | ||||||||||||||||||||||||||||
| 81 | #Returns undef if AUTOLOAD is not in the inheritance hierarchy. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 16 | shift;#Don't care about our package name. | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 16 | my $object_or_package = shift; | |||||||||||||||||||||||||||
| 86 | 16 | ▶ | my $orig_class = ref($object_or_package) || $object_or_package; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $object_or_package || $object_or_package
MC/DC0%0/2
ref $object_or_package || $object_or_package
ref $object_or_package
$object_or_package
Truth table33%1/3
A || B
A= ref $object_or_packageB= $object_or_package
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 16 | ▶ | return undef unless UNIVERSAL::can($orig_class, 'AUTOLOAD'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 16 | my @classes = (Class::ISA::self_and_super_path($orig_class), 'UNIVERSAL'); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 16 | my $package; | |||||||||||||||||||||||||||
| 93 | 16 | foreach my $class (@classes) | |||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | 6 | ▶ | no strict 'refs';#Symbol table munging ahead | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 24 | $package = $class; | |||||||||||||||||||||||||||
| 97 | 24 | ▶ | last if defined(*{$package.'::AUTOLOAD';}{CODE}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 16 | return $package; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub _prepareAUTOLOADRef | ||||||||||||||||||||||||||||
| 104 | 19 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_prepareAUTOLOADRef: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 105 | #Takes 2 arguments: | ||||||||||||||||||||||||||||
| 106 | # either an object or the name of a package | ||||||||||||||||||||||||||||
| 107 | # the fully qualified method name to make AUTOLOAD think it was called as a result of | ||||||||||||||||||||||||||||
| 108 | #Sets the appropriate package's $AUTOLOAD so that when the AUTOLOAD method is called on the first argument, it will think it was called as a result of a call to the method specified by the second argument. | ||||||||||||||||||||||||||||
| 109 | #Returns the result of (UNIVERSAL::can($first_arg, 'AUTOLOAD')); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 19 | my $class = shift; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 19 | my ($object, $method) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 19 | ▶ | if (UNIVERSAL::can($object, 'AUTOLOAD'))#no point in doing any of this if it can't AUTOLOAD. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 16 | my $package = $class->_findAUTOLOADPackage($object); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 16 | *{$package.'::AUTOLOAD'} = \$method; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 19 | return UNIVERSAL::can($object, 'AUTOLOAD'); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | #defaults, these are overridable when someone calls ->inherit | ||||||||||||||||||||||||||||
| 129 | Class::Data::TIN->new(__PACKAGE__, | ||||||||||||||||||||||||||||
| 130 | 24 | ▶ | inflate => sub {return $_[0]->new_inflate($_[1]);}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 6 | debug => 0, | |||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub inherit | ||||||||||||||||||||||||||||
| 135 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
inherit: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 136 | #calls to Class::LazyObject->inherit are forwarded here. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 6 | my $class = shift; #don't care about our own package name. | |||||||||||||||||||||||||||
| 139 | 6 | my %params = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 6 | my @required_params = qw(inflated_class deflated_class); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 6 | foreach my $param (@required_params) | |||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 11 | ▶ | croak "You did not pass '$param', which is a required parameter." unless exists $params{$param}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 5 | my %param_map = ( #keys are key names in the parameters passed to this function. Values are corrisponding class data names. | |||||||||||||||||||||||||||
| 149 | inflated_class => 'class' | ||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 5 | my %class_data = %params; | |||||||||||||||||||||||||||
| 153 | 5 | delete @class_data{keys %param_map, 'deflated_class'};#we'll stick these in with their appropriate names: | |||||||||||||||||||||||||||
| 154 | 5 | @class_data{values %param_map} = @params{keys %param_map};#pass the parameters whose names have changed | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 5 | my $method_package = $params{deflated_class}.'::Methods'; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||
| 160 | 6 | ▶ | no strict 'refs'; #more symbol table munging | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 5 | push(@{$method_package.'::ISA'}, __PACKAGE__); #Create a package to hold all the methods, that inherits from THIS class, or add this class to its inheritance if it does exist. #Should this be $class instead of __PACKAGE__ | |||||||||||||||||||||||||||
| 162 | #^Push is used instead of unshift so that someone can override their ::Methods package with its own inheritence hierarchy, and methods will be called here only AFTER Perl finds they don't exist in the overridden ISA. | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 5 | Class::Data::TIN->new($method_package, %class_data); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | sub new | ||||||||||||||||||||||||||||
| 169 | 40 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 170 | 40 | my ($own_package, $class, $id) = @_;#first argument is this method's, class, not the lazy object's | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 40 | ▶ | if (ref($id) && UNIVERSAL::isa($id, $own_package->get_classdata('class'))) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $id and UNIVERSAL::isa($id, $own_package->get_classdata("class"))
MC/DC0%0/2
ref $id and UNIVERSAL::isa($id, $own_package->get_classdata("class"))
ref $id
UNIVERSAL::isa($id, $own_package->get_classdata("class"))
Truth table33%1/3
A and B
A= ref $idB= UNIVERSAL::isa($id, $own_package->get_classdata("class"))
| |||||||||||||||||||||||||||||
| 173 | { | ||||||||||||||||||||||||||||
| 174 | 0 | croak "A Lazy Object's ID cannot be a an object of same class (or of a class inherited from the one) it is proxying!"; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 40 | return bless \$id, $class; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 6 | 1; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | #LAUNDRY LIST: | ||||||||||||||||||||||||||||
| 183 | #LAZYNESS, impatience, hubris | ||||||||||||||||||||||||||||
| 184 | #should we document the $AUTOLOAD persistence thingy as a caveat? | ||||||||||||||||||||||||||||
| 185 | #CALLING AUTOLOAD on inflate | ||||||||||||||||||||||||||||
| 186 | #CAVEAT: can't distinguish between no id and an id of undef. | ||||||||||||||||||||||||||||
| 187 | # -solve by storing a Class::LazyObject::NoID object instead of undef? | ||||||||||||||||||||||||||||
| 188 | #Does goto propogate scalar/list context? | ||||||||||||||||||||||||||||
| 189 | #Lvalue subs? | ||||||||||||||||||||||||||||
| 190 | #PROBLEM: Can't inherit from a lazy object (that has already called inherit) it right now without calling inherit because the corrisponding ::Methods class hasn't been created yet. Instead of being completely nonpolluting, can we create a method with a long convoluted name like __________Class_____LazyObject______Find____Methods and use the NEXT module to make redispatch any calls of it that are object method calls instead of static class calls? | ||||||||||||||||||||||||||||
| 191 | #Does ORL's can work correctly for classes that inherit from an unrealized class? | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||