You could call this part 2 of Function Prototype Context Conversion. Another bit of code which may not operate exactly as you would assume. This one is a little more challenging:
#!/usr/bin/perl -w use strict; sub foo($) { print "$_[0]\n"; } my @foo_bar = qw[ foo bar ]; sub returns_array { return @foo_bar; } sub returns_two { return qw[ foo bar ]; } sub returns_two_2 { return ('foo','bar'); } foreach ('foo(@foo_bar);', 'foo($foo_bar[0..$#foo_bar]);', 'foo(@foo_bar[0,$#foo_bar]);', 'foo("foo","bar");', 'foo(map{$_}"foo","bar");', 'foo(qw[ foo bar ]);', 'foo(@{[qw[ foo bar ]]});', 'foo(${[qw[ foo bar ]]}[0,1]);', 'foo(map{$_}qw[ foo bar ]);', 'foo(reverse @foo_bar);', 'foo(returns_two());', 'foo(returns_two_2());', 'foo(returns_array());') { printf("%-30s : ", $_); eval($_) || print "# $@\n"; }
There's some errors/warnings in there, just to keep things exciting.


The output of this program is hidden in this grey box:
foo(@foo_bar); : 2 Argument "" isn't numeric in array element at (eval 2) line 1. foo($foo_bar[0..$#foo_bar]); : foo foo(@foo_bar[0,$#foo_bar]); : bar foo("foo","bar"); : # Too many arguments for main::foo at + (eval 4) line 1, near ""bar")" foo(map{$_}"foo","bar"); : 2 Useless use of a constant in void context at (eval 6) line 1. foo(qw[ foo bar ]); : bar foo(@{[qw[ foo bar ]]}); : 2 foo(${[qw[ foo bar ]]}[0,1]); : bar foo(map{$_}qw[ foo bar ]); : 2 foo(reverse @foo_bar); : raboof foo(returns_two()); : bar foo(returns_two_2()); : bar foo(returns_array()); : 2
The question is, is there a simple method to convert or expand an array into a list? If you know the number of elements in advance, sure, this is simple, but what about the general case?

In reply to Function Prototypes and Array vs. List (Pt. 2) by tadman

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.