Perl Language Opening A FileHandle for Reading


Example

Opening Generic ASCII Text Files

5.6.0
open my $filehandle, '<', $name_of_file or die "Can't open $name_of_file, $!";

This is the basic idiom for "default" File IO and makes $filehandle a readable input stream of bytes, filtered by a default system-specific decoder, which can be locally set with the open pragma

Perl itself does not handle errors in file opening, so you have to handle those yourself by checking the exit condition of open. $! is populated with the error message that caused open to fail.

On Windows, the default decoder is a "CRLF" filter, which maps any "\r\n" sequences in the input to "\n"

Opening Binary Files

5.8.0
open my $filehandle, '<:raw', 'path/to/file' or die "Can't open $name_of_file, $!";

This specifies that Perl should not perform a CRLF translation on Windows.

Opening UTF8 Text Files

5.8.0
open my $filehandle, '<:raw:encoding(utf-8)', 'path/to/file' 
   or die "Can't open $name_of_file, $!";

This specifies that Perl should both avoid CRLF translation, and then decode the resulting bytes into strings of characters ( internally implemented as arrays of integers which can exceed 255 ), instead of strings of bytes