How to get a subroutine to know its own name 
Author Message
 How to get a subroutine to know its own name

        I'm wondering if there's any way (in Perl5) to get a subroutine
to know its own name?  The only mention of this I've seen is for the
special AUTOLOAD subroutine, where the subroutine's name "magically
appears in the $AUTOLOAD variable in the same package as the AUTOLOAD
routine".  Is there a way to do this in a normal subroutine (without
explicitly passing it as an argument)?

                                                thanks for any help,
                                                gregg



Mon, 28 Apr 1997 20:21:33 GMT  
 How to get a subroutine to know its own name

:
:       I'm wondering if there's any way (in Perl5) to get a subroutine
:to know its own name?  The only mention of this I've seen is for the
:special AUTOLOAD subroutine, where the subroutine's name "magically
:appears in the $AUTOLOAD variable in the same package as the AUTOLOAD
:routine".  Is there a way to do this in a normal subroutine (without
:explicitly passing it as an argument)?

Try

    my $subname = (caller(0))[3];

--tom
--

    "The usability of a computer language is inversely proportional to the
    number of theoretical axes the language designer tries to grind."
        --Larry Wall



Tue, 29 Apr 1997 11:27:53 GMT  
 How to get a subroutine to know its own name

Quote:

>    I'm wondering if there's any way (in Perl5) to get a subroutine
>to know its own name?  The only mention of this I've seen is for the
>special AUTOLOAD subroutine, where the subroutine's name "magically
>appears in the $AUTOLOAD variable in the same package as the AUTOLOAD
>routine".  Is there a way to do this in a normal subroutine (without
>explicitly passing it as an argument)?

(caller(0))[3] contains the subroutine name (including the package).
(caller(0))[0] is the package, in case you want to get rid of that
part.

Anno



Tue, 29 Apr 1997 22:10:22 GMT  
 How to get a subroutine to know its own name

Quote:

>     my $subname = (caller(0))[3];

But beware of excessive use of "caller".  We found that repeatedly
using "caller" in Perl 4.036 on IRIX 4.x (and probably 5.2) causes
Perl to core dump (in seemingly unrelated sections of the script).

--

Structural Dynamics Research Corporation
2000 Eastman Drive, Milford OH 45150 USA



Fri, 02 May 1997 05:05:49 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. knowing it's own name?

2. Subroutine knowing its name?

3. Getting the subroutine name from within a sub

4. Import own Subroutines in Perl

5. Own module: how to export constants/subroutines of other modules

6. should subroutines do their own error checking?

7. Whats a programs own name?

8. process name of own

9. How do you get the script own name?

10. Q: getting my own process id?

11. Q: getting my own process id?

12. Anyone know of a subroutine hierarchy documenter

 

 
Powered by phpBB® Forum Software