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:
#!/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();
> 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |