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.
|
|---|
| 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 | |
by jeffa (Bishop) on May 18, 2015 at 16:19 UTC | |
by Anonymous Monk on May 18, 2015 at 22:24 UTC | |
by Anonymous Monk on May 18, 2015 at 22:28 UTC |