#!/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 example 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 entering # print > times global function level var plus name of calling function $::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 exiting }; 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