<?xml version="1.0" encoding="windows-1252"?>
<node id="176157" title="Debugging with Hook::WrapSub" created="2002-06-20 17:10:28" updated="2005-08-10 15:17:31">
<type id="120">
perlmeditation</type>
<author id="128665">
gav^</author>
<data>
<field name="doctext">
I was tempted to call this a 'debugging trick of the week' but my [id://162145|last] one was nearly 2 months ago :)
&lt;p&gt;
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...
&lt;p&gt;
&lt;code&gt;
# turn on debugging
use constant DEBUG_HOOKS =&gt; 0;

if (DEBUG_HOOKS) {
    require Hook::WrapSub;
    require Devel::GetSymbols;
    require Data::Dump;
    no warnings 'once';
    my $hook_pre = sub {
            printf "&lt;hr&gt;&lt;pre&gt;Calling: &lt;b&gt;%s&lt;/b&gt;\nArgs: %s&lt;/pre&gt;&lt;hr&gt;",
                $Hook::WrapSub::name, Data::Dump::dump(@_);
        };
    my $hook_post = sub {
            printf "&lt;hr&gt;&lt;pre&gt;Called: &lt;b&gt;%s&lt;/b&gt; Result: %s&lt;/pre&gt;&lt;hr&gt;",
                $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);
    }
}
&lt;/code&gt;
Replace [CPAN://Data::Dump] with your favorite (I prefer it as it is compact) and the [perldoc://grep] to match your naming conventions.
&lt;p&gt;gav^</field>
</data>
</node>
