in reply to reading files from a directory

The problem is not in the open, but in the way you loop through the file.

You must not have blanks between the filehandle and the angle-operator (because then the angle-operator sees the blanks, finds that this is not a filehandle and tries to glob it.

You first attempt shoud be ok as long as you do it like this:

while ( <IN> ){ # no blanks araound IN ... while ( <$fh> ){ # dito
And while we are at it the second form (using a lexical variable rather than a package-filehandle) is considered to be better style and you should also use the 3-arg form of open:
open (IN, "<", $dir/$file")

Replies are listed 'Best First'.
Re^2: reading files from a directory
by kevyt (Scribe) on Oct 28, 2010 at 05:50 UTC
    Thank you. Are you saying that IN is better than $fh?

      The recommendation these days is to use the three argument form of open, lexical filehandles rather than package filehandles (i.e. $fh rather then IN) and to check for the success of the operation, showing the O/S error ($!) in the error message on failure.

      ... my $inputFile = q{/path/to/myFile; open my $inputFH, q{<}, $inputFile or die qq{open: < $inputFile: $!\n}; ...

      I hope this is helpful.

      Cheers,

      JohnGG