Skip Menu |

This queue is for tickets about the Class-MOP CPAN distribution.

Report information
The Basics
Id: 48985
Status: resolved
Priority: 0/
Queue: Class-MOP

People
Owner: Nobody in particular
Requestors: paul.mooney [...] phymatics.co.uk
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: 0.93



Subject: Moose::Meta::Class add_methid only works once
I'm using add_method() on a Moose::Meta::Class to dynamically add a method. I have attached a simple test case to demonstrate this (it uses Test::More). Using Moose version 0.79 and Class::MOP version 0.84 the attached test works. Using Moose version 0.89, Class::MOP version 0.92, it does not.
Subject: test_sub.t
#!/opt/perl-5.8.8/bin/perl Test::go(); package Test; use Moose; use Moose::Meta::Class; use Class::MOP; use Test::More tests => 3; sub go { #print "Moose version = " . $Moose::VERSION . "\n"; #print "Moose::Meta::Class = ". $Moose::Meta::Class::VERSION . "\n"; #print "Class::MOP = ". $Class::MOP::VERSION . "\n"; foreach my $name_space ('a', 'b', 'c') { my $provider = Moose::Meta::Class->create($name_space); my $sub = sub { my ($self, $c, $message) = @_; my $response = { status => 'PONG' }; $c->stash->{response} = $response; }; my $p = $provider->add_method('ping', $sub); my $method = $provider->get_method('ping'); ok($method, "Got my ping method back!"); } }
The example code attached to this bug report creates a very simplistic subroutine repeatedly in a loop and calls add_method() for each object. Perl is clever and optimizes things so it only actually creates *one* subroutine with the same code reference. Internally, one reference maps to one object/class. Since it is trivial to make perl un-optimize things I suggest a POD enhancement to Class::MOP::Package, something like this at the end of add_method(): "NOTE: it is possible to add the same code reference to two or more objects. Internally it will only belong to one of them (the first). This is particularly relevant if you create a simple anonymous subroutine multiple times in a loop and Perl makes an optimization to only create one."