Re: if(wantarray){...}else{...}



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.

.



Relevant Pages

  • Why isnt void context a real context
    ... Context that scalar- and list ... distinguish between list, scalar and void context, I also can overload ... wantarray, I can let behave my subroutine 3 different ways, i.e. ...
    (comp.lang.perl.misc)
  • Re: About Lagrange in GR II
    ... Yes, but remember that's the classical Lagrangian, not the Lagrangian density of GR. ... local and coordinate dependant unit, ... The action itself is a scalar. ... I suspect you are confusing the context of GR with the context of Quantum Field Theory in which there are often "running coupling constants". ...
    (sci.physics.relativity)
  • Re: Is Perl still worth learning?
    ... the internal behavior of a function changes depending on whether you assign its return value into a scalar or an array. ... It depends on whether you evaluate the function a list context ... Getting back to Perl and its notion of scalar vs. list context, ... For example, in Perl, there is a "grep" function. ...
    (comp.programming)
  • Re: Sorting and Writing Effecient Code
    ... date and extracts the file names that start with the current date, ... loops through the contents of those files to extract data in a scalar ... You seem to be mis-using the term "scalar context", ... Should I be reading the variables I have extracted into an array? ...
    (comp.lang.perl.misc)
  • Re: Is Perl still worth learning?
    ... whether you assign its return value into a scalar or an array. ... It depends on whether you evaluate the function a list context ... The context does not influence the internal behavior of the ... In list context, "grep" returns the list. ...
    (comp.programming)