martin has asked for the wisdom of the Perl Monks concerning the following question:
while (<$fh>) { # do something with one line of input }
However, if I can't trust my input --and who can?-- this is unsafe, as the program might come upon a line that is too long to fit into memory. Perl will happily keep allocating space to store it until the process runs out of virtual memory, which may happen only after the system has suffered serious performance loss trying to satisfy the demand.
In order to survive malformed input while keeping the normal procedure line-oriented, I need perhaps a getline variant with some sort of length limitation. I'd like to be able to give up gracefully when excessively long lines are detected.
Do you, fellow monks, know of a good way to handle this?
I know about sysread and could with some effort write a wrapper around it to reassemble fixed-length chunks into lines, but am quite open to learn about alternatives first.
Update: This topic has indeed been discussed here before: Read a line with max length ?. Thanks to runrig for a pointer to File::GetLineMaxLength. I'll follow up with a report once I have looked into it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Safely reading line by line
by moritz (Cardinal) on Jun 27, 2007 at 10:51 UTC | |
by martin (Friar) on Jun 27, 2007 at 16:49 UTC | |
Re: Safely reading line by line
by RMGir (Prior) on Jun 27, 2007 at 12:28 UTC | |
by moritz (Cardinal) on Jun 27, 2007 at 12:38 UTC | |
Re: Safely reading line by line
by cdarke (Prior) on Jun 27, 2007 at 12:59 UTC | |
Re: Safely reading line by line
by runrig (Abbot) on Jun 28, 2007 at 23:19 UTC | |
by martin (Friar) on Jun 30, 2007 at 18:39 UTC | |
Re: Safely reading line by line
by martin (Friar) on Jun 28, 2007 at 22:22 UTC |