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

S

Stumproot

Posted from/using google news.



I have "return" snippets like this in several funktions
and I'm getting tired of writing them.

Is there a better way?


sub fun1 {
 
A

A. Sinan Unur

Posted from/using google news.

Why is that relevant?
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.
# 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.

#! /usr/bin/perl

my $r = A_Funktion(qw(Hello Goodbye));
my @r = A_Funktion(qw(Hello Goodbye));

print "$r\n@r\n";

sub A_Funktion_Caller {
A_Funktion(@_);
}

sub A_Funktion {
my ($x, $y) = @_;
"$x and $y";
}

__END__

On the other hand, is the following more like the situation above?

#! /usr/bin/perl

my $r = A_Funktion(qw(Hello Goodbye));
my @r = A_Funktion(qw(Hello Goodbye));

print "$r\n@r\n";

sub A_Funktion_Caller {
wantarray ? A_Funktion(@_) : scalar A_Funktion(@_);
}

sub A_Funktion {
my ($x, $y) = @_;
my $r = "$x and $y";
wantarray ? $r : length $r;
}

__END__

Please do read the posting guidelines posted here regularly. The
guidelines contain valuable information on how to help others help you.

Sinan
 
A

A. Sinan Unur

Please note the corrections below.
#! /usr/bin/perl

my $r = A_Funktion(qw(Hello Goodbye));
my @r = A_Funktion(qw(Hello Goodbye));

my $r = A_Funktion_Caller(qw(Hello Goodbye));
my @r = A_Funktion_Caller(qw(Hello Goodbye));
print "$r\n@r\n";

sub A_Funktion_Caller {
A_Funktion(@_);
}

sub A_Funktion {
my ($x, $y) = @_;
"$x and $y";
}

__END__

On the other hand, is the following more like the situation above?

.... is the following a better approximation to your situation?
#! /usr/bin/perl

my $r = A_Funktion(qw(Hello Goodbye));
my @r = A_Funktion(qw(Hello Goodbye));

my $r = A_Funktion_Caller(qw(Hello Goodbye));
my @r = A_Funktion_Caller(qw(Hello Goodbye));
print "$r\n@r\n";

sub A_Funktion_Caller {
wantarray ? A_Funktion(@_) : scalar A_Funktion(@_);
}

sub A_Funktion {
my ($x, $y) = @_;
my $r = "$x and $y";
wantarray ? $r : length $r;
}

__END__

Sorry about the typos.
 
S

Stumproot

A. Sinan Unur said:
Why is that relevant?

To explain any weird formatting of the code or replies.
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.
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.
 
T

Tad McClellan

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"
}
}
 
A

axel

I am not sure because I don't really understand what problem you are
attempting to solve.
Does A_Funktion behave differenly in scalar versus array context? If it
does not, then you shouldn't worry about wantarray.

Is there any need to worry about this - if the whatever A_Funktion
returns is itself to be immediately returned there should be no
need for the intermediate function to ascertain the context.
On the other hand, is the following more like the situation above?
#! /usr/bin/perl

my $r = A_Funktion(qw(Hello Goodbye));
my @r = A_Funktion(qw(Hello Goodbye));

print "$r\n@r\n";

sub A_Funktion_Caller {
wantarray ? A_Funktion(@_) : scalar A_Funktion(@_);
}

So the following would have the same effect:

sub A_Funktion_Caller {
A_Funktion(@_);
}
sub A_Funktion {
my ($x, $y) = @_;
my $r = "$x and $y";
wantarray ? $r : length $r;
}

__END__

Axel
 
A

A. Sinan Unur

(e-mail address removed) wrote in (e-mail address removed):
So the following would have the same effect:

sub A_Funktion_Caller {
A_Funktion(@_);
}

In this case, yes. But it might be that A_Funktion_caller needs to do
something with the return value of A_Funktion, and what it needs to do
might depend on context. Dunno. I just tried to reduce the setup to bare
essentials.

