A subroutine that returns a scalar or a list.
@array = scalarOrList();
$" = "\n";
print "Returned:\n@array\n";
print "\nReturned: " . scalarOrList(); # scalar context
sub scalarOrList
{
if ( wantarray() ) { # if list context
return 'this', 'is', 'a', 'list', 'of', 'strings';
}
else { # if scalar context
return 'hello';
}
}
Related examples in the same category