in reply to Re^2: Context aware functions - best practices?
in thread Context aware functions - best practices?
You are aware thatwill evaluate to the number of elements returned by foo rather than return the last one of them, right? :)scalar (@x = foo(1,2,3,5));
Yes, of course. I wrote that in reference to my $x = foo(1); setting $x to 1 :oP
I agree that it would be a rare case when the flattened array would be an acceptable return value, but I still don't like the idea of throwing away data. Oh, well. The only alternative I can think of is the hideous
return wantarray ? @arr : $#arr ? \@arr : $arr[0];
which borders on the obfuscated. I wouldn't use something like that if I were you. Of course I'm me, and love using lines like that, but I digress...
Zaxo's suggestion of using $arr[-1] makes sense too, especially if the sub is doing something cumulative with its args. Imagine a sum() function that keeps track of all the intermediate sums, or whatever.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Context aware functions - best practices?
by Aristotle (Chancellor) on Jan 14, 2003 at 23:32 UTC | |
by LAI (Hermit) on Jan 15, 2003 at 14:33 UTC |