Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730371]
help
Chatterbox?
[Corion]: Do you have any specific interests or general Perl knowledge?
[Cosmic37]: should I slurp? should I grep? Noble Lords I wish you good karma and beg your advice
[Corion]: Cosmic37: Ah, see perlfaq4, about How do I compute the intersection of two arrays
[Cosmic37]: I am out of practice; I use Perl for scientific programming for number crunching
[Corion]: Cosmic37: Basically, you read one file into a hash, keyed by your key, and then match the lines from the second file to that hash
[Cosmic37]: note that the two files only have datetimes which may match whereas other data per line is different format in file1 and file2 - is that really intersection?
[jedikaiti]: Hi Monks
[Corion]: Cosmic37: Well, if you want to use only parts of a line for the key, see split or whatever other mechanism to extract the key from the line
[Corion]: Hi jedikaiti!
[Cosmic37]: is there a webpage for full CB for this chat rather than side panel chat?

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-06-29 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (672 votes). Check out past polls.