From c89ec958fc7dceb47e7f097ca76cce104e5dde77 Mon Sep 17 00:00:00 2001 From: doy Date: Thu, 9 Apr 2009 01:56:09 -0500 Subject: factor the constructor out into a separate sub --- lib/MooseX/NonMoose.pm | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'lib/MooseX/NonMoose.pm') diff --git a/lib/MooseX/NonMoose.pm b/lib/MooseX/NonMoose.pm index 4f8e2b6..de2660a 100644 --- a/lib/MooseX/NonMoose.pm +++ b/lib/MooseX/NonMoose.pm @@ -6,6 +6,20 @@ Moose::Exporter->setup_import_methods( with_caller => ['extends_nonmoose'], ); +sub constructor { + my $class = shift; + my $meta = Class::MOP::Class->initialize($class); + my $super_new = $meta->find_next_method_by_name('new'); + my $self = $super_new->execute($class, @_); + my $params = $class->BUILDARGS(@_); + my $moose_self = $meta->new_object( + __INSTANCE__ => $self, + %$params, + ); + $moose_self->BUILDALL($params); + return $moose_self; +} + sub extends_nonmoose { my $caller = shift; my @superclasses = @_; @@ -15,19 +29,7 @@ sub extends_nonmoose { Moose::extends($caller, @superclasses); - Class::MOP::Class->initialize($caller)->add_method(new => sub { - my $class = shift; - my $meta = Class::MOP::Class->initialize($class); - my $super_new = $meta->find_next_method_by_name('new'); - my $self = $super_new->execute($class, @_); - my $params = $class->BUILDARGS(@_); - my $moose_self = $meta->new_object( - __INSTANCE__ => $self, - %$params, - ); - $moose_self->BUILDALL($params); - return $moose_self; - }); + Class::MOP::Class->initialize($caller)->add_method(new => \&constructor); } sub init_meta { -- cgit v1.2.3-54-g00ecf