<?xml version='1.0' encoding='utf-8'?>
<pod xmlns="http://axkit.org/ns/2000/pod2xml">
<head>
	<title>perlipc - Perl interprocess communication (signals, fifos, pipes, safe subprocesses, sockets, and semaphores)</title>
</head>
<sect1>
<title>perlipc - Perl interprocess communication (signals, fifos, pipes, safe subprocesses, sockets, and semaphores)</title>
<para>
perlipc - Perl のプロセス間通信 (シグナル, fifo, パイプ, 安全な副プロセス, ソケット, セマフォ)
</para>
</sect1>
<sect1>
<title>DESCRIPTION</title>
<para>
The basic IPC facilities of Perl are built out of the good old Unix
signals, named pipes, pipe opens, the Berkeley socket routines, and SysV
IPC calls.  Each is used in slightly different situations.
</para>
<para>
Perl の基本的な IPC 機能は、古きよき UNIX のシグナル、名前付きパイプ、
パイプ、Berkeley ソケットルーチン、 SysV IPC コールから構成されています。
これら各々ははっきりと異なる状況で使われます。
</para>
</sect1>
<sect1>
<title>Signals</title>
<para>
(シグナル)
</para>
<para>
Perl uses a simple signal handling model: the %SIG hash contains names
or references of user-installed signal handlers.  These handlers will
be called with an argument which is the name of the signal that
triggered it.  A signal may be generated intentionally from a
particular keyboard sequence like control-C or control-Z, sent to you
from another process, or triggered automatically by the kernel when
special events transpire, like a child process exiting, your process
running out of stack space, or hitting file size limit.
</para>
<para>
Perl は単純なシグナルハンドリングモデルを使っています: %SIG という
ハッシュは、ユーザーがインストールしたシグナルハンドラの名前、
もしくはハンドラに対するリファレンスを保持します。
これらのハンドラは、起動されたシグナルの名前を引数として呼び出されます。
シグナルは control-C や control-Z のような特定のキーボードシーケンスで
意識的に生成することもできますし、他のプロセスがシグナルを
送ることもあります。
あるいは子プロセスが終了したとか、プロセスがスタックを使いきった、
ファイルサイズ制限に引っ掛かったといった特殊なイベントが発生したときに、
カーネルが自動的にシグナルを発生させることもあります。
</para>
<para>
For example, to trap an interrupt signal, set up a handler like this:
</para>
<para>
例えば割り込みシグナル(interrupt signal)をトラップするには、
以下の例のようにハンドラを設定します:
</para>
<verbatim><![CDATA[
sub catch_zap {
	my $signame = shift;
	$shucks++;
	die "Somebody sent me a SIG$signame";
}
$SIG{INT} = 'catch_zap';  # could fail in modules
$SIG{INT} = \&catch_zap;  # best strategy
]]></verbatim>
<para>
Prior to Perl 5.7.3 it was necessary to do as little as you possibly
could in your handler; notice how all we do is set a global variable
and then raise an exception.  That's because on most systems,
libraries are not re-entrant; particularly, memory allocation and I/O
routines are not.  That meant that doing nearly <emphasis>anything</emphasis> in your
handler could in theory trigger a memory fault and subsequent core
dump - see <link xref='Deferred Signals (Safe Signals)'>/Deferred Signals (Safe Signals)</link> below.
</para>
<para>
Perl 5.7.3 以前では、自分のハンドラの中を使うことができます。
私たちが認識していることは、グローバル変数に設定した後で例外を
引き起こすということだけであることに注意してください。
これは、ほとんどのシステム上ではライブラリ、とくにメモリ割り付けや
入出力に関するものは再入可能ではないためです。
これは、あなたのハンドラ内のほとんど <emphasis>あらゆること</emphasis> が理論的には
メモリフォールトやそれに続くコアダンブを引き起こす可能性が
あるということです - 以下の <link xref='Deferred Signals (Safe Signals)'>/Deferred Signals (Safe Signals)</link> を
参照してください。
</para>
<para>
The names of the signals are the ones listed out by <code>kill -l</code> on your
system, or you can retrieve them from the Config module.  Set up an
@signame list indexed by number to get the name and a %signo table
indexed by name to get the number:
</para>
<para>
あなたの使っているシステムにおけるシグナルの名称は <code>kill -l</code> に
よってリストアップされます。
あるいはコンフィグモジュールから取得することもできます。
名前を得るための番号によって添え字づけされる @signame というリストと
番号を得るための名前によって添え字づけされる %signo というテーブルを
セットアップします。
</para>
<verbatim><![CDATA[
use Config;
defined $Config{sig_name} || die "No sigs?";
foreach $name (split(' ', $Config{sig_name})) {
	$signo{$name} = $i;
	$signame[$i] = $name;
	$i++;
}
]]></verbatim>
<para>
So to check whether signal 17 and SIGALRM were the same, do just this:
</para>
<para>
ですから、17 番のシグナルと SIGALRM が同一であるかどうかは以下のようにして
チェックできます:
</para>
<verbatim><![CDATA[
print "signal #17 = $signame[17]\n";
if ($signo{ALRM}) {
	print "SIGALRM is $signo{ALRM}\n";
}
]]></verbatim>
<para>
You may also choose to assign the strings <code>'IGNORE'</code> or <code>'DEFAULT'</code> as
the handler, in which case Perl will try to discard the signal or do the
default thing.
</para>
<para>
ハンドラとして <code>'IGNORE'</code> や <code>'DEFAULT'</code> といった文字列を
代入することも選択できます。
これらのハンドラは、perl がシグナルを破棄させるようにしたり
デフォルトの動作を行うようにさせるものです。
</para>
<para>
On most Unix platforms, the <code>CHLD</code> (sometimes also known as <code>CLD</code>) signal
has special behavior with respect to a value of <code>'IGNORE'</code>.
Setting <code>$SIG{CHLD}</code> to <code>'IGNORE'</code> on such a platform has the effect of
not creating zombie processes when the parent process fails to <code>wait()</code>
on its child processes (i.e. child processes are automatically reaped).
Calling <code>wait()</code> with <code>$SIG{CHLD}</code> set to <code>'IGNORE'</code> usually returns
<code>-1</code> on such platforms.
</para>
<para>
ほとんどの UNIX プラットフォームでは、<code>CHLD</code>(<code>CLD</code> の場合もあり)
シグナルは <code>'IGNORE'</code> の値に対応するような特別な振る舞いをします。
そのようなプラットフォームでは <code>'IGNORE'</code> に <code>$SIG{CHLD}</code> を
設定することによって、親プロセスが <code>wait()</code> に失敗したようなときに
ゾンビプロセスができることを防ぎます(子プロセスは自動的に
刈り取られます)。
<code>$SIG{CHLD}</code> を <code>'IGNORE'</code> を設定して <code>wait()</code> を呼び出すことによって、
このようなプラットフォームでは <code>-1</code> が通常返されます。
</para>
<para>
Some signals can be neither trapped nor ignored, such as
the KILL and STOP (but not the TSTP) signals.  One strategy for
temporarily ignoring signals is to use a local() statement, which will be
automatically restored once your block is exited.  (Remember that local()
values are &quot;inherited&quot; by functions called from within that block.)
</para>
<para>
KILL シグナルや STOP シグナル(TSTP ではない)のような幾つかのシグナルは、
トラップすることも無視することもできません。
一時的にシグナルを無視するための戦略は local() 文を使うというものです。
これはその local() 文を囲むブロックから出たときに、自動的に元の状態に
復帰します。
(local() による値は ブロックの内側から呼び出された関数に
“引き継がれる”(inherited) ことを忘れないでください)。
</para>
<verbatim><![CDATA[
sub precious {
	local $SIG{INT} = 'IGNORE';
	&more_functions;
}
sub more_functions {
	# interrupts still ignored, for now...
}
]]></verbatim>
<para>
Sending a signal to a negative process ID means that you send the signal
to the entire Unix process-group.  This code sends a hang-up signal to all
processes in the current process group (and sets $SIG{HUP} to IGNORE so
it doesn't kill itself):
</para>
<para>
負のプロセス ID に対してシグナルを送ることは、UNIX プロセスグループ
全体にシグナルを送ることになります。
以下のコードは、カレントのプロセスグループに属する全てのプロセスに
hang-up シグナルを送出します(そして、$SIG{HUP} に IGNORE を設定するので、
自分自身を kill することはありません)。
</para>
<verbatim><![CDATA[
{
	local $SIG{HUP} = 'IGNORE';
	kill HUP => -$$;
	# snazzy writing of: kill('HUP', -$$)
}
]]></verbatim>
<para>
Another interesting signal to send is signal number zero.  This doesn't
actually affect a child process, but instead checks whether it's alive
or has changed its UID.
</para>
<para>
この他の興味深いシグナルは、シグナル番号 0 です。
これは実際には子プロセスに影響を及ぼすことはありませんが、UID が
生きているのか、あるいは変更されたのかのチェックを行います。
</para>
<verbatim><![CDATA[
unless (kill 0 => $kid_pid) {
	warn "something wicked happened to $kid_pid";
}
]]></verbatim>
<para>
When directed at a process whose UID is not identical to that
of the sending process, signal number zero may fail because
you lack permission to send the signal, even though the process is alive.
You may be able to determine the cause of failure using <code>%!</code>.
</para>
<para>
送り先のプロセスの UID が、送り元と同じではない場合、たとえプロセスが
生きていても、シグナルを送る権限がないので、シグナル番号 0 は失敗します。
失敗の原因は、<code>%!</code> を使って決定できます。
</para>
<verbatim><![CDATA[
unless (kill 0 => $pid or $!{EPERM}) {
	warn "$pid looks dead";
}
]]></verbatim>
<para>
You might also want to employ anonymous functions for simple signal
handlers:
</para>
<para>
単純なシグナルハンドラには無名関数を使いたくなるかもしれません:
</para>
<verbatim><![CDATA[
$SIG{INT} = sub { die "\nOutta here!\n" };
]]></verbatim>
<para>
But that will be problematic for the more complicated handlers that need
to reinstall themselves.  Because Perl's signal mechanism is currently
based on the signal(3) function from the C library, you may sometimes be so
unfortunate as to run on systems where that function is &quot;broken&quot;, that
is, it behaves in the old unreliable SysV way rather than the newer, more
reasonable BSD and POSIX fashion.  So you'll see defensive people writing
signal handlers like this:
</para>
<para>
しかしこれは、自分自身を再インストールする必要があるような、もっと複雑な
ハンドラに対しては問題になる可能性があります。
Perl のシグナル機構は現在のところ C ライブラリの  signal(3) 関数に基づいた
ものなので、関数が“ぶっ壊れた”ような、つまり、より新しくて信頼性のある
BSD 形式や POSIX 形式ではなく古くて信用できない SysV 方式の振る舞いを
するようなものシステムで実行するような不運な立場になるかもしれません。
そのため、以下のようにシグナルハンドラを書いて人を守っているものを
見るでしょう:
</para>
<verbatim><![CDATA[
sub REAPER {
	$waitedpid = wait;
	# loathe sysV: it makes us not only reinstate
	# the handler, but place it after the wait
	$SIG{CHLD} = \&REAPER;
}
$SIG{CHLD} = \&REAPER;
# now do something that forks...
]]></verbatim>
<para>
or better still:
</para>
<para>
あるいはもっとましなように:
</para>
<verbatim><![CDATA[
use POSIX ":sys_wait_h";
sub REAPER {
	my $child;
	# If a second child dies while in the signal handler caused by the
	# first death, we won't get another signal. So must loop here else
	# we will leave the unreaped child as a zombie. And the next time
	# two children die we get another zombie. And so on.
    while (($child = waitpid(-1,WNOHANG)) > 0) {
	    $Kid_Status{$child} = $?;
	}
	$SIG{CHLD} = \&REAPER;  # still loathe sysV
}
$SIG{CHLD} = \&REAPER;
# do something that forks...
]]></verbatim>
<para>
Signal handling is also used for timeouts in Unix,   While safely
protected within an <code>eval{}</code> block, you set a signal handler to trap
alarm signals and then schedule to have one delivered to you in some
number of seconds.  Then try your blocking operation, clearing the alarm
when it's done but not before you've exited your <code>eval{}</code> block.  If it
goes off, you'll use die() to jump out of the block, much as you might
using longjmp() or throw() in other languages.
</para>
<para>
UNIX では、シグナルハンドラはタイムアウトのためにも使われます。
<code>eval{}</code> ブロックの中では安全に保護されいる間に、alarm シグナルを
トラップするためにシグナルハンドラを設定して、その後で解放される
秒数をスケジューリングします。
それからブロッキング操作を試して、
それが完了したら <code>eval{}</code> ブロックを抜ける前に alarm をクリアーします。
設定した時間が経ってしまった場合には、他の言語における longjmp() や
throw() を使ったときと同じようにブロックを抜けるために die() を
使います。
</para>
<para>
Here's an example:
</para>
<para>
例を挙げましょう:
</para>
<verbatim><![CDATA[
eval {
    local $SIG{ALRM} = sub { die "alarm clock restart" };
    alarm 10;
    flock(FH, 2);   # blocking write lock
    alarm 0;
};
if ($@ and $@ !~ /alarm clock restart/) { die }
]]></verbatim>
<para>
If the operation being timed out is system() or qx(), this technique
is liable to generate zombies.    If this matters to you, you'll
need to do your own fork() and exec(), and kill the errant child process.
</para>
<para>
時間切れを起こした操作が system() か qx() なら、このテクニックはゾンビを
生成する傾向があります。
もしそれが問題なら、自力で fork() と exec() を行って、エラーの起きた
子プロセスを kill する必要があるでしょう。
</para>
<para>
For more complex signal handling, you might see the standard POSIX
module.  Lamentably, this is almost entirely undocumented, but
the <filename>t/lib/posix.t</filename> file from the Perl source distribution has some
examples in it.
</para>
<para>
より複雑なシグナル処理のためには、標準の POSIX モジュールを確かめると
よいかもしれません。
嘆かわしいことにこれはほとんどアンドキュメントな状態ですが、Perl の
ソース配布キットにある <filename>t/lib/posix.t</filename> というファイルには幾つかの
サンプルがあります。
</para>
<sect2>
<title>Handling the SIGHUP Signal in Daemons</title>
<para>
(デーモンで SIGHUP シグナルを扱う)
</para>
<para>
A process that usually starts when the system boots and shuts down
when the system is shut down is called a daemon (Disk And Execution
MONitor). If a daemon process has a configuration file which is
modified after the process has been started, there should be a way to
tell that process to re-read its configuration file, without stopping
the process. Many daemons provide this mechanism using the <code>SIGHUP</code>
signal handler. When you want to tell the daemon to re-read the file
you simply send it the <code>SIGHUP</code> signal.
</para>
<para>
普通システムブート時に起動し、システムシャットダウン時に終了するプロセスの
ことをデーモン(daemon: Disk And Execution MONitor)と呼びます。
もしデーモンプロセスに設定ファイルがあり、それがプロセスが開始してから
変更された場合、プロセスを止めることなく、プロセスに設定ファイルを
再読み込みすることを知らせる方法があるべきです。
多くのデーモンはこの機構を <code>SIGHUP</code> シグナルハンドラを使って
提供しています。
デーモンにファイルを再読み込みしてほしい場合、単にデーモンに
<code>SIGHUP</code> シグナルを送ります。
</para>
<para>
Not all platforms automatically reinstall their (native) signal
handlers after a signal delivery.  This means that the handler works
only the first time the signal is sent. The solution to this problem
is to use <code>POSIX</code> signal handlers if available, their behaviour
is well-defined.
</para>
<para>
全てのプラットフォームにおいて、シグナル配送の後自動的に(ネイティブな)
シグナルハンドラを再設定するわけではありません。
これは、ハンドラは最初にシグナルが送られたときにのみ動作することを
意味します。
この問題の解決策は、もし利用可能であれば、振る舞いが明確である
<code>POSIX</code> シグナルハンドラを使うことです。
</para>
<para>
The following example implements a simple daemon, which restarts
itself every time the <code>SIGHUP</code> signal is received. The actual code is
located in the subroutine <code>code()</code>, which simply prints some debug
info to show that it works and should be replaced with the real code.
</para>
<para>
以下の例は、<code>SIGHUP</code> を受け取る毎に再起動するという単純なデーモンを
実装しています。
実際のコードはサブルーチン <code>code()</code> に置かれていて、どのように動作するかを
示し、実際のコードと置き換えられるように、単にデバッグ情報を出力します。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
]]></verbatim>
<verbatim><![CDATA[
use POSIX ();
use FindBin ();
use File::Basename ();
use File::Spec::Functions;
]]></verbatim>
<verbatim><![CDATA[
$|=1;
]]></verbatim>
<verbatim><![CDATA[
# make the daemon cross-platform, so exec always calls the script
# itself with the right path, no matter how the script was invoked.
my $script = File::Basename::basename($0);
my $SELF = catfile $FindBin::Bin, $script;
]]></verbatim>
<verbatim><![CDATA[
# POSIX unmasks the sigprocmask properly
my $sigset = POSIX::SigSet->new();
my $action = POSIX::SigAction->new('sigHUP_handler',
                                   $sigset,
                                   &POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGHUP, $action);
]]></verbatim>
<verbatim><![CDATA[
sub sigHUP_handler {
    print "got SIGHUP\n";
    exec($SELF, @ARGV) or die "Couldn't restart: $!\n";
}
]]></verbatim>
<verbatim><![CDATA[
code();
]]></verbatim>
<verbatim><![CDATA[
sub code {
    print "PID: $$\n";
    print "ARGV: @ARGV\n";
    my $c = 0;
    while (++$c) {
        sleep 2;
        print "$c\n";
    }
}
__END__
]]></verbatim>
</sect2>
</sect1>
<sect1>
<title>Named Pipes</title>
<para>
(名前付きパイプ)
</para>
<para>
A named pipe (often referred to as a FIFO) is an old Unix IPC
mechanism for processes communicating on the same machine.  It works
just like a regular, connected anonymous pipes, except that the
processes rendezvous using a filename and don't have to be related.
</para>
<para>
名前付きパイプ(しばしば FIFO として参照されます)は、同じマシン上での
プロセス間通信のための古い UNIX IPC 機構です。
これは通常の無名パイプの接続と同様に動作しますが、ファイル名を使って
プロセスのランデブーが行われ、関連づける必要がないという点が異なります。
</para>
<para>
To create a named pipe, use the <code>POSIX::mkfifo()</code> function.
</para>
<para>
名前付きパイプを生成するには、<code>POSIX::mkfifo()</code> 関数を使ってください。
</para>
<verbatim><![CDATA[
use POSIX qw(mkfifo);
mkfifo($path, 0700) or die "mkfifo $path failed: $!";
]]></verbatim>
<para>
You can also use the Unix command mknod(1) or on some
systems, mkfifo(1).  These may not be in your normal path.
</para>
<para>
一部のシステムでは Unix コマンド mknod(1) か、システムによっては
mkfifo(1) を使えます。
これらはあなたの通常パス(normal path)に置くことはできません。
</para>
<verbatim><![CDATA[
# system return val is backwards, so && not ||
#
$ENV{PATH} .= ":/etc:/usr/etc";
if  (      system('mknod',  $path, 'p')
	    && system('mkfifo', $path) )
{
	die "mk{nod,fifo} $path failed";
}
]]></verbatim>
<para>
A fifo is convenient when you want to connect a process to an unrelated
one.  When you open a fifo, the program will block until there's something
on the other end.
</para>
<para>
fifo は、あるプロセスをそれとは関係ない別のプロセスに接続したいときに
便利です。
fifo をオープンしたとき、プログラムはもう一方の端点に何かが
置かれるまでブロックされます。
</para>
<para>
For example, let's say you'd like to have your <filename>.signature</filename> file be a
named pipe that has a Perl program on the other end.  Now every time any
program (like a mailer, news reader, finger program, etc.) tries to read
from that file, the reading program will block and your program will
supply the new signature.  We'll use the pipe-checking file test <strong>-p</strong>
to find out whether anyone (or anything) has accidentally removed our fifo.
</para>
<para>
たとえば、<filename>.signature</filename> というファイルを、もう一方の端点に Perl
プログラムが置かれている名前付きパイプに接続したいとしましょう。
このとき、任意のプログラムが任意の時点で(メイラー、ニューズリーダー、
finger プログラムなどのように)そのファイルを読み出そうとしますから、
読み出しプログラムはブロックをしてからあなたのプログラムが新しい
シグネチャーを提供するようにしなければなりません。
誰か(もしくは何か)が間違って私たちのfifoを削除したりしないかどうかを
監視するために <strong>-p</strong> という pipe-cheching ファイルテストを使用します。
</para>
<verbatim><![CDATA[
chdir; # go home
$FIFO = '.signature';
]]></verbatim>
<verbatim><![CDATA[
while (1) {
	unless (-p $FIFO) {
	    unlink $FIFO;
	    require POSIX;
	    POSIX::mkfifo($FIFO, 0700)
		or die "can't mkfifo $FIFO: $!";
	}
]]></verbatim>
<verbatim><![CDATA[
# next line blocks until there's a reader
open (FIFO, "> $FIFO") || die "can't write $FIFO: $!";
print FIFO "John Smith (smith\@host.org)\n", `fortune -s`;
close FIFO;
sleep 2;    # to avoid dup signals
    }
]]></verbatim>
<sect2>
<title>Deferred Signals (Safe Signals)</title>
<para>
(保留シグナル(安全なシグナル))
</para>
<para>
In Perls before Perl 5.7.3 by installing Perl code to deal with
signals, you were exposing yourself to danger from two things.  First,
few system library functions are re-entrant.  If the signal interrupts
while Perl is executing one function (like malloc(3) or printf(3)),
and your signal handler then calls the same function again, you could
get unpredictable behavior--often, a core dump.  Second, Perl isn't
itself re-entrant at the lowest levels.  If the signal interrupts Perl
while Perl is changing its own internal data structures, similarly
unpredictable behaviour may result.
</para>
<para>
Perl 5.7.3 以前の Perl では、シグナルを取り扱う Perl プログラムを
インストールすることによって、あなたは二つの危険性に直面することに
なりました。
第一に、再入可能なライブラリ関数を備えたシステムは数少ないこと。
Perl がある関数(malloc(3) や printf(3) など)を実行中にシグナル割り込みが
あったとすると、あなたのシグナルハンドラは同じ関数を
再度呼び出し、結果として予測のつかない動作になる可能性があります。
第二に、Perl 自身も最低レベルにおいては再入可能に
なっていないということです。
Perl が自分の内部構造を操作しているときにシグナル割り込みが
あったとすると、これもまた予測のつかない動作になるでしょう。
</para>
<para>
There were two things you could do, knowing this: be paranoid or be
pragmatic.  The paranoid approach was to do as little as possible in your
signal handler.  Set an existing integer variable that already has a
value, and return.  This doesn't help you if you're in a slow system call,
which will just restart.  That means you have to <code>die</code> to longjmp(3) out
of the handler.  Even this is a little cavalier for the true paranoiac,
who avoids <code>die</code> in a handler because the system <emphasis>is</emphasis> out to get you.
The pragmatic approach was to say &quot;I know the risks, but prefer the
convenience&quot;, and to do anything you wanted in your signal handler,
and be prepared to clean up core dumps now and again.
</para>
<para>
あなたの取りうる手段が二つありました:
偏執狂的になるか現実的になるかです。
偏執狂的アプローチは、あなたのシグナルハンドラの中でできるだけ
少ないことを行うというものでした。
既に値をもって存在している整数変数に値を設定してリターンします。
これは単にリスタートするような遅いシステムコールの中にいるときには
助けになりません。
これはつまり、ハンドラの外に longjmp(3) するには <code>die</code> する必要が
あるということです。
これは本当の偏執狂というのにはちょっとおおげさですが、ハンドラ中で
<code>die</code> を排除します。
現実的なアプローチは「リスクがあるのは分かってるけどさ、便利なら
いいじゃない」というもので、シグナルハンドラ中で行いたいことを
全て行い、コアダンプを掃除する準備をしてから再度行うというものでした。
</para>
<para>
In Perl 5.7.3 and later to avoid these problems signals are
&quot;deferred&quot;-- that is when the signal is delivered to the process by
the system (to the C code that implements Perl) a flag is set, and the
handler returns immediately. Then at strategic &quot;safe&quot; points in the
Perl interpreter (e.g. when it is about to execute a new opcode) the
flags are checked and the Perl level handler from %SIG is
executed. The &quot;deferred&quot; scheme allows much more flexibility in the
coding of signal handler as we know Perl interpreter is in a safe
state, and that we are not in a system library function when the
handler is called.  However the implementation does differ from
previous Perls in the following ways:
</para>
<para>
Perl 5.7.3 以降では、この問題を避けるために、シグナルは「保留」されます --
これは、システムによってシグナルが (Perl を実装している C コードに)
配送されると、フラグをセットして、ハンドラは直ちに返ります。
それから、Perl インタプリタの戦略的に「安全な」地点
(例えば、新しいオペコードを実行しようとしている時点) で、
フラグをチェックして、%SIG の Perl レベルハンドラを実行します。
「保留」スキームは、Perl インタプリタが安全な状態にあって、ハンドラが
呼び出されたときにシステムライブラリの中ではないことが分かっているので、
シグナルハンドラのコーディングが遥かに柔軟になります。
しかし、実装は以下の方向で以前の Perl と異なります:
</para>
<list>
<item><itemtext>Long-running opcodes</itemtext>
<para>
(長時間実行されるオペコード)
</para>
<para>
As the Perl interpreter only looks at the signal flags when it is about
to execute a new opcode, a signal that arrives during a long-running
opcode (e.g. a regular expression operation on a very large string) will
not be seen until the current opcode completes.
</para>
<para>
Perl インタプリタは、もし長時間実行されるオペコード
(例えばとても長い文字列での正規表現操作)の途中でシグナルが
到着すると、新しいオペコードを実行しようとする時にのみシグナルフラグを
見るので、シグナルは現在のオペコードが完了するまで現れません。
</para>
<para>
N.B. If a signal of any given type fires multiple times during an opcode 
(such as from a fine-grained timer), the handler for that signal will
only be called once after the opcode completes, and all the other
instances will be discarded.  Furthermore, if your system's signal queue
gets flooded to the point that there are signals that have been raised
but not yet caught (and thus not deferred) at the time an opcode
completes, those signals may well be caught and deferred during
subsequent opcodes, with sometimes surprising results.  For example, you
may see alarms delivered even after calling <code>alarm(0)</code> as the latter
stops the raising of alarms but does not cancel the delivery of alarms
raised but not yet caught.  Do not depend on the behaviors described in
this paragraph as they are side effects of the current implementation and
may change in future versions of Perl.
</para>
<para>
注意: もしどのような種類のシグナルでも(例えば高精度のタイマーから)
1 つのオペコードの間に複数回発生した場合、そのシグナルのハンドラは
オペコードの完了後 1 度しか呼び出されず、その他の実体は破棄されます。
さらに、システムのシグナルキューがあふれたために
あるオペコードが完了時に、発生したけれどもまだ捕捉されていない
(従ってまだ保留されていない)シグナルがあると
これらのシグナルが引き続くオペコードの間捕捉および保留され、時々
驚くべき結果となることがあります。
例えば、<code>alarm(0)</code> はアラームの発生を止めますが、
発生したけれどもまだ捕捉していないアラームの配達がキャンセルしないので、
<code>alarm(0)</code> を呼び出した後にもアラームが配送されることがあります。
これは現在の実装の副作用であり、将来のバージョンの Perl では
変更されるかもしれないので、この段落に記述された振る舞いに依存しては
いけません。
</para>
</item>
<item><itemtext>Interrupting IO</itemtext>
<para>
(I/O 割り込み)
</para>
<para>
When a signal is delivered (e.g. INT control-C) the operating system
breaks into IO operations like <code>read</code> (used to implement Perls
&lt;&gt; operator). On older Perls the handler was called
immediately (and as <code>read</code> is not &quot;unsafe&quot; this worked well). With
the &quot;deferred&quot; scheme the handler is not called immediately, and if
Perl is using system's <code>stdio</code> library that library may re-start the
<code>read</code> without returning to Perl and giving it a chance to call the
%SIG handler. If this happens on your system the solution is to use
<code>:perlio</code> layer to do IO - at least on those handles which you want
to be able to break into with signals. (The <code>:perlio</code> layer checks
the signal flags and calls %SIG handlers before resuming IO operation.)
</para>
<para>
(INT control-C などで) シグナルが配送されると、
OS は (Perl &lt;&gt; 演算子の実装で使われている) <code>read</code> のような
I/O 操作を中断します。
古い Perl ではハンドラをすぐに呼び出します
(そして <code>read</code> はうまく動くので「安全ではない」ことはありません)。
「保留」スキームではハンドラはすぐに呼び出されず、
もし Perl がシステムの <code>stdio</code> ライブラリを使っていると、ライブラリは
Perl に返って %SIG ハンドラを呼び出しする機会なしに <code>read</code> を
再起動します。
もしこれが起きた場合、解決法は、I/O を行うときにー
- 少なくともシグナルで中断できるようにしたいハンドルで -
<code>:perlio</code> 層を使うことです 
(<code>:perlio</code> 層はシグナルフラグをチェックして、I/O 操作を続行する前に
%SIG ハンドラを呼び出します。)
</para>
<para>
Note that the default in Perl 5.7.3 and later is to automatically use
the <code>:perlio</code> layer.
</para>
<para>
Perl 5.7.3 以降ではデフォルトでは自動的に <code>:perlio</code> 層が使われることに
注意してください。
</para>
<para>
Note that some networking library functions like gethostbyname() are
known to have their own implementations of timeouts which may conflict
with your timeouts.  If you are having problems with such functions,
you can try using the POSIX sigaction() function, which bypasses the
Perl safe signals (note that this means subjecting yourself to
possible memory corruption, as described above).  Instead of setting
<code>$SIG{ALRM}</code>:
</para>
<para>
gethostbyname() のようなネットワークライブラリ関数は独自のタイムアウト
実装を持っていることが知られているので、あなたのタイムアウトと
競合するかもしれないことに注意してください。
もしこのような関数で問題が起きた場合は、Perl の安全なシグナルを回避する
POSIX sigaction() 関数を試すことができます
(これは上述の、メモリ破壊の可能性があるということに注意してください)。
<code>$SIG{ALRM}</code> をセットする代わりに:
</para>
<verbatim><![CDATA[
local $SIG{ALRM} = sub { die "alarm" };
]]></verbatim>
<para>
try something like the following:
</para>
<para>
以下のようなことを試してみてください:
</para>
<verbatim><![CDATA[
use POSIX qw(SIGALRM);
POSIX::sigaction(SIGALRM,
                 POSIX::SigAction->new(sub { die "alarm" }))
      or die "Error setting SIGALRM handler: $!\n";
]]></verbatim>
<para>
Another way to disable the safe signal behavior locally is to use
the <code>Perl::Unsafe::Signals</code> module from CPAN (which will affect
all signals).
</para>
<para>
安全なシグナルの振る舞いを局所的に無効にするもう一つの方法は
CPAN の <code>Perl::Unsafe::Signals</code> モジュールを使うことです
(全てのシグナルに影響を与えます)。
</para>
</item>
<item><itemtext>Restartable system calls</itemtext>
<para>
(再起動可能なシステムコール)
</para>
<para>
On systems that supported it, older versions of Perl used the
SA_RESTART flag when installing %SIG handlers.  This meant that
restartable system calls would continue rather than returning when
a signal arrived.  In order to deliver deferred signals promptly,
Perl 5.7.3 and later do <emphasis>not</emphasis> use SA_RESTART.  Consequently, 
restartable system calls can fail (with $! set to <code>EINTR</code>) in places
where they previously would have succeeded.
</para>
<para>
これに対応しているシステムでは、より古いバージョンの Perl では %SIG
ハンドラを設定するときに SA_RESTART フラグを使います。
これは、再起動可能なシステムコールではシグナルが配送されたときに
戻るのではなく続行することを意味します。
保留シグナルを素早く配送するために、
Perl 5.7.3 以降では SA_RESTART を <emphasis>使っていません</emphasis>。
結果として、再起動可能なシステムコールは、いままで成功していたところでも
($! に <code>EINTR</code> をセットして) 失敗することがあります。
</para>
<para>
Note that the default <code>:perlio</code> layer will retry <code>read</code>, <code>write</code>
and <code>close</code> as described above and that interrupted <code>wait</code> and 
<code>waitpid</code> calls will always be retried.
</para>
<para>
デフォルトの <code>:perlio</code> 層では <code>read</code>, <code>write</code>, <code>close</code> は上述したように
再試行し、中断された <code>wait</code> と <code>waitpid</code> の呼び出しは常に
再試行されることに注意してください。
</para>
</item>
<item><itemtext>Signals as &quot;faults&quot;</itemtext>
<para>
(「障害」としてのシグナル)
</para>
<para>
Certain signals, e.g. SEGV, ILL, and BUS, are generated as a result of
virtual memory or other &quot;faults&quot;. These are normally fatal and there is
little a Perl-level handler can do with them, so Perl now delivers them
immediately rather than attempting to defer them.
</para>
<para>
SEGV, ILL, BUS のようなシグナルは、仮想メモリやその他の「障害」の結果として
生成されます。
これらは普通致命的で、Perl-レベルのハンドラができることはほとんど
ないので、これらを保留しようとせず、直ちに配送します。
</para>
</item>
<item><itemtext>Signals triggered by operating system state</itemtext>
<para>
(OS の状態によって発生するシグナル)
</para>
<para>
On some operating systems certain signal handlers are supposed to &quot;do
something&quot; before returning. One example can be CHLD or CLD which
indicates a child process has completed. On some operating systems the
signal handler is expected to <code>wait</code> for the completed child
process. On such systems the deferred signal scheme will not work for
those signals (it does not do the <code>wait</code>). Again the failure will
look like a loop as the operating system will re-issue the signal as
there are un-waited-for completed child processes.
</para>
<para>
OS によっては、ある種のシグナルハンドラは返る前に「何かをする」ことに
なっているものもあります。
1 つの例としては、CHLD や CLD は子プロセスが完了したことを示しています。
OS によっては、シグナルハンドルは完了した子プロセスのために
<code>wait</code> することを想定されているものもあります。
このようなシステムでは保留シグナルスキームはこれらのシグナルでは
動作しません(<code>wait</code> しません)。
再び、問題は、wait していない完了した子プロセスがあるかのようにシグナルが
再び発生することでループのように見えます。
</para>
</item>
</list>
<para>
If you want the old signal behaviour back regardless of possible
memory corruption, set the environment variable <code>PERL_SIGNALS</code> to
<code>&quot;unsafe&quot;</code> (a new feature since Perl 5.8.1).
</para>
<para>
もし、メモリ破壊の可能性にもかかわらず、古いシグナルの振る舞いが
ほしいなら、環境変数 <code>PERL_SIGNALS</code> を <code>&quot;unsafe&quot;</code> に設定してください
(Perl 5.8.1 からの新機能です)。
</para>
</sect2>
</sect1>
<sect1>
<title>Using open() for IPC</title>
<para>
(IPC のために open() を使う)
</para>
<para>
Perl's basic open() statement can also be used for unidirectional
interprocess communication by either appending or prepending a pipe
symbol to the second argument to open().  Here's how to start
something up in a child process you intend to write to:
</para>
<para>
Perlの open() 文は、その第二引数でパイプシンボルを前置するか末尾に
付加することによって、一方向のプロセス間通信のために使うことができます。
以下の例は、書き込みを行いたい子プロセスを起動させるやり方です:
</para>
<verbatim><![CDATA[
open(SPOOLER, "| cat -v | lpr -h 2>/dev/null")
		    || die "can't fork: $!";
local $SIG{PIPE} = sub { die "spooler pipe broke" };
print SPOOLER "stuff\n";
close SPOOLER || die "bad spool: $! $?";
]]></verbatim>
<para>
And here's how to start up a child process you intend to read from:
</para>
<para>
そして以下の例はそこから読み込みを行いたい子プロセスを起動する方法です:
</para>
<verbatim><![CDATA[
open(STATUS, "netstat -an 2>&1 |")
		    || die "can't fork: $!";
while (<STATUS>) {
	next if /^(tcp|udp)/;
	print;
}
close STATUS || die "bad netstat: $! $?";
]]></verbatim>
<para>
If one can be sure that a particular program is a Perl script that is
expecting filenames in @ARGV, the clever programmer can write something
like this:
</para>
<para>
特定のプログラムの一つが @ARGV にあるファイル名を期待している
Perl スクリプトであっていいのなら、賢いプログラマは以下のように
書くこともできます:
</para>
<verbatim><![CDATA[
% program f1 "cmd1|" - f2 "cmd2|" f3 < tmpfile
]]></verbatim>
<para>
and irrespective of which shell it's called from, the Perl program will
read from the file <filename>f1</filename>, the process <filename>cmd1</filename>, standard input (<filename>tmpfile</filename>
in this case), the <filename>f2</filename> file, the <filename>cmd2</filename> command, and finally the <filename>f3</filename>
file.  Pretty nifty, eh?
</para>
<para>
そしてそれを呼んだシェルには関係なく、この Perl プログラムは <filename>f1</filename> という
ファイル、<filename>cmd1</filename> というプロセス、標準入力(この例では <filename>tmpfile</filename>)、
<filename>f2</filename> というファイル、<filename>cmd2</filename> というコマンド、<filename>f3</filename> というファイルから
読み込みを行います。
すごくいいよね?
</para>
<para>
You might notice that you could use backticks for much the
same effect as opening a pipe for reading:
</para>
<para>
読み込みのためのパイプを開くために、逆クォートを使って同じことが
できることに気がつくかもしれません:
</para>
<verbatim><![CDATA[
print grep { !/^(tcp|udp)/ } `netstat -an 2>&1`;
die "bad netstat" if $?;
]]></verbatim>
<para>
While this is true on the surface, it's much more efficient to process the
file one line or record at a time because then you don't have to read the
whole thing into memory at once.  It also gives you finer control of the
whole process, letting you to kill off the child process early if you'd
like.
</para>
<para>
その推測は表面的には正しいことのように見えるかも知れませんが、一度に
メモリにすべてを読み込む必要がないので、一度にファイルの一行や
一レコードを処理するには(最初の例のほうが)より効率的なのです。
同様にプロセス全体の制御を与えるので、あなたが望めば早い時期に
子プロセスを kill することができます。
</para>
<para>
Be careful to check both the open() and the close() return values.  If
you're <emphasis>writing</emphasis> to a pipe, you should also trap SIGPIPE.  Otherwise,
think of what happens when you start up a pipe to a command that doesn't
exist: the open() will in all likelihood succeed (it only reflects the
fork()'s success), but then your output will fail--spectacularly.  Perl
can't know whether the command worked because your command is actually
running in a separate process whose exec() might have failed.  Therefore,
while readers of bogus commands return just a quick end of file, writers
to bogus command will trigger a signal they'd better be prepared to
handle.  Consider:
</para>
<para>
open() と close() の戻り値をチェックするときは注意してください。
パイプに対して <strong>書き込み</strong> をしたのなら、SIGPIPE をトラップすべきです。
そうしなければ、存在しないコマンドに対するパイプを起動したときに
起こることについて考え込むことになるでしょう: open() はほとんどの
場合成功すると見込まれるでしょうが(これは fork() の成功だけを
反映します)、あなたの出力はその後で壮観に(spectacularly)失敗するでしょう。
コマンドは、実際には exec() が失敗している別のプロセスで
実行されているので、Perl はコマンドがうまく動いているかどうかを
知ることはできません。
したがって、偽のコマンド(bugus command)の読み手はすぐに end of file を
受け取り、偽のコマンドに対する書き手は事前に取り扱っておくべき
シグナルを発生させるでしょう。
以下の例を考えてみましょう:
</para>
<verbatim><![CDATA[
open(FH, "|bogus")	or die "can't fork: $!";
print FH "bang\n"	or die "can't write: $!";
close FH		or die "can't close: $!";
]]></verbatim>
<para>
That won't blow up until the close, and it will blow up with a SIGPIPE.
To catch it, you could use this:
</para>
<para>
これはクローズするまで爆発せず、爆発すると SIGPIPE を発生させます。
これを捕らえるには、以下のようにします:
</para>
<verbatim><![CDATA[
$SIG{PIPE} = 'IGNORE';
open(FH, "|bogus")  or die "can't fork: $!";
print FH "bang\n"   or die "can't write: $!";
close FH            or die "can't close: status=$?";
]]></verbatim>
<sect2>
<title>Filehandles</title>
<para>
(ファイルハンドル)
</para>
<para>
Both the main process and any child processes it forks share the same
STDIN, STDOUT, and STDERR filehandles.  If both processes try to access
them at once, strange things can happen.  You may also want to close
or reopen the filehandles for the child.  You can get around this by
opening your pipe with open(), but on some systems this means that the
child process cannot outlive the parent.
</para>
<para>
メインプロセスと子プロセスで同じファイルハンドル STDIN, STDOUT, 
STDERR を共有します。
両方のプロセスが同時にそれらのハンドルにアクセスしようとした場合、
おかしな事が発生する可能性があります。
あなたは子プロセスのためにファイルハンドルのクローズと再オープンにしたいと
考えるかもしれません。
これは、open() を使ってパイプをオープンすることによって対処することが
できますが、一部のシステムにおいては子プロセスはその親プロセスよりも
長生きすることはできません。
</para>
</sect2>
<sect2>
<title>Background Processes</title>
<para>
(バックグラウンドプロセス)
</para>
<para>
You can run a command in the background with:
</para>
<para>
以下のようにしてコマンドをバックグラウンドで実行することができます:
</para>
<verbatim><![CDATA[
system("cmd &");
]]></verbatim>
<para>
The command's STDOUT and STDERR (and possibly STDIN, depending on your
shell) will be the same as the parent's.  You won't need to catch
SIGCHLD because of the double-fork taking place (see below for more
details).
</para>
<para>
コマンドの STDOUT と STDERR (とあなたの使うシェルによっては STDIN も)は
その親プロセスのものと同一になります。
double-fork の実行によって、SIGCHLD を捕捉する必要はありません
(詳しくは後述します)。
</para>
</sect2>
<sect2>
<title>Complete Dissociation of Child from Parent</title>
<para>
(子の親からの完全な分離)
</para>
<para>
In some cases (starting server processes, for instance) you'll want to
completely dissociate the child process from the parent.  This is
often called daemonization.  A well behaved daemon will also chdir()
to the root directory (so it doesn't prevent unmounting the filesystem
containing the directory from which it was launched) and redirect its
standard file descriptors from and to <filename>/dev/null</filename> (so that random
output doesn't wind up on the user's terminal).
</para>
<para>
一部の場合(例えばサーバープロセスのスタート)、親プロセスと子プロセスを
完全に無関係のものにしたいことがあるでしょう。
これはしばしばデーモン化と呼ばれます。
礼儀正しいデーモンはルートディレクトリに chdir() (そのため実行ファイルの
あったディレクトリを含むファイルシステムのアンマウントを邪魔することが
ありません)し、その標準ファイル記述子を <filename>/fdev/null</filename> に
リダイレクトします。
</para>
<verbatim><![CDATA[
use POSIX 'setsid';
]]></verbatim>
<verbatim><![CDATA[
sub daemonize {
	chdir '/'		or die "Can't chdir to /: $!";
	open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
	open STDOUT, '>/dev/null'
				or die "Can't write to /dev/null: $!";
	defined(my $pid = fork)	or die "Can't fork: $!";
	exit if $pid;
	setsid			or die "Can't start a new session: $!";
	open STDERR, '>&STDOUT'	or die "Can't dup stdout: $!";
}
]]></verbatim>
<para>
The fork() has to come before the setsid() to ensure that you aren't a
process group leader (the setsid() will fail if you are).  If your
system doesn't have the setsid() function, open <filename>/dev/tty</filename> and use the
<code>TIOCNOTTY</code> ioctl() on it instead.  See tty(4) for details.
</para>
<para>
fork() はプロセスグループリーダー(もしそうなら setsid() は
失敗するでしょう)でないことを保証するために
setsid() の前になければなりません。
あなたの使っているシステムが sesid() 関数を持っていないのであれば、
<filename>/dev/tty</filename> をオープンして <code>TIONCNOTTY</code> ioctl() を代わりに使います。
詳しくは tty(4) を参照してください。
</para>
<para>
Non-Unix users should check their Your_OS::Process module for other
solutions.
</para>
<para>
非 UNIX ユーザーはあなたの OS の Process モジュールをチェックして
他の解決策を探しましょう。
</para>
</sect2>
<sect2>
<title>Safe Pipe Opens</title>
<para>
(安全なパイプオープン)
</para>
<para>
Another interesting approach to IPC is making your single program go
multiprocess and communicate between (or even amongst) yourselves.  The
open() function will accept a file argument of either <code>&quot;-|&quot;</code> or <code>&quot;|-&quot;</code>
to do a very interesting thing: it forks a child connected to the
filehandle you've opened.  The child is running the same program as the
parent.  This is useful for safely opening a file when running under an
assumed UID or GID, for example.  If you open a pipe <emphasis>to</emphasis> minus, you can
write to the filehandle you opened and your kid will find it in his
STDIN.  If you open a pipe <emphasis>from</emphasis> minus, you can read from the filehandle
you opened whatever your kid writes to his STDOUT.
</para>
<para>
もう一つの、IPC のための興味深いアプローチはマルチプロセスになって
それぞれの間で通信を行う単一のプログラムを作るというものです。
open() 関数は <code>&quot;-|&quot;</code> や <code>&quot;|-&quot;</code> といったファイル引数を非常に
おもしろいことを行うために受け付けます: これはあなたがオープンした
ファイルハンドのための子プロセスをfork()するのです。
その子プロセスは親プロセスと同じプログラムを実行します。
これはたとえば、仮定された UID や GID のもとで実行する際に安全にファイルを
オープンするのに便利です。
マイナスに<strong>対する</strong>パイプをオープンすると、あなたはオープンした
ファイルハンドルに書き込みができ、子プロセスはそれを自分の STDIN に
見いだします。
マイナス <strong>からの</strong> パイプをオープンした場合には、子プロセスがその
STDOUT に書き出したものがオープンしたファイルハンドルから
読みだしすることができるのです。
</para>
<verbatim><![CDATA[
use English '-no_match_vars';
my $sleep_count = 0;
]]></verbatim>
<verbatim><![CDATA[
do {
	$pid = open(KID_TO_WRITE, "|-");
	unless (defined $pid) {
	    warn "cannot fork: $!";
	    die "bailing out" if $sleep_count++ > 6;
	    sleep 10;
	}
} until defined $pid;
]]></verbatim>
<verbatim><![CDATA[
if ($pid) {  # parent
	print KID_TO_WRITE @some_data;
	close(KID_TO_WRITE) || warn "kid exited $?";
} else {     # child
	($EUID, $EGID) = ($UID, $GID); # suid progs only
	open (FILE, "> /safe/file")
	    || die "can't open /safe/file: $!";
	while (<STDIN>) {
	    print FILE; # child's STDIN is parent's KID
	}
	exit;  # don't forget this
}
]]></verbatim>
<para>
Another common use for this construct is when you need to execute
something without the shell's interference.  With system(), it's
straightforward, but you can't use a pipe open or backticks safely.
That's because there's no way to stop the shell from getting its hands on
your arguments.   Instead, use lower-level control to call exec() directly.
</para>
<para>
このやり方を行うもう一つの一般的な例は、シェルのインターフェース抜きで
何かを実行する必要があるときでしょう。
system() を使うとそれは直接的なものですが、パイプのオープンや
バッククォートを安全に使うことができません。
これはシェルがあなたの引数を触るのを止める方法がないからです。
代わりに、exec() を直接呼び出す低レベルな制御を使います。
</para>
<para>
Here's a safe backtick or pipe open for read:
</para>
<para>
以下の例は、読み込み用の安全なバッククォートやパイプオープンのものです:
</para>
<verbatim><![CDATA[
# add error processing as above
$pid = open(KID_TO_READ, "-|");
]]></verbatim>
<verbatim><![CDATA[
if ($pid) {   # parent
	while (<KID_TO_READ>) {
	    # do something interesting
	}
	close(KID_TO_READ) || warn "kid exited $?";
]]></verbatim>
<verbatim><![CDATA[
} else {      # child
	($EUID, $EGID) = ($UID, $GID); # suid only
	exec($program, @options, @args)
	    || die "can't exec program: $!";
	# NOTREACHED
}
]]></verbatim>
<para>
And here's a safe pipe open for writing:
</para>
<para>
そして以下の例は、書き込み用の安全なバッククォートや
パイプオープンのものです:
</para>
<verbatim><![CDATA[
# add error processing as above
$pid = open(KID_TO_WRITE, "|-");
$SIG{PIPE} = sub { die "whoops, $program pipe broke" };
]]></verbatim>
<verbatim><![CDATA[
if ($pid) {  # parent
	for (@data) {
	    print KID_TO_WRITE;
	}
	close(KID_TO_WRITE) || warn "kid exited $?";
]]></verbatim>
<verbatim><![CDATA[
} else {     # child
	($EUID, $EGID) = ($UID, $GID);
	exec($program, @options, @args)
	    || die "can't exec program: $!";
	# NOTREACHED
}
]]></verbatim>
<para>
Since Perl 5.8.0, you can also use the list form of <code>open</code> for pipes :
the syntax
</para>
<para>
Perl 5.8.0 から パイプのために <code>open</code> のリスト形式を使えます;
以下のような文法で:
</para>
<verbatim><![CDATA[
open KID_PS, "-|", "ps", "aux" or die $!;
]]></verbatim>
<para>
forks the ps(1) command (without spawning a shell, as there are more than
three arguments to open()), and reads its standard output via the
<code>KID_PS</code> filehandle.  The corresponding syntax to write to command
pipes (with <code>&quot;|-&quot;</code> in place of <code>&quot;-|&quot;</code>) is also implemented.
</para>
<para>
ps(1) コマンドを fork し(3 以上の引数の open() のようにシェルを
起動することなく)、<code>KID_PS</code> ファイルハンドル経由で標準出力を読み込みます。
コマンドパイプに書き込むための対応する文法 (<code>&quot;-|&quot;</code> の代わりに
<code>&quot;|-&quot;</code>) も実装されています。
</para>
<para>
Note that these operations are full Unix forks, which means they may not be
correctly implemented on alien systems.  Additionally, these are not true
multithreading.  If you'd like to learn more about threading, see the
<filename>modules</filename> file mentioned below in the SEE ALSO section.
</para>
<para>
これらの操作は UNIX の fork でいっぱいで、その fork が他のシステムでは
適切に実装されていない可能性があるのだということに注意してください。
それに加えて、これらのやり方は本当のマルチスレッドではありません。
スレッドについてより学びたいのであれば、後述する SEE ALSO の章で
言及されている <filename>modules</filename> ファイルを参照してください。
</para>
</sect2>
<sect2>
<title>Bidirectional Communication with Another Process</title>
<para>
(他のプロセスとの双方向通信)
</para>
<para>
While this works reasonably well for unidirectional communication, what
about bidirectional communication?  The obvious thing you'd like to do
doesn't actually work:
</para>
<para>
これは、片方向通信に対してうまく働きます。
では双方向通信は?
あなたがやりたいだろうとまず考えることは、実際にはうまくいきません:
</para>
<verbatim><![CDATA[
open(PROG_FOR_READING_AND_WRITING, "| some program |")
]]></verbatim>
<para>
and if you forget to use the <code>use warnings</code> pragma or the <strong>-w</strong> flag,
then you'll miss out entirely on the diagnostic message:
</para>
<para>
この状態で <code>use warnings</code> プラグマか <strong>-w</strong> フラグを使うのを忘れてしまうと、
以下のような診断メッセージを得る機会を逃すことになるでしょう。
</para>
<verbatim><![CDATA[
Can't do bidirectional pipe at -e line 1.
]]></verbatim>
<para>
If you really want to, you can use the standard open2() library function
to catch both ends.  There's also an open3() for tridirectional I/O so you
can also catch your child's STDERR, but doing so would then require an
awkward select() loop and wouldn't allow you to use normal Perl input
operations.
</para>
<para>
本当にこういったことをしたいのなら、標準の open2() というライブラリ関数を
使うことで、パイプの両方の端点を得ることができます。
三方向(tridirectional)の入出力のための open3() もあるので、子プロセスの
STDERR を捕捉することもできるのですが、そのためには 不格好な
select() ループが必要となり、Perl の通常の入力操作を行うことが
できません。
</para>
<para>
If you look at its source, you'll see that open2() uses low-level
primitives like Unix pipe() and exec() calls to create all the connections.
While it might have been slightly more efficient by using socketpair(), it
would have then been even less portable than it already is.  The open2()
and open3() functions are  unlikely to work anywhere except on a Unix
system or some other one purporting to be POSIX compliant.
</para>
<para>
open2() のソースを見てみれば、それが全ての接続を生成するために
UNIX での pipe() と exec() のような低水準のプリミティブを使っていることに
気がつくでしょう。
これは socketpair() を使うより効率が格段に良いのですが、移植性という面では
見劣りします。
open2() と open3() は UNIX システムやその他の POSIX に従ったシステムを
除いてはおそらく動作しないでしょう。
</para>
<para>
Here's an example of using open2():
</para>
<para>
以下は open2() を使った例です:
</para>
<verbatim><![CDATA[
use FileHandle;
use IPC::Open2;
$pid = open2(*Reader, *Writer, "cat -u -n" );
print Writer "stuff\n";
$got = <Reader>;
]]></verbatim>
<para>
The problem with this is that Unix buffering is really going to
ruin your day.  Even though your <code>Writer</code> filehandle is auto-flushed,
and the process on the other end will get your data in a timely manner,
you can't usually do anything to force it to give it back to you
in a similarly quick fashion.  In this case, we could, because we
gave <emphasis>cat</emphasis> a <strong>-u</strong> flag to make it unbuffered.  But very few Unix
commands are designed to operate over pipes, so this seldom works
unless you yourself wrote the program on the other end of the
double-ended pipe.
</para>
<para>
これに関する問題は、UNIX のバッファリングが実際にはある時点に至るまで
留まるということにあります。
ファイルハンドル <code>Writer</code> を自動フラッシュにしたとしても、もう一方の
端点にあるプロセスは送られたデータを適当なタイミングで
受け取ることになります; 一般的に云って、全てのことに関して即座に
反応するように強制することはできません。
上記の例では、バッファリングをしないようにするために <strong>-u</strong> フラグを
<emphasis>cat</emphasis> に与えることができたのでそれができました。
パイプ越しに使われることを想定して設計された UNIX コマンドは非常に
少数なので、この例のようなことは、両端のあるパイプのもう一方の端点にある
プログラムを自分自身で書かない限りはほとんどできないのです。
</para>
<para>
A solution to this is the nonstandard <filename>Comm.pl</filename> library.  It uses
pseudo-ttys to make your program behave more reasonably:
</para>
<para>
この解決策は非標準の <filename>Comm.pl</filename> ライブラリです。
これはあなたのプログラムをより信頼性のあるものとするために
擬似 TTY を使います:
</para>
<verbatim><![CDATA[
require 'Comm.pl';
$ph = open_proc('cat -n');
for (1..10) {
	print $ph "a line\n";
	print "got back ", scalar <$ph>;
}
]]></verbatim>
<para>
This way you don't have to have control over the source code of the
program you're using.  The <filename>Comm</filename> library also has expect()
and interact() functions.  Find the library (and we hope its
successor <filename>IPC::Chat</filename>) at your nearest CPAN archive as detailed
in the SEE ALSO section below.
</para>
<para>
このやり方では、あなたが使おうとしているプログラムのソースコードを
いじくりまわすような必要はありません。
<filename>Comm</filename> ライブラリには他にも expect() や interact() といった関数もあります。
後述する SEE ALSO の章にある説明の最寄りの CPAN で、このライブラリ(と
その後継者と期待している <filename>IPC::Chat</filename>) を見つけてください。
</para>
<para>
The newer Expect.pm module from CPAN also addresses this kind of thing.
This module requires two other modules from CPAN: IO::Pty and IO::Stty.
It sets up a pseudo-terminal to interact with programs that insist on
using talking to the terminal device driver.  If your system is
amongst those supported, this may be your best bet.
</para>
<para>
CPAN にあるより新しいモジュール Expect.pm はこの問題に対処するものです。
このモジュールは他に IO:Pty、IO::Stty という二つの CPAN モジュールを
必要とします。
このモジュールは、ターミナルデバイスドライバと応答するような
プログラムと対話するために、擬似端末をセットアップします。
もしあなたの使っているシステムがこういったものをサポートしているのであれば、
そちらを使った方が良いでしょう。
</para>
</sect2>
<sect2>
<title>Bidirectional Communication with Yourself</title>
<para>
(自分で双方向通信する)
</para>
<para>
If you want, you may make low-level pipe() and fork()
to stitch this together by hand.  This example only
talks to itself, but you could reopen the appropriate
handles to STDIN and STDOUT and call other processes.
</para>
<para>
もし望むのなら、低レベルなシステムコール pipe() や fork() を
手作業で行うために使うことができます。
以下の例は単に説明のためのものですが、STDIN や STDOOUT に対する
適切なハンドルを再オープンすることができ、さらに別のプロセスを
呼び出すことができます。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
# pipe1 - bidirectional communication using two pipe pairs
#         designed for the socketpair-challenged
use IO::Handle;	# thousands of lines just for autoflush :-(
pipe(PARENT_RDR, CHILD_WTR);		# XXX: failure?
pipe(CHILD_RDR,  PARENT_WTR);		# XXX: failure?
CHILD_WTR->autoflush(1);
PARENT_WTR->autoflush(1);
]]></verbatim>
<verbatim><![CDATA[
if ($pid = fork) {
	close PARENT_RDR; close PARENT_WTR;
	print CHILD_WTR "Parent Pid $$ is sending this\n";
	chomp($line = <CHILD_RDR>);
	print "Parent Pid $$ just read this: `$line'\n";
	close CHILD_RDR; close CHILD_WTR;
	waitpid($pid,0);
} else {
	die "cannot fork: $!" unless defined $pid;
	close CHILD_RDR; close CHILD_WTR;
	chomp($line = <PARENT_RDR>);
	print "Child Pid $$ just read this: `$line'\n";
	print PARENT_WTR "Child Pid $$ is sending this\n";
	close PARENT_RDR; close PARENT_WTR;
	exit;
}
]]></verbatim>
<para>
But you don't actually have to make two pipe calls.  If you
have the socketpair() system call, it will do this all for you.
</para>
<para>
しかし実際に二つの pipe の呼び出しを行う必要はありません。
あなたの使っているシステムが socketpair() システムコールを
サポートしていれば、それがあなたの代わりに作業を行ってくれます。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
# pipe2 - bidirectional communication using socketpair
#   "the best ones always go both ways"
]]></verbatim>
<verbatim><![CDATA[
use Socket;
use IO::Handle;	# thousands of lines just for autoflush :-(
# We say AF_UNIX because although *_LOCAL is the
# POSIX 1003.1g form of the constant, many machines
# still don't have it.
socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
				or  die "socketpair: $!";
]]></verbatim>
<verbatim><![CDATA[
CHILD->autoflush(1);
PARENT->autoflush(1);
]]></verbatim>
<verbatim><![CDATA[
if ($pid = fork) {
	close PARENT;
	print CHILD "Parent Pid $$ is sending this\n";
	chomp($line = <CHILD>);
	print "Parent Pid $$ just read this: `$line'\n";
	close CHILD;
	waitpid($pid,0);
} else {
	die "cannot fork: $!" unless defined $pid;
	close CHILD;
	chomp($line = <PARENT>);
	print "Child Pid $$ just read this: `$line'\n";
	print PARENT "Child Pid $$ is sending this\n";
	close PARENT;
	exit;
}
]]></verbatim>
</sect2>
</sect1>
<sect1>
<title>Sockets: Client/Server Communication</title>
<para>
(ソケット: クライアント/サーバー通信)
</para>
<para>
While not limited to Unix-derived operating systems (e.g., WinSock on PCs
provides socket support, as do some VMS libraries), you may not have
sockets on your system, in which case this section probably isn't going to do
you much good.  With sockets, you can do both virtual circuits (i.e., TCP
streams) and datagrams (i.e., UDP packets).  You may be able to do even more
depending on your system.
</para>
<para>
UNIX から派生したオペレーティングシステムに限定されることはない
(例えば PC では WinSock が(幾つかの VMS ライブラリのように)
ソケットサポートを提供しています)にも関らず、あなたが使っている
システムではソケットが使えないかもしれません。
その場合、この項に書かれていることはあなたの役には立たないでしょう。
ソケットを使えば、仮想回路(つまり TCP ストリーム)やデータグラム(UDP
パケット)の両方が可能になります。
使っているシステムに、より一層依存することになるかもしれません。
</para>
<para>
The Perl function calls for dealing with sockets have the same names as
the corresponding system calls in C, but their arguments tend to differ
for two reasons: first, Perl filehandles work differently than C file
descriptors.  Second, Perl already knows the length of its strings, so you
don't need to pass that information.
</para>
<para>
ソケットを扱うための Perl の関数呼び出しは対応する C でのシステムコールと
同じ名前を持っています。
しかし、引数に関しては二つの理由によって異なるものとなっています:
第一に、Perl のファイルハンドルは C のファイル記述子とは
異なる働きをするものである。
第二に、Perl はすでに文字列の長さを知っているので、その情報を渡す
必要がない、というものです。
</para>
<para>
One of the major problems with old socket code in Perl was that it used
hard-coded values for some of the constants, which severely hurt
portability.  If you ever see code that does anything like explicitly
setting <code>$AF_INET = 2</code>, you know you're in for big trouble:  An
immeasurably superior approach is to use the <code>Socket</code> module, which more
reliably grants access to various constants and functions you'll need.
</para>
<para>
Perl における古いソケットプログラムに関連する大きな問題とは、一部の
定数の値として(著しく移植性を損なってしまう)ハードコードされたものが
使われていたということです。
<code>$AF_INET = 2</code> のように陽に何かを設定しているプログラムを見た事があれば、
あなたはそれが大きなトラブルになるということを知っているでしょう:
より優れたやり方は <code>Socket</code> モジュールを使うというものです。
これは、あなたが必要とするであろう様々な定数や関数に対するより信頼の置ける
アクセスを提供します。
</para>
<para>
If you're not writing a server/client for an existing protocol like
NNTP or SMTP, you should give some thought to how your server will
know when the client has finished talking, and vice-versa.  Most
protocols are based on one-line messages and responses (so one party
knows the other has finished when a &quot;\n&quot; is received) or multi-line
messages and responses that end with a period on an empty line
(&quot;\n.\n&quot; terminates a message/response).
</para>
<para>
NNTP や SMTP のように既に存在しているプロトコルのためにサーバー/
クライアントを書くのでなければ、あなたの作るサーバーがどのようにして
クライアントが通話を終えたときを知ったり、その反対のことを
知るのかということを考えておくべきでしょう。
ほとんどのプロトコルは一行のメッセージと返事(片方のプロセスは、
“\n”を受け取ったときにもう一つのプロセスでの処理が終了したことを
知ります)か、空行に置かれたピリオドで終端される(&quot;\n.\n&quot; がメッセージ/返事を
終端する)複数行メッセージと返事に基づいています。
</para>
<sect2>
<title>Internet Line Terminators</title>
<para>
(インターネットの行終端子)
</para>
<para>
The Internet line terminator is &quot;\015\012&quot;.  Under ASCII variants of
Unix, that could usually be written as &quot;\r\n&quot;, but under other systems,
&quot;\r\n&quot; might at times be &quot;\015\015\012&quot;, &quot;\012\012\015&quot;, or something
completely different.  The standards specify writing &quot;\015\012&quot; to be
conformant (be strict in what you provide), but they also recommend
accepting a lone &quot;\012&quot; on input (but be lenient in what you require).
We haven't always been very good about that in the code in this manpage,
but unless you're on a Mac, you'll probably be ok.
</para>
<para>
インターネットでの行終端子は &quot;\015\012&quot; です。
UNIX で使われる ASCII のバリエーションでは、通常は“\r\n”のように
記述しますが、他のシステムでは、“\r\n”は&quot;\015\015\012&quot; だったり、
&quot;\012\012\015&quot; だったりあるいはまったく違うものであったりします。
“\015\012”は標準的な書き方(あなたが書いたままのものになります)ですが、
入力中にある独立した“\012”を受け付けることも推奨されています(が、
あなたが要求するときにはそれを穏やかなものにしましょう)。
このマニュアルページにあるプログラムについて、常に最善のものを
使ってはいませんが、あなたが Mac を使っているのでなければ
気にすることはないでしょう。
</para>
</sect2>
<sect2>
<title>Internet TCP Clients and Servers</title>
<para>
(インターネットの TCP クライアントとサーバー)
</para>
<para>
Use Internet-domain sockets when you want to do client-server
communication that might extend to machines outside of your own system.
</para>
<para>
自分の使っているシステムの外側にあるもマシンにまでクライアント-
サーバーの通信を広げたい場合には Internet-domain ソケットを使います。
</para>
<para>
Here's a sample TCP client using Internet-domain sockets:
</para>
<para>
以下のプログラムは、Internet-domain ソケットを使った TCP クライアントの
例です:
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use strict;
use Socket;
my ($remote,$port, $iaddr, $paddr, $proto, $line);
]]></verbatim>
<verbatim><![CDATA[
$remote  = shift || 'localhost';
$port    = shift || 2345;  # random port
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr   = inet_aton($remote) 		|| die "no host: $remote";
$paddr   = sockaddr_in($port, $iaddr);
]]></verbatim>
<verbatim><![CDATA[
$proto   = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto)	|| die "socket: $!";
connect(SOCK, $paddr)    || die "connect: $!";
while (defined($line = <SOCK>)) {
	print $line;
}
]]></verbatim>
<verbatim><![CDATA[
close (SOCK)	    || die "close: $!";
exit;
]]></verbatim>
<para>
And here's a corresponding server to go along with it.  We'll
leave the address as INADDR_ANY so that the kernel can choose
the appropriate interface on multihomed hosts.  If you want sit
on a particular interface (like the external side of a gateway
or firewall machine), you should fill this in with your real address
instead.
</para>
<para>
そして以下に挙げるのが上記のクライアントと対応するサーバーです。
ここではアドレスを INADDR_ANY としているので、カーネルは multihomed
hosts 上の適切なインターフェースを選択することができます。
(ファイヤーウォールやゲイトウェイの外側のように)特定のインターフェースを
使いたいのであれば、この部分を、自分の使いたい本当のアドレスで
埋めるようにすべきです。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -Tw
use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket;
use Carp;
my $EOL = "\015\012";
]]></verbatim>
<verbatim><![CDATA[
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
]]></verbatim>
<verbatim><![CDATA[
my $port = shift || 2345;
my $proto = getprotobyname('tcp');
]]></verbatim>
<verbatim><![CDATA[
($port) = $port =~ /^(\d+)$/                        or die "invalid port";
]]></verbatim>
<verbatim><![CDATA[
socket(Server, PF_INET, SOCK_STREAM, $proto)	|| die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
					pack("l", 1)) 	|| die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY))	|| die "bind: $!";
