From: Scott McWhirter Date: Fri, 26 Jun 2009 01:25:58 +0000 (+0100) Subject: Make method body load lazily X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=868853dcc5f009222bcea739980f5c18d548eeac;p=gitmo%2FClass-MOP.git Make method body load lazily --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 7da9660..5ac9773 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -517,7 +517,8 @@ Class::MOP::Attribute->meta->add_method('clone' => sub { ## Class::MOP::Method Class::MOP::Method->meta->add_attribute( Class::MOP::Attribute->new('body' => ( - reader => { 'body' => \&Class::MOP::Method::body }, + lazy => 1, + default => \&Class::MOP::Method::body, )) ); diff --git a/lib/Class/MOP/Method/Accessor.pm b/lib/Class/MOP/Method/Accessor.pm index 3891fd0..6dfd22c 100644 --- a/lib/Class/MOP/Method/Accessor.pm +++ b/lib/Class/MOP/Method/Accessor.pm @@ -36,8 +36,6 @@ sub new { # needed weaken($self->{'attribute'}); - $self->_initialize_body; - return $self; } diff --git a/lib/Class/MOP/Method/Generated.pm b/lib/Class/MOP/Method/Generated.pm index 056bc77..d717f83 100644 --- a/lib/Class/MOP/Method/Generated.pm +++ b/lib/Class/MOP/Method/Generated.pm @@ -28,6 +28,16 @@ sub _initialize_body { confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class"; } +sub body { + my $self = shift; + + $self->{'body'} ||= do { + $self->_initialize_body; + }; + + return $self->{'body'}; +} + sub _eval_closure { # my ($self, $captures, $sub_body) = @_; my $__captures = $_[1];