Dieser Post wurde aus meiner alten WordPress-Installation importiert. Sollte es Darstellungsprobleme, falsche Links oder fehlende Bilder geben, bitte einfach hier einen Kommentar hinterlassen. Danke.
I'm working on one Moose-based project and ran into bad difficulties yesterday. Suspicious compile time errors, default value subroutines being called way too early and with an empty hash reference as argument instead of an object. It took me hours and some hints from #moose on irc.perl.org to find a workaround and finally today, I was able to reproduce the problem.
The script is very simple:
#!/usr/bin/perlLoad two modules, create two objects and print them.use MyMoose::Project::Template;use MyMoose::Project;
print "project = ".($project = MyMoose::Project->new(pk => 1));print "template = ".($template = $project->Template);
The first module file MyMoose/Project.pm doesn't do much more than creating a Template property:
use MooseX::Declare;use Method::Signatures::Modifiers;The second module file MyMoose/Project/Template.pm doesn't need much at all:class MyMoose::Project { use MyMoose::Project::Template;
has 'pk' => (is => 'rw', isa => 'Num', required => 1);
has 'Template' => ( is => 'ro', isa => 'MyMoose::Project::Template', lazy => 1, default => sub { warn join(':',(caller)[1,2]).' '.Dumper(@_); return MyMoose::Project::Template->new( project_pk => $_[0]->pk, project => $_[0]); }, );}
1;
use MooseX::Declare;use Method::Signatures::Modifiers;class MyMoose::Project::Template { use MyMoose::Project; # Required lateron by cutted code parts}
1;I shortend both files here to reduce the size of this blog post. The complete files are available in the download package.
Running moose.pl returns an error:
$ ./moose.pl reader MyMoose::Project::Template (defined at MyMoose/Project.pm line 33):6 $VAR1 = {};Can't call method "pk" on unblessed reference at MyMoose/Project.pm line 31.Compilation failed in require at ./moose.pl line 3.BEGIN failed--compilation aborted at ./moose.pl line 3.I colored the error message lines and the referenced lines in the source code.
The problem doesn't appear if the use lines in the script are switched. Loading MyMoose::Project before MyMoose::Project::Template solves the problem. The problem appeared in my real word project because the second-level-Module was loaded by some old code snippets still waiting for being refactored.
The solution for the project: Rename the property Template to template which violates the project rules (I didn't write them) but solves the namespace collision between the package/class MyMoose::Project::Template and the Template method of MyMoose::Project.
The problem doesn't affect real Perl, a pure Perl sample is also part of the download package.
Noch keine Kommentare. Schreib was dazu