What *exactly* is a "global watch-expression" in the context of
the perl debugger? And what exactly needs to happen for a global
watch-expression to stop the execution of the program?
Of course, I tried to RTFM, but all it says is this:
w expr Add a global watch-expression. We hope you know
what one of these is, because they're supposed to
be obvious.
For once, I kind of agree with Purl Gurl's rant. For years, I
misunderstood what a watch-expression was because of this explanation,
and it wasn't until I was studying Ruby with the Ruby "Pickaxe" book
(that is, "Programming Ruby" by Dave Thomas) that I understood it by
reading the following:
wat[ch] expr Break when expression becomes true.
It even provides an example:
(rdb:1) watch n==1
With this watch-expression the debugger will break as soon as the
expression "n==1" evaluates to true (which is when n equals 1).
In Perl, however, it seems like the debugger breaks whenever the
expression changes (and not necessarily when it becomes true). So if
you have this script:
/usr/bin/perl
$n = 1;
$n = 2;
$n = 3;
$n = 4;
$n = 5;
$n = 6;
$n = 7;
__END__
and you run the debugger with:
perl -d script.pl
and set a watch expression like this:
DB<1> w $n == 3
then continue with the "c" command, you'll see it breaks AFTER having
set $n to 3 (because now "$n == 3" evaluates to true). If you re-
continue (again with the "c" command), you'll break at the very next
line (after setting $n to 4), because now "$n == 3" evaluates to
false.
If you continue again, you'll run to the end of the script, because
the expression "$n == 3" never changes.
If you used this instead:
DB<1> w $n
the debugger would break at every line because $n evaluates to
something different at every line.
I hope this explanation helps. Personally, I think it would be a
lot easier to just have written:
w expr Halts program execution whenever the value
of the expression changes.
instead of writing "We hope you know what one of these is...". The
first way is not only blatantly unhelpful, but also fosters
misconceptions if the programmer happens to thinks he/she understands
when in fact he/she really does not.
Ironically, it was an explanation in a Ruby book that helped me
understand what the Perl documentation should have explained. (And it
did it using less words, too, since it didn't bother explaining that
it should already be obvious.)
-- Jean-Luc