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



Stumproot <burlo.stumproot@xxxxxxxxx> wrote:


> Posted from/using google news.


What is the relevance of including that?


> I have "return" snippets like this in several funktions
> and I'm getting tired of writing them.
>
> Is there a better way?


Is there a better way to what?


> sub fun1 {
> .
> .
> .
> lots going on
> .
> .
>
> # Start return snippet
> if(wantarray) {
> my @res = A_Funktion($a1,$a2);
> return @res;
> } else {
> return A_Funktion($a1,$a2);
> }
> # End return snippet
> }


I have no idea why are you doing this.

If A_Funktion() is written to be used in either context, then
the wantarray() checking would be inside of A_Funktion()'s
declaration rather than wrapped around every call to A_Funktion().

No wonder you're getting tired. :-)

Context is "see through". In the code below, A_Funktion() sees
whatever context fun1() got called in.


-----------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $scalar = fun1();
my($list) = fun1();
my($a1,$a2);

sub fun1 {
# lots going on...
return A_Funktion($a1,$a2);
}

sub A_Funktion {
if(wantarray) {
warn "A_Funktion() in list context\n"
} else {
warn "A_Funktion() in scalar context\n"
}
}
-----------------------------


--
Tad McClellan SGML consulting
tadmc@xxxxxxxxxxxxxx Perl programming
Fort Worth, Texas
.



Relevant Pages

  • Re: Returning multiple MySQL rows from module
    ... Tom Phoenix wrote: ... I hope there is enough context to see why I think it ... the snippet you submitted seems pretty straightforward. ... changed the code to only allow a single row return, ...
    (perl.beginners)
  • Re: MOA, Restricted and probhibited and warning areas
    ... out of the AIM was but a small snippet, out of context, pertained only to ... IFR, and is inapplicable to student pilots and you are trying to deflect ...
    (rec.aviation.student)
  • Re: Returning multiple MySQL rows from module
    ... I hope there is enough context to see why I think it ... the snippet you submitted seems pretty straightforward. ... --Tom Phoenix ... Stonehenge Perl Training ...
    (perl.beginners)
  • Re: Is this "racist?"
    ... and permanent. ... Another snippet taken out of context to stir controversy. ...
    (alt.guitar.amps)
  • if(wantarray){...}else{...}
    ... I have "return" snippets like this in several funktions ... and I'm getting tired of writing them. ... # Start return snippet ... return @res; ...
    (comp.lang.perl.misc)