Re: if(wantarray){...}else{...}
- From: "Stumproot" <burlo.stumproot@xxxxxxxxx>
- Date: 10 Apr 2005 15:29:41 -0700
A. Sinan Unur wrote:
> "Stumproot" <burlo.stumproot@xxxxxxxxx> wrote in
> news:1113167548.426110.231150@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:
>
> > Posted from/using google news.
>
> Why is that relevant?
To explain any weird formatting of the code or replies.
> > I have "return" snippets like this in several funktions
> > and I'm getting tired of writing them.
>
> OK.
>
> > Is there a better way?
>
> I am not sure because I don't really understand what problem you are
> attempting to solve.
I want to type less, and produce clearer code.
> > # Start return snippet
> > if(wantarray) {
> > my @res = A_Funktion($a1,$a2);
> > return @res;
> > } else {
> > return A_Funktion($a1,$a2);
> > }
>
> Does A_Funktion behave differenly in scalar versus array context? If
> it does not, then you shouldn't worry about wantarray.
Yes it does, in scalar context it returns an object, in list context a
list representation of the object.
What I'm doing is wrapping a bunch of functions that work on lists in
an object. And I found myself writing alot of the above lines when I
wrote the object manipulation functions.
(A side note. After thinking about it I may remove the wantarray part
alltogether because using the list representation should very rarly be
nessesary and can be extracted when needed.)
But for future use I'll remember this snippet, using scalar on a
function never occured to me even though I know it works.
> wantarray ? A_Funktion(@_) : scalar A_Funktion(@_);
Thanks.
.
- Follow-Ups:
- Re: if(wantarray){...}else{...}
- From: Anno Siegel
- Re: if(wantarray){...}else{...}
- References:
- if(wantarray){...}else{...}
- From: Stumproot
- Re: if(wantarray){...}else{...}
- From: A. Sinan Unur
- if(wantarray){...}else{...}
- Prev by Date: Re: newbie baffled by de/referencing with subroutines
- Next by Date: Re: if(wantarray){...}else{...}
- Previous by thread: Re: if(wantarray){...}else{...}
- Next by thread: Re: if(wantarray){...}else{...}
- Index(es):
Relevant Pages
|