perlquestion
mjbetts
Hi,
<p>
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.
</p>
<p>
I've tried to write the simplest set of code that replicates the problem:
</p>
<p>
In Hello.pm:
<code>
package Hello;
use strict;
use warnings;
use Moose::Role;
use namespace::autoclean;
use Greeting;
sub hello_world {
print "hello world\n";
}
1;
</code>
</p>
<p>
In Greeting.pm:
<code>
package Greeting;
use strict;
use warnings;
use Moose;
use namespace::autoclean;
with 'Hello';
__PACKAGE__->meta->make_immutable;
1;
</code>
</p>
<p>
In Greeting2.pm:
<code>
package Greeting2;
use strict;
use warnings;
use Moose;
use namespace::autoclean;
with 'Hello';
__PACKAGE__->meta->make_immutable;
1;
</code>
</p>
<p>
And then a script test.pl that uses these:
<code>
#!/usr/bin/perl -w
use Greeting2;
use Greeting;
my $greeting = Greeting->new();
print $greeting, "\t", $greeting->does('Hello') ? "does Hello\n" : "does not do Hello\n";
$greeting->hello_world();
</code>
</p>
<p>
When I run test.pl I get the following error:
<pre>
Greeting=HASH(0x8e5da40) does Hello
Can't locate object method "hello_world" via package "Greeting" at ./test.pl line 9.
</pre>
</p>
<p>
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.
</p>
<p>
I'd be really grateful if you could point me in the right direction.
</p>
<p>Thanks,</p>
Matthew