in reply to Assigning to a ArrayRef accessor type

Hello tomred, and congratulations on your first post!

MockObj::files is not an array, it’s an array reference. So instead of assigning to an array variable:

my @strings = $mock->files;

you should assign to a scalar:

my $strings = $mock->files; note explain $strings; cmp_deeply($strings, bag(@list));

which gives the desired test result:

1:14 >perl 2056_SoPW.pl # [ # 'string one', # 'Different string', # 'With feeling' # ] # [ # 'string one', # 'Different string', # 'With feeling' # ] ok 1 1..1 1:14 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Assigning to an ArrayRef accessor type
by tobyink (Canon) on Sep 21, 2020 at 17:08 UTC

    An alternative to this:

    my $strings = $mock->files;

    Would be this:

    my @strings = @{ $mock->files };

    Or if you've got a very recent version of Perl:

    my @strings = $mock->files->@*;

    Moose also has an optional feature where it can detect if an accessor is called in a list context and return a list instead of a reference, but Moo didn't implement that because it can get confusing to work with.

      Arrrh.

      Thank you

      Thanks you both. I should have spotted that.