#! /usr/bin/perl use strict; use warnings; FILE: for my $file ( @ARGV ) { # check if $file contains a file name if ( !-f $file ) { warn "Sorry, '$file' doesn't look like a file!\n"; next FILE; } # if we can't open the file for reading, warn user if ( !open(my $infh, '<', $file) ) { warn "Sorry, couldn't open $file: $!\n"; } # we could open the file for reading else { # so, let's try to open a temporary file for writing if ( !open( my $outfh, '>', $file.'.new' ) ) { # and warn user if it failed warn "Sorry, couldn't open $file.new for writing: $!\n"; } else { # we now have two filehandles: # one to read from and one to write to. # read linewise from input filehandle while ( my $line = <$infh> ) { # print (to output filehandle) original line and append a newline print $outfh $line, "\n" or die "writing to $file.new failed: $!\n"; } # writing should be done now; close filehandle close $outfh or die "closing $file.new failed: $!\n"; } # reading should be done now; close filehandle close $infh or die "closing $file failed: $!\n"; } }