How do I read the contents of a small text file into a scalar in Perl? -


I have a small text file that I would like to read in a scalar variable as it is in the file (protected line separator And other whitespace).

Something will be equivalent to Python

  buffer = "" try: try the file = open ("filename", 'ru'): buffer + = file.read ( ) Finally: except file.close () IOError: buffer + = "The file could not be opened."  

This is just to display the contents of the file on a web page, so my error message is going into my file buffer.

to:

  my $ filename = 'file.txt' ; Open (File, '& lt;', $ filename) or 'File can not be opened:' $!! Undef $ / / my $ whole_file = & lt; FILE & gt;  

I will localize the changes though:

  my $ whole_file = ''; {Local $ /; $ Whole_file = & lt; FILE & gt; }  

Comments