I have used these simple subs many times to great effect. I put them in a "standard utility module" that I use in many programs. I recommend just setting these short subs to return a string, don't use values in main calling program to separate things out. Perhaps these things should/could be called iwascalledby() hewascalledby(); or whatever suits your fancy.
#!usr/bin/perl -w use strict; sub whoami { (caller(1))[3] }; sub whowasi { (caller(2))[3] }; some_sub(); sub some_sub { print "Now inside some_sub\n"; print " called_by ". whoami.".\n"; another_sub(); } sub another_sub { print "Now inside another_sub\n"; print " called by ". whoami(). " \n"; print " previous sub ". whowasi(). " \n"; } __END__ Prints: Now inside some_sub called_by main::some_sub. Now inside another_sub called by main::another_sub previous sub main::some_sub
caller() has more params and they can be useful, eg. line number etc. But basically if one of my library routines gets bogus input, then I usually just need to know the name of the caller and the name of previous caller to figure out where bogus input came from.

In reply to Re: getting a subroutine's name by Marshall
in thread getting a subroutine's name by biohisham

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.