well~

i've created a sub to pretty-print subroutine entry/exit for debugging purposes. i'm not alltogether happy with the way i have to use this. in particular, i MUST add two lines of code to every function, and i must be careful where i place them. also, i wonder if there is a better method to getting the sub name rather than (caller(1))[3]....

i'd be much happier with something transparent that could tell me when i've entered and exited a function, and write that to my output log, as well. is this possible, or should i be satisfied with what i have?

any hints or suggestions GREATLY appreciated!

find my test script below:

Script:

#!/usr/bin/perl -w use strict; # be good! sub hello; sub goodbye; sub imlate; my $LOG; # dummy log this example... $::DBUG = shift; # take from command-line this e +xample sub ::f { # pretty-print sub entry/exit my $n = "\n"; # shortcut for newline my $enter = shift; # entering -> 1, exiting -> 0 $enter && $::FUNC_LEVEL++; # increment global var if enter +ing # print > times global function level var plus name of calling fun +ction $::DBUG && print( # print to screen ( ($enter) ? ">" : "<" ) x $::FUNC_LEVEL . ( " " ) . ( caller(1) )[3] . $n # calling function name ); $::DBUG && $LOG && LOG->print( # print to log ( ($enter) ? ">" : "<" ) x $::FUNC_LEVEL . ( " " ) . ( caller(1) )[3] . $n ); $enter || $::FUNC_LEVEL--; # decrement global var if exiti +ng }; sub hello { # standard usage ::f(1); # function entry for(1..2) { print "hello!\n" . goodbye(); # call some sub... } ::f(0); # function exit }; sub goodbye { # a tricky bit... ::f(1); # regular entrance ::f(0); # must execute BEFORE return return "goodbye!\n"; }; sub imlate { # just for giggles... ::f(1); print "i'm late!\n" x 3; ::f(0); }; hello(); imlate();

Output:

so perl imlate.pl 1 prints
> main::hello >> main::goodbye << main::goodbye hello! goodbye! >> main::goodbye << main::goodbye hello! goodbye! < main::hello > main::imlate i'm late! i'm late! i'm late! < main::imlate

~Particle


In reply to code review: pretty print sub entry/exit by particle

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.