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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: moose role that uses a class that does the role
by duelafn (Parson) on Jun 28, 2012 at 14:14 UTC | |
|
Re: moose role that uses a class that does the role
by tospo (Hermit) on Jun 28, 2012 at 13:29 UTC | |
by mjbetts (Initiate) on Jun 28, 2012 at 14:48 UTC | |
by chromatic (Archbishop) on Jun 29, 2012 at 01:45 UTC | |
by tospo (Hermit) on Jun 28, 2012 at 15:15 UTC | |
|
Re: moose role that uses a class that does the role
by tobyink (Canon) on Jun 29, 2012 at 08:51 UTC |