Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Neat Debugger tricks

by salva (Abbot)
on Nov 17, 2005 at 10:17 UTC ( #509353=note: print w/ replies, xml ) Need Help??

in reply to Neat Debugger tricks

the new assertion subs, available on blead perl and on 5.10 in the future, act as conditional breakpoints under the debugger.

For example, this script uses an assertion to test that the argument passed to my_sqrt is a positive number:

#!/usr/local/bin/perl5.9.3 use assertions '1'; # assrt. are always active use strict; use warnings; sub assert (&@) :assertion { my $sub = shift; &{$sub}() or die "assertion failed: @_" } sub my_sqrt { my $n = shift; assert { $n >=0 } "argument has to be a positive number"; return sqrt($n); } for my $n (0.3, 0, -4, 7, 9) { printf "sqrt(%f) = %f\n", $n, my_sqrt($n); }
then running it under the debugger:
toledo:~# perl5.9.3 -d /tmp/ main::(/tmp/ for my $n (0.3, 0, -4, 7, 9) { DB<1> c sqrt(0.300000) = 0.547723 sqrt(0.000000) = 0.000000 assertion failed: argument has to be a positive number at /tmp/ l +ine 10. main::my_sqrt(/tmp/ return sqrt($n); DB<1> l 16==> return sqrt($n); 17 } 18 19: for my $n (0.3, 0, -4, 7, 9) { 20: printf "sqrt(%f) = %f\n", $n, my_sqrt($n); 21 } 22 DB<1> p $n -4

Comment on Re: Neat Debugger tricks
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://509353]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2015-11-27 03:53 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (717 votes), past polls