NIS(3pm) | User Contributed Perl Documentation | NIS(3pm) |
Net::NIS - Interface to Sun's Network Information Service
use Net::NIS; tie %hash, 'Net::NIS', $mapname [, $domainname]; $value = $hash{$key};
or
($status, $value) = Net::NIS::yp_match (Net::NIS::yp_get_default_domain(), $mapname, $key);
The Net::NIS interface comes in three parts:
This document describes the NIS API implementation and the 'Tied' mechanism.
NIS maps are simple key/value pairs, perfectly suited for Perl hashes. Net::NIS allows any given NIS map to be treated as a hash (read-only). Usage is:
tie %hash, 'Net::NIS', $mapname [, $domainname];
$mapname must be specified, and be a valid map in the given domain. If the file /var/yp/nicknames exists, it is used to obtain a list of acceptable shortcut names, such as "aliases" for "mail.aliases". Otherwise, a hardcoded set of the "usual suspects" is consulted.
If $domainname is not given, the "yp_get_default_domain" function is used to determine the current NIS domain. This is usually the same as will be displayed by the "domainname" command.
If Net::NIS cannot tie to a given map, it returns "undef", with an appropriate error value in the variable $yperr. See "ERRORS".
To look up an entry in a YP map, simply use the entry name as a key in the tied hash. Net::NIS returns a string if the key exists in the map, or "undef" if it is not found. For any errors other than YPERR_KEY, Net::NIS raises a fatal exception through "croak".
Example
tie %alias, 'Net::NIS', 'mail.aliases' or die "Cannot tie to mail.aliases YP map: $yperr\n"; print "postmaster is ", $alias{postmaster} || "<unknown>", "\n";
As a special case, the magic map __YPMASTER can be used as an equivalent to 'ypwhich -m':
tie %ypmaster, 'Net::NIS', '__YPMASTER' or die ...; printf "ypmaster(passwd) = %s\n", $ypmaster{'passwd.byname'}; print $_, "\n" for sort keys %ypmaster; # Only works on Linux!
Note that keys() only works on Linux, because Linux includes a helpful yp_maplist() function. On Linux, you can get a list of existing YP maps. On other OSes, you can't -- but given the name of an existing map, $ypmaster{$map} will work as expected.
The NIS package implements all functions described in the ypclnt(3N) manual page.
The following commands have been implemented:
If called in scalar context, yp_match returns only $value, and it is up to the user to check $yperr.
The magic variable $yperr is exported by default (see "ERRORS").
The following error status constants can be imported individually, or by using the ':all' symbol:
YPERR_SUCCESS There is no error YPERR_BADARGS Args to function are bad YPERR_RPC RPC failure YPERR_DOMAIN Can't bind to a server with this domain YPERR_MAP No such map in server's domain YPERR_KEY No such key in map YPERR_YPERR Internal yp server or client error YPERR_RESRC Local resource allocation failure YPERR_NOMORE No more records in map database YPERR_PMAP Can't communicate with portmapper YPERR_YPBIND Can't communicate with ypbind YPERR_YPSERV Can't communicate with ypserv YPERR_NODOM Local domain name not set YPERR_BADDB yp data base is bad YPERR_VERS YP version mismatch YPERR_ACCESS Access violation YPERR_BUSY Database is busy
Instead of having 'tie' succeed and the first access fail, TIEHASH() (the function executed when performing a tie) performs some sanity checks: it ensures the validity of the domain and map names. On failure, 'tie' returns "undef", with an appropriate error value in $yperr :
tie %myhash, 'Net::NIS', 'foo-bar' or die "Unable to access foo-bar map: $yperr\n"
Note that the $yperr variable is magic, like Perl's $!. If accessed in a string context, it returns a human-friendly string obtained from the "yperr_string" library function. In a numeric context, $yperr returns the numeric status code returned from the last YP function. This can be compared against the error constants above, if you so desire.
Your vendor has not defined Net::NIS macro YPERR_xxxx
This indicates that one of the standard YPERR_xxx constants is not defined in your host's <rpcsct/ypclnt.h> file. You might see this during make test on an old system, perhaps.
Unable to find 'KEY' in 'MAP'. Reason: ...
If an attempt to access a tied variable fails for any reason other than 'no such key in map', FETCH() raises this fatal exception. It probably indicates that YP has gone down, or there is some other fatal error. This can be caught with eval{}, but I'm not sure what you can do about it...
Copyright (c) 1995, 2002 Rik Harris (rik.harris@fulcrum.com.au), 2002-2014 Ed Santiago. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Net::NIS is currently maintained by Ed Santiago <esm@cpan.org>.
The Network Information Service (NIS) was formerly known as Sun Yellow Pages (YP). The functionality of the two remains the same; only the name has changed. The name Yellow Pages is a registered trademark in the United Kingdom of British Telecommunications plc, and may not be used without permission.
2022-10-19 | perl v5.36.0 |