Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

To Debug a Hanging Perl Script

by Anonymous Monk
on Mar 02, 2006 at 14:36 UTC ( #533894=perlquestion: print w/replies, xml ) Need Help??

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

Dear Guru,
I have a perl script that "usually" works well. It is executed in this way.The code prints to STDOUT:
$ perl input_file.txt > output.txt
Now one time when it take certain file (smaller than usual one), my code above just "hang in there". It doesn't die off and not giving segfault message or any error message as per normal error message we encounter.

My question is what's is the best way to debug find out the cause of this hanging problem?

Replies are listed 'Best First'.
Re: To Debug a Hanging Perl Script
by samizdat (Vicar) on Mar 02, 2006 at 14:54 UTC
    1. make sure you've added "or die" (or "&& die" for system() calls) clauses to every instance where system interface functions are called (open, chdir, etc.)
    2. use strict; use warnings;
    3. change to 'perl input_file.txt 2&>output.txt' and run 'tail -f output.txt' from another console window to see where it stops
    4. post your code here

    Don Wilde
    "There's more than one level to any answer."
Re: To Debug a Hanging Perl Script
by tirwhan (Abbot) on Mar 02, 2006 at 15:25 UTC

    If you're on Linux, use the strace utilitiy to tell you what your script is doing while it hangs. Either run

    strace -f perl input_file.txt > output.txt

    or start your script normally, wait till it hangs, find out the PID of the script process and execute

    strace -f  -p <PID>

    strace prints to STDERR, so you'll see the strace output normally in your terminal while STDOUT still gets written to the output file. It will tell you what the process is doing on a system call level. To find out where it hangs at the perl level you could execute it in the debugger and step through until it hangs.

    perl -d input_file.txt > output.txt

    All dogma is stupid.

      Similar utilities for other platforms are ktrace and kdump for BSDen (including OS X), and truss for Solaris.

      They're all handy if you're really stumped, but go for tossing in a few debugging prints to STDERR and running with -d first.

Re: To Debug a Hanging Perl Script
by ikegami (Pope) on Mar 02, 2006 at 15:02 UTC
    Auto-flush STDOUT ($| = 1;) so you can see everything that's printed as it is printed. Then you can add print statements (or warns) to track the progress of your script.

      Along the same lines you could change your print statements to warn, which will give you information as to where you are in the script.

      Update: Oops... I see you already mentioned them...

      Hi ikegami,
      Where and how should I put this line in my script?
      Auto-flush STDOUT ($| = 1;)
        Anywhere. Presumably near the top so it gets executed soon. And it's:
        $| = 1; # Auto-flush STDOUT

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://533894]
Approved by wfsp
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2021-05-11 14:50 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (117 votes). Check out past polls.