I was tempted to call this a 'debugging trick of the week' but my last one was nearly 2 months ago :)
Basically the problem I had was in a CGI script I wanted to know what page was being displayed and if any utility subs were being used. I was just about to go through and add some debugging prints to the the top of each sub when I deicided that I had to be able to be lazy...
# turn on debugging
use constant DEBUG_HOOKS => 0;
if (DEBUG_HOOKS) {
require Hook::WrapSub;
require Devel::GetSymbols;
require Data::Dump;
no warnings 'once';
my $hook_pre = sub {
printf "<hr><pre>Calling: <b>%s</b>\nArgs: %s</pre><hr>",
$Hook::WrapSub::name, Data::Dump::dump(@_);
};
my $hook_post = sub {
printf "<hr><pre>Called: <b>%s</b> Result: %s</pre><hr>",
$Hook::WrapSub::name,
Data::Dump::dump(@Hook::WrapSub::result);
};
foreach my $sub (grep /^(page|do)_/, Devel::GetSymbols::subs()) {
Hook::WrapSub::wrap_subs($hook_pre, $sub, $hook_post);
}
}
Replace Data::Dump with your favorite (I prefer it as it is compact) and the grep to match your naming conventions.
gav^
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|