in reply to h_errno constants?
package HErrno; # tlhackque - roughly based on a scheme by Perlbotics # in http://www.perlmonks.org/index.pl?node_id=773351 # # use HErrno.pm; # # print $herrno{$?} # String from h_errno # print $herrno{NETDB_SUCCESS} # String from name or value # use HErrno.pm qw( :NAMES ); # Constant (subroutines) for all name +s # use HErrno.pm qw ( HOST_NOT_FOUND ); # Constant for one name # # my h_errno = $? # if( h_errno == HOST_NOT_FOUND ) ... # our (@EXPORT, @EXPORT_OK,%EXPORT_TAGS,@ISA,%herrno,$VERSION); use Exporter (); use strict; $VERSION = "0.01_00"; $VERSION = eval $VERSION; @ISA = qw(Exporter); @EXPORT = qw(%herrno); BEGIN { %herrno = ( # Symbols to obtain => error string to return. NETDB_INTERNAL => "See errno.", NETDB_SUCCESS => "No problem.", HOST_NOT_FOUND => "Authoritative Answer Host not found.", TRY_AGAIN => "Non-Authoritative Host not found, or SER +VERFAIL.", NO_RECOVERY => "Non recoverable errors, FORMERR, REFUSED +, NOTIMP.", NO_DATA => "Valid name, no data record of requested +type.", NO_ADDRESS => "No address, look for MX record.", ); @EXPORT_OK = ( qw(NO_ADDRESS) ); %EXPORT_TAGS = ( NAMES => [qw(NO_ADDRESS)] ); # Get symbol values from netdb.h. This isn't robust... open my $errdb, '<', '/usr/include/netdb.h' or die; while (my $line = <$errdb>) { if (my ($symbol, $val) = $line =~ /^\s*\#\s*define\s+([^_]\w+)\s+( +-?\d+)/) { next unless exists $herrno{$symbol}; # skip irrelevant symbols die "FATAL: $symbol = $val - conflicts with $herrno{$val}\n" if exists $herrno{$val}; # Map numeric codes to text strings $herrno{$val} = $herrno{$symbol}; # Provide a symbolic value { no strict 'refs' ; *{$symbol} = sub () { $val; }; } # Export the symbol & add to the NAMES tag push @EXPORT_OK, $symbol; push @{$EXPORT_TAGS{NAMES}}, $symbol; } } # NO_ADDRESS is an alias for NO_DATA sub NO_ADDRESS() { NO_DATA() }; close $errdb or die; } 1; __END__
This communication may not represent my employer's views, if any, on the matters discussed.
|
|---|