in reply to Re^2: Opening a File Twice
in thread Opening a File Twice
With Perl 5.8.5, I can't reproduce your error. I've changed your code around a bit to make it work with strict and removed some of the file tests because they are redundant but still:
outputs for me:use strict; my @files = qw(CaseComment.csv); my $dir1 = "."; my $dir2 = "."; ######################## # OPEN THE STEP 1 FILE # ######################## foreach (@files) { my $filename = "$dir1/$_"; #chdir($dir1); # replace by "$dir/$_" # Redundant - open() will die with # an informative message anyway ## Verify Files exist #die "File: '$filename' does not exist: $!" # unless -e $filename; ## Verify Files are read/writable #die "File: $filename isn't read or writable \n $!" # unless ((-r $filename ) && (-w $filename )); ### Open the file open (FILE, "<", $filename ) or die "Cannot open the file '$filena +me': $!\n"; #select((select(FILE), $/ = undef)[0]); # what is that for? my @array_contents = <FILE>; close (FILE) or die $!; ## DO SOME STUFF ########################## # CREATE THE STEP 2 FILE # ########################## #chdir($dir2); my $contents = <<TESTCOMMENTS; Line 1 line 2 linE 3 TESTCOMMENTS my $outputfile = "$dir2/STEP2_" . $_; warn "Writing '$contents'"; open(OUTFILE, ">" , $outputfile) or die "Couldn't create '$outputf +ile':$!"; print OUTFILE $contents; close OUTFILE or die $!; }; ########################## # ACCESS THE STEP 2 FILE # ########################## #chdir($dir2); my $commentsfile = "$dir2/STEP2_CaseComment.csv"; open (SFCOMMENTS, "<", $commentsfile) or die "Cannot open Salesforce C +omments file '$commentsfile' : $! \n"; my $count = 0; while (<SFCOMMENTS>) { print "Count is: $count \n"; $count ++; } close SFCOMMENTS or die $!;
C:\Projekte>perl -w tmp.pl Writing 'Line 1 line 2 linE 3 ' at tmp.pl line 44. Count is: 0 Count is: 1 Count is: 2
Maybe your assignment to $contents is somehow wrong? More debugging information is needed.
|
|---|