in reply to Moose required creates empty object Can't locate object method "x" via package "Point"

Hah missing BEGIN ... I fool
#!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); BEGIN { package Point; use Moose; has 'x' => (isa => 'Int', is => 'rw', required => 1); has 'y' => (isa => 'Int', is => 'rw', required => 1); sub clear { my $self = shift; $self->x(0); $self->y(0); } } sub Main { print Point->new->dump; print Point->new({})->dump; print Point->new({})->x; } __END__ Attribute (x) is required at C:\perl\site\lib\Moose\Object.pm line 24 Moose::Object::new('Point') called at - line 18 main::Main at - line 4
  • Comment on Re: Moose required creates empty object Can't locate object method "x" via package "Point"
  • Download Code

Replies are listed 'Best First'.
Re^2: Moose required creates empty object Can't locate object method "x" via package "Point"
by jeffa (Bishop) on May 18, 2015 at 16:19 UTC

    You can remove a lot unnecessary "scaffolding" from your code:

    #!/usr/bin/perl package Point; use Moose; has 'x' => (isa => 'Int', is => 'rw', required => 1); has 'y' => (isa => 'Int', is => 'rw', required => 1); sub clear { my $self = shift; $self->x(0); $self->y(0); } package main; use strict; use warnings; print Point->new->dump; print Point->new({})->dump; print Point->new({})->x; __END__ Attribute (x) is required at /PATH/TO/Moose/Object.pm line 24 Moose::Object::new('Point') called at foo.pl line 17
    Strange things can happen when you don't put your modules into a proper lib/ dir, etc. etc. ... but i love and utilize the convenience of being able to put everything into one source file from time to time. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      You can remove a lot unnecessary "scaffolding" from your code:

      What a ridiculous statement

      Strange things can happen when you don't put your modules into a proper lib/ dir, etc. etc. ...

      Nothing really strange, order of operations matters even to moose

        What a ridiculous statement

        What a ridiculous comment. Care to detail?

        Nothing really strange, order of operations matters even to moose

        More vague jibberish. Care to actually ask questions or do you just want to troll?