Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Context sensitive functions 1

Status
Not open for further replies.

redgenie

Programmer
Aug 10, 2000
36
GB
IS it possible to create context sensitive functions?

Ie. The function returns different results depending on what context it's called?

my @array =someFunction();
my $scalar =someFunction();

If the function is called in array context I want to return an array, but if it's called in scalar context I want to join the array and return the results....
 
I believe you'll need to use something called 'wantarray' in your subroutine. Here's an excerpt from the docs:
wantarray
Returns true if the context of the currently executing subrou-
tine is looking for a list value. Returns false if the con-
text 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";

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top