|laziness, impatience, and hubris|
Safely reading line by lineby martin (Friar)
|on Jun 27, 2007 at 10:38 UTC||Need Help??|
martin has asked for the wisdom of the Perl Monks concerning the following question:
It is very common practice to use variations of a pattern like this in Perl programs:
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.