slurp

In Perl, how can I read an entire file into a string?

孤者浪人 提交于 2019-11-26 03:50:48
问题 I\'m trying to open an .html file as one big long string. This is what I\'ve got: open(FILE, \'index.html\') or die \"Can\'t read file \'filename\' [$!]\\n\"; $document = <FILE>; close (FILE); print $document; which results in: <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN However, I want the result to look like: <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <meta http-equiv=\"Content-Type\" content=\

Why is “slurping” a file not a good practice?

僤鯓⒐⒋嵵緔 提交于 2019-11-25 22:46:34
问题 Why is \"slurping\" a file not a good practice for normal text-file I/O, and when is it useful? For example, why shouldn\'t I use these? File.read(\'/path/to/text.txt\').lines.each do |line| # do something with a line end or File.readlines(\'/path/to/text.txt\').each do |line| # do something with a line end 回答1: Again and again we see questions asking about reading a text file to process it line-by-line, that use variations of read , or readlines , which pull the entire file into memory in