J
J Krugman
I don't think I'll ever manage to learn all of Perl's little
quirks... I just discovered that Perl has two very different ways
of returning multiple values (see below). What's the rationale
for this ambiguous behavior?
If one defines "sub foo { (7, 8, 9) }", then "scalar foo()" evaluates
to 9. But if one defines "sub bar { my @x = (7, 8, 9) }", then
"scalar bar()" evaluates to 3.
Is there a way to get bar's behavior without the superfluous
assignment?
The more annoying problem is when one is using some function like
foo above written by someone else, and wants to get only the number
of values returned. Then it seems like the simplest way to do this
requires another superfluous assignment:
my $n = (my @x = foo());
or
my $n = do { my @x = foo() };
or
my $n = @{[foo()]};
Is there a way to code this that is both succinct and does not
require extraneous constructs? In other words, is there a way
(other than through a superfluous assignment) to tell perl (and
anyone reading the code) that "(7, 8, 9)" in the definition of foo
should be intepreted as an array, and not as a list?
Thanks!
jill
quirks... I just discovered that Perl has two very different ways
of returning multiple values (see below). What's the rationale
for this ambiguous behavior?
If one defines "sub foo { (7, 8, 9) }", then "scalar foo()" evaluates
to 9. But if one defines "sub bar { my @x = (7, 8, 9) }", then
"scalar bar()" evaluates to 3.
Is there a way to get bar's behavior without the superfluous
assignment?
The more annoying problem is when one is using some function like
foo above written by someone else, and wants to get only the number
of values returned. Then it seems like the simplest way to do this
requires another superfluous assignment:
my $n = (my @x = foo());
or
my $n = do { my @x = foo() };
or
my $n = @{[foo()]};
Is there a way to code this that is both succinct and does not
require extraneous constructs? In other words, is there a way
(other than through a superfluous assignment) to tell perl (and
anyone reading the code) that "(7, 8, 9)" in the definition of foo
should be intepreted as an array, and not as a list?
Thanks!
jill