Abigail-II:# And for something completely different... sub foo { if (1 != @_) { return map $foo($_), @_; } # body of foo here. return $whatever; }
Why the test on the number of arguments, and the recursion? Wouldn't the following be equivalent?:Tilly:sub foo { map { # body of foo here. $whatever } @_ }
Answering the last question first, no that isn't equivalent to just have a map since a map in scalar context coerces like an array does - it tells you how many elements that you have, not what any of them are.The original function also has a map in scalar context problem. Something like this is probably what you want:
Though I this may be clearer:sub foo { (map { # body of foo here. $whatever } @_)[0..$#_]; }
sub foo { my @ret = map { # body of foo here $whatever; } @_; @ret[0..$#ret]; }
In reply to Re: Re: Re: What should be returned in scalar context?
by ysth
in thread What should be returned in scalar context?
by tilly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |