cybo
November 13th, 2008, 07:03 PM
does anyone know what does wantarray function in perl does? i looked it up in here (http://perldoc.perl.org/functions/wantarray.html).
below is its contents:
Returns true if the context of the currently executing subroutine or eval is looking for a list value. Returns false if the context is looking for a scalar. Returns the undefined value if the context is looking for no value (void context).
return unless defined wantarray; # don't bother doing more
my @a = complex_calculation();
return wantarray ? @a : "@a";
wantarray()'s result is unspecified in the top level of a file, in a BEGIN , UNITCHECK , CHECK , INIT or END block, or in a DESTROY method.
This function should have been named wantlist() instead.
i can't understand what it is trying to say. what is meant by context, void context and non-void context?
i tried the following code but i always get the same result - error
#!/usr/bin/perl
use warnings;
use strict;
&my_sub();
&my_sub("something");
&my_sub("something", "something");
sub my_sub {
if (defined wantarray) {
print "wantarray\n";
} else {
print "error\n";
}
}
Also does anyone know any dedicated perl forums for newbies and perl gurus wanna be?
thanx
below is its contents:
Returns true if the context of the currently executing subroutine or eval is looking for a list value. Returns false if the context is looking for a scalar. Returns the undefined value if the context is looking for no value (void context).
return unless defined wantarray; # don't bother doing more
my @a = complex_calculation();
return wantarray ? @a : "@a";
wantarray()'s result is unspecified in the top level of a file, in a BEGIN , UNITCHECK , CHECK , INIT or END block, or in a DESTROY method.
This function should have been named wantlist() instead.
i can't understand what it is trying to say. what is meant by context, void context and non-void context?
i tried the following code but i always get the same result - error
#!/usr/bin/perl
use warnings;
use strict;
&my_sub();
&my_sub("something");
&my_sub("something", "something");
sub my_sub {
if (defined wantarray) {
print "wantarray\n";
} else {
print "error\n";
}
}
Also does anyone know any dedicated perl forums for newbies and perl gurus wanna be?
thanx