In Perl, can I limit the length of a line as I read it in from a file (like fgets)

Posted by SB on Stack Overflow See other posts from Stack Overflow or by SB
Published on 2010-05-28T15:50:43Z Indexed on 2010/05/28 15:51 UTC
Read the original article Hit count: 181

Filed under:
|
|

I'm trying to write a piece of code that reads a file line by line and stores each line, up to a certain amount of input data. I want to guard against the end-user being evil and putting something like a gig of data on one line in addition to guarding against sucking in an abnormally large file. Doing $str = <FILE> will still read in a whole line, and that could be very long and blow up my memory.

fgets lets me do this by letting me specify a number of bytes to read during each call and essentially letting me split one long line into my max length. Is there a similar way to do this in perl? I saw something about sv_gets but am not sure how to use it (though I only did a cursory Google search).

Thanks.

© Stack Overflow or respective owner

Related posts about perl

Related posts about fgets