in reply to Re: Use of wantarray Considered Harmful (bad use)
in thread Use of wantarray Considered Harmful

I agree that wantarray has good uses, but I rarely see them. I might even go so far as to say the good uses of wantarray are as rare as the good uses of prototypes. I'm curious as to what you'd consider a good use of it. I don't consider Contextual::Return to be a "good use" mostly because it does the same thing only moreso. It's like saying a good use of open is IPC::Open3.

  • Comment on Re^2: Use of wantarray Considered Harmful (bad use)

Replies are listed 'Best First'.
Re^3: Use of wantarray Considered Harmful (bad use)
by shmem (Chancellor) on Dec 13, 2008 at 01:09 UTC

    Well, as I wrote some time ago, one such use are subroutines that just produce data and behave different in void, scalar and list context

    $thingy -> foo (1,2); # results printed to current filehandl +e $string = $thingy -> foo (1,2); # results returned as a single string @lines = $thingy -> foo (1,2); # results returned as a bunch of lines

    which works fine and is just fine - if documented.

    Another such use would be subroutines which behave like google's "I feel lucky" (scalar) vs. "normal" (list) search.

    I agree with you that good uses of wantarray are rare, and the only type of prototype I use now and then is '&', which lets me write subs that get passed blocks, like map or sort.

    I don't consider Contextual::Return to be a "good use" mostly because it does the same thing only moreso.

    That statement contradicts the OP's last heading - "Better" versions of wantarray.