perl-5.38.0
gethostent
getnetent
getprotoent
getservent
setpwent
setgrent
sethostent STAYOPEN
setnetent STAYOPEN
setprotoent STAYOPEN
setservent STAYOPEN
endpwent
endgrent
endhostent
endnetent
endprotoent
endservent

These routines are the same as their counterparts in the system C library. In list context, the return values from the various get routines are as follows:

これらのルーチンは、システムの C ライブラリの同名の関数と同じです。 リストコンテキストでは、さまざまな get ルーチンからの返り値は、次のようになります:

 #    0        1          2           3         4
 my ( $name,   $passwd,   $gid,       $members  ) = getgr*
 my ( $name,   $aliases,  $addrtype,  $net      ) = getnet*
 my ( $name,   $aliases,  $port,      $proto    ) = getserv*
 my ( $name,   $aliases,  $proto                ) = getproto*
 my ( $name,   $aliases,  $addrtype,  $length,  @addrs ) = gethost*
 my ( $name,   $passwd,   $uid,       $gid,     $quota,
    $comment,  $gcos,     $dir,       $shell,   $expire ) = getpw*
 #    5        6          7           8         9

(If the entry doesn't exist, the return value is a single meaningless true value.)

(エントリが存在しなければ、返り値は単一の意味のない真の値です。)

The exact meaning of the $gcos field varies but it usually contains the real name of the user (as opposed to the login name) and other information pertaining to the user. Beware, however, that in many system users are able to change this information and therefore it cannot be trusted and therefore the $gcos is tainted (see perlsec). The $passwd and $shell, user's encrypted password and login shell, are also tainted, for the same reason.

$gcos フィールドの正確な意味はさまざまですが、通常は(ログイン名ではなく) ユーザーの実際の名前とユーザーに付随する情報を含みます。 但し、多くのシステムではユーザーがこの情報を変更できるので、この情報は 信頼できず、従って $gcos は汚染されます(perlsec を参照してください)。 ユーザーの暗号化されたパスワードとログインシェルである $passwd と $shell も、同様の理由で汚染されます。

In scalar context, you get the name, unless the function was a lookup by name, in which case you get the other thing, whatever it is. (If the entry doesn't exist you get the undefined value.) For example:

スカラコンテキストでは、*nam、*byname といった NAME で検索するもの以外は、 name を返し、NAME で検索するものは、何か別のものを返します。 (エントリが存在しなければ、未定義値が返ります。) 例えば:

    my $uid   = getpwnam($name);
    my $name  = getpwuid($num);
    my $name  = getpwent();
    my $gid   = getgrnam($name);
    my $name  = getgrgid($num);
    my $name  = getgrent();
    # etc.

In getpw*() the fields $quota, $comment, and $expire are special in that they are unsupported on many systems. If the $quota is unsupported, it is an empty scalar. If it is supported, it usually encodes the disk quota. If the $comment field is unsupported, it is an empty scalar. If it is supported it usually encodes some administrative comment about the user. In some systems the $quota field may be $change or $age, fields that have to do with password aging. In some systems the $comment field may be $class. The $expire field, if present, encodes the expiration period of the account or the password. For the availability and the exact meaning of these fields in your system, please consult getpwnam(3) and your system's pwd.h file. You can also find out from within Perl what your $quota and $comment fields mean and whether you have the $expire field by using the Config module and the values d_pwquota, d_pwage, d_pwchange, d_pwcomment, and d_pwexpire. Shadow password files are supported only if your vendor has implemented them in the intuitive fashion that calling the regular C library routines gets the shadow versions if you're running under privilege or if there exists the shadow(3) functions as found in System V (this includes Solaris and Linux). Those systems that implement a proprietary shadow password facility are unlikely to be supported.

getpw*() では、$quota, $comment, $expire フィールドは、 多くのシステムでは対応していないので特別な処理がされます。 $quota が非対応の場合、空のスカラになります。 対応している場合、通常はディスククォータの値が入ります。 $comment フィールドが非対応の場合、空のスカラになります。 対応している場合、通常はユーザーに関する管理上のコメントが入ります。 $quota フィールドはパスワードの寿命を示す $change や $age である システムもあります。 $comment フィールドは $class であるシステムもあります。 $expire フィールドがある場合は、アカウントやパスワードが時間切れになる 期間が入ります。 動作させるシステムでのこれらのフィールドの有効性と正確な意味については、 getpwnam(3) のドキュメントと pwd.h ファイルを参照してください。 $quota と $comment フィールドが何を意味しているかと、$expire フィールドが あるかどうかは、Config モジュールを使って、d_pwquota, d_pwage, d_pwchange, d_pwcomment, d_pwexpire の値を 調べることによって Perl 自身で調べることも出来ます。 シャドウパスワードは、通常の C ライブラリルーチンを権限がある状態で 呼び出すことでシャドウ版が取得できるか、System V にあるような (Solaris と Linux を含みます) shadow(3) 関数があるといった、 直感的な方法で実装されている場合にのみ対応されます。 独占的なシャドウパスワード機能を実装しているシステムでは、 それに対応されることはないでしょう。

The $members value returned by getgr*() is a space-separated list of the login names of the members of the group.

getgr*() によって返る値 $members は、グループのメンバの ログイン名をスペースで区切ったものです。

For the gethost*() functions, if the h_errno variable is supported in C, it will be returned to you via $? if the function call fails. The @addrs value returned by a successful call is a list of raw addresses returned by the corresponding library call. In the Internet domain, each address is four bytes long; you can unpack it by saying something like:

gethost*() 関数では、C で h_errno 変数がサポートされていれば、 関数呼出が失敗したときに、$? を通して、その値が返されます。 成功時に返される @addrs 値は、対応するシステムコールが返す、 生のアドレスのリストです。 インターネットドメインでは、個々のアドレスは、4 バイト長です; 以下のようにして unpack することができます:

    my ($w,$x,$y,$z) = unpack('W4',$addr[0]);

The Socket library makes this slightly easier:

Socket ライブラリを使うともう少し簡単になります。

    use Socket;
    my $iaddr = inet_aton("127.1"); # or whatever address
    my $name  = gethostbyaddr($iaddr, AF_INET);

    # or going the other way
    my $straddr = inet_ntoa($iaddr);

In the opposite way, to resolve a hostname to the IP address you can write this:

逆方向に、ホスト名から IP アドレスを解決するには以下のように書けます:

    use Socket;
    my $packed_ip = gethostbyname("www.perl.org");
    my $ip_address;
    if (defined $packed_ip) {
        $ip_address = inet_ntoa($packed_ip);
    }

Make sure gethostbyname is called in SCALAR context and that its return value is checked for definedness.

gethostbyname はスカラコンテキストで 呼び出すようにして、返り値が定義されているかを必ずチェックしてください。

The getprotobynumber function, even though it only takes one argument, has the precedence of a list operator, so beware:

getprotobynumber 関数は、一つの引数しか 取らないにも関わらず、リスト演算子の優先順位を持ちます; 従って 注意してください:

    getprotobynumber $number eq 'icmp'   # WRONG
    getprotobynumber($number eq 'icmp')  # actually means this
    getprotobynumber($number) eq 'icmp'  # better this way

If you get tired of remembering which element of the return list contains which return value, by-name interfaces are provided in standard modules: File::stat, Net::hostent, Net::netent, Net::protoent, Net::servent, Time::gmtime, Time::localtime, and User::grent. These override the normal built-ins, supplying versions that return objects with the appropriate names for each field. For example:

返り値のリストの何番目がどの要素かを覚えるのに疲れたなら、 名前ベースのインターフェースが標準モジュールで提供されています: File::stat, Net::hostent, Net::netent, Net::protoent, Net::servent, Time::gmtime, Time::localtime, User::grent です。 これらは通常の組み込みを上書きし、 それぞれのフィールドに適切な名前をつけたオブジェクトを返します。 例えば:

   use File::stat;
   use User::pwent;
   my $is_his = (stat($filename)->uid == pwent($whoever)->uid);

Even though it looks as though they're the same method calls (uid), they aren't, because a File::stat object is different from a User::pwent object.

同じメソッド(uid)を呼び出しているように見えますが、違います; なぜなら File::stat オブジェクトは User::pwent オブジェクトとは 異なるからです。

Many of these functions are not safe in a multi-threaded environment where more than one thread can be using them. In particular, functions like getpwent() iterate per-process and not per-thread, so if two threads are simultaneously iterating, neither will get all the records.

これらの関数の多くは、複数のスレッドがこれらを使うような マルチスレッド環境では安全ではありません。 特に、 getpwent() のような関数はスレッド単位ではなくプロセス単位で 反復するので、二つのスレッドが同時に反復すると、 どちらも全てのレコードを得られません。

Some systems have thread-safe versions of some of the functions, such as getpwnam_r() instead of getpwnam(). There, Perl automatically and invisibly substitutes the thread-safe version, without notice. This means that code that safely runs on some systems can fail on others that lack the thread-safe versions.

一部のシステムは、 getpwnam() の代わりの getpwnam_r() のように、一部の関数について スレッドセーフ版を持っています。 その場合、Perl は自動的かつ目に見えないように、通知なしで スレッドセーフ版に置き換えます。 つまり、一部のシステムで安全に実行できるコードが スレッドセーフ版のないその他のシステムでは失敗することがあるということです。

移植性の問題: "getpwnam" in perlport から "endservent" in perlport