How do I read the contents of a small text file into a scalar in Perl?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a small text file that I'd like to read into a scalar variable exactly as it is in the file (preserving line separators and other whitespace). The equivalent in Python would be something like buffer = "" try: file = open("fileName", 'rU') try: buffer += file.read() finally: file.close() except IOError: buffer += "The file could not be opened." This is for simply redisplaying the contents of the file on a web page, which is why my error message is going into my file buffer. 回答1: From the Perl Cookbook : my $filename = 'file.txt'; open(