Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

O_RDWR and O_APPEND???

by dbs (Sexton)
on Aug 30, 2011 at 20:07 UTC ( #923297=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, Why cannot I read/write/append to the same filehandle? I open my FH like
my $CF = new FileHandle "$cntrF", O_RDWR | O_APPEND || die '"cntrF' di +d not open $!\n";

My logic is if a file exists and has data in it, set flag to 1, set counter to 0, open filehandle as above, while (<$FH>), while (m/\b$flag\b/g), counter++, end while, if counter > 3.... I cannot do both, read to get the # of times flag appears in my file and append the flag + \n to my file.

$| = 1; if ( -e $log ) { my $flag = 1; my $cntr = 0; no warnings; my $CF = new FileHandle "$cntrF", O_RDWR | O_APPEND || die "'cntrF +' did not open $!\n"; print $CF "$flag\n"; while (<$CF>) { while (m/\b$flag\b/g) { $cntr++; } } print "$cntr\n"; if ( $cntr <= 3 ) { _mailme($alertaddr, $Esubject, $log); $CF->close; exit 1; } elsif ( $cntr > 3 && $percnt >= 98 ) { _mailme($alertaddr, $Esubject, $log); $CF->close; exit 1; } else { $CF->close; print "closed\n"; exit 0;
thnkU!

Replies are listed 'Best First'.
Re: O_RDWR and O_APPEND???
by jwkrahn (Monsignor) on Aug 30, 2011 at 20:21 UTC
    my $CF = new FileHandle "$cntrF", O_RDWR | O_APPEND || die '"cntrF' di +d not open $!\n";

    Because of the relatively high precedence of the || operator that expression is interpreted as:

    my $CF = new FileHandle $cntrF, ( O_RDWR | O_APPEND || die "'cntrF' di +d not open $!\n" );

    You need to either use parentheses:

    ( my $CF = new FileHandle $cntrF, O_RDWR | O_APPEND ) || die "'cntrF' +did not open $!\n";

    Or use the lower precedence "or" operator:

    my $CF = new FileHandle $cntrF, O_RDWR | O_APPEND or die "'cntrF' did +not open $!\n";
Re: O_RDWR and O_APPEND???
by Eliya (Vicar) on Aug 30, 2011 at 22:53 UTC

    In addition to what the others have said, another problem with your code is that a file handle maintains only one file pointer, which is used for both read and write operations.  In other words, if you append to a file, the file pointer is positioned to the end of the file, so if you want to read from the same file thereafter, you have to reposition the file pointer to the beginning (using seek):

    #!/usr/bin/perl -w use strict; use FileHandle; use Fcntl qw(O_RDWR O_APPEND SEEK_SET); $| = 1; my $flag = 1; my $cntr = 0; my $cntrF = shift @ARGV; my $CF = new FileHandle $cntrF, O_RDWR | O_APPEND or die "'$cntrF' did + not open: $!\n"; print $CF "$flag\n"; seek $CF, 0, SEEK_SET; while (<$CF>) { while (m/\b$flag\b/g) { $cntr++; } } print "cntr = $cntr\n";
    $ echo 1 1 1 > foo.dat $ ./923297.pl foo.dat cntr = 4
Re: O_RDWR and O_APPEND???
by JavaFan (Canon) on Aug 30, 2011 at 20:56 UTC
    Did you import O_RDWR and O_APPEND? Your example code doesn't show this - and your "no warnings" is a red flag.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://923297]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-04-23 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?