http://www.perlmonks.org?node_id=682752

walto has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I want read/write access to a file. I run this script on Vista with ActiveState Perl-5.10.0.
use warnings; use strict; open my $FH,">>","test.txt" or die ">>: $!"; my @file = <$FH>; if (defined $file[0]){ print "Open with >>: ",shift @file,"\n"; } else { print "Open with >>: undef\n"; } close $FH; open my $IN,"+<","test.txt" or die "#<: $!n"; my @file_new = <$IN>; if (defined $file_new[0]){ print "Open with +<: ",shift @file_new,"\n"; } else { print "Open with +<: undef\n"; } close $IN;

I get this output:
Open with >>: undef Open with +<: Test

Until this "problem" occured I opened the files I had to access (read/write) with ">>" and it worked as I expected.
Is this something related with Perl 5.10?

Replies are listed 'Best First'.
Re: Problems with open $FH
by jwkrahn (Abbot) on Apr 25, 2008 at 05:11 UTC
    I opened the files I had to access (read/write) with ">>" and it worked as I expected.

     ">>" mode is not read/write.   From perlopentut:

    To open a file for appending, creating one if necessary: open(FH, ">> $path"); sysopen(FH, $path, O_WRONLY | O_APPEND | O_CREAT);

    Notice the O_WRONLY (write only) attribute.