I have a simple application which uses Moose (example copied from Moose::Manual::MooseX pages)

package User;

use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has 'name'  => (is => 'rw', isa => 'Str');
has 'email' => (is => 'rw', isa => 'Str');

package main;

my $bob = User->new( name => 'Bob', email => 'b...@example.com' );

say $bob->name; # prints 'Bob'
say $bob->naem; # Exits with error: 'Can't locate object method "naem" ..

All is good so far.
Then, much later, in an Idiot Moment, I forgot my "objects" were Moose objects and not hashrefs, and wrote:

say $bob->{name}; # prints 'Bob' (!)

say "OK" unless($bob->{naem}); # prints "OK", gives no error!

$bob->name = "Robert"; # dies with "Can't modify non-lvalue subroutine call..." - Good!
$bob->{name} = "Robert"; # doesn't die!

say $bob->name; # prints 'Robert' (!)

$bob->{naem} = "Roberto";

say $bob->{naem}; # prints 'Roberto' (!)

say $bob->naem; # Exits with error: 'Can't locate object method "naem" ' (Whew!)

say Dumper($bob); # gives:
                  # $VAR1 = bless( {
                  #                  'email' => 'b...@example.com',
                  #                  'naem' => 'Roberto',
                  #                  'name' => 'Robert'
                  #                }, 'User' );

Now I know any software cannot be totally immune from extreme idiocy like mine, but I was surprised how quickly I got myself in trouble. Is there a MooseX::StrictSomething which could have help me avoided this? (Yes I know: "more exercise of the Little Grey Cells" - apart from that?)

Thanks

Sam Brain

--
Sam Brain
Department of Radiation Oncology
Stanford Cancer Center
Phone: 650-723-6967

Reply via email to