Re: Usage of wantarray

2010-05-12 Thread Shlomi Fish
On Wednesday 12 May 2010 00:55:13 Lutz Gehlen wrote:
 Hello everybody,
 recently, I read the following statement in a CPAN Ratings entry:
 this package also uses wantarray (a transgression amongst interface
 sensibilities).
 
 I also sometimes use wantarray and don't see anything bad about it
 if the behaviour is documented (which should also be the case
 otherwise). I wondered if the above statement reflects the general
 opinion among experienced Perl authors. If yes I would be interested
 in the reasons.
 

Making use of wantarray is a valid way to distinguish between list and scalar 
(and void) contexts. Perl 6 and http://search.cpan.org/dist/Want/ take it a 
step further and provide other contexts. Personally, I'm not a big fan of 
clobbering the return values with a list of values (like perldoc -f split) 
does, and prefer returning an array reference. But if you like to distinguish 
between array and scalar contexts, then wantarray is naturally an option.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Humanity - Parody of Modern Life - http://shlom.in/humanity

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .


Re: Usage of wantarray

2010-05-12 Thread David Nicol
On Wed, May 12, 2010 at 7:08 AM, Shlomi Fish shlo...@iglu.org.il wrote:
 On Wednesday 12 May 2010 00:55:13 Lutz Gehlen wrote:
 Hello everybody,
 recently, I read the following statement in a CPAN Ratings entry:
 this package also uses wantarray (a transgression amongst interface
 sensibilities).

Cwantarray is how to distinguish between array, scalar, and void contexts.

When you want to distinguish between numeric, string, scalar-ref,
array-ref, and hash-ref scalar contexts, Coverload works for that.

The sensibilities allegedly transgressed may be peculiar to the author
of that comment.

-- 
To avoid both financial loss and jail time, we provide security
mechanisms, including access control. -- Mike Burrows, 2006


Re: Usage of wantarray

2010-05-12 Thread Eric Wilhelm
# from Hans Dieter Pearcey
# on Wednesday 12 May 2010 08:49:

...just because it's not how arrays work anywhere else in Perl,

This is not about how arrays work, but how functions work.  See 
localtime(), caller(), each(), glob(), readline(), etc.

But then see sort() and split().

There are reasonable cases for context sensitivity, with some reasons 
better than others.  It's easy to wrongly think you've found a good use 
for it, and maybe even easier to get angry when it is used well but 
catches you off guard.

but even the relatively simple return an array in list context, or an
arrayref in scalar context ...

That might be simple, but no builtins do this, which seems to say 
something.  If you're still working with lists, you expect them to 
behave like lists, and not try to package themselves in a reference 
with postage to Cairo.  The only case where wantarray ? (@x) : [...@x] 
even has utility is in e.g. perl5i trying to allow method chaining on 
list objects without needing an explicit -flatten, but that is just a 
horrible hack workaround to the lack of a method context IMO.  It 
might be worth the caveats, but that just makes it good enough, not 
a good idea.

or because I have to remember 
 that even though I got an arrayref when I wrote '$foo = function()',
 I'll get a list back when I write 'bar(x = 1, y = function(), z =
 3)'.

perlfunc:
  In general, they do what you want, unless you want consistency.

--Eric
-- 
Cult: A small, unpopular religion.
Religion: A large, popular cult.
-- Unknown
---
http://scratchcomputing.com
---


Re: Usage of wantarray

2010-05-12 Thread Lutz Gehlen
Hello everybody,
thanks for all your comments.

On Wed, May 12, 2010 at 11:49:09AM -0400, Hans Dieter Pearcey wrote:
 relatively simple return an array in list context, or an arrayref in scalar
 context often ends up irritating me more than it's worth (including when I
 wrote the function that uses wantarray to begin with), just because it's not
 how arrays work anywhere else in Perl, or because I have to remember that even
 though I got an arrayref when I wrote '$foo = function()', I'll get a list 
 back

[...]

 tl;dr: wantarray often seems intuitive when you write it and then isn't when
 you want to use it.

When I read this I have to admit that this reflects very well parts
of my own experience partly contradicting what I wrote in my
introduction. I have been using wantarray in this context sometimes
because I thought that then users could get back an array or
arrayref depending on what they prefer, but I also found that in the
end it caused more trouble than benefit.

Lutz


Re: Usage of wantarray

2010-05-12 Thread Lutz Gehlen
On Wed, May 12, 2010 at 11:32:59AM -0700, Eric Wilhelm wrote:
 That might be simple, but no builtins do this, which seems to say 
 something.  If you're still working with lists, you expect them to 
 behave like lists, and not try to package themselves in a reference 

The following comment has admittedly not much to do with wantarray
any more, but I would still like to point out that returning array
references can make sense sometimes, e.g. when the referenced list
is very big. Of course, returning the internal reference to the list
bears the danger of the user messing up the object's internals, but
if this behaviour is well documented the gain in CPU and memory
efficiency might be worth the loss of encapsulation.

Best wishes,
Lutz


Re: Usage of wantarray

2010-05-12 Thread nadim khemir
 Hello everybody,
 recently, I read the following statement in a CPAN Ratings entry:
 this package also uses wantarray (a transgression amongst interface
 sensibilities).
 
Hi Lutz,

I've read that too and my reaction was what a totally ridiculous claim!. And 
it is based on ... nil or maybe  
http://www.perlfoundation.org/perl5/index.cgi?pbp_module_recommendation_commentary
 
which is a total shame because it's like saying Let's not go to the Mars 
because it is already complicated to go to the Moon.

If it was a a transgression amongst interface sensibilities then those 
sensibilities would belong to the people that have developed Perl. I believe 
the distinction between scalar, void and list context exists because it 
reflects the way we think differently in different contexts. Context 
differentiation is not only for core functionality. It's a pity module authors 
forgot that.

If there was a need to defend context (a pillar of Perl) the simplest 
example would be the usage of @any_array in scalar context. Once you've 
learned that an array in scalar context returns the number of elements in the 
array you just can't think differently because it _makes_ sense even if it 
looks unreal.

P6 is taking it further but maybe the P6 ppl have very bad API sensibilities. 
They certainly do but they correct, or will correct, them with time because 
that's how a language evolves.

Look at http://search.cpan.org/~dconway/Contextual-Return-
v0.2.1/lib/Contextual/Return.pm You can either say Wow! how do I use this 
intelligently or Eeek! I will never be able to master this so it is crap.

This is the same kind of silly statement as Perl is not a real object 
oriented language. Give it a break.

Perl is a complex language. You don't have to use all the tricks in the book 
at the same time but you still want to use a trick or two now and then. There 
are certainly examples of API's that got wild because of wantarray. But the 
ugliest transgression seen in modules is a crappy documentation and many more 
modules suffer of it.

Again, Perl is a complex high level language. If it's too complicated, fuzzy, 
has bad APIs or doesn't fit one's way of thinking, is too slow or looks like 
line noise, we have been blessed (no pun intended) with many supra-efficient, 
controlling-freak, with laser-sculpted APIs, boring languages. People, take 
your pick.

Cheers, Nadim.



Usage of wantarray

2010-05-11 Thread Lutz Gehlen
Hello everybody,
recently, I read the following statement in a CPAN Ratings entry:
this package also uses wantarray (a transgression amongst interface
sensibilities).

I also sometimes use wantarray and don't see anything bad about it
if the behaviour is documented (which should also be the case
otherwise). I wondered if the above statement reflects the general
opinion among experienced Perl authors. If yes I would be interested
in the reasons.

Thank you for your comments
Lutz