You've got excellent answers, but I think it's important to explain that they fall into different categories.

The one category shown is basically installing a wrapper with the same name, and execute code before the original sub is entered and after it is exited. This is very stable and easy to achieve without changing the monitored sub, but comes with the drawback that information about internal state- i.e. lexical vars - is lost.

The other category shown is adding a lexical inside the sub, which holds an object. At the moment the sub (or generally the scope) is left , lexicals without reference count will be destructed and the DESTROY method will be called. This method can trigger a call back which can display the internal state in its closure.

But I'm not sure that you can know achieve info where - i.e. at which return - the sub is left.

A third - yet not demonstrated - category is to use the debugger. There you can define watch and trace expressions for subs which will trigger at different points. But this approach comes with dramatically reduced execution speed and won't be appropriate inside a long running process, like e.g. a web application.

So the appropriate answer depends on the exact nature of the information you (or someone else digging up this thread) wants to gather. :)

HTH

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!


In reply to Re: Just before subroutine returns (depends) by LanX
in thread Just before subroutine returns by solegaonkar

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.