NAME

Net::Tools::Socket::Err - error codes and error state shared by the Net::Tools::Socket modules

SYNOPSIS

use Net::Tools::Socket;
use Net::Tools::Socket::Err;

my $data = socket_read_message( $socket, $timeout );
if( ! defined $data )
  {
  print "error: $SOCK_ERRSTR\n" if defined $SOCK_ERR;
  reconnect() if $SOCK_ERR == SOCK_E_EOF;
  print "name: " . sock_err_name() . "\n"; # 'SOCK_E_EOF', 'EBADF', etc.
  }

DESCRIPTION

Net::Tools::Socket, Net::Tools::Socket::Protocols and Net::Tools::Socket::Pair share one error state. Every function of those modules clears it on entry and sets it when it fails, so it describes the last call only and must be read right after it.

VARIABLES

$SOCK_ERR

The last error code: a negative SOCK_E_* constant, or the positive errno value when a system call failed, undef when there is no error. errno values are always positive and SOCK_E_* codes negative, so they never collide.

$SOCK_ERRSTR

The text of the last error, undef when there is no error. For errno values it is the system text ("$!"), for SOCK_E_* codes it is taken from %SOCK_ERRSTR and may be followed by ": " and details, for example the rejected protocol type.

%SOCK_ERRSTR

SOCK_E_* code => text map. It is locked, reading an unknown key or changing it dies.

ERROR CODES

SOCK_E_AGAIN     nothing to read or peer's queue is full, retry
SOCK_E_EOF       end of file, peer closed
SOCK_E_TOOBIG    message larger than allowed
SOCK_E_EMPTY     empty or undef message
SOCK_E_MSGLEN    invalid or unreadable message length
SOCK_E_SHORT     short read or write below requested count
SOCK_E_TIMEOUT   timed out
SOCK_E_PROTO     unknown or forbidden protocol type, or invalid data
SOCK_E_SYS       system call failed but $! is not set
SOCK_E_INVAL     invalid argument or data (wide characters, etc.)

Which codes a function sets is documented with the function.

FUNCTIONS

These are used by the socket modules to set the state, and by callers to read it.

sock_err( $code, $ret, $xtra )

Sets $SOCK_ERR to $code and $SOCK_ERRSTR to its text, then returns $ret, so it can be used as:

return sock_err( SOCK_E_EOF, undef ) if ...;

A positive $code is an errno value and its text is the system one, $! is not changed. An unknown negative $code gets the text "unknown error code [$code]". $xtra, if given and not empty, is appended to the text of a negative code after ": ", it is ignored for errno values.

sock_err_sys( $ret )

Sets the state from the current $! after a failed system call and returns $ret. If $! is not set, $SOCK_ERR is SOCK_E_SYS. Must be called right after the failed call, before anything else changes $!.

sock_err_name( $code )

Returns the name of $code, or of $SOCK_ERR if no $code is given: 'SOCK_E_EOF', 'SOCK_E_TIMEOUT', etc. for SOCK_E_* codes, 'SOCK_E_UNKNOWN' for an unknown negative code, 'EPIPE', 'EBADF', etc. for errno values ('ERRNO_<value>' if the value has no name), undef if not set.

sock_err_clear()

Clears $SOCK_ERR and $SOCK_ERRSTR to undef, returns 1.

EXPORTS

All variables, functions and SOCK_E_* constants are exported by default.

SEE ALSO

Net::Tools::Socket, Net::Tools::Socket::Protocols, Net::Tools::Socket::Pair, Errno

AUTHOR

Vladi Belperchinov-Shabanski "Cade"
      <cade@noxrun.com>
http://cade.noxrun.com/

COPYRIGHT AND LICENSE

Copyright (c) 2026 Vladi Belperchinov-Shabanski "Cade"

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.