Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

capturing syswrite failures

by Brad.Walker (Sexton)
on Mar 01, 2007 at 17:00 UTC ( #602721=perlquestion: print w/replies, xml ) Need Help??
Brad.Walker has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl script that works most of the time. When it doesn't, it always fails in the syswrite() call with an error message on the console of "Write failure". I believe this is a bug in my lower level NFS failing for some reason. But, I would like to capture the failure in my Perl script.

My question is how do I capture a failure from my syswrite() call. The documentation is a little sparse about how to handle failures in syswrite().

Thanks.

-brad w.

Replies are listed 'Best First'.
Re: capturing syswrite failures
by kyle (Abbot) on Mar 01, 2007 at 17:22 UTC

    Maybe I don't understand what you're trying to do here. The documentation for syswrite seems clear enough. On failure, it returns undef and sets the $! variable (for use in error messages). Is there something you'd like to do in that case that you're having trouble with?

    Maybe it would help if you'd show us the code you have so far.

Re: capturing syswrite failures
by jettero (Monsignor) on Mar 01, 2007 at 17:39 UTC

    Additionally, why are you using syswrite? Perl does a pretty darn good job of hiding some important details from you when you use open/print/close and you get pretty much the same functionality — not the least of which is better error handling. I'm wondering if you'd be better off using print along with local $| = 1 or something like that?

    -Paul

      print makes sense if you turn off buffering -- which is pretty close to the same as using syswrite. If buffering is involved, print makes it very hard to recover and continue after an error since the error can be deferred. You also need to remember to do an explicit close and check the error on that.

      For example, if NFS returns ESTALE (e.g. after a server reboot), you may have to close and reopen the file before the NFS server will allow you to write. That close is going to try, and fail, to send the buffered data. Figuring out what data to resend in this case is easy with syswrite (e.g. close, open, syswrite the buffer again).

Re: capturing syswrite failures (die)
by tye (Sage) on Mar 01, 2007 at 18:13 UTC

    It sounds to me like you are already capturing syswrite failures and then just doing die "Write failure\n", but that is a bit of a guess on my part. syswrite doesn't write error messages when it fails.

    So likely all you need to do is replace the code that is already detecting these failures with code that does what you want done instead. You didn't show us any code so it is hard for me to be more specific than that.

    - tye        

      FWIW, it's possible the NFS client itself is outputting the message (e.g. similar to the way unreachable NFS servers are reported by most clients I've seen) though that seems like a pretty worthless message for it to use.
Re: capturing syswrite failures
by zentara (Archbishop) on Mar 01, 2007 at 18:21 UTC
    How about putting the syswrite in an unless block?
    unless (syswrite($pc, $data)) { print STDERR $rc->peerhost,":",$rc->peerport, "<->",$pc->peerhost,":",$pc->peerport, " closed by ", $pc->peerhost,":",$pc->peerport,"\n"; $sel->remove($pc); $sel->remove($rc); close $pc; close $rc; next; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      In case $data is empty,

      unless ( syswrite( $pc, $data ) == length $data ) { # ... }

      Or maybe just

      unless ( defined syswrite( $pc, $data ) ) { # ... }

      (Should it be considered an error if syswrite writes some number of bytes different from the length of the string you give it?)

        Should it be considered an error if syswrite writes some number of bytes different from the length of the string you give it?

        No.

        my $to_write = length($data); my $offset = 0; while ($to_write > 0) { my $written = syswrite($fh, $data, $to_write, $offset); die("Unable to write to ...: $!\n") if not defined $written; $to_write -= $written; $offset += $written; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://602721]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-07-19 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (405 votes). Check out past polls.

    Notices?