Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How much can a function know about the context in which it is called?

by jacques (Priest)
on Jul 29, 2005 at 20:01 UTC ( [id://479519]=perlquestion: print w/replies, xml ) Need Help??

jacques has asked for the wisdom of the Perl Monks concerning the following question:

I have a bunch of calls to the same function. The function ultimately prints to STDOUT. I want to change the function to return a string rather than print it. So that I can say:
print function(); # prints the string that function returns.
Easy enough. But instead of putting the print before the funtion call, I would like to simply say:
funtion(); # prints string since not assigned $value = funtion(); # Does not print. $value holds the return string.
In other words, I want function() to know the context in which it is called, so that it knows whether to print a string or to return one. And I don't want to use IO::Scalar.

Is this possible?

Replies are listed 'Best First'.
Re: How much can a function know about the context in which it is called?
by friedo (Prior) on Jul 29, 2005 at 20:07 UTC
    Certainly. The unfortunately named wantarray function can tell you:

    Returns true if the context of the currently executing subroutine or eval() block 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).
Re: How much can a function know about the context in which it is called?
by Joost (Canon) on Jul 29, 2005 at 20:07 UTC
Re: How much can a function know about the context in which it is called?
by ikegami (Patriarch) on Jul 29, 2005 at 20:08 UTC
    Use wantarray:
    sub test { print(defined(wantarray)?1:0, " "); print(wantarray?1:0, "\n"); } test(); # 0 0 $a = test(); # 1 0 @a = test(); # 1 1
    sub function { my $rv = "Hello World"; if (defined wantarray) { return $rv; } else { print("$rv\n"); return; } } function(); # Hello World my $greet = function(); print("[$greet]\n"); # [Hello World]
Re: How much can a function know about the context in which it is called?
by Corion (Patriarch) on Jul 29, 2005 at 20:12 UTC

    If you are not afraid to use a module by TheDamian, Contextual::Return can take out much of the uglyness that wantarray syntax imposes.

Re: How much can a function know about the context in which it is called?
by jacques (Priest) on Jul 29, 2005 at 20:27 UTC
    Thanks everyone.
Re: How much can a function know about the context in which it is called?
by brian_d_foy (Abbot) on Jul 30, 2005 at 18:33 UTC

    Note that the context doesn't tell you what happens with the return values or what the user wants to do.

    For instance, in this common idiom, function() is in list context but I want to the output in @values, not printed. I wouldn't like functions that try to be smarter than me.

    my @values = map { function( $_ ) } @input;

    Every time I've thought it would be good to simply print to STDOUT, someone comes along and wants the output in some other way. Instead of making the function know that stuff, I let the user decide. I can always make a print_function() that does that and still lets me use the "dumb" version/ :)

    --
    brian d foy <brian@stonehenge.com>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://479519]
Approved by Joost
Front-paged by traveler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-24 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found