in reply to Re: Using ArrayRef data members in Moose
in thread Using ArrayRef data members in Moose

Well, whaddaya know...

Whack! “Doh!!”

I figured it out!

It seems that the magic is this:   this definition adds new methods, to the object, not the array attribute, which have the “magical effect” of doing the appropriate operation to the appropriate attribute... automagically knowing both what the attribute is, and what to do with it.

What was throwing me off, for the longest time, was this incorrect code:

 $n = $object->items->num_items();  ##INCORRECT
which was giving me the very mysterious error:
# died: Can't call method "num_items" on unblessed reference...

Which is incorrect because the method is attached to the object. Oh... I get it... Gee, it's even seriously cool...

Therefore, this works:

 $n = $object->num_items();  ##CORRECT

Replies are listed 'Best First'.
Re^3: Using ArrayRef data members in Moose
by Anonymous Monk on Feb 18, 2011 at 17:49 UTC
    I know this is OLD! But i want to post this example, that works
    use MooseX::Declare; ... ... ... has 'error_log' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); ... ... ... method register_error { shift; my $s_message = shift; return unless defined $s_message; my @a_temp = ( $s_message ); $self->error_log->push ($s_message); return $self->error_log->length; }
    self->error_log->push does not work until i assign a "default" for the array.
Re^3: Using ArrayRef data members in Moose
by Anonymous Monk on Feb 18, 2011 at 17:49 UTC
    I know this is OLD! But i want to post this example, that works
    use MooseX::Declare; ... ... ... has 'error_log' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); ... ... ... method register_error { shift; my $s_message = shift; return unless defined $s_message; $self->error_log->push ($s_message); return $self->error_log->length; }
    self->error_log->push does not work until i assign a "default" for the array.
      self->error_log->push does not work until i assign a "default" for the array.

      Actually it won't work then either until you add a use Moose::Autobox as well.

      -stvn