Sinan
 
S

Stumproot

Tad said:
What is the relevance of including that?
Somtimes google does funny things with formatting of replies and I
thought it would be nice to warn people I'm using a less than ideal way
to post.

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"
}
}
-----------------------------


This I did not know, very handy.
Thanks.
 
A

Anno Siegel

Stumproot said:
A. Sinan Unur said:
"Stumproot" <[email protected]> wrote in
[...]

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(@_);

This is a no-op. In array context it calls A_Function in array context,
in scalar context it calls it in scalar context. It would do the same
without explicit "scalar" in the last operand. I'm not sure why Sinan
mentioned it in his posting, he never used it anywhere.

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
Stumproot said:
A. Sinan Unur said:
"Stumproot" <[email protected]> wrote in
[...]

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(@_);

This is a no-op. In array context it calls A_Function in array
context, in scalar context it calls it in scalar context. It would do
the same without explicit "scalar" in the last operand. I'm not sure
why Sinan mentioned it in his posting, he never used it anywhere.

I was assuming that of A_Funktion_Caller is called in scalar context, it
needs to call A_Funktion in scalar context for some *intermediate*
calculations. The fact that I did not put anything of substance in the
body of A_Funktion_Caller and typos I corrected in a later message
obscured that assumption.

Sorry.

Sinan
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in
Stumproot said:
A. Sinan Unur wrote:
[...]

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(@_);

This is a no-op. In array context it calls A_Function in array
context, in scalar context it calls it in scalar context. It would do
the same without explicit "scalar" in the last operand. I'm not sure
why Sinan mentioned it in his posting, he never used it anywhere.

I was assuming that of A_Funktion_Caller is called in scalar context, it
needs to call A_Funktion in scalar context for some *intermediate*
calculations. The fact that I did not put anything of substance in the
body of A_Funktion_Caller and typos I corrected in a later message
obscured that assumption.

I still don't see how

wantarray ? A_Funktion(@_) : scalar A_Funktion(@_);

would ever do anything different from just

A_Funktion(@_);

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
....

I still don't see how

wantarray ? A_Funktion(@_) : scalar A_Funktion(@_);

would ever do anything different from just

A_Funktion(@_);

Of course it wouldn't :)

To tell you the truth, this was the first time that I ever used
wantarray, and I was really straining to find a possible justification
for using wantarray. In the process, I might have overdone it.

The following is another convoluted example. If I am still failing, I'll
just go learn APL or something :)

#! /usr/bin/perl

my @r = call_letters();
my $r = call_letters();

print "@r\n$r\n";


sub call_letters {
wantarray
? select_letters()
: length(scalar select_letters());
}


sub select_letters {
my @letters;

for my $letter ( 'A' .. 'Z' ) {
if(rand(1) < 0.5) {
push @letters, $letter;
}
}
wantarray ? @letters : join('', @letters);
}

__END__
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in

[use of wantarray]
The following is another convoluted example. If I am still failing, I'll
just go learn APL or something :)

#! /usr/bin/perl

my @r = call_letters();
my $r = call_letters();

print "@r\n$r\n";


sub call_letters {
wantarray
? select_letters()
: length(scalar select_letters()); ^^^^^^
}


sub select_letters {
my @letters;

for my $letter ( 'A' .. 'Z' ) {
if(rand(1) < 0.5) {
push @letters, $letter;
}
}
wantarray ? @letters : join('', @letters);
}

__END__

Yes, that's an example where wantarray does something useful. The
indicated "scalar" could go away since length() already provides scalar
context.

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in

[use of wantarray]

....
sub call_letters {
wantarray
? select_letters()
: length(scalar select_letters()); ^^^^^^

....

Yes, that's an example where wantarray does something useful. The
indicated "scalar" could go away since length() already provides
scalar context.

Yes, absolutely. That was an editing error.

Now, where is that APL book ...

Joking aside, thank you very much for helping me better understand
wantarray.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top