in reply to File handling basics

As already suggested by vinoth.ree, but somewhat buried in a longer post, I would recommend using the glob which makes things easier that the opendir/readdir combination for 2 reasons: glob does not return the . and .. special entries (wgich you don't want to process) and returns files with their path. So you could simply write:
my $dir = "/temp/"; for my $file (glob "$dir/*") { next unless -f $file; # process only regular files, not dirs open (my $F, $file) or die "cannot open file $!"; while (<$F>){ print $_; } close $F; } # closedir(DIR); -- not needed with glob
I made a few additional changes reflecting the currently widely accepted good practices.

Replies are listed 'Best First'.
Re^2: File handling basics
by Monk::Thomas (Friar) on Jul 17, 2015 at 15:15 UTC

    I made a few additional changes reflecting the currently widely accepted good practices.

    you missed one ;)
    - open (my $F, $file) or die "cannot open file $!"; + open (my $F), '<', $file or die "cannot open file $!";

    @OP: If for some reason you have a file '>123.txt', then open (my $F, $file) would open the file for writing and truncate it.

    Two purely cosmetical changes would be to replace $F with $fh and also add the filename to the error message, like "cannot open file '$file': $!".

    @OP: I would consider replacing 'die' with 'warn and next', so that you can process the remaining files even if one is not readable to the current user.

    Variant a)

    open (my $F), '<', $file or warn "cannot open file '$file': $!"; defined $F or next;

    Variant b)

    open (my $F), '<', $file or warn "cannot open file '$file': $!" and next;

    I prefer Variant a) because chaining conditionals may lead to surprises. Mostly because 'or' short circuits the evaluation and due to operator precedence.

      you missed one ;)
      Yes, indeed, Monk::Thomas, you're right, I missed one good-practice improvement, and a quite important one. The three-argument syntax for open has been around for quite a number of years and is definitely better than the old two-arguments syntax. Thank you for pointing out this.
Re^2: File handling basics
by marinersk (Priest) on Jul 17, 2015 at 20:59 UTC

    I am also happy to report that a spot check of globdemonstrates no immediate ability to confuse it or get the wrong answers back. Way back in the old days it failed frequently for me in a Windows environment -- I no longer remember how, but suspect the use of drive letters and current directory on each were amongst the issues.

    However, I threw, off the cuff, a bunch of potential curve balls at globrecently and I was very happy to discover it worked perfectly on every test I could think of in 90 seconds.

    Hardly deterministic, but I used to be able to break it without having to work at it, so something has gotten better -- or perhaps I have simply become less warped.