Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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:

Hi,

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 => '10.0.0.1', 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?

Thanks

Comment on write/syswrite return value differences
Download Code
Re: write/syswrite return value differences
by dave_the_m (Parson) 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.

    Dave.

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!
Reaped: Re: write/syswrite return value differences
by NodeReaper (Curate) on Nov 25, 2011 at 12:57 UTC
Reaped: Re: write/syswrite return value differences
by NodeReaper (Curate) on Nov 28, 2011 at 12:36 UTC
Reaped: Re: write/syswrite return value differences
by NodeReaper (Curate) on Nov 29, 2011 at 13:00 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-08-31 00:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls