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

Hola monks, I was wondering if there was a way to detect if a function is being used on a variable or alone. I dont know a better way to phrase that, but lets say you have the following:
&myfunction; $variable = &myfunction;
Is there a way for &myfunction to discern between the two?

Replies are listed 'Best First'.
Re: subroutines & functions
by davorg (Chancellor) on Jun 22, 2003 at 19:51 UTC

    You're asking what context the function is being called in. There are (basically) three contexts in Perl - void, scalar and list. You can use the wantarray function to determine how your function has been called.

    sub how { my $how = wantarray; if (defined $how) { if ($how) { print "list\n"; } else { print "scalar\n"; } } else { print "void\n"; } } how; my $s = how; my @a = how;

    For your question I think you just need to check if the return value from wantarray is defined.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      For your question I think you just need to check if the return value from wantarray is defined.
      I rarely, if ever, care about the difference between scalar and void context. To me, and I think to perl, too, void context is a special case of scalar context. Hence, undef is false, too. I definitely don't care about the difference between returning a scalar or not returning anything, and most certainly not for "efficiency reasons": that extra test likely wastes more time than returning and wasting a scalar value ever would.

      I do care about the difference between list context and scalar context.

        I definitely don't care about the difference between returning a scalar or not returning anything, and most certainly not for "efficiency reasons": that extra test likely wastes more time

        Um, if you are doing it for efficiency reasons, then you don't do all of the work and then, just at the end, check whether they want a value back or not. You note that they don't need a value back and avoid the whole computation. For example, in Win32::TieRegistry, you can use delete to delete a key/value from the registry. Just like when using delete on a regular hash, delete returns what the key (that was just deleted) contained. But if you use delete in a void context, then all of this making of copies is avoided.

        Then there is the whole 'failures in a void context should resort to die' concept that I rather like.

                        - tye
Re: subroutines & functions
by BrowserUk (Patriarch) on Jun 22, 2003 at 19:50 UTC

    Check out perlfunc:wantarray. It will return undef if the function has been called in a void context.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: subroutines & functions
by particle (Vicar) on Jun 23, 2003 at 01:07 UTC

    BrowserUK and davorg have probably given you the answer you need. but i'd like to suggest the Want module. it determines many more contexts, and might give you some insight as to the importance of context, and examples of what you can use it to accomplish.

    ~Particle *accelerates*