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

Why does give 'Use of uninitialized value in print' instead of printing "two"?
use strict; use warnings; package Stuff { use Moose; has 'options' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { all_options => 'elements', add_option => 'push', map_options => 'map', filter_options => 'grep', find_option => 'first', get_option => 'get', join_options => 'join', count_options => 'count', has_options => 'count', has_no_options => 'is_empty', sorted_options => 'sort', }, ); sub test { my ($self, $arg) = @_; $self->get_option($arg); } no Moose; 1; }; my $s = Stuff->new({elements => [ qw/one two three/ ] }); print $s->test(1);

Replies are listed 'Best First'.
Re: Moose problem.
by choroba (Cardinal) on Dec 17, 2014 at 09:06 UTC
    To initialise an object, pass the attribute name to the constructor, not a method name:
    my $s = Stuff->new({options => [ qw/one two three/ ] });

    You could have used Data::Dumper, it would have shown you $s has no options populated.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      "To initialise an object, pass the attribute name to the constructor, not a method name:"

      Thanks. (Shame about the lack of error handling!)

        Shame about the lack of error handling!
        MooseX::StrictConstructor
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Moose problem.
by Anonymous Monk on Dec 17, 2014 at 09:10 UTC
    Because  $self->get_option( 1 ); returns undef? Try Data::Dump::dd( $s ); and see what you get