I think that use of wantarray() to modify return behavior should be generally avoided. In my opinion it is a clear violation of the principal of least surprise.
This obviously depends on what exactly is being returned in the first pace, but in general I agree. When you don't have any particular reason to think about context,
don't use wantarray. I find that, often, the 'problem' solves itself without me thinking about it.
Having said that, I do use wantarry sometimes, and my reasons are usually effiency related:
Runtime efficiency
Returning an arrayref instead of a list, thus avoiding copying many scalars. Well, we can't agree on everything :-)
-
Programmer efficiency
Using count(*) instead of select( values ... ) in an SQL query, if you only want to know the number of rows matching. This saves the user from having to remember yet another subroutine/method name, and me from having to put the WHERE clause in 2 places (or factoring it out).
Also, it seems like a better idea than just returning the last column, or the number of columns, or anything like that, which would likely be the result of not using wantarray in this case.
There might be better examples, but I'm too lazy to think of them now.
Joost.
--
#!/usr/bin/perl -w
use strict;$;=
";Jtunsitr pa;ngo;t1h\$e;r. )p.e(r;ls ;h;a;c.k^e;rs
";$_=$;;do{$..=chop}while(chop);$_=$;;eval$.;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.