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:

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 $!;
outputs for me:
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.


In reply to Re^3: Opening a File Twice by Corion
in thread Opening a File Twice by Jamin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.