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

Having trouble opening a file to read and append things to. Do I need to open the file to read, close, and then reopen for appending? I thought I could use >>+ and save some time.. The matching works fine, and output is boo, so it's getting up to that point.

if ($hostUrl =~ m/(org|net|com|co|edu|gov)/ ) { open NAGHOST, "+>>$host_file" or die "can't open the $host_fil +e $!"; print "\nboo\n"; while (my $line = <NAGHOST>) { #check for duplicate file if ($line =~ m/($hostName|$hostUrl)/) { print "Looks like the data you entered is already on f +ile\n"; } #no duplicate, move forward else { print "\nbah\n"; } }

output:

boo

Replies are listed 'Best First'.
Re: Openind file for read+append
by Eliya (Vicar) on Feb 08, 2012 at 18:37 UTC

    Opening a file for appending positions the file pointer at the end of the file. If you also want the read its contents, you have to seek to the beginning (or wherever you want to) before reading. (The OS maintains only one file pointer per file handle, not one for reading, and one for writing.)

      ... and seek to the end before writing again.

      --MidLifeXis

      Thanks @eliya , I didn't realize that!
Re: Openind file for read+append
by roboticus (Chancellor) on Feb 08, 2012 at 18:43 UTC

    jaldama:

    You're reading from the end of the file, so you won't find anything. Example:

    $ cat t.pl #!/usr/bin/perl use strict; use warnings; use autodie; open my $FH, '>', 'TEST.x'; print $FH <<EOTXT; now is the time for all good men to come to the aid of their party. EOTXT close $FH; open $FH, '+>>', 'TEST.x'; while (<$FH>) { chomp; print "$.: $_\n"; } print $FH "foobar\n"; close $FH; $ perl t.pl $ cat TEST.x now is the time for all good men to come to the aid of their party. foobar

    As you can see, it didn't find *anything* to print, but added 'foobar' to the end of the file. You can either use '+>' to put the file in I/O mode or use seek to position the file handle to the beginning of the file before reading.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Openind file for read+append
by Riales (Hermit) on Feb 08, 2012 at 18:41 UTC

    Have you looked into using the 3-argument form of open? It's safer just in case $host_file starts with a '<' or a '>'; this could change what your code does.

    Beyond that, +> opens the file for both read/write but +< is probably better because +> would clobber the file first. I don't see where you are attempting to append anything to the file though...

Re: Openind file for read+append
by locked_user sundialsvc4 (Abbot) on Feb 08, 2012 at 22:09 UTC

    I would simply open the file for writing, seek() to wherever you need to go, then write.

      I'm having trouble matching strings in the file I'm opening. I just have it opening the file for reading now, because I"m trying to figure out what is going wrong. Is there something obvious that I am missing? It never matches the string on that file even though it IS there.

      open FILE,'/Users/j/Desktop/test.log'; while (<FILE>) { chomp; #check for duplicate file if ($_ =~ m/appending/) { print "Looks like the data you entered is already on file\n"; close (FILE); } else { print "hey hey success\n"; close (FILE); &append; } }