Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

debug: local watch expression

by InfiniteLoop (Hermit)
on Dec 06, 2006 at 17:41 UTC ( [id://588140] : perlquestion . print w/replies, xml ) Need Help??

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

Greetings Monks,
 While debugging my code, I had to watch a variable, $attribute, in a for loop. However if I set "w $attribute" , the debugger will break at all usages of $attribute, as the perldebug says: " w expr : Add a global watch-expression.". How do I watch variables, only in a certain package ? Thanks.

Replies are listed 'Best First'.
Re: debug: local watch expression
by wjw (Priest) on Dec 06, 2006 at 18:04 UTC
    quick and dirty... put a print statement after the setting of your $attribute variable, and set a break point after the print statement... . Will save you a lot of time I think...

    $attribute = "whatever"; print "$attribute\n"; #set break here

    Even better look Here about 1/3 down the page. You will see a line that states-

    "For example, this will print out $foo every time line 53 is passed:"

    a 53 print "DB FOUND $foo\n"

    I believe that is what you want...

    ...the majority is always wrong, and always the last to know about it...

      Thanks wjw. I guess there is more than one way to do it :). However, it still bugs me that I can't watch, under debugger, a variable in a selected package. I have used Komodo to debug perl code and I was able to watch a selected variable, but it wouldn't break on the watched variable though.
        Yeah, I generally use Devel::ptkdb, but of course that requires that Tk be installed. I have also used ddd on Linux and Cygwin, and like that a lot. I simply refuse to pay for Komodo, and have found the Epic plugin for MyEclipse sort of wierd too... . I generally find that I end up back at the CLI debug with some variation of what I mentioned earlier. I usually just don't have the time to mess around setting up a 'convenient' debug environment when at work. :-)

        Glad you brought up that question too. Made me go back and review the perldebug docs, which pointed out a few things that I could be doing to speed things up...

        ...the majority is always wrong, and always the last to know about it...

Re: debug: local watch expression
by rmcgowan (Sexton) on Sep 17, 2015 at 23:38 UTC

    I found this post searching for help for the same sort of problem. The solutions suggested did not help, but did point me something that did. I've known about the $DB::single variable for some time, and came up with this solution:

    if (! $var) { $DB::single = 1; }

    Put this in the source file, just before the point of failure, and run the script usign perl -d script.

    Execution will stop when $var becomes zero or undef, but only at the point where you put the code. If it changes anywhere else, outside the scope of the test, nothing is done.

    The sixty four thousand dollar question is if this has any side effects that might cause other problems. That would be nice to know.


Re: debug: local watch expression
by texasperl (Sexton) on Dec 07, 2006 at 18:59 UTC
    I don't quite know if this would apply in your case, but it is good for quick debugging.
    #!/usr/bin/perl -w use strict; use Smart::Comments; for my $val (@loop_values) { ### $val do_stuff_to_val($val); ### $val }
    This will print out the values of $val before and after you modify it. And then once you don't need them anymore, comment out the use Smart::Comments line and you're set. Check out the POD on Smart::Comments for more info. Quite handy. -- texasperl