Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Moose: does not export its sugar to the 'main' package

by Angharad (Pilgrim)
on Jun 29, 2010 at 16:14 UTC ( #847147=perlquestion: print w/ replies, xml ) Need Help??
Angharad has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've been trying to teach myself Moose today and am following a tutorial.
However, I've come across this error and can't find any explanation as to what it means:
Mouse does not export its sugar to the 'main' package. Can't locate object method "new" via package "Mouse" at test.pl line 2 +2.
Heres the relevant code. Any pointers in the right direction appreciated!!!
package Animal; use Moose::Role; has 'name' => (is => 'rw'); has 'color' => (is => 'rw', default => sub { shift->default_colour }); requires 'default_colour'; sub speak { my $self = shift; print $self->name, " goes ", $self->sound, "\n"; } # sub sound { confess shift, " should have defined sound!"; } requires 'sound'; no Moose::Role; 1;
package Mouse; use Moose; with 'Animal'; sub default_colour { 'white'; } sub sound { 'squeak'; } after 'speak' => sub { print "[but you can barely hear it!]\n"; }; no Moose; 1;
#!/usr/local/bin/perl -w use strict; use Moose; use Animal; use Horse; use Sheep; use Mouse; my $talking = Horse->new(name => 'Mr. Ed'); $talking->speak; # says "Mr. Ed goes neigh" my $baab = Sheep->new(colour => 'white', name => 'Baab'); $baab->speak; # prints "Baab goes baaaah" my $mickey = Mouse->new(name => 'Mickey'); $mickey->speak;

Comment on Moose: does not export its sugar to the 'main' package
Select or Download Code
Re: Moose: does not export its sugar to the 'main' package
by ikegami (Pope) on Jun 29, 2010 at 16:37 UTC

    You're loading Mouse instead of your module with the same name.

    You could rename the module, or use

    perl -I. script.pl

    Note that newer versions Mouse no longer throw that error. You still need the rename your module to get it to load, though.

    Update: Added workaround.
    Update: Added note.

      It's a bit unfortunate that my tutorial on Moose also uses Mouse, which can now cause conflicts. In my defense, Mouse was invented much later. :)

      -- Randal L. Schwartz, Perl hacker

      The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

        Now we have to hope and pray that nobody spins off another variant named Mousse.

        It sounds a lot like Moose and is spelled a lot like Mouse. (I already checked CPAN:)

Re: Moose: does not export its sugar to the 'main' package
by Anonymous Monk on Jun 29, 2010 at 17:51 UTC
    You have:
    #!/usr/local/bin/perl -w use strict; use Moose;
    You are not supposed to have 'use Moose' in your 'main' program. It only should be used in packages.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://847147]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (18)
As of 2014-09-30 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (378 votes), past polls