listen(Server,SOMAXCONN) 				|| die "listen: $!";
]]></verbatim>
<verbatim><![CDATA[
logmsg "server started on port $port";
]]></verbatim>
<verbatim><![CDATA[
my $paddr;
]]></verbatim>
<verbatim><![CDATA[
$SIG{CHLD} = \&REAPER;
]]></verbatim>
<verbatim><![CDATA[
for ( ; $paddr = accept(Client,Server); close Client) {
	my($port,$iaddr) = sockaddr_in($paddr);
	my $name = gethostbyaddr($iaddr,AF_INET);
]]></verbatim>
<verbatim><![CDATA[
logmsg "connection from $name [",
	inet_ntoa($iaddr), "]
	at port $port";
]]></verbatim>
<verbatim><![CDATA[
print Client "Hello there, $name, it's now ",
		scalar localtime, $EOL;
    }
]]></verbatim>
<para>
And here's a multithreaded version.  It's multithreaded in that
like most typical servers, it spawns (forks) a slave server to
handle the client request so that the master server can quickly
go back to service a new client.
</para>
<para>
以下の例はマルチスレッドバージョンです。
これはほとんどの典型的なサーバーがそうであるようにマルチスレッドに
なっていて、クライアントのリクエストを処理するためにスレイブサーバーを
spawn(fork) します。
このため、マスターサーバーは新しいクライアントに対してサービスするために
即座に復帰できます。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -Tw
use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket;
use Carp;
my $EOL = "\015\012";
]]></verbatim>
<verbatim><![CDATA[
sub spawn;  # forward declaration
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
]]></verbatim>
<verbatim><![CDATA[
my $port = shift || 2345;
my $proto = getprotobyname('tcp');
]]></verbatim>
<verbatim><![CDATA[
($port) = $port =~ /^(\d+)$/                        or die "invalid port";
]]></verbatim>
<verbatim><![CDATA[
socket(Server, PF_INET, SOCK_STREAM, $proto)	|| die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
					pack("l", 1)) 	|| die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY))	|| die "bind: $!";
