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

Dear monks

The perl debugger help on b says:

b $var Set breakpoint at first line of subroutine referenced by $var.
However when I try with the following program:
$ cat namevsref.pl use warnings; use strict; my $time = 1; my $x = sub { print "Inside sub. time = ".($time++)."\n"; }; no strict 'refs'; *{"tutu"} = $x; $x->(); tutu();
I got this behavior:
pp2@nereida:~/src/perl/testing$ perl -wd namevsref.pl Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(namevsref.pl:4): my $time = 1; DB<1> l 1,14 1: use warnings; 2: use strict; 3 4==> my $time = 1; 5 6 my $x = sub { 7: print "Inside sub. time = ".($time++)."\n"; 8: }; 9 10: no strict 'refs'; 11: *{"tutu"} = $x; 12 13: $x->(); 14: tutu(); DB<2> c 11 main::(namevsref.pl:11): *{"tutu"} = $x; DB<3> x $x 0 CODE(0x825a074) -> &main::__ANON__[namevsref.pl:8] in namevsref.pl:6-8 DB<4> b $x DB<5> L namevsref.pl: 11: *{"tutu"} = $x; break if ($x) DB<5> c Inside sub. time = 1 Inside sub. time = 2 Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB<5>
The atttempt to set a breakpoint in the sub referenced by $x seems to be interpreted as a conditional breakpoint. Consequently the debugger never stops

What am I doing wrong here? (Perl version is v5.8.8)

Thanks

Casiano

Replies are listed 'Best First'.
Re: b $var
by jethro (Monsignor) on Jun 17, 2008 at 12:52 UTC
    b subname [condition] Set a breakpoint before the first line of the named + subroutine. subname may be a variable containing a code reference (in this case condition is not supported) +.
    The perldoc perldebug has a bit more information. Note the word "named". Seems to imply that you can't do that with anonymous subroutines.

      Seems to imply that you can't do that with anonymous subroutines.

      subname may be a variable containing a code reference

      "A variable containing a code reference" can be an anonymous subroutine. The doc bit that says "named subroutine" doesn't mean it the way you read it.

      <radiant.matrix>
      Ramblings and references
      “A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.” — Herm Albright
      I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: b $var
by ww (Archbishop) on Jun 17, 2008 at 12:39 UTC

    On Ubuntu, perl 5.8.7 and on W32, AS Perl 5.8.8 my debug help says:

    b [ln|event|sub] [cnd] Set breakpoint

    On the linux box, man perldebug provides similar guidance. Are you sure about your b $var?

    In any case, it occurred to me that setting the breakpoint to the line number INSIDE your ANONYMOUS sub would be worth trying.

    And, lo and behold, doing so causes my debugger to behave as expected. I believe that since the sub is anonymous, we can't b sub_name (and, of course, the preceding line, calling the sub is not breakable).

    Update: fixed square brackets

Re: b $var
by casiano (Pilgrim) on Jun 17, 2008 at 13:03 UTC
    Thanks jethro that explains the whole thing.

    ww: My problem is that the subs I am debugging are dynamically generated (and run on a remote perl interpreter started via SSH) and so the associated file is s.t. I can't predict in advance, like eval 223. Having s.t. like b CODE reference expression in the Perl debugger would be helpful

    Many, many thanks for your time and for sharing your wisdom

    Casiano

Re: b $var
by casiano (Pilgrim) on Jun 17, 2008 at 13:09 UTC
    Here is a cut and paste of the debugger h b command:
    pp2@nereida:~/src/perl/testing$ perl -wd namevsref.pl Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(namevsref.pl:4): my $time = 1; DB<1> h b b Sets breakpoint on current line) b [line] [condition] Set breakpoint; line defaults to the current execution line; condition breaks if it evaluates to true, defaults to '1'. b subname [condition] Set breakpoint at first line of subroutine. b $var Set breakpoint at first line of subroutine referenced by + $var. b load filename Set breakpoint on 'require'ing the given file. b postpone subname [condition] Set breakpoint at first line of subroutine after it is compiled. b compile subname Stop after the subroutine is compiled. DB<2>
    The b $var does not say the sub referenced by $var must be named?

    Thanks all of you

    Casiano