Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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/as.pl main::(/tmp/as.pl:19): 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/as.pl l +ine 10. main::my_sqrt(/tmp/as.pl:16): 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

In reply to Re: Neat Debugger tricks by salva
in thread Neat Debugger tricks by osunderdog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-18 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found