No, if it returns a list, then you must know which list
function "expression" represents before you can know what
will be returned in scalar context.
Oh, that's true. Some functions will return the number of elements
when used in scalar context even if they normally return a list in
list context. In that case, nothing special needs to be done other
than assigning "expression" to a scalar (or evaluating it in a scalar
context).
In the past, though, I have been burned by shoddy documentation
that said an array would be returned, when in fact it returned a
list. The code:
my $numElements = function();
should have worked, but instead it put the last element into
$numElements, which isn't what I wanted. As a result, I often use
instead:
my $numElements = () = function();
especially when the documentation makes no mention of context. That
way, if in some future revision of the code, a maintainer changed code
like:
my @array = (1, -1);
return @array;
to:
return (1, -1);
and not updated the documentation to reflect that change, my own code
would be safe.
(and also note that it does NOT "return a list". It is not possible
to return a list into scalar context...)
Sure, agreed. I didn't mean to imply that it could. What I meant
to imply is that if you call a function that normally returns a list,
you will get a scalar if you call it in scalar context. And unlike
with arrays, this scalar will not necessarily be the number of
elements. (It can be, but unless the documentation explicitly says
so, I wouldn't depend on it.)
What I DID neglect to mention were functions that behave
differently based on context (such as those that use the wantarray()
function).
-- Jean-Luc