in reply to File handling basics

Aside from your immediate problem, there are a few things that will help write robust file handling code now and in the future.

First, always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

Use the three parameter version of open and lexical file handles:

my $path = "$dir/$entry"; open my $inFile, '<', $path, or die "Can't open '$path': $!\n";

Checking that an entry is a file before trying to open it will save failures, especially for . and .. directory entries which are present in each directory on common file systems.

Your code is better written:

use strict; use warnings; use 5.010; my $startDir = "/temp/"; opendir my ($dir), $startDir or die "cannot open '$startDir'\n"; while (defined (my $entry = readdir $dir)) { my $path = "$dir/$entry"; next if !-f $path; open my $inFile, '<', $path, or die "Can't open '$path': $!\n"; print <$inFile>; }

Note that lexical file and directory handles close when they go out of scope so explicit closes are not needed in this code. Also note that including file or directory information and the failure mode in a die makes the error reporting much more useful.

Premature optimization is the root of all job security