Well there is the obvious place to start (perldoc -f wantarray):

     wantarray
             Returns true if the context of the currently
             executing subroutine is looking for a list value.
             Returns false if the context is looking for a
             scalar.  Returns the undefined value if the context
             is looking for no value (void context).

                 return unless defined wantarray;    # don't bother doing more
                 my @a = complex_calculation();
                 return wantarray ? @a : "@a";

             This function should have been named wantlist()
             instead.

So then the simple answer is, so you can tell when the user is calling a function/sub 
wanting a list returned, a scalar returned, or nothing (void).

For example you create an object that has an underlying structure similar to an array, 
then you write one method to do a couple of different things based on how it is 
called, for instance if the method is called in scalar context you can return the 
number of items in the list, if it is called in list context you return a list of 
references to each of those items in the list.

The place I have used it is in (a previously poorly written so I had to) a 
configuration file reader, where a person could call the function with a list of 
options they wanted and the values associated with that list were returned. I wanted 
to update the function so it could just return a hash of all the values, or the values 
specified with their associated keys, but I couldn't break old code.  So I used 
wantarray to tell if I was called in scalar context, if so I passed back a reference 
to my hash, but in list context I did what it had always done, that is passed back a 
list of the items they wanted.  The real question is, knowing the basics how can you 
use it?  .......

http://danconia.org

------------------------------------------------
On Tue, 26 Nov 2002 15:35:01 +0100, "Mystik Gotan" <[EMAIL PROTECTED]> wrote:

> I can't really figure out what the purpose of wantarray() is.
> Can someone please give me a good, decent explanation?
> 
> Thanks in advance :-)
> 
> 
> --------------
> Bob Erinkveld (Webmaster Insane Hosts)
> www.insane-hosts.net
> MSN: [EMAIL PROTECTED]
> 
> 
> 
> 
> _________________________________________________________________
> MSN Zoeken, voor duidelijke zoekresultaten! 
> http://search.msn.nl/worldwide.asp
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to