You can't even return an array in Perl (you can return a reference to an array and you can write "return @array;" but that returns (a copy of) the list of scalar values that are in the array).

This is not 100% correct. The array is returned from the function. But to get a hold of it, you should COPY it somewhere. Either in another array, or into list of variables. Or where ever.

perl -e 'sub test{my @a=qw(a b c); return @a} my $v = test(); print $v +,"\n"' perl -e 'sub test{return qw(a b c);} my $v = test(); print $v,"\n"' perl -e 'sub test{my @a=qw(a b c); return @a} my ($a, $v) = test(); pr +int $v,"\n"' perl -e 'sub test{my @a=qw(a b c); return (@a)} my $v = test(); print +$v,"\n"' perl -e 'sub test{my @a=qw(a b c); return (@a, "b")} my $v = test(); p +rint $v,"\n"'

Everything in perl becomes logical if you think about @abc as about OBJECT, and about list as COLLECTION of objects. So obtaining a slice from the array gives you collection of objects. grep or map work with collections of objects, so if you pass array to them, this array is turned into collection of objects. Of course, neither grep nor map can return array since they have only collection of objects as input.

Placing array as member into collection/list does not make this array disappear. It stays in the list as the object. But when the list is copied somewhere, then the array object might be replaced with its elements. Depends on the context.


In reply to Re^2: Confused as to why the "casting context" is mis-behaving ("list vs array", again) by andal
in thread Confused as to why the "casting context" is mis-behaving by kiz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.