Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Title: Moose required creates empty object Can't locate object method "x" via package "Point"

So I'm trying stuff from Moose::Cookbook::Basics::Point_AttributesAndSubclassing which says among other things The required => 1 parameter means that this attribute must be provided when a new object is created. A point object without coordinates doesn't make much sense, so we don't allow it.

But then a point object without coordinates is allowed

I don't get it , shouldn't this die without x/y being provided in constructor?

And then an empty Point doesn't have attributes?

#!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); { 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__ $VAR1 = bless( {}, 'Point' ); $VAR1 = bless( {}, 'Point' ); Can't locate object method "x" via package "Point" at - line 20.
  • Comment on Moose required creates empty object Can't locate object method "x" via package "Point"
  • Download Code

Replies are listed 'Best First'.
Re: Moose required creates empty object Can't locate object method "x" via package "Point"
by Anonymous Monk on May 18, 2015 at 10:31 UTC
    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

      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