in reply to Re^3: No data received on client socket
in thread No data received on client socket

When the value of a variable is undefined (as opposed to the variable being undefined), it means there's no way to predict what value the variable has.

In context, it means that $! could be true even no error occured. $! is only meaningful if sysread returned undef.

Replies are listed 'Best First'.
Re^5: No data received on client socket
by caelifer (Scribe) on Sep 20, 2006 at 20:20 UTC

    I see your point now. For some reason I thought that undef value would always evalutes to false in conditional statements in perl. However, when I thought more about it and given the fact that $! is a 'synonym' for C's errno, I see no guaranty that it would be reset to some specific value if no system error occurs and may indeed contain phantom residule value that could evaluate to true. Basically, without looking through perl's code there is no way to say which statement is true.

    Here is a modified version of the read_msg sub...

    sub read_msg { my ($sock, $bcount, $buf, $msg) = (shift, 0, '', ''); my $bytes_read; # Use sysread for stream-oriented sockets READ_MSG: while ($bytes_read = sysread($sock, $buf, 1024)) { # Handle partial read $bcount += $bytes_read; $msg .= $buf; } # sysread returns undef on system error, check $! if (not defined $bytes_read) { # Just in case, save $! value my $error = $!; # To be safe here... redo READ_MSG if $error =~ /Resource temporarily unavailable/; # All other errors trigger unconditional return with error return ($msg, $bcount, $error); } # Return success return ($msg, $bcount, ''); }
      undef *is* always false. Again, I'm not talking about undef. That $!'s value is undefined (as opposed to $! being undefined) means $! could be 0, it could be -123, it could be the string "the answer to life, the universe, everything".
        :) If you read my post more carefully that is exactly what I was talking about. :)

        Actually, just peeked at pp_sys.c of perl-5.8.8 and verified that $! is set to 0 unconditionally there with call to SETERRNO(0,0) before reading file handle. Thus you can safely rely on $!'s value even if no system error occures.

        BR