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


Illustration: I have 2 packages and 1 script that is executed:

SCRIPT -> A -> B

If the script says " new->B() " then B could know the path-name of the process by saying $0.

BUT: If package A says: " new->B() " then B would get WRONG path when saying $0.

Q: How do I get the path-name of the calling location?

  • Comment on How to find out the path of which package called a function?

Replies are listed 'Best First'.
Re: How to find out the path of which package called a function?
by Corion (Patriarch) on Sep 03, 2008 at 08:16 UTC

    Have you looked at caller? It gives you the line number, filename and package of your callers.

    One day, I hope Perl will accept negative arguments to the caller function as well and still give the correct results, always.

      You Sir, you are a lifesavior!
      Did not know of "caller". It seems like its natively installed also in perl version 5.7 so its perfect for me. Thanks a lot Corion!

        The caller function has been in Perl since at least Perl 5.0.

        As an aside, there is no production release of Perl with the version number 5.7. The old production release is Perl 5.8 with the current minor version 5.8.8 and the current production release is Perl 5.10 with the current minor version 5.10.0.