MC logo

File Reader III

  Code Examples

<<File Reader II File Copier>>
use strict;

# This script also prints the contents of all the listed files, but
# it first scans through the list to check that each file exists and
# is readable.  It will stop if there are any errors.

my $bad = 0;
foreach my $fn (@ARGV) {
    if(! -r $fn) {
        # File cannot be read.  See if it exists or not for a better 
        # error message.
        if(-e $fn) {
            print STDERR "You do not have permission to read $fn.\n";
        } else {
            print STDERR "File $fn does not exist.\n";

        # One way or the other, it's bad.
        $bad = 1;

# If there was a problem, bail out.
if($bad) { exit 2; }

# Copy all the files.
while(my $fn = shift @ARGV) {

    # Open the file.
    if(!open(INFILE, $fn)) {
        # We know the file is readable, but sometimes something else goes 
        # wrong.  It's safer to check.
        print STDERR "Cannot open $fn: $!\n";

    # Copy it.
    while(my $l = <INFILE>) {
        print $l;

    close INFILE;
# See the man page for the very long list of file tests, some of which are
# Unix-specific.  To find it from perl documentation, choose the
# standard documentation pack, supporting manpages, perlfunc, then
# choose -X (first in the alphabetical list).
<<File Reader II File Copier>>