Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Object not created as well with Moose

by woodpeaker (Novice)
on Dec 10, 2008 at 21:01 UTC ( #729503=perlquestion: print w/replies, xml ) Need Help??
woodpeaker has asked for the wisdom of the Perl Monks concerning the following question:

Hi Mongers, Today I tried to refactor my own code. And today I tried to use Moose, not strict/warnings. Well, I created need structure in exists object, for example:
package Package::Systen::Foo; use Moose; use Package::System::Config; has 'CONFIG' => (is => 'rw', isa => 'Object'); sub _init { my $self = shift; $self->CONFIG(Package::System::Config->new()); } 1;
Well, when I creating object it give me blank package without any methods and properties. Also, when I trying to $foo->_init() nothing happen and nothing changes in *Foo object. Also, after that application stops and nothing show me as error. I tried to use strict, warnings, perl -w and eval. Test::More said just 'Test failed'. How do you think what problem in it? And how can I resolve that. PS. perl version is 5.10

Replies are listed 'Best First'.
Re: Object not created as well with Moose
by Corion (Pope) on Dec 10, 2008 at 21:49 UTC

    Is the package name the name you want?

    package Package::Systen::Foo;

    If so, then you will need to show us some code where you actually use things.

      #!/usr/bin/perl -w use strict; use Package::System::Foo; use Data::Dumper; my $foo = Package::System::Foo->new(); $foo->start(); print Dumper $foo;
      package Package::System::Foo; use Moose; extends 'Package::Contol::Base::Daemon::Server'; use Package::System::Config; has 'CONFIG' => (is=>'rw',isa=>'Object'); # OR I tried isa=>'Any' sub _init{ my $self = shift; $self->CONFIG(Package::System::Config->new()); #not executing after that } sub start{ my $self = shift; $self->_init(); # not executing after _init() ......... } 1;
      package Package::Contol::Base::Daemon::Server; use Moose; has 'DAEMON' => (is=>rw,isa=>'Object'); 1;
      'Package' it's just sample. I cannot show real module name for some reason, sorry.
        Try re-reading Corion's comment.

        It points out a typo in your SOPW.

        If you wrap your Moose class definitions in a BEGIN {} block it should work correctly. This is needed because the Moose "keywords" are just plain perl functions and so in the example you show above, they will not have run prior to your test code running.

        Also, _init has no meaning to Moose, you must be looking for BUILD.

Re: Object not created as well with Moose
by duckyd (Hermit) on Dec 10, 2008 at 23:10 UTC
    I'm not sure why you'd try to use Moose but not use strict or use warnings. Moose is not a substitute for strict/warnings - you should always use strict and use warnings, regardless of whether or not you're using Moose.
    Update You learn something new every day. Or at least, I do :)
      Moose is not a substitute for strict/warnings

      Sure it is. For explicitly useing them anyway. From the Pod-

      use Moose; # automatically turns on strict and warnings
Re: Object not created as well with Moose
by rhesa (Vicar) on Dec 11, 2008 at 12:23 UTC
    Wouldn't it make more sense to use default and/or lazy?
    has 'CONFIG' => ( is => 'rw', isa => 'Object', lazy => 1, default => sub { Package::System::Config->new }, );
    That way you won't need _init().

    It would probably be even better to make a subtype for the Config object.

      Thanks, your answer helps, but also I found my mistakes))) as always just need to sleep about 6-8 hours, not just 2))) Thanks to all
Re: Object not created as well with Moose
by woodpeaker (Novice) on Dec 11, 2008 at 05:06 UTC
    I just have a time to refactor complete perl-system. So using Moose is a one of the steps. And also I start refactor code after test that on
    So, Main problem not in Moose, seems to be in me :-) I just cannot understand why perl not show me errors and warnings.
    I mean If method created by Moose doesn't exists why I'm not see that with switched on warnings.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://729503]
Approved by ikegami
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-01-21 15:32 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (228 votes). Check out past polls.