On Wed, 12 May 2010 09:55:13 +1200, Lutz Gehlen <l.r.geh...@massey.ac.nz> wrote: > Hello everybody, > recently, I read the following statement in a CPAN Ratings entry: > "this package also uses wantarray (a transgression amongst interface > sensibilities)." > > I also sometimes use wantarray and don't see anything bad about it > if the behaviour is documented (which should also be the case > otherwise). I wondered if the above statement reflects the general > opinion among experienced Perl authors. If yes I would be interested > in the reasons.
Many uses of wantarray end up being more clever than good. The worst are when scalar vs. list context involves some drastic return value change, but even the relatively simple "return an array in list context, or an arrayref in scalar context" often ends up irritating me more than it's worth (including when I wrote the function that uses wantarray to begin with), just because it's not how arrays work anywhere else in Perl, or because I have to remember that even though I got an arrayref when I wrote '$foo = function()', I'll get a list back when I write 'bar(x => 1, y => function(), z => 3)'. The least offensive use I think I've seen for it is when you either return "fail" or "fail and some details about the failure" depending on context -- but then I'd just throw an exception instead, which is an entire new (and irrelevant) discussion. tl;dr: wantarray often seems intuitive when you write it and then isn't when you want to use it. hdp.