Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by TGI (Vicar)
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


Comment on Re^4: Use of wantarray Considered Harmful (bad use)
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-27 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls