Re: if(wantarray){...}else{...}
- From: Tad McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Sun, 10 Apr 2005 17:49:32 -0500
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
.
- Follow-Ups:
- Re: if(wantarray){...}else{...}
- From: Stumproot
- Re: if(wantarray){...}else{...}
- References:
- if(wantarray){...}else{...}
- From: Stumproot
- if(wantarray){...}else{...}
- Prev by Date: Re: if(wantarray){...}else{...}
- Next by Date: Re: newbie baffled by de/referencing with subroutines
- Previous by thread: Re: if(wantarray){...}else{...}
- Next by thread: Re: if(wantarray){...}else{...}
- Index(es):
Relevant Pages
|