Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Assigning to an ArrayRef accessor type

by Athanasius (Archbishop)
on Sep 21, 2020 at 15:15 UTC ( [id://11122002]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122002]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found