mjbetts has asked for the wisdom of the Perl Monks concerning the following question:
I have a moose role and two classes that do it. However, the role uses one of these classes. Is this kind of circularity allowed? I'm getting an error that I don't understand - an instance does a role but can't find a method defined in that role.
I've tried to write the simplest set of code that replicates the problem:
In Hello.pm:
package Hello; use strict; use warnings; use Moose::Role; use namespace::autoclean; use Greeting; sub hello_world { print "hello world\n"; } 1;
In Greeting.pm:
package Greeting; use strict; use warnings; use Moose; use namespace::autoclean; with 'Hello'; __PACKAGE__->meta->make_immutable; 1;
In Greeting2.pm:
package Greeting2; use strict; use warnings; use Moose; use namespace::autoclean; with 'Hello'; __PACKAGE__->meta->make_immutable; 1;
And then a script test.pl that uses these:
#!/usr/bin/perl -w + + use Greeting2; use Greeting; my $greeting = Greeting->new(); print $greeting, "\t", $greeting->does('Hello') ? "does Hello\n" : "do +es not do Hello\n"; $greeting->hello_world();
When I run test.pl I get the following error:
Greeting=HASH(0x8e5da40) does Hello Can't locate object method "hello_world" via package "Greeting" at ./test.pl line 9.
How come Greeting does Hello but can't locate the hello_world method? If I 'use Greeting' before 'use Greeting2', or if I don't 'use Greeting' in Hello.pm, I don't get the error.
I'd be really grateful if you could point me in the right direction.
Thanks,
Matthew