Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

array slice?

by zeltus (Beadle)
on Jan 31, 2014 at 14:18 UTC ( #1072850=perlquestion: print w/replies, xml ) Need Help??
zeltus has asked for the wisdom of the Perl Monks concerning the following question:

I think my question is to do with array slices... but of course, I may be wrong!

my @tarray = qw(one two three four five); my ($v2) = @tarray; my $v3 = (@tarray)[0];

Now, I am used to extracting slices using

my $v3 = (@tarray)[0];

in this case, extract the first element from @tarray and "give" it to $v3...but upon examining some legacy code, I came across...

my ($v2) = @tarray;

...which does the same thing. But I can't work out why it works. Can someone please explain? Is it classifiable as a form or array slicing? I prefer my way for clarity, but are there any pros and cons for either method?

Thank you!

Replies are listed 'Best First'.
Re: array slice?
by Athanasius (Chancellor) on Jan 31, 2014 at 15:32 UTC

      Thank you!

      Amongst other things, this is me trying to reduce the problem to the simplest demo code and losing the plot ever so slightly!

      Reductio ad absurdum or somesuch... but no matter, I no where I am now


Re: array slice?
by zeltus (Beadle) on Jan 31, 2014 at 14:33 UTC

    aah, I begin to see. After a lookaround, I found some info in perlsub: I see my question is a variant of

    my($key, $value) = @_;

    (copy a subroutines' parameters into named variables)

    I guess this is clear enough... my fud was because my legacy code was dealing with just one parameter, making the code look a bit strange to me, is all

Re: array slice?
by kcott (Chancellor) on Feb 01, 2014 at 01:13 UTC

    G'day zeltus,

    I see ++Athanasius has provided feedback on the code you posted.

    In addition to the FAQ links he posted, you might also like to look at perldata: Slices.

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1072850]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-05-26 18:47 GMT
Find Nodes?
    Voting Booth?