Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 55896
Status: rejected
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: eugene_l_a [...] yahoo.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Moose doesn't work in "main" script(?)
Date: Wed, 24 Mar 2010 06:47:39 -0700 (PDT)
To: bug-moose [...] rt.cpan.org
From: Eugene Lukianov <eugene_l_a [...] yahoo.com>
Dear Moose Supporters: I'm new to Moose so, perhaps, this is a silly question. When I run the following script: (everything is in one file): #!/usr/bin/perl -w use Data::Dump qw(dump); my $ip = InnerPackage->new; print "\n\nInnerPackage dump: ",         dump($ip),"\n"; print "InnerPackage attr: ",$ip->{attr},"\n"; print "InnerPackage t returns: ",$ip->t,"\n"; exit; package InnerPackage;   use Moose;   with 'Non::Existent';   has 'attr' => (     default => 'Some value',   );   sub t { return "test sub" } 1; # ------- End of file ------- Moose does not complain it cannot find "Non::Existent.pm" module, instead it just returns empty instance of InnerPackage object and the script prints: InnerPackage dump: bless({}, "InnerPackage") Use of uninitialized value in print at ./testMoose.pl line 7. InnerPackage attr: InnerPackage t returns: test sub However, when I place "package InnerPackage;" definition to a separate InnerPackage.pm file and "use InnerPackage;", Moose complains it cannot find "Non::Existent.pm" module in "with 'Non::Existent';" statement. When I replace "Non::Existent" with some existing module, Moose correctly inherits from it. Could you please let me know if this is a bug in Moose or am I doing something wrong. Any help is greatly appreciated - I really need to place local module in a "main" perl script. Thank you in advance for your help and sorry for troubling you with this. I look forward for your earliest reply. Best regards,   Eugene A.Lukianov.
Subject: Re: [rt.cpan.org #55896] Moose doesn't work in "main" script(?)
Date: Thu, 25 Mar 2010 00:44:38 +0200
To: bug-Moose [...] rt.cpan.org
From: Yuval Kogman <nuffin [...] cpan.org>
Moose's syntax works at runtime, except for the 'use Moose' line. This means that as the file is processed, first Perl will first parse the whole thing, and see: use Data::Dump qw(dump) and package InnerPackage; and use Moose; and process that immediately, loading Moose.pm and calling import. Then the script will go back to the begining, and run it line by line. When it runs: my $ip = InnerPackage->new; the class is already defined (because of Moose's import) but it has no attributes or anything. after those lines it gets back to package InnerPackage and switches the current package again, and then it runs with 'Non::Existent'; has 'attr' => ( default => 'Some value', ); which will change the definition of the class. To get around this wrap the class definition BEGIN { }, move it to the top of the file, or put it in a .pm file which you load with 'use'. See Moose's tests for some inline class definition examples.