Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

To end infinite while loop

by codewalker (Acolyte)
on Nov 20, 2014 at 08:03 UTC ( [id://1107887]=perlquestion: print w/replies, xml ) Need Help??

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

how to find which "while" loop in our code is running infinitely?

Replies are listed 'Best First'.
Re: To end infinite while loop
by davido (Cardinal) on Nov 20, 2014 at 08:24 UTC

    One way would be to see which test in the test suite hangs. But it sounds like that hasn't been implemented. ;)

    Another way would be to pepper the code with print statements to figure out at what stage in the program's execution you most recently got a heartbeat before things fell silent. Or put a print inside of each while loop.

    Another way would be to run the Perl debugger and watch closely. Eventually you'll find the loop.

    And yet another way would be to implement a %SIG handler to catch INT, which you will trigger by hitting control-C when you sense the program has hung. Set a sig handler that provides some useful information:

    use Carp; $SIG{INT} = sub { croak shift }; while(1) { sleep 1; }

    When I run this, and then hit control-C, I am told the line number within the sig handler's subroutine where termination occurred, but I'm also told the line number where the SIG was intercepted. If you hit control-C when you're stuck in the loop, that second line number reported will be a line inside the loop.

    Be sure to remove the sig handler once you get it figured out.


    Dave

      perl -Msigtrap=stack-trace,INT groggy.pl

        Nice!
        Thanks! :)


        Dave

Re: To end infinite while loop
by GrandFather (Saint) on Nov 20, 2014 at 08:15 UTC

    Comment out half your code. If the problem goes away it was in the chunk you commented out. If the problem remains it's in the uncommented code. Iteratively apply the technique to narrow in on the problem code.

    Perl is the programming world's equivalent of English
Re: To end infinite while loop
by hippo (Bishop) on Nov 20, 2014 at 10:09 UTC
Re: To end infinite while loop
by Anonymous Monk on Nov 20, 2014 at 08:16 UTC
    Read the code, interpret the code, ... add print statements.

    if all else fails, post code here :)

Re: To end infinite while loop
by FloydATC (Deacon) on Nov 21, 2014 at 10:00 UTC

    If the problem can't be solved by adding more print statements then the universe did not mean for that problem to be solved.

    -- FloydATC

    Time flies when you don't know what you're doing

Re: To end infinite while loop
by pajout (Curate) on Nov 21, 2014 at 10:52 UTC
    Write loop counters into suspected while loops, and compare it with value, which would not be achieved.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1107887]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found