Files and directories

Read files easily with open and the <> operator

Opening and reading files with Perl is simple.Here's how to open a file,read it line-by-line,check it for text matching a regular expression,and print the lines that match.

  1. open( my $fh, '<', $filename ) or die "Can't open $filename: $!";
  2. while ( my $line = <$fh> ) {
  3. if ( $line =~ /wanted text/ ) {
  4. print $line;
  5. }
  6. }
  7. close $fh;

Always check the return code from open() for truthiness. If there's a failure, the result is in $!.

Remove trailing linefeeds with chomp

Lines read from a file have their trailing linefeed still attached. If you have a text file where the first line is

  1. Aaron

"Aaron", is actually "Aaron\n", six characters. This code will fail:

  1. my $line = <$fh>;
  2. if ( $line eq 'Aaron' ) {
  3. # won't reach here, because it's really "Aaron\n";
  4. }

To remove the "\n", and any other trailing whitespace, call chomp.

  1. my $line = <$fh>;
  2. chomp $line;

Now $line is five characters long.

Change your line delimiter with $/

It's possible to change your input record separator, $/. It's only set to "\n" by default.

Set $/ to read a paragraph at a time. Set $/ to undef to read the entire file at once. See perlvar for details.

Slurp an entire file at once

Typically you'll see novices read a file using one of thse two methods:

  1. open (FILE,$filename) || die "Cannot open '$filename': $!";
  2. undef $/;
  3. my $file_as_string = <FILE>;

or

  1. open (FILE,$filename) || die "Cannot open '$filename': $!";
  2. my $file_as_string = join '', <FILE>;

Of those two, choose the former. The second one reads all the lines into an array, and then glomps together a big string. The first one just reads into a string, without creating the intervening list of lines.

The best way yet is like so:

  1. my $file_as_string = do {
  2. open( my $fh, $filename ) or die "Can't open $filename: $!";
  3. local $/ = undef;
  4. <$fh>;
  5. };

The do() block returns the last value evaluated in the block. This method localizes the $/ so that it gets set back outside the scope of the block. Without localizing $/, it retains the value being set to it and another piece of code might not be expecting it to have been set to undef.

Here's another way:

  1. use File::Slurp qw( read_file );
  2. my $file_as_string = read_file( $filename );

File::Slurp is a handy module for reading and writing a file at a time, and it does magic fast processing on the back end.

Get lists of files with glob()

Use standard shell globbing patterns to get a list of files.

  1. my @files = glob( "*" );

Pass them through grep to do quick filtering. For example, to get files and not directories:

  1. my @files = grep { -f } glob( "*" );

Use unlink to remove a file

The Perl built-in delete deletes elements from a hash, not files from the filesystem.

  1. my %stats;
  2.  
  3. $stats{filename} = 'foo.txt';
  4.  
  5. unlink $stats{filename}; # RIGHT: Removes "foo.txt" from the filesystem
  6.  
  7. delete $stats{filename}; # WRONG: Removes the "filename" element from %stats

The term "unlink" comes from the Unix idea of removing a link to the file from the directory nodes.

Use Unix-style directories under Windows

Even though Unix uses paths like /usr/local/bin and Windows uses C:\foo\bar\bat, you can still use forward slashes in your filenames.

  1. my $filename = 'C:/foo/bar/bat';
  2. open( my $fh, '<', $filename ) or die "Can't open $filename: $!";

In this case, Perl magically changes the C:/foo/bar/bat to C:\foo\bar\bat before opening the file. This also prevents the problem where an unquoted backslash screws up a filename, as in:

  1. my $filename = "C:\tmp";

In this case, $filename contains five characters: 'C', ':', a tab character, 'm' and 'p'. Instead, it should have been written as one of:

  1. my $filename = 'C:\tmp';
  2. my $filename = "C:\\tmp";

Or, you can let Perl take care of it for you with:

  1. my $filename = 'C:/tmp';

Want to contribute?

Submit a PR to github.com/petdance/perl101