our $DEBUG_LEVEL = 5; sub __dbg (&;$) { my ($code, $verbosity) = @_; if ($DEBUG_LEVEL >= ($verbosity // 1)) { my $msg = join('', $code->()); print STDERR "DEBUG: ", $msg, "\n" if length($msg); } } # ...the meat of the package is below... #### # ...the meat of the package is above... __dbg { no strict 'refs'; no warnings 'redefine'; my $package = __PACKAGE__; my %patched = ('__dbg' => 1); foreach (grep { defined &{"${package}::$_"} } keys %{"${package}::"}) { unless (exists $patched{$_}) { $patched{$_} = 1; __dbg { "Monkeypatching '${package}::$_' for tracing..." } 5; my $fname = $_; my $fn = \&{"${package}::$_"}; *$_ = sub { __dbg { "Starting ${package}::${fname}(", join(',', @_), ')' } 4; my @ret = $fn->(@_); __dbg { "Ending ${package}::${fname} with return values: ", join(',', @ret) } 4; return unless defined(wantarray); return @ret if wantarray; return @ret[0] if scalar(@ret); } } } }; 1;