http://www.perlmonks.org?node_id=1022165

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

Hi, I am debugging my perl code using -d option. I Set a breakpoint at the first line of my named subroutine. Since that subroutine is called so many times, I want to set the break pointin that subroutine with some condition so that I can avoid some iterations.

Below is the code part of the subroutine:

Engine::Execute { : foreach (@commands) { $cmd = $_; if($cmd =~ /INITIAL/i) { unless (fork) { eval $cmd; if ($@) { print "When executing: $cmd\nFollowing error occured: $@\n" +; } exit; } } else { eval $cmd; if ($@) { print "When executing: $cmd\nFollowing error occured: $@\n"; } } } }

I tried to set as b Engine::_Execute /INITIAL/i but my script runs without wait. Please let me know how to set the break point so that execution will continue until the line if($cmd =~ /INITIAL/i) hits.

Replies are listed 'Best First'.
Re: Setting break point for particular condition in the subroutine
by LanX (Saint) on Mar 07, 2013 at 08:22 UTC
    > I tried to set as b Engine::_Execute /INITIAL/i but my script runs without wait.

    you're setting a breakpoint on the routine with a condition which checks $_ which doesn't make much sense.

    If I where you I would set the breakpoint at first line of body of if($cmd =~ /INITIAL/i) condition, that is unless (fork)

    There is more than one way to do it.

    Find the line number by investigating the routine with l Engine::_Execute and set b line_number.

    Or manipulate your source and include $DB::single=1 before the line you want to break.

    Cheers Rolf

      Sorry for not mentioned. I tried $DB::single=1 and I am able to set the break point at the required place. Thanks..

      I am getting the below error only when I try to create child process using fork in the code mentioned in this Thread.

      Engine::_Execute(..\..\perl\modules/Engine.pm:122): 122: unless (fork) { DB<1> s Bizarre SvTYPE [37] at ..\..\perl\modules/Engine.pm line 122. Engine::_Execute(C:/Perl/lib/Carp.pm:97): 97: { local $@; require Carp::Heavy; } # XXX fix require to not clea +r $@?

      It means there are some localized scalar variables are trying to restore in a child process hence throwing error

      "Attempt to free unreferenced scalar"

      There is some modules where carp.pm is used. I replaced all the places of carp with warn and tried but no luck still it is showing the same. Please let me know how to debug this issue.

        No idea, better start a new thread: "Forking Problem: Attempt to free unreferenced scalar" to attract the experts.

        One tip: Following the local markup conventions using <p> instead of <pre> will help in gaining acceptance and finding support. =)

        Cheers Rolf

        Please, do not use <pre> tags for text. Read <pre> Versus <code> Tags for more information as already advised in a /msg.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Setting break point for particular condition in the subroutine
by rjt (Curate) on Mar 07, 2013 at 08:15 UTC

    It sounds like you want to insert the breakpoint at the if ($cmd =~ /INITIAL/i) line. Let's say that's line 123. Then, type:

    b 123 $cmd =~ /INITIAL/i

    Setting it to the subroutine entry is problematic because $cmd isn't set yet (or at least isn't set to a member of @commands. In addition to that, simply using /INITIAL/i as your condition would be running that regexp on $_, not $cmd.

    See perldebug for more info.

      This subroutine is called from other subroutine which is intern called from my perl script. So setting break point b 123 $cmd =~ /INITIAL/i throws error Line 118 not breakable. because my script does not have those many lines.

      From the google docs I found the below statement b subname [condition] Can we use this in anyway?

        Yes, that's why you need to figure out the actual line number. "123" was just an example. You can determine the line number with l Engine::_Execute ... lines that are breakable will show with a colon (:) after the line number in the debugger. Hence, if you see this:

        100 sub Engine::Execute 101 { 102 103: foreach (@commands) 104 { 105: $cmd = $_; 106: if($cmd =~ /INITIAL/i) 107 {

        ... then you would type b 106 $cmd =~ /INITIAL/i

        Again, these numbers are just examples. You need to determine the actual line number for yourself with l Engine::_Execute!

        > From the google docs I found the below statement b subname [condition] Can we use this in anyway?

        No and I already showed you two ways to solve this. Why didn't you try?

        Cheers Rolf

Re: Setting break point for particular condition in the subroutine
by choroba (Cardinal) on Mar 07, 2013 at 15:13 UTC
    Please, do not use <pre> tags for text. Read <pre> Versus <code> Tags for more information as already advised in a /msg.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Setting break point for particular condition in the subroutine
by clueless newbie (Curate) on Mar 07, 2013 at 12:22 UTC

    Sometimes it's easier to use "$DB::single=1;" directly in your code. For example:

    my @caller=caller(0); $DB::single=1 if ($caller[0] eq 'package::of::interest' and $caller[3] + eq 'thatsub');