Perl | wantarray() Function
Last Updated :
07 May, 2019
wantarray() function in Perl returns True if the currently executing subroutine expects to return a list value, and false if it is looking for a scalar value.
Syntax: wantarray()
Returns: true for list value and false for scalar values
Example 1:
sub geeks
{
return ( wantarray () ? ( "Geeks" , "For" , "Geeks" ) : 1);
}
$value = geeks();
@value = geeks();
print ( "Value in Scalar context: $value\n" );
print ( "Value in List Context: @value" );
|
Output:
Value in Scalar context: 1
Value in List Context: Geeks For Geeks
Example 2:
sub geeks
{
if ( wantarray ())
{
$c = $a + $b ;
}
else
{
return 1;
}
}
$a = 10; $b = 20; $c = 0;
$value = geeks( $a , $b );
@value = geeks( $a , $b );
print ( "Value when called in Scalar context: $value\n" );
print ( "Value when called in List Context: @value" );
|
Output:
Value when called in Scalar context: 1
Value when called in List Context: 30
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...