Hi,

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

In reply to moose role that uses a class that does the role by mjbetts

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.