listen(Server,SOMAXCONN) 				|| die "listen: $!";
]]></verbatim>
<verbatim><![CDATA[
logmsg "server started on port $port";
]]></verbatim>
<verbatim><![CDATA[
my $waitedpid = 0;
my $paddr;
]]></verbatim>
<verbatim><![CDATA[
use POSIX ":sys_wait_h";
use Errno;
]]></verbatim>
<verbatim><![CDATA[
sub REAPER {
    local $!;   # don't let waitpid() overwrite current error
    while ((my $pid = waitpid(-1,WNOHANG)) > 0 && WIFEXITED($?)) {
        logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
    }
    $SIG{CHLD} = \&REAPER;  # loathe sysV
}
]]></verbatim>
<verbatim><![CDATA[
$SIG{CHLD} = \&REAPER;
]]></verbatim>
<verbatim><![CDATA[
while(1) {
    $paddr = accept(Client, Server) || do {
        # try again if accept() returned because a signal was received
        next if $!{EINTR};
        die "accept: $!";
    };
    my ($port, $iaddr) = sockaddr_in($paddr);
    my $name = gethostbyaddr($iaddr, AF_INET);
]]></verbatim>
<verbatim><![CDATA[
logmsg "connection from $name [",
       inet_ntoa($iaddr),
       "] at port $port";
]]></verbatim>
<verbatim><![CDATA[
spawn sub {
    $|=1;
    print "Hello there, $name, it's now ", scalar localtime, $EOL;
    exec '/usr/games/fortune'       # XXX: `wrong' line terminators
        or confess "can't exec fortune: $!";
};
close Client;
    }
]]></verbatim>
<verbatim><![CDATA[
sub spawn {
    my $coderef = shift;
]]></verbatim>
<verbatim><![CDATA[
unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
    confess "usage: spawn CODEREF";
}
]]></verbatim>
<verbatim><![CDATA[
my $pid;
if (! defined($pid = fork)) {
    logmsg "cannot fork: $!";
    return;
} 
elsif ($pid) {
    logmsg "begat $pid";
    return; # I'm the parent
}
# else I'm the child -- go spawn
]]></verbatim>
<verbatim><![CDATA[
open(STDIN,  "<&Client")   || die "can't dup client to stdin";
open(STDOUT, ">&Client")   || die "can't dup client to stdout";
## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
exit &$coderef();
    }
]]></verbatim>
<para>
This server takes the trouble to clone off a child version via fork()
for each incoming request.  That way it can handle many requests at
once, which you might not always want.  Even if you don't fork(), the
listen() will allow that many pending connections.  Forking servers
have to be particularly careful about cleaning up their dead children
(called &quot;zombies&quot; in Unix parlance), because otherwise you'll quickly
fill up your process table.  The REAPER subroutine is used here to
call waitpid() for any child processes that have finished, thereby
ensuring that they terminate cleanly and don't join the ranks of the
living dead.
</para>
<para>
このサーバーはリクエストがくる度に fork() を使って子バージョンの複製を
行うので、問題を取り除きます。
このやり方は、あなたが常に望んではいないかもしれませんが、一度に多くの
リクエストを処理することができます。
fork()を使わなかったとしても、listen() は多くの一時停止した
コネクション(pending connections) を扱えます。
サーバーを fork するので、死んだ子供(UNIX 世界ではゾンビと呼ばれるもの)の
後始末に関して注意深くする必要があります。
なぜなら、そうしなければプロセステーブルがすぐに埋めつくされることに
なるからです。
ここで REAPER サブルーチンは終了した全ての子プロセスのために
waitpid() を呼び出すために使われ、従って子プロセスがきれいに終了し、
ゾンビの集団に加わらないことを確実にします。
</para>
<para>
Within the while loop we call accept() and check to see if it returns
a false value.  This would normally indicate a system error that needs
to be reported.  However the introduction of safe signals (see
<link xref='Deferred Signals (Safe Signals)'>/Deferred Signals (Safe Signals)</link> above) in Perl 5.7.3 means that
accept() may also be interrupted when the process receives a signal.
This typically happens when one of the forked sub-processes exits and
notifies the parent process with a CHLD signal.
</para>
<para>
while ループの中で accept() を呼び出して、偽の値を返すかどうかを
チェックします。
これは普通報告する必要のあるシステムエラーを示しています。
しかし、Perl 5.7.3 で導入された安全なシグナル(上述の
<link xref='Deferred Signals (Safe Signals)'>/Deferred Signals (Safe Signals)</link> を参照してください) の導入は、
プロセスがシグナルを受信した場合にも accept() が中断されうることを
意味します。
これは典型的には、fork した子プロセスの一つが終了して、親プロセスに
CHLD シグナルを通知した場合に起こります。
</para>
<para>
If accept() is interrupted by a signal then $! will be set to EINTR.
If this happens then we can safely continue to the next iteration of
the loop and another call to accept().  It is important that your
signal handling code doesn't modify the value of $! or this test will
most likely fail.  In the REAPER subroutine we create a local version
of $! before calling waitpid().  When waitpid() sets $! to ECHILD (as
it inevitably does when it has no more children waiting), it will
update the local copy leaving the original unchanged.
</para>
<para>
もし accept() がシグナルで中断されると、$! に EINTR がセットされます。
これが起きた場合、次の反復と次の accept() の呼び出しを安全に続行できます。
シグナルハンドラコードが $! を変更しないようにしないと、このテストが
ほぼ確実に失敗するということは重要です。
REAPER サブルーチンでは waitpid() を呼び出す前にローカル版の $! を
作成しています。
waitpid() が $! に ECHILD をセットすると(これはどの子プロセスも待っていない
ときに必然的に発生します)、これは元の値を変更せず、ローカルなコピーを
更新します。
</para>
<para>
We suggest that you use the <strong>-T</strong> flag to use taint checking (see <link xref='perlsec'>perlsec</link>)
even if we aren't running setuid or setgid.  This is always a good idea
for servers and other programs run on behalf of someone else (like CGI
scripts), because it lessens the chances that people from the outside will
be able to compromise your system.
</para>
<para>
setuid やsetgid をされていない状態で実行されているとしても、汚染検査
(<link xref='perlsec'>perlsec</link> を参照)をするために <strong>-T</strong> フラグを使うことをお勧めします。
これは、サーバーやその他の他の誰かのために実行される(CGI スクリプトのような)
プログラムに対しては常に良い考えになります。
なぜならそうすることによって、外部の人間があなたのシステムに入ってくる
ことができる可能性を減らすからです。
</para>
<para>
Let's look at another TCP client.  This one connects to the TCP &quot;time&quot;
service on a number of different machines and shows how far their clocks
differ from the system on which it's being run:
</para>
<para>
もう一つの TCP クライアントを見てみましょう。
これは複数の異なるマシン上の TCP の“time”サービスに接続してクライアントが
走っているシステムと時計がどれくらい違っているのを出力します:
</para>
<verbatim><![CDATA[
#!/usr/bin/perl  -w
use strict;
use Socket;
]]></verbatim>
<verbatim><![CDATA[
my $SECS_of_70_YEARS = 2208988800;
sub ctime { scalar localtime(shift) }
]]></verbatim>
<verbatim><![CDATA[
my $iaddr = gethostbyname('localhost');
my $proto = getprotobyname('tcp');
my $port = getservbyname('time', 'tcp');
my $paddr = sockaddr_in(0, $iaddr);
my($host);
]]></verbatim>
<verbatim><![CDATA[
$| = 1;
printf "%-24s %8s %s\n",  "localhost", 0, ctime(time());
]]></verbatim>
<verbatim><![CDATA[
foreach $host (@ARGV) {
	printf "%-24s ", $host;
	my $hisiaddr = inet_aton($host)     || die "unknown host";
	my $hispaddr = sockaddr_in($port, $hisiaddr);
	socket(SOCKET, PF_INET, SOCK_STREAM, $proto)   || die "socket: $!";
	connect(SOCKET, $hispaddr)          || die "bind: $!";
	my $rtime = '    ';
	read(SOCKET, $rtime, 4);
	close(SOCKET);
	my $histime = unpack("N", $rtime) - $SECS_of_70_YEARS;
	printf "%8d %s\n", $histime - time, ctime($histime);
}
]]></verbatim>
</sect2>
<sect2>
<title>Unix-Domain TCP Clients and Servers</title>
<para>
(UNIX ドメインの TCP クライアントとサーバー)
</para>
<para>
That's fine for Internet-domain clients and servers, but what about local
communications?  While you can use the same setup, sometimes you don't
want to.  Unix-domain sockets are local to the current host, and are often
used internally to implement pipes.  Unlike Internet domain sockets, Unix
domain sockets can show up in the file system with an ls(1) listing.
</para>
<para>
Internet-domain のサーバーとクライアントは良いものですが、ローカル
コミュニケーションに関してはどうでしょうか?
同じようにセットアップすることができますが、同じ様に(通信を)行うことは
できません。
UNIX ドメインソケットはカレントホストにローカルで、しばしばパイプを
実装するために内部的に使われています。
Internet ドメインソケットとは異なり、UNIX ドメインソケットは、
ls(1) を使ってファイルシステムの中で見つけることができます。
</para>
<verbatim><![CDATA[
% ls -l /dev/log
srw-rw-rw-  1 root            0 Oct 31 07:23 /dev/log
]]></verbatim>
<para>
You can test for these with Perl's <strong>-S</strong> file test:
</para>
<para>
これを、Perl のファイルテスト <strong>-S</strong> を使って行えます:
</para>
<verbatim><![CDATA[
unless ( -S '/dev/log' ) {
	die "something's wicked with the log system";
}
]]></verbatim>
<para>
Here's a sample Unix-domain client:
</para>
<para>
UNIX ドメインクライアントの例です:
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use Socket;
use strict;
my ($rendezvous, $line);
]]></verbatim>
<verbatim><![CDATA[
$rendezvous = shift || 'catsock';
socket(SOCK, PF_UNIX, SOCK_STREAM, 0)	|| die "socket: $!";
connect(SOCK, sockaddr_un($rendezvous))	|| die "connect: $!";
while (defined($line = <SOCK>)) {
	print $line;
}
exit;
]]></verbatim>
<para>
And here's a corresponding server.  You don't have to worry about silly
network terminators here because Unix domain sockets are guaranteed
to be on the localhost, and thus everything works right.
</para>
<para>
そして対応するサーバーです。
ここで、あなたはやっかいなネットワーク終端子について思い煩う
必要はありません。
なぜなら、UNIX のドメインソケットはローカルホストにおいて完全に
満たされるものであって、そのため、全てはうまくいくのです。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -Tw
use strict;
use Socket;
use Carp;
]]></verbatim>
<verbatim><![CDATA[
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
sub spawn;  # forward declaration
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
]]></verbatim>
<verbatim><![CDATA[
my $NAME = 'catsock';
my $uaddr = sockaddr_un($NAME);
my $proto = getprotobyname('tcp');
]]></verbatim>
<verbatim><![CDATA[
socket(Server,PF_UNIX,SOCK_STREAM,0) 	|| die "socket: $!";
unlink($NAME);
bind  (Server, $uaddr) 			|| die "bind: $!";
listen(Server,SOMAXCONN)			|| die "listen: $!";
]]></verbatim>
<verbatim><![CDATA[
logmsg "server started on $NAME";
]]></verbatim>
<verbatim><![CDATA[
my $waitedpid;
]]></verbatim>
<verbatim><![CDATA[
use POSIX ":sys_wait_h";
sub REAPER {
	my $child;
    while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {
	    logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
	}
	$SIG{CHLD} = \&REAPER;  # loathe sysV
}
]]></verbatim>
<verbatim><![CDATA[
$SIG{CHLD} = \&REAPER;
]]></verbatim>
<verbatim><![CDATA[
for ( $waitedpid = 0;
	  accept(Client,Server) || $waitedpid;
	  $waitedpid = 0, close Client)
{
	next if $waitedpid;
	logmsg "connection on $NAME";
	spawn sub {
	    print "Hello there, it's now ", scalar localtime, "\n";
	    exec '/usr/games/fortune' or die "can't exec fortune: $!";
	};
}
]]></verbatim>
<verbatim><![CDATA[
sub spawn {
	my $coderef = shift;
]]></verbatim>
<verbatim><![CDATA[
unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
    confess "usage: spawn CODEREF";
}
]]></verbatim>
<verbatim><![CDATA[
my $pid;
if (!defined($pid = fork)) {
    logmsg "cannot fork: $!";
    return;
} elsif ($pid) {
    logmsg "begat $pid";
    return; # I'm the parent
}
# else I'm the child -- go spawn
]]></verbatim>
<verbatim><![CDATA[
open(STDIN,  "<&Client")   || die "can't dup client to stdin";
open(STDOUT, ">&Client")   || die "can't dup client to stdout";
## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
exit &$coderef();
    }
]]></verbatim>
<para>
As you see, it's remarkably similar to the Internet domain TCP server, so
much so, in fact, that we've omitted several duplicate functions--spawn(),
logmsg(), ctime(), and REAPER()--which are exactly the same as in the
other server.
</para>
<para>
見てわかるように、Internet domain TCP サーバーとほとんど同じです。
実際には、まったく変っていない幾つかの重複した関数 spawn(), 
logmsg(), ctime(), REAPER() を取り除いてあります。
</para>
<para>
So why would you ever want to use a Unix domain socket instead of a
simpler named pipe?  Because a named pipe doesn't give you sessions.  You
can't tell one process's data from another's.  With socket programming,
you get a separate session for each client: that's why accept() takes two
arguments.
</para>
<para>
ではなぜ、単純な名前付きパイプではなく UNIX ドメインソケットを
使いたがるのでしょうか?
その理由は、名前付きパイプがあなたにセッションを与えないからです。
あなたはあるプロセスから来たデータと、それとは別のプロセスからきた
データとを区別することができません。
ソケットプログラミングを行うことで、クライアント毎に別々のセッションを
持てるようになります: これが accept() が二つの引数を取る理由です。
</para>
<para>
For example, let's say that you have a long running database server daemon
that you want folks from the World Wide Web to be able to access, but only
if they go through a CGI interface.  You'd have a small, simple CGI
program that does whatever checks and logging you feel like, and then acts
as a Unix-domain client and connects to your private server.
</para>
<para>
例えば、CGI インターフェースを通してのみ World Wide Web からアクセスされる、
長時間実行されているデータベースサーバーデーモンを持っているとしましょう。
この場合、あなたの好きなようにチェックやログの記録を実行するような小さく、
単純な CGI プログラムを持って、UNIX ドメインクライアントとして振る舞う
プライベートなサーバーに接続させるといったことができるでしょう。
</para>
</sect2>
</sect1>
<sect1>
<title>TCP Clients with IO::Socket</title>
<para>
(IO::Socket を使った TCP クライアント)
</para>
<para>
For those preferring a higher-level interface to socket programming, the
IO::Socket module provides an object-oriented approach.  IO::Socket is
included as part of the standard Perl distribution as of the 5.004
release.  If you're running an earlier version of Perl, just fetch
IO::Socket from CPAN, where you'll also find modules providing easy
interfaces to the following systems: DNS, FTP, Ident (RFC 931), NIS and
NISPlus, NNTP, Ping, POP3, SMTP, SNMP, SSLeay, Telnet, and Time--just
to name a few.
</para>
<para>
ソケットプログラミングに対する高水準インターフェースのために、IO::Socket
モジュールはオブジェクト指向アプローチを提供します。
IO::Socket モジュールはリリース 5.004 の標準 Perl 配布キットの一部として
含まれています。
あなたが以前のバージョンの Perl を使っているのであれば、
CPAN から IO::Socket を入手します。
そこでは、以下に挙げるようなシステムに対する簡単なインターフェースを
提供するシステムも見つけることができるでしょう: DNS, FTP, Ident (RFC 931),
NIS, NISPlus, NNTP, Ping, POP3, SMTP, SNMP, SSLeay, Telnet, Time など。
</para>
<sect2>
<title>A Simple Client</title>
<para>
(単純なクライアント)
</para>
<para>
Here's a client that creates a TCP connection to the &quot;daytime&quot;
service at port 13 of the host name &quot;localhost&quot; and prints out everything
that the server there cares to provide.
</para>
<para>
以下の例は“localhost”というホスト名の 13 番ポートにある“dyatime”
サービスに対する TCP コネクションを作成するクライアントで、そのサーバーが
提供するデータをすべて出力します。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use IO::Socket;
$remote = IO::Socket::INET->new(
			Proto    => "tcp",
			PeerAddr => "localhost",
			PeerPort => "daytime(13)",
		    )
		  or die "cannot connect to daytime port at localhost";
