Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^4: Use of wantarray Considered Harmful (bad use)

by TGI (Parson)
on Dec 15, 2008 at 08:03 UTC ( #730371=note: print w/replies, xml ) Need Help??

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

Oh yes it is! :)

Granted, if you create your @x by copying the large array you may wish to avoid copying, then it is pretty stupid to use wantarray to avoid making another copy.

But if you are working with an extant huge @x, its OK.

With only the one line we have to guess what the provenance of @x is.

Even if your read is destructive (like from a filehandle, or another similar iterator), there should be no problem. The ternary short circuits, and only one term is evaluated. So @x never gets evaluated in a list context and we have no massive destruction.

This assumes that you are working with a preexisting @x. If you are destructively copying from some source into @x, then the results will be ugly.

Anyhow, I think we basically agree that context sensitive return values can be useful to improve an API, but the should be used with care, and that they must be clearly documented. Also that creating an interface similar to readline (without creating massive unneeded copying of data) is a valid use of the technique.

TGI says moo

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730371]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-12-17 06:42 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (462 votes). Check out past polls.