hlen has asked for the wisdom of the Perl Monks concerning the following question:

I found myself using cluck() today. Nothing wrong about that, except I couldn't find a nice way to ditch it for perl -d. I'm trying to see how to schedule/stop for a stack trace in this situation, with the debugger:
sub message_id($) { my $tr = shift; defined $tr or cluck(), return undef; $tr->isa('HTML::Element') or return undef; my $td = ($tr->content_list())[0]; return $td->as_trimmed_text(); }
This function is called hundreds of times, I only want a backtrace when it's about to return in that specific condition.
(and yeah, I know cluck works perfectly there).

Thanks in advance

Replies are listed 'Best First'.
Re: Translate debug code to a debugging technique
by shenme (Priest) on Oct 02, 2004 at 04:03 UTC
    Would "$DB::single = 1;" be enough to return you control while in the debugger? You could then do whatever you needed to track down your problem?
      Seems nice =). I'm trying to test that, but am having some trouble. My code forks, and the debugger is opening some `Daughter' windows, and then I can't get it to respond. It seems to be entering an infinite loop when I close them. I need some practice, maybe there's a way to make it not follow children.

      I still wonder if there's a way to do that without editing the code, out of curiosity.

      Thanks a lot.

Re: Translate debug code to a debugging technique
by Prior Nacre V (Hermit) on Oct 02, 2004 at 05:38 UTC

    You could call cluck() conditionally based on the value of $^D. Something as simple as ( $^D || cluck() ) may suffice.

    See both perlrun and perldebug for a number of gotchas with this method.

    Regards,

    PN5