title: What's the best way to open and read a file in Perl?
body: <p>Please note - I am not looking for the "right" way to open/read a file, or the way I should open/read a file every single time. I am just interested to find out what way most people use, and maybe learn a few new methods at the same time :)*</p>
  
  <p>A very common block of code in my Perl programs is opening a file and reading or writing to it. I have seen so many ways of doing this, and my style on performing this task has changed over the years a few times. I'm just wondering what the <em>best</em> (if there is a best way) method is to do this?</p>
  
  <p>I used to open a file like this:</p>
  
  <pre><code>my $input_file = "/path/to/my/file";
  open INPUT_FILE, "&lt;$input_file"  || die "Can't open $input_file: $!\n";
  </code></pre>
  
  <p>But I think that has problems with error trapping.</p>
  
  <p>Adding a parenthesis seems to fix the error trapping:</p>
  
  <pre><code>open (INPUT_FILE, "&lt;$input_file")  || die "Can't open $input_file: $!\n";
  </code></pre>
  
  <p>I know you can also assign a filehandle to a variable, so instead of using "INPUT_FILE" like I did above, I could have used $input_filehandle - is that way better?</p>
  
  <p>For reading a file, if it is small, is there anything wrong with globbing, like this?</p>
  
  <pre><code>my @array = &lt;INPUT_FILE&gt;;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>my $file_contents = join( "\n", &lt;INPUT_FILE&gt; );
  </code></pre>
  
  <p>or should you always loop through, like this:</p>
  
  <pre><code>my @array;
  while (&lt;INPUT_FILE&gt;) {
    push(@array, $_);
  }
  </code></pre>
  
  <p>I know there are so many ways to accomplish things in perl, I'm just wondering if there are preferred/standard methods of opening and reading in a file?</p>