See perlfunc:caller ... From the POD ...

caller EXPR caller Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or `eval()' or `require()', and the undefined value otherwise. In list context, returns ($package, $filename, $line) = caller; With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to go back before the current one. ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require) = caller($i); Here `$subroutine' may be `"(eval)"' if the frame is not a subroutine call, but an `eval()'. In such a case additional elements `$evaltext' and `$is_require' are set: `$is_require' is true if the frame is created by a `require' or `use' statement, `$evaltext' contains the text of the `eval EXPR' statement. In particular, for a `eval BLOCK' statement, `$filename' is `"(eval)"', but `$evaltext' is undefined. (Note also that each `use' statement creates a `require' frame inside an `eval EXPR') frame. Furthermore, when called from within the DB package, caller returns more detailed information: it sets the list variable `@DB::args' to be the arguments with which the subroutine was invoked. Be aware that the optimizer might have optimized call frames away before `caller()' had a chance to get the information. That means that `caller(N)' might not return information about the call frame you expect it do, for `N > 1'. In particular, `@DB::args' might have information from the previous time `caller()' was called.

 

Update - While caller does return information as to the calling subroutine and package, it does not return the reference name that has been used to called the subroutine - This can be confirmed as follows:

#!/usr/bin/perl use Data::Dumper; use strict; my $divesub = \&somesub; my $raisesub = \&somesub; &{$divesub}; sub somesub { print STDERR Data::Dumper::Dumper( caller(0) ); }

... of which the output is as follows ...

$VAR1 = 'main'; $VAR2 = './test'; $VAR3 = 10; $VAR4 = 'main::somesub'; $VAR5 = 0; $VAR6 = undef; $VAR7 = undef; $VAR8 = undef; $VAR9 = 0; $VAR10 = '';

So, while it is possible that the caller function may still be of use to you in your endeavour, it does not address the immediate question.

Thanks also to Kanji++ for pointing this out to me! :-)

 


In reply to Re: sub calling name by rob_au
in thread sub calling name by Dogma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.