in reply to Use of wantarray Considered Harmful
For example a regex match returns a Match object, which returns the success status in boolean context, the matched string in string context, a list of positional captures in list context, and if used as a hash, it provides access to all named captures.
Also note that want, the Perl 6 generalization of wantarray, can't always work due to multi dispatch:
multi a(Str $x) { say "got a string" } mutli a(@a) { say "got an array" } sub b { # what's the context here? } a(b());
Since the dispatcher only knows which a sub it will dispatch to after seeing the return value from b(), it can't provide an unambiguous context to a.
That said, even if we are aware of the problem above and neglect that paradigm shift, return @list; isn't a problem in Perl 6, because there are more specific contexts. In generic item context ("scalar context") it will return an Array object, and only in numeric context will the caller see the number of items in the array.
|
---|