Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: open for append

by perl5ever (Pilgrim)
on May 16, 2011 at 17:26 UTC ( [id://905123]=note: print w/replies, xml ) Need Help??

in reply to open for append

You should use syswrite instead of print.

print goes through libc's stdio (or perl's replacement) whereas syswrite is a direct system call. The corruption you are seeing when LOG_FORMAT has length greater than 4K is likely due to buffering by stdio.

The code for append_like_hell using syswrite would be:

sub append_like_hell { my $id = shift; open my $log, '>>', FILE_NAME or die ... for (1..$NUM_LINES) { syswrite($log, sprintf(...)) or die "..."; } close($log); }

Two notes: 1) flushing has no effect here; 2) even though syswrite returns the number of bytes written, because $log is opened to a disk file it will always be either undef or the length of the passed string. In general, to fully write a string using syswrite (e.g. to a socket or a pipe), one has to loop until the entire string has been written (cf. the following from AnyEvent::Worker):

for (my $ofs = 0; $ofs < length $wbuf; ) { $ofs += (my $wr = syswrite $fh, substr $wbuf, $ofs or die "unable to + write results"); }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://905123]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-07-24 22:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.