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

Re: How to trace a dying process

by Roger (Parson)
on Sep 06, 2003 at 13:59 UTC ( [id://289462]=note: print w/replies, xml ) Need Help??


in reply to How to trace a dying process

The while loop is a gotcha in Perl sometimes. Consider the loop
while (<>) { ... }
It will work most of the time, terminating when '<>' fails with undef (reaches the end of file). However when the while loop encounters a '0' by itself, it would fail as well (while loop fails on undef or '0').

The while loop can be fix with the following:
while (defined <>) { ... }
This while loop will only quit when the input stream really ends.

Now that comes to the second part of the problem, EOF's (^Z or ^D) encountered in the input stream unexpectedly. Perhaps you could switch from the while(){} loop to a do {} while () loop, and print out the input to a log file. Then you have a better chance of catching the error.

Replies are listed 'Best First'.
Re: Re: How to trace a dying process
by ctilmes (Vicar) on Sep 06, 2003 at 14:23 UTC
    Works fine for me:
    while (<>) { print "got $_"; }
    1 got 1 2 got 2 0 got 0 5 got 5
    Update: see perlop2:

    In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see if it is defined. The defined test avoids problems where line has a string value that would be treated as false by perl e.g. ``'' or ``0'' with no trailing newline.

Re: Re: How to trace a dying process
by Notromda (Pilgrim) on Sep 08, 2003 at 23:35 UTC
    This begs an interesting question: how to detect a ^Z or ^D in a line...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found