while ( <$remote> ) { print }
]]></verbatim>
<para>
When you run this program, you should get something back that
looks like this:
</para>
<para>
このプログラムを実行すると、以下のような返事が返ってくるでしょう:
</para>
<verbatim><![CDATA[
Wed May 14 08:40:46 MDT 1997
]]></verbatim>
<para>
Here are what those parameters to the <code>new</code> constructor mean:
</para>
<para>
<code>new</code> コンストラクターに対するパラメーターの意味を説明します:
</para>
<list>
<item><itemtext><code>Proto</code></itemtext>
<para>
This is which protocol to use.  In this case, the socket handle returned
will be connected to a TCP socket, because we want a stream-oriented
connection, that is, one that acts pretty much like a plain old file.
Not all sockets are this of this type.  For example, the UDP protocol
can be used to make a datagram socket, used for message-passing.
</para>
<para>
これは使用するプロトコルです。
この例では、私たちはストリーム指向のコネクション、つまり、昔ながらの普通の
ファイルのように振る舞うものを使いたいので、ソケットは TCP ソケットに
接続されたものを扱います。
ソケットにはこれ以外のタイプもあることに注意してください。
たとえば、UDP プロトコルは(メッセージ送信に使われている)
データグラムソケットを作成するために使うことができます。
</para>
</item>
<item><itemtext><code>PeerAddr</code></itemtext>
<para>
This is the name or Internet address of the remote host the server is
running on.  We could have specified a longer name like <code>&quot;www.perl.com&quot;</code>,
or an address like <code>&quot;204.148.40.9&quot;</code>.  For demonstration purposes, we've
used the special hostname <code>&quot;localhost&quot;</code>, which should always mean the
current machine you're running on.  The corresponding Internet address
for localhost is <code>&quot;127.1&quot;</code>, if you'd rather use that.
</para>
<para>
これはサーバーが実行されているリモートホストの、インターネットアドレス
もしくは名前です。
これを <code>&quot;www.perl.com&quot;</code> のような長い名前で指定することも
<code>&quot;204.148.40.9&quot;</code> のようなアドレスで指定することもできます。
先の例で使った <code>&quot;localhost&quot;</code> は、常に自分が使用している現在のマシンを
意味する特別なホスト名です。
ローカルホストに対するインターネットアドレスは <code>&quot;127.1&quot;</code> で、こちらを
使うこともできます。
</para>
</item>
<item><itemtext><code>PeerPort</code></itemtext>
<para>
This is the service name or port number we'd like to connect to.
We could have gotten away with using just <code>&quot;daytime&quot;</code> on systems with a
well-configured system services file,[FOOTNOTE: The system services file
is in <emphasis>/etc/services</emphasis> under Unix] but just in case, we've specified the
port number (13) in parentheses.  Using just the number would also have
worked, but constant numbers make careful programmers nervous.
</para>
<para>
これは接続したいサービスの名称、もしくはポート番号です。
私たちは先の例で、きちんと設定されたシステムサービスの使える
システムでなら <code>&quot;daytime&quot;</code> を使うこともできました
[FOOTNOTE: UNIXでは <emphasis>/etc/services</emphasis> にシステムサービスファイルがあります]。
しかし、実際には括弧でくくって (13) というポート番号の指定を行っていました。
単に番号を使っても同様に動作するのですが、定数は注意深いプログラマを
神経質にさせてしまいます。
</para>
</item>
</list>
<para>
Notice how the return value from the <code>new</code> constructor is used as
a filehandle in the <code>while</code> loop?  That's what's called an indirect
filehandle, a scalar variable containing a filehandle.  You can use
it the same way you would a normal filehandle.  For example, you
can read one line from it this way:
</para>
<para>
コンストラクター <code>new</code> の戻り値が、<code>while</code> ループの中のファイル
ハンドルとしてどのように使われているかということに気がつきましたか?
これは間接ファイルハンドルと呼ばれるもので、ファイルハンドルを保持している
スカラー変数です。
これは、通常のファイルハンドルと同様のやり方で使うことができます。
例えば、以下のようにすれば一行読み込みができます:
</para>
<verbatim><![CDATA[
$line = <$handle>;
]]></verbatim>
<para>
all remaining lines from is this way:
</para>
<para>
残りの行全ての読み込みは以下のようにします:
</para>
<verbatim><![CDATA[
@lines = <$handle>;
]]></verbatim>
<para>
and send a line of data to it this way:
</para>
<para>
データを一行送るには以下のようにします:
</para>
<verbatim><![CDATA[
print $handle "some data\n";
]]></verbatim>
</sect2>
<sect2>
<title>A Webget Client</title>
<para>
(Webget クライアント)
</para>
<para>
Here's a simple client that takes a remote host to fetch a document
from, and then a list of documents to get from that host.  This is a
more interesting client than the previous one because it first sends
something to the server before fetching the server's response.
</para>
<para>
以下の例は、ドキュメントをそこから取るリモートホストと、そのホストから
取得するドキュメントのリストを引数に取る単純なクライアントです。
これは先の例よりも興味深いものです。
なぜなら、この例においてはサーバーの反応をフェッチする前に最初に何かを
サーバーに送信するからです。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use IO::Socket;
unless (@ARGV > 1) { die "usage: $0 host document ..." }
$host = shift(@ARGV);
$EOL = "\015\012";
$BLANK = $EOL x 2;
foreach $document ( @ARGV ) {
	$remote = IO::Socket::INET->new( Proto     => "tcp",
					 PeerAddr  => $host,
					 PeerPort  => "http(80)",
				        );
	unless ($remote) { die "cannot connect to http daemon on $host" }
	$remote->autoflush(1);
	print $remote "GET $document HTTP/1.0" . $BLANK;
	while ( <$remote> ) { print }
	close $remote;
}
]]></verbatim>
<para>
The web server handing the &quot;http&quot; service, which is assumed to be at
its standard port, number 80.  If the web server you're trying to
connect to is at a different port (like 1080 or 8080), you should specify
as the named-parameter pair, <code>PeerPort =&gt; 8080</code>.  The <code>autoflush</code>
method is used on the socket because otherwise the system would buffer
up the output we sent it.  (If you're on a Mac, you'll also need to
change every <code>&quot;\n&quot;</code> in your code that sends data over the network to
be a <code>&quot;\015\012&quot;</code> instead.)
</para>
<para>
ここでは、“http”サービスを提供する web サーバーがその標準ポートである
80 番ポートを使っていると仮定しています。
あなたの使っている web サーバーが異なるポート(例えば 1080 とか 8080 とか)を
使用しているのであれば、名前付きパラメータペアにして<code>PeerPort =&gt; 8080</code>
のような形式で指定すべきでしょう。
<code>autoflush</code> メソッドがソケットに対して使われます。
そうしなければシステムは私たちが送信した出力を
バッファリングしてしまうでしょう(あなたが Mac を使っているのであれば、
ネットワーク越しにデータを送信するプログラム中にあるすべての
<code>&quot;\n&quot;</code> を <code>&quot;\015\012&quot;</code> に変更する必要もあります)。
</para>
<para>
Connecting to the server is only the first part of the process: once you
have the connection, you have to use the server's language.  Each server
on the network has its own little command language that it expects as
input.  The string that we send to the server starting with &quot;GET&quot; is in
HTTP syntax.  In this case, we simply request each specified document.
Yes, we really are making a new connection for each document, even though
it's the same host.  That's the way you always used to have to speak HTTP.
Recent versions of web browsers may request that the remote server leave
the connection open a little while, but the server doesn't have to honor
such a request.
</para>
<para>
サーバーへの接続は、このプロセスの最初の一部分でしかありません: 
一度接続されてしまえば、サーバーの言語を使うべきなのです。
ネットワーク上の各サーバーは、入力として期待しているそれぞれの小さな
コマンド言語を持っています。
HTTP 構文において最初にサーバーに送信するのは &quot;GET&quot; です。
この場合、単純に指定されたドキュメントのそれぞれをリクエストします。
そう、私たちは実際には、たとえ同じホストであったとしてもドキュメント毎に
新しいコネクションを作成しています。
これが HTTP を使うときに常にそうしなければならない方法なのです。
最近のwebブラウザーではコネクションを開いたままちょっとの間リモートサーバーを
離れるリクエストをすることができますが、サーバーはそのようなリクエストを
処理しなければならないというわけではありません。
</para>
<para>
Here's an example of running that program, which we'll call <emphasis>webget</emphasis>:
</para>
<para>
以下に挙げるのは、私たちが<emphasis>webget</emphasis>と呼ぶであろうプログラムを
実行した例です。
</para>
<verbatim><![CDATA[
% webget www.perl.com /guanaco.html
HTTP/1.1 404 File Not Found
Date: Thu, 08 May 1997 18:02:32 GMT
Server: Apache/1.2b6
Connection: close
Content-type: text/html
]]></verbatim>
<verbatim><![CDATA[
<HEAD><TITLE>404 File Not Found</TITLE></HEAD>
<BODY><H1>File Not Found</H1>
The requested URL /guanaco.html was not found on this server.<P>
</BODY>
]]></verbatim>
<para>
Ok, so that's not very interesting, because it didn't find that
particular document.  But a long response wouldn't have fit on this page.
</para>
<para>
これは特定のドキュメントを見つけられないというものですからあまり
面白いものでもありません。
でも、長いレスポンスをここに載せるわけにもいかないでしょう。
</para>
<para>
For a more fully-featured version of this program, you should look to
the <emphasis>lwp-request</emphasis> program included with the LWP modules from CPAN.
</para>
<para>
このプログラムの全機能バージョン(fully-featured version)は、CPAN にある
LWP モジュール中の <emphasis>lwp-request</emphasis> というプログラムを見るとよいでしょう。
</para>
</sect2>
<sect2>
<title>Interactive Client with IO::Socket</title>
<para>
(IO::Socket を使った対話的クライアント)
</para>
<para>
Well, that's all fine if you want to send one command and get one answer,
but what about setting up something fully interactive, somewhat like
the way <emphasis>telnet</emphasis> works?  That way you can type a line, get the answer,
type a line, get the answer, etc.
</para>
<para>
一つのコマンドを送信し、一つの返答を得るというのであれば具合が良いのですが、
完全に対話的な何かを設定し、<emphasis>telnet</emphasis> のように動作するものはどうでしょうか?
ここでできるのは、ある一行をタイプして答を得て、別の行をタイプしてそれに
対する答を得て…というやり方です。
</para>
<para>
This client is more complicated than the two we've done so far, but if
you're on a system that supports the powerful <code>fork</code> call, the solution
isn't that rough.  Once you've made the connection to whatever service
you'd like to chat with, call <code>fork</code> to clone your process.  Each of
these two identical process has a very simple job to do: the parent
copies everything from the socket to standard output, while the child
simultaneously copies everything from standard input to the socket.
To accomplish the same thing using just one process would be <emphasis>much</emphasis>
harder, because it's easier to code two processes to do one thing than it
is to code one process to do two things.  (This keep-it-simple principle
a cornerstones of the Unix philosophy, and good software engineering as
well, which is probably why it's spread to other systems.)
</para>
<para>
このクライアントは既に出てきた二つのものよりも複雑ですが、あなたが
強力な <code>fork</code> 呼び出しをサポートしているシステムを使っているのであれば、
解決策は乱暴なものではありません。
通信したいなんらかのサービスに対してコネクションを作ってしまえば、プロセスの
複製を作るために <code>fork</code> を呼び出します。
それによる二つのプロセスはそれぞれ、非常に単純なジョブを行います:
親プロセスはソケットから入力されたすべてを標準出力にコピーし、子プロセスは
標準入力をソケットへと同じようにコピーします。
ただ一つのプロセスを使ったときに同じことをするのは <strong>非常に</strong>
難しいでしょう。
なぜなら、二つのことを行う一つのプロセスのためのプログラムよりも
一つのことを行う二つのプロセスのためのプログラムのほうが
簡単だからです(この keep-it-simple 法則は UNIX 文化の要石で、良い
ソフトウェアエンジニアが使うように、(UNIX が)他のシステムよりも広く
使われていることの理由でしょう)。
</para>
<para>
Here's the code:
</para>
<para>
プログラムの例です:
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my ($host, $port, $kidpid, $handle, $line);
]]></verbatim>
<verbatim><![CDATA[
unless (@ARGV == 2) { die "usage: $0 host port" }
($host, $port) = @ARGV;
]]></verbatim>
<verbatim><![CDATA[
# create a tcp connection to the specified host and port
$handle = IO::Socket::INET->new(Proto     => "tcp",
				    PeerAddr  => $host,
				    PeerPort  => $port)
	   or die "can't connect to port $port on $host: $!";
]]></verbatim>
<verbatim><![CDATA[
$handle->autoflush(1);		# so output gets there right away
print STDERR "[Connected to $host:$port]\n";
]]></verbatim>
<verbatim><![CDATA[
# split the program into two processes, identical twins
die "can't fork: $!" unless defined($kidpid = fork());
]]></verbatim>
<verbatim><![CDATA[
# the if{} block runs only in the parent process
if ($kidpid) {
	# copy the socket to standard output
	while (defined ($line = <$handle>)) {
	    print STDOUT $line;
	}
	kill("TERM", $kidpid);  		# send SIGTERM to child
}
# the else{} block runs only in the child process
else {
	# copy standard input to the socket
	while (defined ($line = <STDIN>)) {
	    print $handle $line;
	}
}
]]></verbatim>
<para>
The <code>kill</code> function in the parent's <code>if</code> block is there to send a
signal to our child process (current running in the <code>else</code> block)
as soon as the remote server has closed its end of the connection.
</para>
<para>
親プロセスの <code>if</code> ブロックにある <code>kill</code> 関数は、リモートサーバーが
コネクションを終了してクローズしてすぐに子プロセス(<code>else</code> ブロックを
実行しています)にシグナルを送るためのものです。
</para>
<para>
If the remote server sends data a byte at time, and you need that
data immediately without waiting for a newline (which might not happen),
you may wish to replace the <code>while</code> loop in the parent with the
following:
</para>
<para>
リモートサーバーが一度に一バイト送っていて、そして、あなたが改行を
待つことなしに即座にデータを必要とする(そうそうないことでしょうが)のなら、
<code>while</code> ループを以下のようなものに置き換えたくなるでしょう:
</para>
<verbatim><![CDATA[
my $byte;
while (sysread($handle, $byte, 1) == 1) {
	print STDOUT $byte;
}
]]></verbatim>
<para>
Making a system call for each byte you want to read is not very efficient
(to put it mildly) but is the simplest to explain and works reasonably
well.
</para>
<para>
読み出しのために一バイト毎にシステムコールを行うのは実に非効率的ですが、
説明するのに簡単でとりあえずは動くのです:
</para>
</sect2>
</sect1>
<sect1>
<title>TCP Servers with IO::Socket</title>
<para>
(IO::Socket を使った TCP サーバー)
</para>
<para>
As always, setting up a server is little bit more involved than running a client.
The model is that the server creates a special kind of socket that
does nothing but listen on a particular port for incoming connections.
It does this by calling the <code>IO::Socket::INET-&gt;new()</code> method with
slightly different arguments than the client did.
</para>
<para>
常にそうであるように、サーバーのセッティングはクライアントを実行するよりも
ほんのちょっと手間がかかります。
ここで使うのは、サーバーが特定のポートで接続を待つだけという特殊な種類の
ソケットを作成するというモデルです。
これは、<code>IO::Socket::INET-&gt;new()</code> というメソッドをはっきりと異なる
引数を付けて呼び出してからクライアントを実行することで行います。
</para>
<list>
<item><itemtext>Proto</itemtext>
<para>
This is which protocol to use.  Like our clients, we'll
still specify <code>&quot;tcp&quot;</code> here.
</para>
<para>
これは使用するプロトコルです。
クライアントと同様、ここでは <code>&quot;tcp&quot;</code> を指定します。
</para>
</item>
<item><itemtext>LocalPort</itemtext>
<para>
We specify a local
port in the <code>LocalPort</code> argument, which we didn't do for the client.
This is service name or port number for which you want to be the
server. (Under Unix, ports under 1024 are restricted to the
superuser.)  In our sample, we'll use port 9000, but you can use
any port that's not currently in use on your system.  If you try
to use one already in used, you'll get an &quot;Address already in use&quot;
message.  Under Unix, the <code>netstat -a</code> command will show
which services current have servers.
</para>
<para>
<code>LocalPort</code> 引数でローカルポートを指定します。
これはサーバーにしたいサービス名かポート番号のいずれかです(UNIX では、
1024 未満のポートはスーパーユーザー限定です)。
私たちのサンプルでは 9000 番ポートを使いますが、あなたの使っているシステムで
重複しなければ好きな番号を使うことができます。
もし既に使われているポートを使おうとすれば、&quot;Address already in use&quot; の
ようなメッセージを得ることとなるでしょう。
UNIX では、<code>netstat -a</code> コマンドを使ってサービスが現在使っているサーバーを
見ることができます。
</para>
</item>
<item><itemtext>Listen</itemtext>
<para>
The <code>Listen</code> parameter is set to the maximum number of
pending connections we can accept until we turn away incoming clients.
Think of it as a call-waiting queue for your telephone.
The low-level Socket module has a special symbol for the system maximum, which
is SOMAXCONN.
</para>
<para>
<code>Listen</code> パラメーターは、クライアントを待たせておいて受け付けることのできる
コネクションの最大数を設定します。
電話の呼び出しを考えてみてください。
低水準ソケットモジュールは SOMAXCONN というそのシステムの最大値を表す
特殊なシンボルを持っています。
</para>
</item>
<item><itemtext>Reuse</itemtext>
<para>
The <code>Reuse</code> parameter is needed so that we restart our server
manually without waiting a few minutes to allow system buffers to
clear out.
</para>
<para>
<code>Reuse</code> パラメーターは、システムがバッファーをクリアするための時間を
掛けずに私たちのサーバーを手作業で再起動するのに必要です。
</para>
</item>
</list>
<para>
Once the generic server socket has been created using the parameters
listed above, the server then waits for a new client to connect
to it.  The server blocks in the <code>accept</code> method, which eventually accepts a
bidirectional connection from the remote client.  (Make sure to autoflush
this handle to circumvent buffering.)
</para>
<para>
上で述べたパラメーターを持った汎用のサーバーソケットが生成されれば、
そのサーバーは接続される新たなクライアントを待ちます。
<code>accept</code> メソッドにあるサーバーブロックはリモートクライアントからの
双方向接続を許可します(バッファリングを抑制するためにハンドルに対して
autoflush することを忘れないように)。
</para>
<para>
To add to user-friendliness, our server prompts the user for commands.
Most servers don't do this.  Because of the prompt without a newline,
you'll have to use the <code>sysread</code> variant of the interactive client above.
</para>
<para>
ユーザーに親切にするために、私たちのサーバーはコマンドの入力のプロンプトを
表示します。
ほとんどのサーバーはこうしたことをしていません。
プロンプトには改行がないので、上の例にあったような対話的な
クライアントの類では <code>sysread</code> を使う必要があるでしょう。
</para>
<para>
This server accepts one of five different commands, sending output
back to the client.  Note that unlike most network servers, this one
only handles one incoming client at a time.  Multithreaded servers are
covered in Chapter 6 of the Camel.
</para>
<para>
このサーバーは五種類のコマンドのいずれか一つを取り、クライアントに対して
(コマンドに応じた)出力を行います。
多くのネットワークサーバーとは異なり、このサーバープログラムは一度に一つの
クライアントしか扱えないということに注意してください。
マルチスレッド化されたサーバーはらくだ本の第六章でカバーされています。
</para>
<para>
Here's the code.  We'll
</para>
<para>
以下プログラムです。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use IO::Socket;
use Net::hostent;		# for OO version of gethostbyaddr
]]></verbatim>
<verbatim><![CDATA[
$PORT = 9000;			# pick something not in use
]]></verbatim>
<verbatim><![CDATA[
$server = IO::Socket::INET->new( Proto     => 'tcp',
                                 LocalPort => $PORT,
                                 Listen    => SOMAXCONN,
                                 Reuse     => 1);
]]></verbatim>
<verbatim><![CDATA[
die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";
]]></verbatim>
<verbatim><![CDATA[
while ($client = $server->accept()) {
  $client->autoflush(1);
  print $client "Welcome to $0; type help for command list.\n";
  $hostinfo = gethostbyaddr($client->peeraddr);
  printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost;
  print $client "Command? ";
  while ( <$client>) {
    next unless /\S/;	     # blank line
    if    (/quit|exit/i)    { last;                                     }
    elsif (/date|time/i)    { printf $client "%s\n", scalar localtime;  }
    elsif (/who/i )         { print  $client `who 2>&1`;                }
    elsif (/cookie/i )      { print  $client `/usr/games/fortune 2>&1`; }
    elsif (/motd/i )        { print  $client `cat /etc/motd 2>&1`;      }
    else {
      print $client "Commands: quit date who cookie motd\n";
    }
  } continue {
     print $client "Command? ";
  }
  close $client;
}
]]></verbatim>
</sect1>
<sect1>
<title>UDP: Message Passing</title>
<para>
(UDP: メッセージ配送)
</para>
<para>
Another kind of client-server setup is one that uses not connections, but
messages.  UDP communications involve much lower overhead but also provide
less reliability, as there are no promises that messages will arrive at
all, let alone in order and unmangled.  Still, UDP offers some advantages
over TCP, including being able to &quot;broadcast&quot; or &quot;multicast&quot; to a whole
bunch of destination hosts at once (usually on your local subnet).  If you
find yourself overly concerned about reliability and start building checks
into your message system, then you probably should use just TCP to start
with.
</para>
<para>
クライアント・サーバーをセットアップするもう一つの種類はコネクションではなく
メッセージを使うものです。
UDP 通信はオーバーヘッドが低いものの、メッセージがすべて到着するという
保証がなく到着の順序もきちんと保たれていることも保証されてないために
信頼性もまた劣るものになっています。
それでも、UDP には一度に宛て先ホストの塊全体
(通常はローカルサブネット)に対して“ブロードキャスト”、“マルチキャスト”が
できるということを含め、TCPに対する幾つかのアドバンテージがあります。
信頼性に関して過度に関心を持ち、作成するメッセージシステムに検査機構を
組み込もうというのであれば、むしろ TCP を使うようにした方がよいでしょう。
</para>
<para>
Note that UDP datagrams are <emphasis>not</emphasis> a bytestream and should not be treated
as such. This makes using I/O mechanisms with internal buffering
like stdio (i.e. print() and friends) especially cumbersome. Use syswrite(),
or better send(), like in the example below.
</para>
<para>
UDP はバイトストリーム <emphasis>ではなく</emphasis> 、そのように扱うべきでもないことに
注意してください。
これは stdio(つまり print() やその親戚) のような内部バッファリング付きの
I/O 機構を特に扱いにくくします。
以下の例のように、syswrite() か、よりよい send() を使ってください。
</para>
<para>
Here's a UDP program similar to the sample Internet TCP client given
earlier.  However, instead of checking one host at a time, the UDP version
will check many of them asynchronously by simulating a multicast and then
using select() to do a timed-out wait for I/O.  To do something similar
with TCP, you'd have to use a different socket handle for each host.
</para>
<para>
以下に挙げた UDP プログラムは先に挙げたインターネト TCP クライアントの
例と似ていますが、一度に一つのホストをチェックするのではなく、
マルチキャストをシミュレートし、かつ、select() を入出力のタイムアウト待ちの
ために使うことにより非同期的にたくさんのチェックを行います。
TCP でこれと同じことを行うには、ホスト毎に異なるソケットハンドルを
使わなければならないでしょう。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use strict;
use Socket;
use Sys::Hostname;
]]></verbatim>
<verbatim><![CDATA[
my ( $count, $hisiaddr, $hispaddr, $histime,
	 $host, $iaddr, $paddr, $port, $proto,
	 $rin, $rout, $rtime, $SECS_of_70_YEARS);
]]></verbatim>
<verbatim><![CDATA[
$SECS_of_70_YEARS      = 2208988800;
]]></verbatim>
<verbatim><![CDATA[
$iaddr = gethostbyname(hostname());
$proto = getprotobyname('udp');
$port = getservbyname('time', 'udp');
$paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
]]></verbatim>
<verbatim><![CDATA[
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto)   || die "socket: $!";
bind(SOCKET, $paddr)                          || die "bind: $!";
]]></verbatim>
<verbatim><![CDATA[
$| = 1;
printf "%-12s %8s %s\n",  "localhost", 0, scalar localtime time;
$count = 0;
for $host (@ARGV) {
	$count++;
	$hisiaddr = inet_aton($host) 	|| die "unknown host";
	$hispaddr = sockaddr_in($port, $hisiaddr);
	defined(send(SOCKET, 0, 0, $hispaddr))    || die "send $host: $!";
}
]]></verbatim>
<verbatim><![CDATA[
$rin = '';
vec($rin, fileno(SOCKET), 1) = 1;
]]></verbatim>
<verbatim><![CDATA[
# timeout after 10.0 seconds
while ($count && select($rout = $rin, undef, undef, 10.0)) {
	$rtime = '';
	($hispaddr = recv(SOCKET, $rtime, 4, 0)) 	|| die "recv: $!";
	($port, $hisiaddr) = sockaddr_in($hispaddr);
	$host = gethostbyaddr($hisiaddr, AF_INET);
	$histime = unpack("N", $rtime) - $SECS_of_70_YEARS;
	printf "%-12s ", $host;
	printf "%8d %s\n", $histime - time, scalar localtime($histime);
	$count--;
}
]]></verbatim>
<para>
Note that this example does not include any retries and may consequently
fail to contact a reachable host. The most prominent reason for this
is congestion of the queues on the sending host if the number of
list of hosts to contact is sufficiently large.
</para>
<para>
この例では一切再試行を行わないので、到達可能なホストへの接続に
失敗することがあることに注意してください。
これのもっとも有名な原因は、もし接続するホストのリストの数が十分に大きいと、
送信ホストのキューが混雑することです。
</para>
</sect1>
<sect1>
<title>SysV IPC</title>
<para>
While System V IPC isn't so widely used as sockets, it still has some
interesting uses.  You can't, however, effectively use SysV IPC or
Berkeley mmap() to have shared memory so as to share a variable amongst
several processes.  That's because Perl would reallocate your string when
you weren't wanting it to.
</para>
<para>
System V IPC はソケットとしてはそれ程広く使われてはいませんが、
幾つかの興味深い使用法があります。
ただし、System V の IPC や Berkley の mmap() を(異なる幾つかのプロセスの
間で変数を共有するための)共有メモリを持つ目的のために効率良く
使うことはできません。
なぜなら Perl が、あなたが望まないときに文字列の再割り付けをやってしまう
可能性があるからです。
</para>
<para>
Here's a small example showing shared memory usage.
</para>
<para>
共有メモリの使い方を例示する小さな例です。
</para>
<verbatim><![CDATA[
use IPC::SysV qw(IPC_PRIVATE IPC_RMID S_IRUSR S_IWUSR);
]]></verbatim>
<verbatim><![CDATA[
$size = 2000;
$id = shmget(IPC_PRIVATE, $size, S_IRUSR|S_IWUSR) || die "$!";
print "shm key $id\n";
]]></verbatim>
<verbatim><![CDATA[
$message = "Message #1";
shmwrite($id, $message, 0, 60) || die "$!";
print "wrote: '$message'\n";
shmread($id, $buff, 0, 60) || die "$!";
print "read : '$buff'\n";
]]></verbatim>
<verbatim><![CDATA[
# the buffer of shmread is zero-character end-padded.
substr($buff, index($buff, "\0")) = '';
print "un" unless $buff eq $message;
print "swell\n";
]]></verbatim>
<verbatim><![CDATA[
print "deleting shm $id\n";
shmctl($id, IPC_RMID, 0) || die "$!";
]]></verbatim>
<para>
Here's an example of a semaphore:
</para>
<para>
以下はセマフォの例です:
</para>
<verbatim><![CDATA[
use IPC::SysV qw(IPC_CREAT);
]]></verbatim>
<verbatim><![CDATA[
$IPC_KEY = 1234;
$id = semget($IPC_KEY, 10, 0666 | IPC_CREAT ) || die "$!";
print "shm key $id\n";
]]></verbatim>
<para>
Put this code in a separate file to be run in more than one process.
Call the file <filename>take</filename>:
</para>
<para>
二つ以上のプロセスで動作するように、このコードを分割されたファイルに
置きます。
そのファイルを <filename>take</filename> と呼びます:
</para>
<verbatim><![CDATA[
# create a semaphore
]]></verbatim>
<verbatim><![CDATA[
$IPC_KEY = 1234;
$id = semget($IPC_KEY,  0 , 0 );
die if !defined($id);
]]></verbatim>
<verbatim><![CDATA[
$semnum = 0;
$semflag = 0;
]]></verbatim>
<verbatim><![CDATA[
# 'take' semaphore
# wait for semaphore to be zero
$semop = 0;
$opstring1 = pack("s!s!s!", $semnum, $semop, $semflag);
]]></verbatim>
<verbatim><![CDATA[
# Increment the semaphore count
$semop = 1;
$opstring2 = pack("s!s!s!", $semnum, $semop,  $semflag);
$opstring = $opstring1 . $opstring2;
]]></verbatim>
<verbatim><![CDATA[
semop($id,$opstring) || die "$!";
]]></verbatim>
<para>
Put this code in a separate file to be run in more than one process.
Call this file <filename>give</filename>:
</para>
<para>
このコードを、二つ以上のプロセスで実行できるように別のファイルに
置きます。
このファイルを <filename>give</filename> と呼びます。
</para>
<verbatim><![CDATA[
# 'give' the semaphore
# run this in the original process and you will see
# that the second process continues
]]></verbatim>
<verbatim><![CDATA[
$IPC_KEY = 1234;
$id = semget($IPC_KEY, 0, 0);
die if !defined($id);
]]></verbatim>
<verbatim><![CDATA[
$semnum = 0;
$semflag = 0;
]]></verbatim>
<verbatim><![CDATA[
# Decrement the semaphore count
$semop = -1;
$opstring = pack("s!s!s!", $semnum, $semop, $semflag);
]]></verbatim>
<verbatim><![CDATA[
semop($id,$opstring) || die "$!";
]]></verbatim>
<para>
The SysV IPC code above was written long ago, and it's definitely
clunky looking.  For a more modern look, see the IPC::SysV module
which is included with Perl starting from Perl 5.005.
</para>
<para>
ここで例示した System V の IPC コードははるかな昔に書かれたもので、
へぼなものに見えます。
より現代的なものについては、Perl 5.005 から含まれている
IPC::SysV モジュールを参照してください。
</para>
<para>
A small example demonstrating SysV message queues:
</para>
<para>
SysV メッセージキューを例示する簡単な例です:
</para>
<verbatim><![CDATA[
use IPC::SysV qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRUSR S_IWUSR);
]]></verbatim>
<verbatim><![CDATA[
my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRUSR | S_IWUSR);
]]></verbatim>
<verbatim><![CDATA[
my $sent = "message";
my $type_sent = 1234;
my $rcvd;
my $type_rcvd;
]]></verbatim>
<verbatim><![CDATA[
if (defined $id) {
    if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) {
        if (msgrcv($id, $rcvd, 60, 0, 0)) {
            ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
            if ($rcvd eq $sent) {
                print "okay\n";
            } else {
                print "not okay\n";
            }
        } else {
            die "# msgrcv failed\n";
        }
    } else {
        die "# msgsnd failed\n";
    }
    msgctl($id, IPC_RMID, 0) || die "# msgctl failed: $!\n";
} else {
    die "# msgget failed\n";
}
]]></verbatim>
</sect1>
<sect1>
<title>NOTES</title>
<para>
Most of these routines quietly but politely return <code>undef</code> when they
fail instead of causing your program to die right then and there due to
an uncaught exception.  (Actually, some of the new <emphasis>Socket</emphasis> conversion
functions  croak() on bad arguments.)  It is therefore essential to
check return values from these functions.  Always begin your socket
programs this way for optimal success, and don't forget to add <strong>-T</strong>
taint checking flag to the #! line for servers:
</para>
<para>
これらのルーチンのほとんどは物静かですが、何かに失敗した場合には
あなたのプログラムを終了させてしまったり捕捉されない例外を
引き起こしたりする代わりに <code>undef</code> を返します(実際には、新しい
<emphasis>Socket</emphasis> 変換関数の幾つかは不正な引数に対して croak() します)。
したがって要点は、これらの関数の戻り値を確認すべきであるということです。
ソケットプログラムは常に最良の成功(optimal success)のためにこのやり方で
始め、そしてサーバーに対して pound-bang line (#! の行のこと)に
汚染検査フラグ <strong>-T</strong> を追加することを忘れないようにしてください。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl -Tw
use strict;
use sigtrap;
use Socket;
]]></verbatim>
</sect1>
<sect1>
<title>BUGS</title>
<para>
All these routines create system-specific portability problems.  As noted
elsewhere, Perl is at the mercy of your C libraries for much of its system
behaviour.  It's probably safest to assume broken SysV semantics for
signals and to stick with simple TCP and UDP socket operations; e.g., don't
try to pass open file descriptors over a local UDP datagram socket if you
want your code to stand a chance of being portable.
</para>
<para>
これらのルーチンは全て、システム固有の移植性問題を作り出します。
他の場所で説明したように、Perl の振る舞いは使用しているCライブラリに
左右されます。
System V のおかしなシグナルのセマンティクスを仮定することと、単純な
TCP および UDP ソケット操作に終始するようにすることがおそらくは最も
安全なもののでしょう。
たとえば、あなたが自分のプログラムに移植性を持たせるようにしたいのであれば、
ローカルな UDP データグラムのソケットを通してファイル記述子を
渡すようなことをしようとしてはいけないということです。
</para>
</sect1>
<sect1>
<title>AUTHOR</title>
<para>
Tom Christiansen, with occasional vestiges of Larry Wall's original
version and suggestions from the Perl Porters.
</para>
<para>
Tom Christiansen (Larry Wall による元の文書の部分的な名残と、
Perl Porters による示唆と共に)
</para>
</sect1>
<sect1>
<title>SEE ALSO</title>
<para>
There's a lot more to networking than this, but this should get you
started.
</para>
<para>
ネットワークに関する事柄はまだまだたくさんあります。
ここにあることはスタートでしかありません。
</para>
<para>
For intrepid programmers, the indispensable textbook is <emphasis>Unix
Network Programming, 2nd Edition, Volume 1</emphasis> by W. Richard Stevens
(published by Prentice-Hall).  Note that most books on networking
address the subject from the perspective of a C programmer; translation
to Perl is left as an exercise for the reader.
</para>
<para>
Richard Stevens による非常に重要な教科書
<emphasis>Unix Network Programming, 2nd Edition, Volume 1</emphasis>
(Prentice-Hall から出版されています)があります。
ネットワークに関するほとんどの本は、C プログラマを対象としている点に
注意してください。
Perl への変換は、読者の宿題として残しておきます。
</para>
<para>
The IO::Socket(3) manpage describes the object library, and the Socket(3)
manpage describes the low-level interface to sockets.  Besides the obvious
functions in <link xref='perlfunc'>perlfunc</link>, you should also check out the <filename>modules</filename> file
at your nearest CPAN site.  (See <link xref='perlmodlib'>perlmodlib</link> or best yet, the <filename>Perl
FAQ</filename> for a description of what CPAN is and where to get it.)
</para>
<para>
IO::Socket(3) マニュアルページにはオブジェクトライブラリの説明があり、
Socket(3) には低水準のソケットに対するインターフェースの説明があります。
<link xref='perlfunc'>perlfunc</link> にある関数の他にも、至近にある CPAN サイトで
<filename>modules</filename> ファイルをチェックしたほうが良いでしょう(<link xref='perlmodlib'>perlmodlib</link> を
参照するか、CPAN がどこにあるかの説明がある <filename>Perl FAQ</filename> を見ると
よいでしょう)。
</para>
<para>
Section 5 of the <filename>modules</filename> file is devoted to &quot;Networking, Device Control
(modems), and Interprocess Communication&quot;, and contains numerous unbundled
modules numerous networking modules, Chat and Expect operations, CGI
programming, DCE, FTP, IPC, NNTP, Proxy, Ptty, RPC, SNMP, SMTP, Telnet,
Threads, and ToolTalk--just to name a few.
</para>
<para>
<filename>modlues</filename> ファイルの第 5 章は
&quot;Networking, Device Control (modems), and Interprocess Communication&quot;
に充てられていて、バンドルされなかった多くのネットワーク関連モジュール、
チャット と Expect operations, CGI プログラミング, DCE, 
FTP, IPC, NNTP, Proxy, Ptty, RPC, SNMP, SMTP, Telnet, スレッド、
そして ToolTalk を含んでいます。
</para>
<para>
Created: KIMURA Koichi
Updated: Kentaro Shirakata &lt;argrath@ub32.org&gt;
</para>
</sect1>
</pod>
