Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-03-28 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found