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

In Rexx a builtin variable (source) contains COMMAND, SUBROUTINE, or FUNCTION (and some other stuff). Can I find out that same info in perl (the method of invocation)? If so, where should I have looked for this info? Thx - Toolsmith

Replies are listed 'Best First'.
Re: invocation mode - how discover?
by Fletch (Bishop) on Dec 09, 2005 at 16:56 UTC

    Maybe you're looking for caller? Hard to tell given the vagueness of "method of invocation" (how your Perl program was called? how a given sub was called? how you were called on the phone? . . .)

      I apologize for the terseness of my message. What I'm really looking for, I think, is the name of the caller. For example, if the script was invoked by typing its name on the command line, that would be the shell and would equate to what I called COMMAND in my original question. If the program is called using backquotes from another perl script, I'd want the name of that script, or at least perl.exe as the caller--that would equate to SUBROUTINE in my original post. And FUNCTION in my original post is probably a rexx-only concept, sorta like using popen. I replied to your message out of the three, because "caller" really seems closest in intention to what I'm looking for, but as far as I can determine it only works for a subroutine called from within the same script. Maybe there's no way. I'm thinking now that a stack trace might give me what I want.
        caller is really only useful in a subroutine, to tell you the name of the subroutine that called it. I think I understand what you're asking for, but I don't understand why. The answer is that it's not easy (you'd have to start with getppid and then find the process that corresponds to that pid, then find its command). But in general, it's relatively rare for perl scripts to be called from each other by backticks. It's usually much better practice to factor out the parts that are needed in different places into subroutines. So the big question I have is, "What exactly are you trying to achieve?" It's usually a very bad idea (for debugging if nothing else) to have your program behave differently depending on who calls it. What are your end goals? There's probably a much more perlish way of doing it.
Re: invocation mode - how discover?
by ikegami (Patriarch) on Dec 09, 2005 at 16:55 UTC

    I'm not sure what you want. Perhaps wantarray?

    It it returns undef, the sub was called as a subroutine; and
    if it returns a defined value, the the sub was called as a function.

    (Furthermore,
    if it returns a true value, the caller is ready to accept a list; and
    if it returns a false value, the caller is not ready to accept a list.)

    That said, I think it's generally a bad idea for a Perl sub to use wantarray to determine whether it was called as a subroutine or as a function. Checking the arguments is usually better. For example,

    sub getter_setter { my $self = shift; if (@_) { $self->{value} = $_[0]; } return $self->{value}; }
Re: invocation mode - how discover?
by ptum (Priest) on Dec 09, 2005 at 17:36 UTC

    Is it possible you are simply looking for the $0 variable, aka $PROGRAM_NAME? It is hard to tell what you need from your terse question. Of course, I know nothing about Rexx -- perhaps to a Rexx monk all would be clear. :)

    $0 "Contains the name of the file containing the Perl script being executed." Programming Perl, 3rd edition, p. 672


    No good deed goes unpunished. -- (attributed to) Oscar Wilde