Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

write/syswrite return value differences

by FreakyGreenLeaky (Sexton)
on Nov 24, 2011 at 09:55 UTC ( #939834=perlquestion: print w/ replies, xml ) Need Help??
FreakyGreenLeaky has asked for the wisdom of the Perl Monks concerning the following question:


Hopefully someone can shed some candle light on the following.

$sock->syswrite() returns number of bytes sent. $sock->write() always returns 1, not number of bytes sent. This seems at odds with the C write() function which it ostensibly is mirroring. The following bit of code demonstrates that:
# Server: use IO::Socket; my $sock = new IO::Socket::INET( LocalPort => '10000', Proto => 'tcp', Listen => 1, Reuse => 1, ) or die; while (1) { my $new_sock = $sock->accept(); while (<$new_sock>) { print $_; } } close $sock; # client: use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => '', PeerPort => '10000', Proto => 'tcp', ) or die; #my $ret = $sock->write("$ARGV[0]\n", length("$ARGV[0]\n")); my $ret = $sock->syswrite("$ARGV[0]\n", length("$ARGV[0]\n")); print "wrote $ret bytes\n"; close $sock;

I've got some code which depends on the return value of syswrite(), but we need to use write() because of chunking, which does not return the number of bytes.

Any idea why on earth write() isn't returning the number of bytes written?


Comment on write/syswrite return value differences
Download Code
Replies are listed 'Best First'.
Re: write/syswrite return value differences
by dave_the_m (Prior) on Nov 24, 2011 at 10:15 UTC
    why on earth write() isn't returning the number of bytes written?
    Because the write() method (which is just a thin wrapper round print()), does buffering, so it will never return having output less bytes than you requested (unless there's an error, in which case it returns false). So there's not a lot of point in returning the number of bytes written.


Re: write/syswrite return value differences
by ikegami (Pope) on Nov 24, 2011 at 17:50 UTC
    Perl's write has nothing to do with the write system call. (What you call the C write.) It a record formatting function.

      I suppose the OP is talking about the IO::Handle method write, and was probably misled by the wording 'is like "write" found in C':

      $io->write ( BUF, LEN [, OFFSET ] )

      This "write" is like "write" found in C, that is it is the opposite of read. The wrapper for the perl "write" function is called "format_write".
        Ah yes. Then it should probably return the bytes written. Unfortunately, it doesn't have that information available, and it wouldn't be feasible to do that.

        That's right, I misread the docs thinking it's mirroring C's write() function...

        oh well, thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://939834]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2016-05-30 01:21 GMT
Find Nodes?
    Voting Booth?