. 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";
}
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?