perlfaq8 > 5.0150042 との差分

perlfaq8 5.0150042 と 5.10.1 の差分

11
22=encoding euc-jp
33
44=head1 NAME
55
66=begin original
77
88perlfaq8 - System Interaction
99
1010=end original
1111
1212perlfaq8 - システムとの相互作用
1313
1414=head1 DESCRIPTION
1515
1616=begin original
1717
1818This section of the Perl FAQ covers questions involving operating
19system interaction. Topics include interprocess communication (IPC),
19system interaction. Topics include interprocess communication (IPC),
2020control over the user-interface (keyboard, screen and pointing
2121devices), and most anything else not related to data manipulation.
2222
2323=end original
2424
2525FAQ のこのセクションでは、オペレーティングシステムとの対話に関する質問を
2626扱っています。
2727これにはプロセス間(IPC)、ユーザーインターフェース
2828(キーボード、スクリーン、ポインティングデバイス)の制御、
2929その他データ操作に関連しないほとんどの事柄を含みます。
3030
3131=begin original
3232
3333Read the FAQs and documentation specific to the port of perl to your
34operating system (eg, L<perlvms>, L<perlplan9>, ...). These should
34operating system (eg, L<perlvms>, L<perlplan9>, ...). These should
3535contain more detailed information on the vagaries of your perl.
3636
3737=end original
3838
3939あなたの使っているオペレーティングシステム向けの移植について特有のことは、
4040それに関する FAQ とドキュメント(L<perlvms>, L<perlplan9> など)を
4141読んでください。
4242そこには、あなたの使う perl についてのより詳しい情報があります。
4343
4444=head2 How do I find out which operating system I'm running under?
4545
4646(実行しているオペレーティングシステムを見分けるには?)
4747
4848=begin original
4949
50The C<$^O> variable (C<$OSNAME> if you use C<English>) contains an
50The $^O variable ($OSNAME if you use English) contains an indication of
51indication of the name of the operating system (not its release
51the name of the operating system (not its release number) that your perl
52number) that your perl binary was built for.
52binary was built for.
5353
5454=end original
5555
56C<$^O> という変数(use C<Engish> をしていれば C<$OSNAME>)は、あなたの使っている
56$^O という変数(use Engish をしていれば $OSNAME)は、あなたの使っている
57perl の実行ファイルがビルドされたオペレーションシステムの名前
57perl の実行ファイルがビルドされたオペレーティシステムの名前
5858(リリース番号ではありません)の情報を持っています。
5959
6060=head2 How come exec() doesn't return?
6161X<exec> X<system> X<fork> X<open> X<pipe>
6262
6363(なぜ exec() は戻ってこないのでしょう?)
6464
6565=begin original
6666
6767(contributed by brian d foy)
6868
6969=end original
7070
7171(brian d foy によって寄贈されました)
7272
7373=begin original
7474
7575The C<exec> function's job is to turn your process into another
7676command and never to return. If that's not what you want to do, don't
7777use C<exec>. :)
7878
7979=end original
8080
8181C<exec> 関数の仕事は、あなたのプロセスを他のコマンドに切り替えて、
8282決して帰ってこないことです。
8383もしこれがあなたのしたいことでないなら、C<exec> を使わないでください。 :)
8484
8585=begin original
8686
8787If you want to run an external command and still keep your Perl process
8888going, look at a piped C<open>, C<fork>, or C<system>.
8989
9090=end original
9191
9292もし外部コマンドを実行して、かつ Perl プロセスを実行したままにしたい場合は、
9393パイプ付きの C<open>, C<fork>, C<system> のいずれかを見てください。
9494
9595=head2 How do I do fancy stuff with the keyboard/screen/mouse?
9696
9797(キーボード/画面/マウスの凝った機能を使うには?)
9898
9999=begin original
100100
101101How you access/control keyboards, screens, and pointing devices
102("mice") is system-dependent. Try the following modules:
102("mice") is system-dependent. Try the following modules:
103103
104104=end original
105105
106106キーボード、画面、ポインティングデバイス(“マウス”)にアクセスしたり
107107それを制御することはシステム依存です。
108108以下のモジュールを試してみてください:
109109
110110=over 4
111111
112112=item Keyboard
113113
114 Term::Cap Standard perl distribution
114 Term::Cap Standard perl distribution
115 Term::ReadKey CPAN
115 Term::ReadKey CPAN
116 Term::ReadLine::Gnu CPAN
116 Term::ReadLine::Gnu CPAN
117 Term::ReadLine::Perl CPAN
117 Term::ReadLine::Perl CPAN
118 Term::Screen CPAN
118 Term::Screen CPAN
119119
120120=item Screen
121121
122 Term::Cap Standard perl distribution
122 Term::Cap Standard perl distribution
123 Curses CPAN
123 Curses CPAN
124 Term::ANSIColor CPAN
124 Term::ANSIColor CPAN
125125
126126=item Mouse
127127
128 Tk CPAN
128 Tk CPAN
129 Wx CPAN
130 Gtk2 CPAN
131 Qt4 kdebindings4 package
132129
133130=back
134131
135132=begin original
136133
137134Some of these specific cases are shown as examples in other answers
138135in this section of the perlfaq.
139136
140137=end original
141138
142139これらの一部の特殊なケースは、このセクションの他の回答の例として
143140示されています。
144141
145142=head2 How do I print something out in color?
146143
147144(色付きで何かを出力するには?)
148145
149146=begin original
150147
151148In general, you don't, because you don't know whether
152the recipient has a color-aware display device. If you
149the recipient has a color-aware display device. If you
153150know that they have an ANSI terminal that understands
154color, you can use the L<Term::ANSIColor> module from CPAN:
151color, you can use the Term::ANSIColor module from CPAN:
155152
156153=end original
157154
158一般的にはできません; なぜなら、あなたはディスプレイデバイスに関する
155一般的にはできません
159レシピについて何も知らないからです。
156なぜなら、あなたはディスプイデバイスに関するレシピについて
157何も知らないからです。
160158もしあなたがカラーの扱える ANSI ターミナルについて知っているのなら、
161CPAN にある L<Term::ANSIColor> モジュールを使うことができます。
159CPAN にある Term::ANSIColor モジュールを使うことができます。
162160
163 use Term::ANSIColor;
161 use Term::ANSIColor;
164 print color("red"), "Stop!\n", color("reset");
162 print color("red"), "Stop!\n", color("reset");
165 print color("green"), "Go!\n", color("reset");
163 print color("green"), "Go!\n", color("reset");
166164
167165=begin original
168166
169167Or like this:
170168
171169=end original
172170
173171あるいは次のようにも書けます:
174172
175 use Term::ANSIColor qw(:constants);
173 use Term::ANSIColor qw(:constants);
176 print RED, "Stop!\n", RESET;
174 print RED, "Stop!\n", RESET;
177 print GREEN, "Go!\n", RESET;
175 print GREEN, "Go!\n", RESET;
178176
179177=head2 How do I read just one key without waiting for a return key?
180178
181179(リターンキーを待たずにキーのデータを一つ読み取るには?)
182180
183181=begin original
184182
185183Controlling input buffering is a remarkably system-dependent matter.
186184On many systems, you can just use the B<stty> command as shown in
187185L<perlfunc/getc>, but as you see, that's already getting you into
188186portability snags.
189187
190188=end original
191189
192190入力バッファを制御するのは非常にシステムに依存したやりかたです。
193191多くのシステムでは、L<perlfunc/getc> にあるように
194192B<stty> コマンドを使うことができますが、今書いた通り
195193移植性の問題があるのです。
196194
197 open(TTY, "+</dev/tty") or die "no tty: $!";
195 open(TTY, "+</dev/tty") or die "no tty: $!";
198 system "stty cbreak </dev/tty >/dev/tty 2>&1";
196 system "stty cbreak </dev/tty >/dev/tty 2>&1";
199 $key = getc(TTY); # perhaps this works
197 $key = getc(TTY); # perhaps this works
200 # OR ELSE
198 # OR ELSE
201 sysread(TTY, $key, 1); # probably this does
199 sysread(TTY, $key, 1); # probably this does
202 system "stty -cbreak </dev/tty >/dev/tty 2>&1";
200 system "stty -cbreak </dev/tty >/dev/tty 2>&1";
203201
204202=begin original
205203
206The L<Term::ReadKey> module from CPAN offers an easy-to-use interface that
204The Term::ReadKey module from CPAN offers an easy-to-use interface that
207205should be more efficient than shelling out to B<stty> for each key.
208206It even includes limited support for Windows.
209207
210208=end original
211209
212CPAN にある L<Term::ReadKey> モジュールは、B<stty> をキー毎にシェルに
210CPAN にある Term::ReadKey モジュールは、B<stty> をキー毎にシェルに
213211送るよりもより効果的に行ってくれて使うのが簡単なインターフェースを
214212提供します。
215213このモジュールは限定的ながら Windows にも対応しています。
216214
217 use Term::ReadKey;
215 use Term::ReadKey;
218 ReadMode('cbreak');
216 ReadMode('cbreak');
219 $key = ReadKey(0);
217 $key = ReadKey(0);
220 ReadMode('normal');
218 ReadMode('normal');
221219
222220=begin original
223221
224222However, using the code requires that you have a working C compiler
225and can use it to build and install a CPAN module. Here's a solution
223and can use it to build and install a CPAN module. Here's a solution
226using the standard L<POSIX> module, which is already on your system
224using the standard POSIX module, which is already on your systems
227225(assuming your system supports POSIX).
228226
229227=end original
230228
231229しかし、このコードを使うには C コンパイラを使えることが条件であり、かつ
232230CPAN モジュールのビルドとインストールができなければなりません。
233以下の例は、標準の L<POSIX> モジュールを使った解決策で、
231以下の例は、標準の POSIX モジュールを使った解決策で、
234232あなたの使っているシステムが POSIX をサポートしていれば
235233即使えるものです。
236234
237 use HotKey;
235 use HotKey;
238 $key = readkey();
236 $key = readkey();
239237
240238=begin original
241239
242And here's the C<HotKey> module, which hides the somewhat mystifying calls
240And here's the HotKey module, which hides the somewhat mystifying calls
243241to manipulate the POSIX termios structures.
244242
245243=end original
246244
247そして以下の例は、C<HotKey> モジュールを使ったものです; これは POSIX の
245そして以下の例は、HotKey モジュールを使ったものです
248termio 構造体の操作を包み隠します。
246この HotKey モジュールは POSIX の termio 構造体の操作を包み隠します。
249247
250 # HotKey.pm
248 # HotKey.pm
251 package HotKey;
249 package HotKey;
252250
253 use strict;
251 @ISA = qw(Exporter);
254 use warnings;
252 @EXPORT = qw(cbreak cooked readkey);
255253
256 use parent 'Exporter';
254 use strict;
257 our @EXPORT = qw(cbreak cooked readkey);
255 use POSIX qw(:termios_h);
256 my ($term, $oterm, $echo, $noecho, $fd_stdin);
258257
259 use POSIX qw(:termios_h);
258 $fd_stdin = fileno(STDIN);
260 my ($term, $oterm, $echo, $noecho, $fd_stdin);
259 $term = POSIX::Termios->new();
260 $term->getattr($fd_stdin);
261 $oterm = $term->getlflag();
261262
262 $fd_stdin = fileno(STDIN);
263 $echo = ECHO | ECHOK | ICANON;
263 $term = POSIX::Termios->new();
264 $noecho = $oterm & ~$echo;
264 $term->getattr($fd_stdin);
265 $oterm = $term->getlflag();
266265
267 $echo = ECHO | ECHOK | ICANON;
266 sub cbreak {
268 $noecho = $oterm & ~$echo;
267 $term->setlflag($noecho); # ok, so i don't want echo either
268 $term->setcc(VTIME, 1);
269 $term->setattr($fd_stdin, TCSANOW);
270 }
269271
270 sub cbreak {
272 sub cooked {
271 $term->setlflag($noecho); # ok, so i don't want echo either
273 $term->setlflag($oterm);
272 $term->setcc(VTIME, 1);
274 $term->setcc(VTIME, 0);
273 $term->setattr($fd_stdin, TCSANOW);
275 $term->setattr($fd_stdin, TCSANOW);
274 }
276 }
275277
276 sub cooked {
278 sub readkey {
277 $term->setlflag($oterm);
279 my $key = '';
278 $term->setcc(VTIME, 0);
280 cbreak();
279 $term->setattr($fd_stdin, TCSANOW);
281 sysread(STDIN, $key, 1);
280 }
282 cooked();
283 return $key;
284 }
281285
282 sub readkey {
286 END { cooked() }
283 my $key = '';
284 cbreak();
285 sysread(STDIN, $key, 1);
286 cooked();
287 return $key;
288 }
289287
290 END { cooked() }
288 1;
291289
292 1;
293
294290=head2 How do I check whether input is ready on the keyboard?
295291
296292(キーの入力待ちがあるかどうかチェックするには?)
297293
298294=begin original
299295
300296The easiest way to do this is to read a key in nonblocking mode with the
301L<Term::ReadKey> module from CPAN, passing it an argument of -1 to indicate
297Term::ReadKey module from CPAN, passing it an argument of -1 to indicate
302298not to block:
303299
304300=end original
305301
306最も簡単な方法は、CPAN にある L<Term::ReadKey> に対して
302最も簡単な方法は、CPAN にある Term::ReadKey に対して
307303(ブロックを行わないという意味である)-1 を引数に渡して
308304使用することによって非ブロックモードでキーを読み取るという方法です。
309305
310 use Term::ReadKey;
306 use Term::ReadKey;
311307
312 ReadMode('cbreak');
308 ReadMode('cbreak');
313309
314 if (defined (my $char = ReadKey(-1)) ) {
310 if (defined ($char = ReadKey(-1)) ) {
315 # input was waiting and it was $char
311 # input was waiting and it was $char
316 } else {
312 } else {
317 # no input was waiting
313 # no input was waiting
318 }
314 }
319315
320 ReadMode('normal'); # restore normal tty settings
316 ReadMode('normal'); # restore normal tty settings
321317
322318=head2 How do I clear the screen?
323319
324320(画面をクリアするには?)
325321
326322=begin original
327323
328324(contributed by brian d foy)
329325
330326=end original
331327
332328(brian d foy によって寄贈されました)
333329
334330=begin original
335331
336332To clear the screen, you just have to print the special sequence
337333that tells the terminal to clear the screen. Once you have that
338334sequence, output it when you want to clear the screen.
339335
340336=end original
341337
342338画面をクリアするには、端末に画面をクリアするように伝える特殊な
343339シーケンスを表示するだけです。
344340このシーケンスが分かれば、画面をクリアしたいときにこれを出力します。
345341
346342=begin original
347343
348You can use the L<Term::ANSIScreen> module to get the special
344You can use the C<Term::ANSIScreen> module to get the special
349345sequence. Import the C<cls> function (or the C<:screen> tag):
350346
351347=end original
352348
353この特殊なシーケンスを得るためには L<Term::ANSIScreen> モジュールが
349この特殊なシーケンスを得るためには C<Term::ANSIScreen> モジュールが
354350使えます。
355351C<cls> 関数 (あるいは C<:screen> タグ) をインポートします:
356352
357 use Term::ANSIScreen qw(cls);
353 use Term::ANSIScreen qw(cls);
358 my $clear_screen = cls();
354 my $clear_screen = cls();
359355
360 print $clear_screen;
356 print $clear_screen;
361357
362358=begin original
363359
364The L<Term::Cap> module can also get the special sequence if you want
360The C<Term::Cap> module can also get the special sequence if you want
365361to deal with the low-level details of terminal control. The C<Tputs>
366362method returns the string for the given capability:
367363
368364=end original
369365
370366端末制御のための低レベルな詳細を扱いたいなら、
371L<Term::Cap> から特殊シーケンスを得られます。
367C<Term::Cap> から特殊シーケンスを得られます。
372368C<Tputs> メソッドは、与えられた能力のための文字列を返します:
373369
374 use Term::Cap;
370 use Term::Cap;
375371
376 my $terminal = Term::Cap->Tgetent( { OSPEED => 9600 } );
372 $terminal = Term::Cap->Tgetent( { OSPEED => 9600 } );
377 my $clear_string = $terminal->Tputs('cl');
373 $clear_string = $terminal->Tputs('cl');
378374
379 print $clear_screen;
375 print $clear_screen;
380376
381377=begin original
382378
383On Windows, you can use the L<Win32::Console> module. After creating
379On Windows, you can use the C<Win32::Console> module. After creating
384380an object for the output filehandle you want to affect, call the
385381C<Cls> method:
386382
387383=end original
388384
389Windows では、L<Win32::Console> モジュールが使えます。
385Windows では、C<Win32::Console> モジュールが使えます。
390386影響を与えたい出力ファイルハンドルのオブジェクトを作った後、
391387C<Cls> メソッドを呼び出します:
392388
393 Win32::Console;
389 Win32::Console;
394390
395 my $OUT = Win32::Console->new(STD_OUTPUT_HANDLE);
391 $OUT = Win32::Console->new(STD_OUTPUT_HANDLE);
396 my $clear_string = $OUT->Cls;
392 my $clear_string = $OUT->Cls;
397393
398 print $clear_screen;
394 print $clear_screen;
399395
400396=begin original
401397
402398If you have a command-line program that does the job, you can call
403399it in backticks to capture whatever it outputs so you can use it
404400later:
405401
406402=end original
407403
408404もしこの処理を行うコマンドラインプログラムがあるなら、そのプログラムが
409405出力するものを得るために逆クォートで呼び出して、後でそれを使うことも
410406できます:
411407
412 my $clear_string = `clear`;
408 $clear_string = `clear`;
413409
414 print $clear_string;
410 print $clear_string;
415411
416412=head2 How do I get the screen size?
417413
418414(画面サイズを得るには?)
419415
420416=begin original
421417
422If you have L<Term::ReadKey> module installed from CPAN,
418If you have Term::ReadKey module installed from CPAN,
423419you can use it to fetch the width and height in characters
424420and in pixels:
425421
426422=end original
427423
428CPAN にある L<Term::ReadKey> モジュールをインストールしているのなら、
424CPAN にある Term::ReadKey モジュールをインストールしているのなら、
429425文字やピクセルでの幅と高さを得ることができます:
430426
431 use Term::ReadKey;
427 use Term::ReadKey;
432 my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
428 ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
433429
434430=begin original
435431
436432This is more portable than the raw C<ioctl>, but not as
437433illustrative:
438434
439435=end original
440436
441437以下の例は生の C<ioctl> よりも移植性がありますが、
442438あまりわかりやすい例ではありません:
443439
444 require 'sys/ioctl.ph';
440 require 'sys/ioctl.ph';
445 die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
441 die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
446 open(my $tty_fh, "+</dev/tty") or die "No tty: $!";
442 open(TTY, "+</dev/tty") or die "No tty: $!";
447 unless (ioctl($tty_fh, &TIOCGWINSZ, $winsize='')) {
443 unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
448 die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
444 die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
449 }
445 }
450 my ($row, $col, $xpixel, $ypixel) = unpack('S4', $winsize);
446 ($row, $col, $xpixel, $ypixel) = unpack('S4', $winsize);
451 print "(row,col) = ($row,$col)";
447 print "(row,col) = ($row,$col)";
452 print " (xpixel,ypixel) = ($xpixel,$ypixel)" if $xpixel || $ypixel;
448 print " (xpixel,ypixel) = ($xpixel,$ypixel)" if $xpixel || $ypixel;
453 print "\n";
449 print "\n";
454450
455451=head2 How do I ask the user for a password?
456452
457453(ユーザーにパスワードを尋ねるには?)
458454
459455=begin original
460456
461(This question has nothing to do with the web. See a different
457(This question has nothing to do with the web. See a different
462458FAQ for that.)
463459
464460=end original
465461
466462(この質問は web とは関係ありません。
467463それに関しては 別の FAQ を参照してください。)
468464
469465=begin original
470466
471There's an example of this in L<perlfunc/crypt>). First, you put the
467There's an example of this in L<perlfunc/crypt>). First, you put the
472468terminal into "no echo" mode, then just read the password normally.
473You may do this with an old-style C<ioctl()> function, POSIX terminal
469You may do this with an old-style ioctl() function, POSIX terminal
474470control (see L<POSIX> or its documentation the Camel Book), or a call
475471to the B<stty> program, with varying degrees of portability.
476472
477473=end original
478474
479475この例が L<perlfunc/crypt> にあります。
480476第一に、端末を“no echo”モードにし、それから通常通りにパスワードを
481477読み込みます。
482これを、古いスタイルの C<ioctl()> 関数を使ってできますし、あるいは
478これを、古いスタイルの ioctl() 関数を使ってできますし、あるいは
483479POSIX の端末制御(L<POSIX> と、らくだ本を参照してください)を使うことも、
484480B<stty> プログラムを呼び出すことも可能です(ただしこれは移植性は劣ります)。
485481
486482=begin original
487483
488You can also do this for most systems using the L<Term::ReadKey> module
484You can also do this for most systems using the Term::ReadKey module
489485from CPAN, which is easier to use and in theory more portable.
490486
491487=end original
492488
493あるいはほとんどのシステムで、CPAN にある L<Term::ReadKey> を使って
489あるいはほとんどのシステムで、CPAN にある Term::ReadKey を使って
494行うこともできます; これは使うのが簡単で、理論的にはより移植性があります
490行うこともできます。
491これは使うのが簡単で、理論的にはより移植性があります。
495492
496 use Term::ReadKey;
493 use Term::ReadKey;
497494
498 ReadMode('noecho');
495 ReadMode('noecho');
499 my $password = ReadLine(0);
496 $password = ReadLine(0);
500497
501498=head2 How do I read and write the serial port?
502499
503500(シリアルポートの読み書きを行うには?)
504501
505502=begin original
506503
507This depends on which operating system your program is running on. In
504This depends on which operating system your program is running on. In
508505the case of Unix, the serial ports will be accessible through files in
509C</dev>; on other systems, device names will doubtless differ.
506/dev; on other systems, device names will doubtless differ.
510507Several problem areas common to all device interaction are the
511508following:
512509
513510=end original
514511
515512これはプログラムを実行するオペレーティングシステムに依存します。
516UNIX の場合、シリアルポートは C</dev> にあるファイルを通じてアクセスが
513UNIX の場合、シリアルポートは /dev にあるファイルを通じてアクセスが
517可能です; 他のシステムでは、デバイス名は異なったものであることでしょう
514可能です。
515他のシステムでは、デバイス名は異なったものであることでしょう。
518516全てのデバイス操作に共通の問題点として以下のものが挙げられます:
519517
520518=over 4
521519
522520=item lockfiles
523521
524522(ロックファイル)
525523
526524=begin original
527525
528Your system may use lockfiles to control multiple access. Make sure
526Your system may use lockfiles to control multiple access. Make sure
529you follow the correct protocol. Unpredictable behavior can result
527you follow the correct protocol. Unpredictable behavior can result
530528from multiple processes reading from one device.
531529
532530=end original
533531
534532あなたの使っているシステムは、多重アクセスを制御するためにロック
535533ファイルを使用しているかもしれません。
536534正しい手順に従うようにしてください。
537535予測のつかない振る舞いは一つのデバイスに対する複数のプロセスの読み出しが
538536原因かもしれません。
539537
540538=item open mode
541539
542540(オープンモード)
543541
544542=begin original
545543
546544If you expect to use both read and write operations on the device,
547545you'll have to open it for update (see L<perlfunc/"open"> for
548details). You may wish to open it without running the risk of
546details). You may wish to open it without running the risk of
549blocking by using C<sysopen()> and C<O_RDWR|O_NDELAY|O_NOCTTY> from the
547blocking by using sysopen() and C<O_RDWR|O_NDELAY|O_NOCTTY> from the
550L<Fcntl> module (part of the standard perl distribution). See
548Fcntl module (part of the standard perl distribution). See
551549L<perlfunc/"sysopen"> for more on this approach.
552550
553551=end original
554552
555553一つのデバイスに対して、読み込みと書き出しの両方の操作ができることを
556554期待しているのなら、それを更新モード(詳しくは L<perlfunc/"open"> を
557555参照)でオープンする必要があるでしょう。
558C<sysopen()>L<Fcntl> モジュールにある C<O_RDWR|O_NDELAY|O_NOCTTY> とを使って
556sysopen() と Fcntl モジュールにある C<O_RDWR|O_NDELAY|O_NOCTTY> とを使って
559557ブロッキングする危険性なしにオープンを実行したいと考えるかもしれません。
560558このアプローチに関する詳細は L<perlfunc/"sysopen"> を参照してください。
561559
562560=item end of line
563561
564562(行の末尾)
565563
566564=begin original
567565
568566Some devices will be expecting a "\r" at the end of each line rather
569than a "\n". In some ports of perl, "\r" and "\n" are different from
567than a "\n". In some ports of perl, "\r" and "\n" are different from
570their usual (Unix) ASCII values of "\015" and "\012". You may have to
568their usual (Unix) ASCII values of "\012" and "\015". You may have to
571569give the numeric values you want directly, using octal ("\015"), hex
572570("0x0D"), or as a control-character specification ("\cM").
573571
574572=end original
575573
576574幾つかのデバイスでは、行の終端に "\n" ではなく "\r" を期待しています。
577575perl の移植の一部では、"\r" と "\n" は通常の(UNIX の) ASCII 値である
578"\015" と "\012" とは異なったものになっています。
576"\012" と "\015" とは異なったものになっています。
579577八進表記 ("\015") や十六進表記("0x0D")、
580578あるいは制御文字指定 ("\cM") を使って
581579直接数値を与える必要があるかもしれません。
582580
583581=begin original
584582
585 print DEV "atv1\012"; # wrong, for some devices
583 print DEV "atv1\012"; # wrong, for some devices
586 print DEV "atv1\015"; # right, for some devices
584 print DEV "atv1\015"; # right, for some devices
587585
588586=end original
589587
590 print DEV "atv1\012"; # 一部のデバイスにとっては間違い
588 print DEV "atv1\012"; # 一部のデバイスにとっては間違い
591 print DEV "atv1\015"; # 一部のデバイスにとっては正しい
589 print DEV "atv1\015"; # 一部のデバイスにとっては正しい
592590
593591=begin original
594592
595593Even though with normal text files a "\n" will do the trick, there is
596594still no unified scheme for terminating a line that is portable
597595between Unix, DOS/Win, and Macintosh, except to terminate I<ALL> line
598596ends with "\015\012", and strip what you don't need from the output.
599597This applies especially to socket I/O and autoflushing, discussed
600598next.
601599
602600=end original
603601
604通常のテキストファイルでさえも、"\n" はいたずらを行う可能性があります;
602通常のテキストファイルでさえも、"\n" はいたずらを行う可能性があります
605603B<全ての行> を "\015\012" で終わらせ、出力から必要のないものを
606604取り除くということを除いては、UNIX、DOS/Win、Macintosh との間で
607605互換性のある行の終端方法は未だに統一されていません。
608これは特にソケットの入出力や自動フラッシュで適用されます; これについては
606これは特にソケットの入出力や自動フラッシュで適用されます
609次に述べます。
607これについては次に述べます。
610608
611609=item flushing output
612610
613611=begin original
614612
615If you expect characters to get to your device when you C<print()> them,
613If you expect characters to get to your device when you print() them,
616you'll want to autoflush that filehandle. You can use C<select()>
614you'll want to autoflush that filehandle. You can use select()
617615and the C<$|> variable to control autoflushing (see L<perlvar/$E<verbar>>
618616and L<perlfunc/select>, or L<perlfaq5>, "How do I flush/unbuffer an
619output filehandle? Why must I do this?"):
617output filehandle? Why must I do this?"):
620618
621619=end original
622620
623デバイスに C<print()> した時にそのキャラクタが反映されるようにしたいのなら、
621デバイスに print() した時にそのキャラクタが反映されるようにしたいのなら、
624622そのハンドルを自動フラッシュするようにしたいでしょう。
625自動フラッシュを制御するのに、C<$|> という変数と、C<select()>
623自動フラッシュを制御するのに、C<$|> という変数と、select() を
626624使うことができます(L<perlvar/$E<verbar>>とL<perlfunc/select> または
627625L<perlfaq5> の "How do I flush/unbuffer an output filehandle? Why must
628626I do this?" を参照してください)。
629627
630 my $old_handle = select($dev_fh);
628 $oldh = select(DEV);
631 $| = 1;
629 $| = 1;
632 select($old_handle);
630 select($oldh);
633631
634632=begin original
635633
636634You'll also see code that does this without a temporary variable, as in
637635
638636=end original
639637
640638以下のように、これを一時変数を使わないで行うコードを見るかもしれません:
641639
642 select((select($deb_handle), $| = 1)[0]);
640 select((select(DEV), $| = 1)[0]);
643641
644642=begin original
645643
646644Or if you don't mind pulling in a few thousand lines
647of code just because you're afraid of a little C<$|> variable:
645of code just because you're afraid of a little $| variable:
648646
649647=end original
650648
651C<$|> というような変数に戸惑いを感じていて、数千行のプログラムを
649$| というような変数に戸惑いを感じていて、数千行のプログラムを
652650引っ張りこむことを気にしないのなら以下のようにできます:
653651
654 use IO::Handle;
652 use IO::Handle;
655 $dev_fh->autoflush(1);
653 DEV->autoflush(1);
656654
657655=begin original
658656
659657As mentioned in the previous item, this still doesn't work when using
660socket I/O between Unix and Macintosh. You'll need to hard code your
658socket I/O between Unix and Macintosh. You'll need to hard code your
661659line terminators, in that case.
662660
663661=end original
664662
665663先のアイテムで述べたように、これでもまだ UNIX と Macintosh の間で
666664ソケット I/O を使った場合にはうまく動作しません。
667665その場合には、行の終端をハードコーディングする必要があるでしょう。
668666
669667=item non-blocking input
670668
671669=begin original
672670
673If you are doing a blocking C<read()> or C<sysread()>, you'll have to
671If you are doing a blocking read() or sysread(), you'll have to
674672arrange for an alarm handler to provide a timeout (see
675L<perlfunc/alarm>). If you have a non-blocking open, you'll likely
673L<perlfunc/alarm>). If you have a non-blocking open, you'll likely
676674have a non-blocking read, which means you may have to use a 4-arg
677C<select()> to determine whether I/O is ready on that device (see
675select() to determine whether I/O is ready on that device (see
678676L<perlfunc/"select">.
679677
680678=end original
681679
682ブロッキング C<read()> やブロッキング C<sysread()> を行うのであれば、
680ブロッキング read() やブロッキング sysread() を行うのであれば、
683681タイムアウトを実現するためにalarmハンドラーをアレンジする必要があるでしょう
684682(L<perlfunc/alarm> を参照)。
685683もしノンブロッキング open を持っているのであれば、デバイスが入出力完了の
686状態であるかどうかを決定するために四つの引数を取る C<select()> を使う必要が
684状態であるかどうかを決定するために四つの引数を取る select() を使う必要が
687685あるであろうノンブロッキング read も同様に持っていることでしょう
688686(L<perlfunc/"select">を参照)。
689687
690688=back
691689
692690=begin original
693691
694While trying to read from his caller-id box, the notorious Jamie
692While trying to read from his caller-id box, the notorious Jamie Zawinski
695Zawinski C<< <jwz@netscape.com> >>, after much gnashing of teeth and
693C<< <jwz@netscape.com> >>, after much gnashing of teeth and fighting with sysread,
696fighting with C<sysread>, C<sysopen>, POSIX's C<tcgetattr> business,
694sysopen, POSIX's tcgetattr business, and various other functions that
697and various other functions that go bump in the night, finally came up
695go bump in the night, finally came up with this:
698with this:
699696
700697=end original
701698
702699caller-id ボックスから読み出すことに挑戦したことで有名な
703700Jamie Zawinski C<< <jwd@netscape.com> >> は、
704C<sysread>C<sysopen>、POSIX の C<tcgetattr> その他さまざまな関数を扱い
701sysread や sysopen、POSIX の tcgetattr その他さまざまな関数を扱い
705702悪戦苦闘しながら、最終的には以下のようものを作りました:
706703
707 sub open_modem {
704 sub open_modem {
708 use IPC::Open2;
705 use IPC::Open2;
709 my $stty = `/bin/stty -g`;
706 my $stty = `/bin/stty -g`;
710 open2( \*MODEM_IN, \*MODEM_OUT, "cu -l$modem_device -s2400 2>&1");
707 open2( \*MODEM_IN, \*MODEM_OUT, "cu -l$modem_device -s2400 2>&1");
711 # starting cu hoses /dev/tty's stty settings, even when it has
708 # starting cu hoses /dev/tty's stty settings, even when it has
712 # been opened on a pipe...
709 # been opened on a pipe...
713 system("/bin/stty $stty");
710 system("/bin/stty $stty");
714 $_ = <MODEM_IN>;
711 $_ = <MODEM_IN>;
715 chomp;
712 chomp;
716 if ( !m/^Connected/ ) {
713 if ( !m/^Connected/ ) {
717 print STDERR "$0: cu printed `$_' instead of `Connected'\n";
714 print STDERR "$0: cu printed `$_' instead of `Connected'\n";
718 }
715 }
719 }
716 }
720717
721718=head2 How do I decode encrypted password files?
722719
723720(暗号化されたパスワードファイルを復号化するには?)
724721
725722=begin original
726723
727724You spend lots and lots of money on dedicated hardware, but this is
728725bound to get you talked about.
729726
730727=end original
731728
732729特別なハードウェアに非常に多額のお金を掛けてください;
733730しかしこれはあなたが何について話しているかによります。
734731
735732=begin original
736733
737734Seriously, you can't if they are Unix password files--the Unix
738password system employs one-way encryption. It's more like hashing
735password system employs one-way encryption. It's more like hashing
739than encryption. The best you can do is check whether something else
736than encryption. The best you can do is check whether something else
740hashes to the same string. You can't turn a hash back into the
737hashes to the same string. You can't turn a hash back into the
741738original string. Programs like Crack can forcibly (and intelligently)
742739try to guess passwords, but don't (can't) guarantee quick success.
743740
744741=end original
745742
746743まじめな話をすると、UNIX のパスワードに対してのものならできません --
747744UNIX のパスワードシステムは一方向の暗号化を採用しています。
748745それは暗号化というよりはむしろハッシュ化といえるものです。
749あなたがチェックできる最善の方法は、同じ文字列に対するハッシュかどうかを
746あなたがチェックできる最善の方法は、同じ文字列に対する
750調べることです。
747ハッシュかどうかを調べることです。
751ハッシュを元の文字列に戻すことはできません。
752748Crack のようなプログラムは考えられるパスワードを力づくで(そして知的に)
753749試しますが、即座に成功するものを生成することはしません(できません)。
754750
755751=begin original
756752
757753If you're worried about users selecting bad passwords, you should
758754proactively check when they try to change their password (by modifying
759L<passwd(1)>, for example).
755passwd(1), for example).
760756
761757=end original
762758
763759もしあなたが、ユーザーが悪いパスワードを選択してしまうことを
764心配しているのであれば、ユーザーが(たとえば L<passwd(1)> を使って)自分の
760心配しているのであれば、ユーザーが(たとえば passwd(1) を使って)自分の
765761パスワードを変更しようとしたときに積極的にチェックをすべきでしょう。
766762
767763=head2 How do I start a process in the background?
768764
769765(バックグラウンドでプロセスを起動するには?)
770766
771767=begin original
772768
773769(contributed by brian d foy)
774770
775771=end original
776772
777773(brian d foy によって寄贈されました)
778774
779775=begin original
780776
781777There's not a single way to run code in the background so you don't
782778have to wait for it to finish before your program moves on to other
783779tasks. Process management depends on your particular operating system,
784and many of the techniques are covered in L<perlipc>.
780and many of the techniques are in L<perlipc>.
785781
786782=end original
787783
788784バックグラウンドでコードを実行するための単一の方法というのはないので、
789785あなたのプログラムが他のアスクに移動する前に終わるのを待つ必要は
790786ありません。
791787プロセス管理は使用するオペレーティングシステムに依存していて、
792788多くのテクニックは L<perlipc> にあります。
793789
794790=begin original
795791
796Several CPAN modules may be able to help, including L<IPC::Open2> or
792Several CPAN modules may be able to help, including IPC::Open2 or
797L<IPC::Open3>, L<IPC::Run>, L<Parallel::Jobs>,
793IPC::Open3, IPC::Run, Parallel::Jobs, Parallel::ForkManager, POE,
798L<Parallel::ForkManager>, L<POE>, L<Proc::Background>, and
794Proc::Background, and Win32::Process. There are many other modules you
799L<Win32::Process>. There are many other modules you might use, so
795might use, so check those namespaces for other options too.
800check those namespaces for other options too.
801796
802797=end original
803798
804L<IPC::Open2>, L<IPC::Open3>, L<IPC::Run>, L<Parallel::Jobs>,
799IPC::Open2, IPC::Open3, IPC::Run, Parallel::Jobs, Parallel::ForkManager,
805L<Parallel::ForkManager>, L<POE>, L<Proc::Background>, L<Win32::Process>
800POE, Proc::Background, Win32::Process といった CPAN モジュールが
806といった CPAN モジュールが助けになるでしょう。
801助けになるでしょう。
807その他にも使えるかもしれない多くのモジュールがありますので、
802その他にもつかるかもしれない多くのモジュールがありますので、
808803これらの名前空間をチェックしてみてください。
809804
810805=begin original
811806
812If you are on a Unix-like system, you might be able to get away with a
807If you are on a unix-like system, you might be able to get away with a
813808system call where you put an C<&> on the end of the command:
814809
815810=end original
816811
817Unix 風のシステムを使っているなら、コマンドの最後に C<&> を置くことで
812unix 風のシステムを使っているなら、コマンドの最後に C<&> を置くことで
818813システムコールから切り離すことができるかもしれません:
819814
820 system("cmd &")
815 system("cmd &")
821816
822817=begin original
823818
824819You can also try using C<fork>, as described in L<perlfunc> (although
825820this is the same thing that many of the modules will do for you).
826821
827822=end original
828823
829824L<perlfunc> に記述されているように、C<fork> を使うこともできます
830825(しかしこれは多くのモジュールがやってくれるのと同じことです)。
831826
832827=over 4
833828
834829=item STDIN, STDOUT, and STDERR are shared
835830
836831(STDIN, STDOUT, STDERR は共有されます)
837832
838833=begin original
839834
840835Both the main process and the backgrounded one (the "child" process)
841share the same STDIN, STDOUT and STDERR filehandles. If both try to
836share the same STDIN, STDOUT and STDERR filehandles. If both try to
842access them at once, strange things can happen. You may want to close
837access them at once, strange things can happen. You may want to close
843or reopen these for the child. You can get around this with
838or reopen these for the child. You can get around this with
844839C<open>ing a pipe (see L<perlfunc/"open">) but on some systems this
845840means that the child process cannot outlive the parent.
846841
847842=end original
848843
849844主プロセスとバックグラウンドプロセス(子プロセス)で同じ STDIN,
850845STDOUT, STDERR のファイルハンドルが共有されます。
851846両方のプロセスが同時にアクセスしようとすると、
852847おかしな事が発生するかもしれません。
853848子プロセス用のこれらのハンドルを、クローズしたり再オープンしたり
854849したくなるかもしれません。
855850これは、パイプを C<open> することで行えますが、
856851一部のシステムにおいてはこれは子プロセスが親プロセスよりも
857852長生きすることはできないということになります。
858853
859854=item Signals
860855
861856(シグナル)
862857
863858=begin original
864859
865860You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too.
866SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is
861SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is
867862sent when you write to a filehandle whose child process has closed (an
868untrapped SIGPIPE can cause your program to silently die). This is
863untrapped SIGPIPE can cause your program to silently die). This is
869864not an issue with C<system("cmd&")>.
870865
871866=end original
872867
873868SIGCHLD シグナルを捕捉する必要があり、可能なら SIGPIPE も
874869捕捉する必要があるでしょう。
875870SIGCHLD はバックグラウンドプロセスが終了したときに送られます。
876871SIGPIPE は既にクローズされている子プロセスを所有するファイルハンドルに
877872書き込みを行ったときに送られます(トラップされていない SIGPIPE は、
878873あなたのプログラムを黙って終わらせてしまうかもしれません)。
879874これは C<system("cmd&")> を使ったときには起こりません。
880875
881876=item Zombies
882877
883878(ゾンビ)
884879
885880=begin original
886881
887882You have to be prepared to "reap" the child process when it finishes.
888883
889884=end original
890885
891886子プロセスが終了したときにそれを“刈り取る”(reap) 準備をする必要があります。
892887
893 $SIG{CHLD} = sub { wait };
888 $SIG{CHLD} = sub { wait };
894889
895 $SIG{CHLD} = 'IGNORE';
890 $SIG{CHLD} = 'IGNORE';
896891
897892=begin original
898893
899You can also use a double fork. You immediately C<wait()> for your
894You can also use a double fork. You immediately wait() for your
900first child, and the init daemon will C<wait()> for your grandchild once
895first child, and the init daemon will wait() for your grandchild once
901896it exits.
902897
903898=end original
904899
9059002 重 fork も使えます。
906あなたは直ちに最初の子に対して C<wait()> し、init デーモンは孫が終了するのを
901あなたは直ちに最初の子に対して wait() し、init デーモンは孫が終了するのを
907C<wait()> します。
902wait() します。
908903
909 unless ($pid = fork) {
904 unless ($pid = fork) {
910 unless (fork) {
905 unless (fork) {
911 exec "what you really wanna do";
906 exec "what you really wanna do";
912 die "exec failed!";
907 die "exec failed!";
913 }
908 }
914 exit 0;
909 exit 0;
915 }
910 }
916 waitpid($pid, 0);
911 waitpid($pid, 0);
917912
918913=begin original
919914
920915See L<perlipc/"Signals"> for other examples of code to do this.
921916Zombies are not an issue with C<system("prog &")>.
922917
923918=end original
924919
925920これを行うサンプルは L<perlipc/"Signals"> を参照してください。
926921ゾンビは C<system("prog &")> を使ったときには発生しません。
927922
928923=back
929924
930925=head2 How do I trap control characters/signals?
931926
932927(制御文字やシグナルをトラップするには?)
933928
934929=begin original
935930
936You don't actually "trap" a control character. Instead, that character
931You don't actually "trap" a control character. Instead, that character
937932generates a signal which is sent to your terminal's currently
938933foregrounded process group, which you then trap in your process.
939934Signals are documented in L<perlipc/"Signals"> and the
940935section on "Signals" in the Camel.
941936
942937=end original
943938
944939実際には制御文字を“トラップ”できません。
945940その代わりに、その文字が生成して
946941端末のフォアグラウンドプロセスに送られることになる
947942シグナルがトラップできます。
948943シグナルは L<perlipc/"Signals"> とらくだ本の "Signal" の章に
949944説明があります。
950945
951946=begin original
952947
953You can set the values of the C<%SIG> hash to be the functions you want
948You can set the values of the %SIG hash to be the functions you want
954to handle the signal. After perl catches the signal, it looks in C<%SIG>
949to handle the signal. After perl catches the signal, it looks in %SIG
955950for a key with the same name as the signal, then calls the subroutine
956951value for that key.
957952
958953=end original
959954
960C<%SIG> ハッシュにシグナルを扱いたい関数をセットできます。
955%SIG ハッシュにシグナルを扱いたい関数をセットできます。
961perl がシグナルを捕捉した後、perl はシグナルと同じ名前をキーとして C<%SIG>
956perl がシグナルを捕捉した後、perl はシグナルと同じ名前をキーとして %SIG を
962957見て、そのキーの値であるサブルーチンを呼び出します。
963958
964 # as an anonymous subroutine
959 # as an anonymous subroutine
965960
966 $SIG{INT} = sub { syswrite(STDERR, "ouch\n", 5 ) };
961 $SIG{INT} = sub { syswrite(STDERR, "ouch\n", 5 ) };
967962
968 # or a reference to a function
963 # or a reference to a function
969964
970 $SIG{INT} = \&ouch;
965 $SIG{INT} = \&ouch;
971966
972 # or the name of the function as a string
967 # or the name of the function as a string
973968
974 $SIG{INT} = "ouch";
969 $SIG{INT} = "ouch";
975970
976971=begin original
977972
978973Perl versions before 5.8 had in its C source code signal handlers which
979974would catch the signal and possibly run a Perl function that you had set
980in C<%SIG>. This violated the rules of signal handling at that level
975in %SIG. This violated the rules of signal handling at that level
981causing perl to dump core. Since version 5.8.0, perl looks at C<%SIG>
976causing perl to dump core. Since version 5.8.0, perl looks at %SIG
982B<after> the signal has been caught, rather than while it is being caught.
977*after* the signal has been caught, rather than while it is being caught.
983978Previous versions of this answer were incorrect.
984979
985980=end original
986981
987バージョン 5.8 より前の Perl には、シグナルを受け取って、可能なら C<%SIG>
982バージョン 5.8 より前の Perl には、シグナルを受け取って、可能なら %SIG
988983にセットされている Perl の関数を実行する C ソースコードのシグナルハンドラが
989984ありました。
990985これはこのレベルでのシグナルハンドリングのルールに違反していて、perl の
991986コアダンプを引き起こしていました。
992987バージョン 5.8.0 から、perl はシグナルを捕捉している間ではなく、シグナルを
993捕捉した B<後に> C<%SIG> を見ます。
988捕捉した後に%SIG を見ます。
994989この答えの以前のバージョンは間違っていました。
995990
996991=head2 How do I modify the shadow password file on a Unix system?
997992
998993(UNIX システムのシャドウパスワードファイルを変更するには?)
999994
1000995=begin original
1001996
1002997If perl was installed correctly and your shadow library was written
1003properly, the C<getpw*()> functions described in L<perlfunc> should in
998properly, the getpw*() functions described in L<perlfunc> should in
1004999theory provide (read-only) access to entries in the shadow password
1005file. To change the file, make a new shadow password file (the format
1000file. To change the file, make a new shadow password file (the format
1006varies from system to system--see L<passwd(1)> for specifics) and use
1001varies from system to system--see L<passwd> for specifics) and use
1007C<pwd_mkdb(8)> to install it (see L<pwd_mkdb(8)> for more details).
1002pwd_mkdb(8) to install it (see L<pwd_mkdb> for more details).
10081003
10091004=end original
10101005
10111006perl が正しくインストールされていて、かつ、シャドウライブラリが
10121007きちんとインストールされていれば、L<perlfunc> で説明されている
1013C<getpw*()> 関数がシャドウパスワードファイルに対する(リードオンリーの)
1008getpw*() 関数がシャドウパスワードファイルに対する(リードオンリーの)
10141009アクセスを提供しています。
10151010ファイルを変更するには、新たなシャドウパスワードファイルを作成して
1016(フォーマットはシステム毎に異なります--詳しくは L<passwd(1)>
1011(フォーマットはシステム毎に異なります--詳しくは L<passwd>
1017を参照してください)、C<pwd_mkdb(8)> を使ってそれをインストールします
1012を参照してください)、pwd_mkdb(8) を使ってそれをインストールします
1018(詳細は L<pwd_mkdb(8)> を参照のこと)。
1013(詳細は L<pwd_mkdb> を参照のこと)。
10191014
10201015=head2 How do I set the time and date?
10211016
10221017(日付や時刻を設定するには?)
10231018
10241019=begin original
10251020
10261021Assuming you're running under sufficient permissions, you should be
1027able to set the system-wide date and time by running the C<date(1)>
1022able to set the system-wide date and time by running the date(1)
1028program. (There is no way to set the time and date on a per-process
1023program. (There is no way to set the time and date on a per-process
10291024basis.) This mechanism will work for Unix, MS-DOS, Windows, and NT;
10301025the VMS equivalent is C<set time>.
10311026
10321027=end original
10331028
1034あなたが十分な権限を持っているとすれば、C<date(1)> プログラムを実行すれば
1029あなたが十分な権限を持っているとすれば、date(1) プログラムを実行すれば
10351030システム全体の日付や時刻を設定できるはずです
10361031(プロセス毎に日付や時刻を設定する方法はありません)。
10371032この機構は、UNIX、MS-DOS、Windows、NT でうまくいくでしょう。
10381033VMS には等価な C<set time> があります。
10391034
10401035=begin original
10411036
10421037However, if all you want to do is change your time zone, you can
10431038probably get away with setting an environment variable:
10441039
10451040=end original
10461041
10471042あなたのやりたいことがタイムゾーンの変更であるのなら、
10481043環境変数を変更することでそれができるでしょう。
10491044
1050 $ENV{TZ} = "MST7MDT"; # Unixish
1045 $ENV{TZ} = "MST7MDT"; # unixish
1051 $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
1046 $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
1052 system('trn', 'comp.lang.perl.misc');
1047 system "trn comp.lang.perl.misc";
10531048
10541049=head2 How can I sleep() or alarm() for under a second?
10551050X<Time::HiRes> X<BSD::Itimer> X<sleep> X<select>
10561051
10571052(一秒未満の時間に対する sleep() や alarm() をするには?)
10581053
10591054=begin original
10601055
10611056If you want finer granularity than the 1 second that the C<sleep()>
10621057function provides, the easiest way is to use the C<select()> function as
1063documented in L<perlfunc/"select">. Try the L<Time::HiRes> and
1058documented in L<perlfunc/"select">. Try the C<Time::HiRes> and
1064the L<BSD::Itimer> modules (available from CPAN, and starting from
1059the C<BSD::Itimer> modules (available from CPAN, and starting from
1065Perl 5.8 L<Time::HiRes> is part of the standard distribution).
1060Perl 5.8 C<Time::HiRes> is part of the standard distribution).
10661061
10671062=end original
10681063
10691064C<sleep()> 関数が一秒未満の単位での動作をサポートすることを求めているのなら、
10701065L<perlfunc/"select"> にあるように、C<select()> を使うのが最も単純な方法です。
1071L<Time::Hires> と L<BSD::Itimer> モジュール(CPAN から利用可能ですし、
1066C<Time::Hires> と C<BSD::Itimer> モジュール(CPAN から利用可能ですし、
1072Perl 5.8 からは L<Time::HiRes> は標準配布の一部です)も
1067Perl 5.8 からは C<Time::HiRes> は標準配布の一部です)も
10731068試してみてください。
10741069
10751070=head2 How can I measure time under a second?
10761071X<Time::HiRes> X<BSD::Itimer> X<sleep> X<select>
10771072
10781073(1 秒未満の時間を計るには?)
10791074
10801075=begin original
10811076
10821077(contributed by brian d foy)
10831078
10841079=end original
10851080
10861081(brian d foy によって寄贈されました)
10871082
10881083=begin original
10891084
1090The L<Time::HiRes> module (part of the standard distribution as of
1085The C<Time::HiRes> module (part of the standard distribution as of
10911086Perl 5.8) measures time with the C<gettimeofday()> system call, which
10921087returns the time in microseconds since the epoch. If you can't install
1093L<Time::HiRes> for older Perls and you are on a Unixish system, you
1088C<Time::HiRes> for older Perls and you are on a Unixish system, you
10941089may be able to call C<gettimeofday(2)> directly. See
10951090L<perlfunc/syscall>.
10961091
10971092=end original
10981093
1099(Perl 5.8 から標準配布の一部である) L<Time::HiRes> モジュールは、
1094(Perl 5.8 から標準配布の一部である) C<Time::HiRes> モジュールは、
11001095紀元からのマイクロ秒を返す C<gettimeofday()> システムコールを使って時間を
11011096計測します。
1102もし古い Perl を使っていて L<Time::HiRes> をインストールできず、
1097もし古い Perl を使っていて C<Time::HiRes> をインストールできず、
11031098Unixish を使っているなら、C<gettimeofday(2)> を直接使えます。
11041099L<perlfunc/syscall> を参照してください。
11051100
11061101=head2 How can I do an atexit() or setjmp()/longjmp()? (Exception handling)
11071102
11081103(atexit() や setjmp()/longjmp() をするには? (例外処理))
11091104
11101105=begin original
11111106
1112You can use the C<END> block to simulate C<atexit()>. Each package's
1107Release 5 of Perl added the END block, which can be used to simulate
1113C<END> block is called when the program or thread ends. See the L<perlmod>
1108atexit(). Each package's END block is called when the program or
1114manpage for more details about C<END> blocks.
1109thread ends (see L<perlmod> manpage for more details).
11151110
11161111=end original
11171112
1118C<atexit()> をシミュレートする C<END> ブロックが使え
1113Perlリリース 5 では、END ブロックが追加されした
1119ぞれのパッケージの C<END> ブロックは、プログラムやスッド終了時
1114 atexit() をシミュートするのに使うことができます。
1120呼び出されます。
1115各パッケージの END ブロックはプログラム、もしくはスレッド
1121C<END> ブロックに関するさらなる詳細については L<perlmod> を
1116(L<perlmod> を参照してください) が終了したときに呼び出されます
1122参照ださい
1117(詳しくは L<perlmod> を参照)
11231118
11241119=begin original
11251120
1126For example, you can use this to make sure your filter program managed
1121For example, you can use this to make sure your filter program
1127to finish its output without filling up the disk:
1122managed to finish its output without filling up the disk:
11281123
11291124=end original
11301125
11311126例を挙げると、あなたのフィルタープログラムが出力を確実に
11321127ディスクに送るようにするためには以下のようにできます:
11331128
1134 END {
1129 END {
1135 close(STDOUT) || die "stdout close failed: $!";
1130 close(STDOUT) || die "stdout close failed: $!";
1136 }
1131 }
11371132
11381133=begin original
11391134
1140The C<END> block isn't called when untrapped signals kill the program,
1135The END block isn't called when untrapped signals kill the program,
1141though, so if you use C<END> blocks you should also use
1136though, so if you use END blocks you should also use
11421137
11431138=end original
11441139
1145C<END> ブロックは、トラップされないシグナルがプログラムを強制終了させた
1140END ブロックは、トラップされないシグナルが
1146場合には呼び出されません; したがって、C<END> ブロックを使う場合には同時に
1141プログラムを強制終了させた場合には呼び出されません
1147以下のようにするべきです:
1142したがって、END ブロックを使う場合には同時に以下のようにするべきです:
11481143
1149 use sigtrap qw(die normal-signals);
1144 use sigtrap qw(die normal-signals);
11501145
11511146=begin original
11521147
1153Perl's exception-handling mechanism is its C<eval()> operator. You
1148Perl's exception-handling mechanism is its eval() operator. You can
1154can use C<eval()> as C<setjmp> and C<die()> as C<longjmp>. For
1149use eval() as setjmp and die() as longjmp. For details of this, see
1155details of this, see the section on signals, especially the time-out
1150the section on signals, especially the time-out handler for a blocking
1156handler for a blocking C<flock()> in L<perlipc/"Signals"> or the
1151flock() in L<perlipc/"Signals"> or the section on "Signals" in
1157section on "Signals" in I<Programming Perl>.
1152the Camel Book.
11581153
11591154=end original
11601155
1161Perl の例外処理機構は C<eval()> 演算子です。
1156Perl の例外処理機構は eval() 演算子です。
1162C<setjmp> として C<eval()> を、C<longjmp> として C<die()> を使うことができます。
1157setjmp() として eval() を、longjmp() として die() を使うことができます。
11631158これに関する詳細は、シグナルに関するセクション、
1164特に L<perlipc/"Signals"> にあるブロッキング C<flock()> のための
1159特に L<perlipc/"Signals"> にあるブロッキング flock() のための
1165タイムアウトハンドラーと、 I<Programming Perl> の "Signal" の章を参照してください。
1160タイムアウトハンドラーと、らくだ本の "Signal" の章を参照してください。
11661161
11671162=begin original
11681163
1169If exception handling is all you're interested in, use one of the
1164If exception handling is all you're interested in, try the
1170many CPAN modules that handle exceptions, such as L<Try::Tiny>.
1165exceptions.pl library (part of the standard perl distribution).
11711166
11721167=end original
11731168
1174例外処理そのものに興味があるのなら、L<Try::Tiny> のような、例外を
1169例外処理そのものに興味があるのなら、exception.pl ライブラリ
1175扱うための多くの CPAN モジュールの一使ってください。
1170(これは標準 perl 配布キットの一部です)試してみてください。
11761171
11771172=begin original
11781173
1179If you want the C<atexit()> syntax (and an C<rmexit()> as well), try the
1174If you want the atexit() syntax (and an rmexit() as well), try the
1180C<AtExit> module available from CPAN.
1175AtExit module available from CPAN.
11811176
11821177=end original
11831178
1184C<atexit()> 構文(と、C<rmexit()>) が欲しいのなら、CPAN にある
1179atexit() 構文(と、rmexit()) が欲しいのなら、CPAN にある
1185C<AtExit> モジュールを試してみてください。
1180AtExit モジュールを試してみてください。
11861181
1187=head2 Why doesn't my sockets program work under System V (Solaris)? What does the error message "Protocol not supported" mean?
1182=head2 Why doesn't my sockets program work under System V (Solaris)? What does the error message "Protocol not supported" mean?
11881183
11891184(なぜ私のソケットプログラムはSystem V (Solaris) ではうまく動かないの? "Protocol not supported"というエラーメッセージの意味するところは?)
11901185
11911186=begin original
11921187
11931188Some Sys-V based systems, notably Solaris 2.X, redefined some of the
1194standard socket constants. Since these were constant across all
1189standard socket constants. Since these were constant across all
1195architectures, they were often hardwired into perl code. The proper
1190architectures, they were often hardwired into perl code. The proper
11961191way to deal with this is to "use Socket" to get the correct values.
11971192
11981193=end original
11991194
12001195一部の System 5 ベースのシステム、特に Solaris 2.x では標準のソケット定数の
12011196幾つかが再定義されています。
12021197これらの定数は全てのアーキテクチャに渡るものであったので、
12031198しばしば perl コードにハードコーディングされています。
12041199これに対処する適切な方法は正しい値を得るために "use Socket" とすることです。
12051200
12061201=begin original
12071202
12081203Note that even though SunOS and Solaris are binary compatible, these
1209values are different. Go figure.
1204values are different. Go figure.
12101205
12111206=end original
12121207
12131208SunOS と Solaris とではバイナリ互換性があるにも関らず、
12141209これらの値が異なるということに注意してください。
12151210不思議なことです。
12161211
12171212=head2 How can I call my system's unique C functions from Perl?
12181213
12191214(Perl から私のシステムに固有の C 関数を呼び出すには?)
12201215
12211216=begin original
12221217
12231218In most cases, you write an external module to do it--see the answer
12241219to "Where can I learn about linking C with Perl? [h2xs, xsubpp]".
12251220However, if the function is a system call, and your system supports
1226C<syscall()>, you can use the C<syscall> function (documented in
1221syscall(), you can use the syscall function (documented in
12271222L<perlfunc>).
12281223
12291224=end original
12301225
12311226ほとんどの場合、その方法は外部モジュールを作るというものです --
12321227"Where can I learn about linking C with Perl? [h2xs, xsubpp]"
12331228の回答を参照してください。
12341229ただし、その関数がシステムコールでありあなたの使っているシステムが
1235C<syscall()> をサポートしているのであれば、
1230syscall() をサポートしているのであれば、
1236C<syscall> 関数(L<perlfunc> に説明があります)を使うことができます。
1231syscall 関数(L<perlfunc> に説明があります)を使うことができます。
12371232
12381233=begin original
12391234
12401235Remember to check the modules that came with your distribution, and
12411236CPAN as well--someone may already have written a module to do it. On
1242Windows, try L<Win32::API>. On Macs, try L<Mac::Carbon>. If no module
1237Windows, try Win32::API. On Macs, try Mac::Carbon. If no module
12431238has an interface to the C function, you can inline a bit of C in your
1244Perl source with L<Inline::C>.
1239Perl source with Inline::C.
12451240
12461241=end original
12471242
12481243一緒に配布されたモジュールや CPAN にあるモジュールをチェックすることを
12491244忘れないでください--誰かが求めるモジュールを既に作っているかもしれません。
1250Windows では、L<Win32::API> を試してください。
1245Windows では、Win32::API を試してください。
1251Mac では、L<Mac::Carbon> を試してください。
1246Mac では、Mac::Carbon を試してください。
12521247C 関数へのインターフェースを持つモジュールがなければ、
1253L<Inline::C> を使って Perl のソースコードにちょっとした C のコードを
1248Inline::C を使って Perl のソースコードにちょっとした C のコードを
12541249インライン化できます。
12551250
12561251=head2 Where do I get the include files to do ioctl() or syscall()?
12571252
12581253(ioctl() や syscall() で使うための include ファイルはどこで入手できますか?)
12591254
12601255=begin original
12611256
1262Historically, these would be generated by the L<h2ph> tool, part of the
1257Historically, these would be generated by the h2ph tool, part of the
1263standard perl distribution. This program converts C<cpp(1)> directives
1258standard perl distribution. This program converts cpp(1) directives
12641259in C header files to files containing subroutine definitions, like
1265C<SYS_getitimer()>, which you can use as arguments to your functions.
1260&SYS_getitimer, which you can use as arguments to your functions.
12661261It doesn't work perfectly, but it usually gets most of the job done.
12671262Simple files like F<errno.h>, F<syscall.h>, and F<socket.h> were fine,
1268but the hard ones like F<ioctl.h> nearly always need to be hand-edited.
1263but the hard ones like F<ioctl.h> nearly always need to hand-edited.
12691264Here's how to install the *.ph files:
12701265
12711266=end original
12721267
1273伝統的に、これらのファイルは標準配布に含まれる L<h2ph> というツールによって
1268伝統的に、これらのファイルは標準配布に含まれる h2ph というツールによって
12741269生成されるものです。
1275このプログラムは C のヘッダーファイルにある C<cpp(1)> 指示子を
1270このプログラムはCのヘッダーファイルにある cpp(1) 指示子を
1276C<SYS_getitimer()> のような、関数に対する引数として使うことのできる
1271&SYS_getitimer のような、関数に対する引数として使うことのできる
12771272サブルーチン定義を含むファイルに変換するものです。
12781273これは完璧なものではありませんが、ほとんどの場合には十分な仕事を行います。
12791274F<errno.h>, F<syscall.h>, F<socket.h> のような単純なファイルはよいのですが、
12801275F<ioctl.h> のように難しいものはほとんど常に手で編集する必要があります。
12811276以下の手順は、*.ph ファイルをインストールするためのものです。
12821277
1283 1. Become the super-user
1278 1. become super-user
1284 2. cd /usr/include
1279 2. cd /usr/include
1285 3. h2ph *.h */*.h
1280 3. h2ph *.h */*.h
12861281
12871282=begin original
12881283
12891284If your system supports dynamic loading, for reasons of portability and
1290sanity you probably ought to use L<h2xs> (also part of the standard perl
1285sanity you probably ought to use h2xs (also part of the standard perl
1291distribution). This tool converts C header files to Perl extensions.
1286distribution). This tool converts C header files to Perl extensions.
1292See L<perlxstut> for how to get started with L<h2xs>.
1287See L<perlxstut> for how to get started with h2xs.
12931288
12941289=end original
12951290
12961291あなたの使っているシステムが動的ローディングをサポートしているのであれば、
1297移植性と健全性(sanity)のために、L<h2xs> を使うべきでしょう
1292移植性と健全性(sanity)のために、h2xs を使うべきでしょう
12981293(これも標準の perl 配布キットに含まれています)。
12991294このツールは、C のヘッダーファイルを Perl のエクステンションに変換します。
1300L<h2xs> の使い方は L<perlxstut> を参照してください。
1295h2xs の使い方は L<perlxstut> を参照してください。
13011296
13021297=begin original
13031298
13041299If your system doesn't support dynamic loading, you still probably
1305ought to use L<h2xs>. See L<perlxstut> and L<ExtUtils::MakeMaker> for
1300ought to use h2xs. See L<perlxstut> and L<ExtUtils::MakeMaker> for
13061301more information (in brief, just use B<make perl> instead of a plain
13071302B<make> to rebuild perl with a new static extension).
13081303
13091304=end original
13101305
13111306あなたの使っているシステムが動的ローディングをサポートしていない
1312場合であっても、やはり L<h2xs> を使うべきでしょう。
1307場合であっても、やはり h2xs を使うべきでしょう。
13131308より詳しい情報は L<perlxstut> と L<ExtUtils::MakeMaker> を参照してください
13141309(簡単に言うと、新しい静的エクステションを伴った perl を再ビルドするのに
13151310通常の B<make> を使うのではなく、B<make perl> を使うだけです)。
13161311
13171312=head2 Why do setuid perl scripts complain about kernel problems?
13181313
13191314(なぜ setuid された perl スクリプトはカーネルの問題について文句を言うのでしょうか?)
13201315
13211316=begin original
13221317
13231318Some operating systems have bugs in the kernel that make setuid
1324scripts inherently insecure. Perl gives you a number of options
1319scripts inherently insecure. Perl gives you a number of options
13251320(described in L<perlsec>) to work around such systems.
13261321
13271322=end original
13281323
13291324幾つかのオペレーティングシステムは、setuid スクリプトを本質的に
13301325安全でなくするようなカーネルのバグを抱えています。
13311326Perl は、そういったシステムに対処して動作させるための幾つかのオプションを
13321327持っています(L<perlsec> に説明があります)。
13331328
13341329=head2 How can I open a pipe both to and from a command?
13351330
13361331(あるコマンドに対する双方向のパイプをオープンするには?)
13371332
13381333=begin original
13391334
1340The L<IPC::Open2> module (part of the standard perl distribution) is
1335The IPC::Open2 module (part of the standard perl distribution) is an
1341an easy-to-use approach that internally uses C<pipe()>, C<fork()>, and
1336easy-to-use approach that internally uses pipe(), fork(), and exec() to do
1342C<exec()> to do the job. Make sure you read the deadlock warnings in
1337the job. Make sure you read the deadlock warnings in its documentation,
1343its documentation, though (see L<IPC::Open2>). See
1338though (see L<IPC::Open2>). See
13441339L<perlipc/"Bidirectional Communication with Another Process"> and
13451340L<perlipc/"Bidirectional Communication with Yourself">
13461341
13471342=end original
13481343
1349L<IPC::Open2> モジュール(標準の perl の配布に含まれています)は内部的に
1344IPC::Open2 モジュール(標準の perl の配布に含まれています)は内部的に
1350C<pipe()>, C<fork()>, C<exec()> を使った使いやすい手法です。
1345pipe()fork()exec() を使った使いやすい手法です。
13511346ただし、ドキュメントにあるデッドロックの警告をよく読んでください
13521347(L<IPC::Open2>を参照)。
13531348L<perlipc/"Bidirectional Communication with Another Process"> と
13541349L<perlipc/"Bidirectional Communication with Yourself"> も参照してください。
13551350
13561351=begin original
13571352
1358You may also use the L<IPC::Open3> module (part of the standard perl
1353You may also use the IPC::Open3 module (part of the standard perl
13591354distribution), but be warned that it has a different order of
1360arguments from L<IPC::Open2> (see L<IPC::Open3>).
1355arguments from IPC::Open2 (see L<IPC::Open3>).
13611356
13621357=end original
13631358
1364L<IPC::Open3> モジュール(標準配布パッケージに含まれています)も使えますが、
1359IPC::Open3 モジュール(標準配布パッケージに含まれています)も使えますが、
1365これは L<IPC::Open2> とは引数の順序が違うということに注意してください
1360これは IPC::Open2 とは引数の順序が違うということに注意してください
13661361(L<IPC::Open3> を参照してください)。
13671362
13681363=head2 Why can't I get the output of a command with system()?
13691364
13701365(なぜ system() を使ったコマンドの出力を得ることができないのでしょうか?)
13711366
13721367=begin original
13731368
1374You're confusing the purpose of C<system()> and backticks (``). C<system()>
1369You're confusing the purpose of system() and backticks (``). system()
13751370runs a command and returns exit status information (as a 16 bit value:
13761371the low 7 bits are the signal the process died from, if any, and
1377the high 8 bits are the actual exit value). Backticks (``) run a
1372the high 8 bits are the actual exit value). Backticks (``) run a
13781373command and return what it sent to STDOUT.
13791374
13801375=end original
13811376
1382C<system()> の目的と逆クォートの目的を混同しているのでしょう。
1377system() の目的と逆クォートの目的を混同しているのでしょう。
1383C<system()> はコマンドを実行して、終了ステータス情報(16bit 値として:
1378system() はコマンドを実行して、終了ステータス情報(16bit 値として:
13841379下位 7bit は(もしあれば)終了したプロセスからのシグナルで、上位 8bit は
13851380実際の終了ステータス)を返します。
13861381逆クォートはコマンドを実行し、そのコマンドが STDOUT に送ったものを返します。
13871382
1388 my $exit_status = system("mail-users");
1383 $exit_status = system("mail-users");
1389 my $output_string = `ls`;
1384 $output_string = `ls`;
13901385
13911386=head2 How can I capture STDERR from an external command?
13921387
13931388(外部コマンドの STDERR を捕捉するには?)
13941389
13951390=begin original
13961391
13971392There are three basic ways of running external commands:
13981393
13991394=end original
14001395
14011396外部コマンドを実行する基本的なやり方が三つあります:
14021397
1403 system $cmd; # using system()
1398 system $cmd; # using system()
1404 my $output = `$cmd`; # using backticks (``)
1399 $output = `$cmd`; # using backticks (``)
1405 open (my $pipe_fh, "$cmd |"); # using open()
1400 open (PIPE, "cmd |"); # using open()
14061401
14071402=begin original
14081403
1409With C<system()>, both STDOUT and STDERR will go the same place as the
1404With system(), both STDOUT and STDERR will go the same place as the
1410script's STDOUT and STDERR, unless the C<system()> command redirects them.
1405script's STDOUT and STDERR, unless the system() command redirects them.
1411Backticks and C<open()> read B<only> the STDOUT of your command.
1406Backticks and open() read B<only> the STDOUT of your command.
14121407
14131408=end original
14141409
1415C<system()> を使った場合の STDOUT と STDERR は、C<system()> コマンドが
1410system() を使った場合の STDOUT と STDERR は、system() コマンドが
14161411リダイレクトを行っていない限りはそれぞれスクリプトが使っていた
14171412STDOUT と STDERR へ出力されます。
1418逆クォートC<open()> はコマンドの STDOUT B<だけを> 読み込みます。
1413backticks と open() はコマンドの STDOUT B<だけを> 読み込みます。
14191414
14201415=begin original
14211416
1422You can also use the C<open3()> function from L<IPC::Open3>. Benjamin
1417You can also use the open3() function from IPC::Open3. Benjamin
14231418Goldberg provides some sample code:
14241419
14251420=end original
14261421
1427L<IPC::Open3>C<open3()> 関数も使えます。
1422IPC::Open3 の open3() 関数も使えます。
14281423Benjamin Goldberg がいくつかのサンプルコードを提供しています:
14291424
14301425=begin original
14311426
14321427To capture a program's STDOUT, but discard its STDERR:
14331428
14341429=end original
14351430
14361431あるプログラムの STDOUT は捕捉したいが、STDERR は捨てたい場合:
14371432
1438 use IPC::Open3;
1433 use IPC::Open3;
1439 use File::Spec;
1434 use File::Spec;
1440 use Symbol qw(gensym);
1435 use Symbol qw(gensym);
1441 open(NULL, ">", File::Spec->devnull);
1436 open(NULL, ">", File::Spec->devnull);
1442 my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
1437 my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
1443 while( <PH> ) { }
1438 while( <PH> ) { }
1444 waitpid($pid, 0);
1439 waitpid($pid, 0);
14451440
14461441=begin original
14471442
14481443To capture a program's STDERR, but discard its STDOUT:
14491444
14501445=end original
14511446
14521447あるプログラムの STDERR は捕捉したいが、STDOUT は捨てたい場合:
14531448
1454 use IPC::Open3;
1449 use IPC::Open3;
1455 use File::Spec;
1450 use File::Spec;
1456 use Symbol qw(gensym);
1451 use Symbol qw(gensym);
1457 open(NULL, ">", File::Spec->devnull);
1452 open(NULL, ">", File::Spec->devnull);
1458 my $pid = open3(gensym, ">&NULL", \*PH, "cmd");
1453 my $pid = open3(gensym, ">&NULL", \*PH, "cmd");
1459 while( <PH> ) { }
1454 while( <PH> ) { }
1460 waitpid($pid, 0);
1455 waitpid($pid, 0);
14611456
14621457=begin original
14631458
14641459To capture a program's STDERR, and let its STDOUT go to our own STDERR:
14651460
14661461=end original
14671462
14681463プログラムの STDERR を捕捉してその STDOUT を自身の STDERR に送るには:
14691464
1470 use IPC::Open3;
1465 use IPC::Open3;
1471 use Symbol qw(gensym);
1466 use Symbol qw(gensym);
1472 my $pid = open3(gensym, ">&STDERR", \*PH, "cmd");
1467 my $pid = open3(gensym, ">&STDERR", \*PH, "cmd");
1473 while( <PH> ) { }
1468 while( <PH> ) { }
1474 waitpid($pid, 0);
1469 waitpid($pid, 0);
14751470
14761471=begin original
14771472
14781473To read both a command's STDOUT and its STDERR separately, you can
14791474redirect them to temp files, let the command run, then read the temp
14801475files:
14811476
14821477=end original
14831478
14841479あるコマンドの STDOUT と STDERR を別々に読み込みたい場合、それをテンポラリ
14851480ファイルにリダイレクトして、コマンドを実行して、テンポラリファイルから
14861481読み込みます:
14871482
1488 use IPC::Open3;
1483 use IPC::Open3;
1489 use Symbol qw(gensym);
1484 use Symbol qw(gensym);
1490 use IO::File;
1485 use IO::File;
1491 local *CATCHOUT = IO::File->new_tmpfile;
1486 local *CATCHOUT = IO::File->new_tmpfile;
1492 local *CATCHERR = IO::File->new_tmpfile;
1487 local *CATCHERR = IO::File->new_tmpfile;
1493 my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd");
1488 my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd");
1494 waitpid($pid, 0);
1489 waitpid($pid, 0);
1495 seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
1490 seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
1496 while( <CATCHOUT> ) {}
1491 while( <CATCHOUT> ) {}
1497 while( <CATCHERR> ) {}
1492 while( <CATCHERR> ) {}
14981493
14991494=begin original
15001495
1501But there's no real need for B<both> to be tempfiles... the following
1496But there's no real need for *both* to be tempfiles... the following
15021497should work just as well, without deadlocking:
15031498
15041499=end original
15051500
1506しかし、実際には B<両方を> テンポラリファイルにするする必要はありません…。
1501しかし、実際には両方をテンポラリファイルにするする必要はありません…。
15071502以下のものもデッドロックなしにうまく動きます:
15081503
1509 use IPC::Open3;
1504 use IPC::Open3;
1510 use Symbol qw(gensym);
1505 use Symbol qw(gensym);
1511 use IO::File;
1506 use IO::File;
1512 local *CATCHERR = IO::File->new_tmpfile;
1507 local *CATCHERR = IO::File->new_tmpfile;
1513 my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd");
1508 my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd");
1514 while( <CATCHOUT> ) {}
1509 while( <CATCHOUT> ) {}
1515 waitpid($pid, 0);
1510 waitpid($pid, 0);
1516 seek CATCHERR, 0, 0;
1511 seek CATCHERR, 0, 0;
1517 while( <CATCHERR> ) {}
1512 while( <CATCHERR> ) {}
15181513
15191514=begin original
15201515
15211516And it'll be faster, too, since we can begin processing the program's
15221517stdout immediately, rather than waiting for the program to finish.
15231518
15241519=end original
15251520
15261521そして、これはプログラムの終了を待つのではなく、プログラムの標準出力を直ちに
15271522処理するので、より速いはずです。
15281523
15291524=begin original
15301525
15311526With any of these, you can change file descriptors before the call:
15321527
15331528=end original
15341529
15351530これらのどれでも、呼び出しの前にファイル記述子を変更できます:
15361531
1537 open(STDOUT, ">logfile");
1532 open(STDOUT, ">logfile");
1538 system("ls");
1533 system("ls");
15391534
15401535=begin original
15411536
15421537or you can use Bourne shell file-descriptor redirection:
15431538
15441539=end original
15451540
15461541Bourne シェルのファイル記述子リダイレクションを使うこともできます:
15471542
1548 $output = `$cmd 2>some_file`;
1543 $output = `$cmd 2>some_file`;
1549 open (PIPE, "cmd 2>some_file |");
1544 open (PIPE, "cmd 2>some_file |");
15501545
15511546=begin original
15521547
15531548You can also use file-descriptor redirection to make STDERR a
15541549duplicate of STDOUT:
15551550
15561551=end original
15571552
15581553同様に、STDERR を STDOUT の複製にするためにファイル記述子
15591554リダイレクションを使うこともできます。
15601555
1561 $output = `$cmd 2>&1`;
1556 $output = `$cmd 2>&1`;
1562 open (PIPE, "cmd 2>&1 |");
1557 open (PIPE, "cmd 2>&1 |");
15631558
15641559=begin original
15651560
15661561Note that you I<cannot> simply open STDERR to be a dup of STDOUT
15671562in your Perl program and avoid calling the shell to do the redirection.
15681563This doesn't work:
15691564
15701565=end original
15711566
15721567STDOUT の複製のために、Perl プログラムの中で単純に STDERR を
15731568オープンすることは B<できない> ということと、リダイレクトのための
15741569シェルの呼び出しを避けることはできないということに注意してください。
15751570以下の例はうまくいきません:
15761571
1577 open(STDERR, ">&STDOUT");
1572 open(STDERR, ">&STDOUT");
1578 $alloutput = `cmd args`; # stderr still escapes
1573 $alloutput = `cmd args`; # stderr still escapes
15791574
15801575=begin original
15811576
1582This fails because the C<open()> makes STDERR go to where STDOUT was
1577This fails because the open() makes STDERR go to where STDOUT was
1583going at the time of the C<open()>. The backticks then make STDOUT go to
1578going at the time of the open(). The backticks then make STDOUT go to
15841579a string, but don't change STDERR (which still goes to the old
15851580STDOUT).
15861581
15871582=end original
15881583
1589これは C<open()> が、STDERR を(C<open()> が呼び出された時点で)STDOUT が
1584これは open() が、STDERR を(open() が呼び出された時点で)STDOUT が
15901585使っていた場所に対応するようにするので失敗します。
15911586その後で逆クォートは STDOUT(に出力された内容)を文字列にしますが、
15921587STDERR を変更することはしません(これは以前の STDOUT が指していたところです)。
15931588
15941589=begin original
15951590
1596Note that you I<must> use Bourne shell (C<sh(1)>) redirection syntax in
1591Note that you I<must> use Bourne shell (sh(1)) redirection syntax in
1597backticks, not C<csh(1)>! Details on why Perl's C<system()> and backtick
1592backticks, not csh(1)! Details on why Perl's system() and backtick
15981593and pipe opens all use the Bourne shell are in the
15991594F<versus/csh.whynot> article in the "Far More Than You Ever Wanted To
1600Know" collection in L<http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz> . To
1595Know" collection in http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz . To
16011596capture a command's STDERR and STDOUT together:
16021597
16031598=end original
16041599
1605backticlsの中では、C<csh(1)> のリダイレクト構文ではなく、Bourne shell
1600backticlsの中では、csh(1) のリダイレクト構文ではなく、Bourne shell
1606(C<sh(1)>) のリダイレクト構文を B<使わなければならない> ということに
1601(sh(1)) のリダイレクト構文を B<使わなければならない> ということに
16071602注意してください!
1608なぜ Perl の C<system()>逆クォート、パイプオープンの全てで Bourne シェルの
1603なぜ Perl の system()、backticks、パイプオープンの全てで Bourne シェルの
1609ものを使うかは L<http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz> にある
1604ものを使うかは http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz にある
16101605"Far More Than You Ever Wanted To Know" の F<versus/csh.whynot> という記事で
16111606説明されています。
16121607あるコマンドの標準出力と標準エラー出力を両方とも捉えるには:
16131608
1614 $output = `cmd 2>&1`; # either with backticks
1609 $output = `cmd 2>&1`; # either with backticks
1615 $pid = open(PH, "cmd 2>&1 |"); # or with an open pipe
1610 $pid = open(PH, "cmd 2>&1 |"); # or with an open pipe
1616 while (<PH>) { } # plus a read
1611 while (<PH>) { } # plus a read
16171612
16181613=begin original
16191614
16201615To capture a command's STDOUT but discard its STDERR:
16211616
16221617=end original
16231618
16241619あるコマンドの標準出力を捉え、標準エラー出力を捨てるには:
16251620
1626 $output = `cmd 2>/dev/null`; # either with backticks
1621 $output = `cmd 2>/dev/null`; # either with backticks
1627 $pid = open(PH, "cmd 2>/dev/null |"); # or with an open pipe
1622 $pid = open(PH, "cmd 2>/dev/null |"); # or with an open pipe
1628 while (<PH>) { } # plus a read
1623 while (<PH>) { } # plus a read
16291624
16301625=begin original
16311626
16321627To capture a command's STDERR but discard its STDOUT:
16331628
16341629=end original
16351630
16361631あるコマンドの標準エラー出力を捉え、標準出力を捨てるには:
16371632
1638 $output = `cmd 2>&1 1>/dev/null`; # either with backticks
1633 $output = `cmd 2>&1 1>/dev/null`; # either with backticks
1639 $pid = open(PH, "cmd 2>&1 1>/dev/null |"); # or with an open pipe
1634 $pid = open(PH, "cmd 2>&1 1>/dev/null |"); # or with an open pipe
1640 while (<PH>) { } # plus a read
1635 while (<PH>) { } # plus a read
16411636
16421637=begin original
16431638
16441639To exchange a command's STDOUT and STDERR in order to capture the STDERR
16451640but leave its STDOUT to come out our old STDERR:
16461641
16471642=end original
16481643
16491644あるコマンドの標準エラー出力を捉えるために
16501645標準出力と標準エラー出力を入れ替えるが、標準出力に
16511646古い標準エラー出力に出るようにするには:
16521647
1653 $output = `cmd 3>&1 1>&2 2>&3 3>&-`; # either with backticks
1648 $output = `cmd 3>&1 1>&2 2>&3 3>&-`; # either with backticks
1654 $pid = open(PH, "cmd 3>&1 1>&2 2>&3 3>&-|");# or with an open pipe
1649 $pid = open(PH, "cmd 3>&1 1>&2 2>&3 3>&-|");# or with an open pipe
1655 while (<PH>) { } # plus a read
1650 while (<PH>) { } # plus a read
16561651
16571652=begin original
16581653
16591654To read both a command's STDOUT and its STDERR separately, it's easiest
16601655to redirect them separately to files, and then read from those files
16611656when the program is done:
16621657
16631658=end original
16641659
16651660標準出力と標準エラー出力の両方を分けて読み出すには、
16661661別々のファイルにリダイレクトしてしまって、
16671662その後でそのファイルをプログラムから読むというのが最も簡単な方法です:
16681663
1669 system("program args 1>program.stdout 2>program.stderr");
1664 system("program args 1>program.stdout 2>program.stderr");
16701665
16711666=begin original
16721667
1673Ordering is important in all these examples. That's because the shell
1668Ordering is important in all these examples. That's because the shell
16741669processes file descriptor redirections in strictly left to right order.
16751670
16761671=end original
16771672
16781673これらの例では順序が重要です。
16791674なぜなら、シェルがリダイレクトのためのファイル記述子を処理する順序は
16801675正確に左から右へという順になっているからです。
16811676
1682 system("prog args 1>tmpfile 2>&1");
1677 system("prog args 1>tmpfile 2>&1");
1683 system("prog args 2>&1 1>tmpfile");
1678 system("prog args 2>&1 1>tmpfile");
16841679
16851680=begin original
16861681
16871682The first command sends both standard out and standard error to the
1688temporary file. The second command sends only the old standard output
1683temporary file. The second command sends only the old standard output
16891684there, and the old standard error shows up on the old standard out.
16901685
16911686=end original
16921687
16931688最初のコマンドは標準出力と標準エラー出力の両方を一時ファイルに送ります。
16941689二番目のコマンドは古い標準出力だけをファイルへと送り、古い標準エラー出力は
16951690古い標準出力へと送り出します。
16961691
16971692=head2 Why doesn't open() return an error when a pipe open fails?
16981693
16991694(なぜ open() は パイプのオープンに失敗したときにエラーを返さないのでしょうか?)
17001695
17011696=begin original
17021697
1703If the second argument to a piped C<open()> contains shell
1698If the second argument to a piped open() contains shell
1704metacharacters, perl C<fork()>s, then C<exec()>s a shell to decode the
1699metacharacters, perl fork()s, then exec()s a shell to decode the
1705metacharacters and eventually run the desired program. If the program
1700metacharacters and eventually run the desired program. If the program
17061701couldn't be run, it's the shell that gets the message, not Perl. All
17071702your Perl program can find out is whether the shell itself could be
1708successfully started. You can still capture the shell's STDERR and
1703successfully started. You can still capture the shell's STDERR and
1709check it for error messages. See L<"How can I capture STDERR from an
1704check it for error messages. See L<"How can I capture STDERR from an
17101705external command?"> elsewhere in this document, or use the
1711L<IPC::Open3> module.
1706IPC::Open3 module.
17121707
17131708=end original
17141709
1715パイプされた C<open()> の第二引数にシェルのメタ文字が含まれている場合、
1710パイプされた open() の第二引数にシェルのメタ文字が含まれている場合、
1716perl は C<fork()> し、メタ文字をデコードするためにシェルを C<exec()> し、
1711perl は fork() し、メタ文字をデコードするためにシェルを exec() し、
17171712最終的に必要なプログラムを実行します。
17181713このプログラムが実行できない場合、メッセージを得るのはシェルであって、
17191714Perl ではありません。
17201715あなたの Perl プログラムがわかることはシェル自身が正しく開始されたかどうか
17211716だけです。
17221717エラーメッセージのためにシェルの STDERR を捕捉してチェックするという方法は
17231718あります。
17241719この文書の別の場所にある L<"How can I capture STDERR from an external
1725command?"> を参照するか、L<IPC::Open3> モジュールを使ってください。
1720command?"> を参照するか、IPC::Open3 モジュールを使ってください。
17261721
17271722=begin original
17281723
1729If there are no shell metacharacters in the argument of C<open()>, Perl
1724If there are no shell metacharacters in the argument of open(), Perl
17301725runs the command directly, without using the shell, and can correctly
17311726report whether the command started.
17321727
17331728=end original
17341729
1735C<open()> の引数にシェルのメタ文字がないなら、Perl はコマンドをシェルを使わずに
1730open() の引数にシェルのメタ文字がないなら、Perl はコマンドをシェルを使わずに
17361731直接実行し、コマンドが開始したかどうかを正しく報告できます。
17371732
17381733=head2 What's wrong with using backticks in a void context?
17391734
17401735(無効コンテキストで逆クォートを使うことのなにが悪いのでしょうか?)
17411736
17421737=begin original
17431738
1744Strictly speaking, nothing. Stylistically speaking, it's not a good
1739Strictly speaking, nothing. Stylistically speaking, it's not a good
1745way to write maintainable code. Perl has several operators for
1740way to write maintainable code. Perl has several operators for
1746running external commands. Backticks are one; they collect the output
1741running external commands. Backticks are one; they collect the output
1747from the command for use in your program. The C<system> function is
1742from the command for use in your program. The C<system> function is
17481743another; it doesn't do this.
17491744
17501745=end original
17511746
17521747厳密に言えばありません。
17531748形式的に言えば、保守しやすいコードを書くための良い方法ではありません。
17541749Perl は外部コマンドを実行するためのいくつかの演算子があります。
17551750逆クォートはその一つです; これはコマンドからの出力を、自身のプログラムで
17561751使うために収集します。
17571752C<system> 関数はもう一つのものです; これはコマンドからの出力を収集しません。
17581753
17591754=begin original
17601755
17611756Writing backticks in your program sends a clear message to the readers
17621757of your code that you wanted to collect the output of the command.
17631758Why send a clear message that isn't true?
17641759
17651760=end original
17661761
17671762プログラム中に逆クォートを書くことで、そのコマンドの出力を使いたいという
17681763はっきりしたメッセージをあなたのコードの読者に送ります。
17691764なぜ正しくないはっきりしたメッセージを送るのですか?
17701765
17711766=begin original
17721767
17731768Consider this line:
17741769
17751770=end original
17761771
17771772以下のような行を考えてみましょう:
17781773
1779 `cat /etc/termcap`;
1774 `cat /etc/termcap`;
17801775
17811776=begin original
17821777
17831778You forgot to check C<$?> to see whether the program even ran
1784correctly. Even if you wrote
1779correctly. Even if you wrote
17851780
17861781=end original
17871782
17881783プログラムが正しく実行されたかどうかを確認するために
17891784C<$?> を見るのを忘れています。
1790以下のように書いたとしても
17911785
1792 print `cat /etc/termcap`;
1786 print `cat /etc/termcap`;
17931787
17941788=begin original
17951789
17961790this code could and probably should be written as
17971791
17981792=end original
17991793
18001794このコードは、以下のように書くべきでしょう
18011795
1802 system("cat /etc/termcap") == 0
1796 system("cat /etc/termcap") == 0
1803 or die "cat program failed!";
1797 or die "cat program failed!";
18041798
18051799=begin original
18061800
18071801which will echo the cat command's output as it is generated, instead
18081802of waiting until the program has completed to print it out. It also
18091803checks the return value.
18101804
18111805=end original
18121806
18131807これは、プログラムが出力を終えるのを待つのではなく、cat コマンドの出力が
18141808生成される毎にエコーします。
18151809これはまたその戻り値のチェックも行います。
18161810
18171811=begin original
18181812
18191813C<system> also provides direct control over whether shell wildcard
18201814processing may take place, whereas backticks do not.
18211815
18221816=end original
18231817
18241818system() はまた、シェルのワイルドカード処理を行えるかどうかを直接
18251819制御しますが、逆クォートはそういった制御は行いません。
18261820
18271821=head2 How can I call backticks without shell processing?
18281822
18291823(シェルの処理をせずに逆クォートを呼び出すには?)
18301824
18311825=begin original
18321826
1833This is a bit tricky. You can't simply write the command
1827This is a bit tricky. You can't simply write the command
18341828like this:
18351829
18361830=end original
18371831
18381832ちょっとトリッキーになります。
18391833単純に以下のようにはコマンドを書けません:
18401834
1841 @ok = `grep @opts '$search_string' @filenames`;
1835 @ok = `grep @opts '$search_string' @filenames`;
18421836
18431837=begin original
18441838
18451839As of Perl 5.8.0, you can use C<open()> with multiple arguments.
18461840Just like the list forms of C<system()> and C<exec()>, no shell
18471841escapes happen.
18481842
18491843=end original
18501844
18511845Perl 5.8.0 から、複数の引数の C<open()> を使えます。
18521846リスト形式の C<system()> や C<exec()> と同様に、シェルエスケープは
18531847起きません。
18541848
1855 open( GREP, "-|", 'grep', @opts, $search_string, @filenames );
1849 open( GREP, "-|", 'grep', @opts, $search_string, @filenames );
1856 chomp(@ok = <GREP>);
1850 chomp(@ok = <GREP>);
1857 close GREP;
1851 close GREP;
18581852
18591853=begin original
18601854
18611855You can also:
18621856
18631857=end original
18641858
18651859以下のようにもできます:
18661860
1867 my @ok = ();
1861 my @ok = ();
1868 if (open(GREP, "-|")) {
1862 if (open(GREP, "-|")) {
1869 while (<GREP>) {
1863 while (<GREP>) {
1870 chomp;
1864 chomp;
1871 push(@ok, $_);
1865 push(@ok, $_);
1872 }
1866 }
1873 close GREP;
1867 close GREP;
1874 } else {
1868 } else {
1875 exec 'grep', @opts, $search_string, @filenames;
1869 exec 'grep', @opts, $search_string, @filenames;
1876 }
1870 }
18771871
18781872=begin original
18791873
18801874Just as with C<system()>, no shell escapes happen when you C<exec()> a
18811875list. Further examples of this can be found in L<perlipc/"Safe Pipe
18821876Opens">.
18831877
18841878=end original
18851879
18861880C<system()> を使ったときと同じく、シェルエスケープは C<exec()> のリストに
18871881対して行われません。
18881882さらなる例が L<perlipc/"Safe Pipe Opens"> にあります。
18891883
18901884=begin original
18911885
18921886Note that if you're using Windows, no solution to this vexing issue is
1893even possible. Even though Perl emulates C<fork()>, you'll still be
1887even possible. Even if Perl were to emulate C<fork()>, you'd still be
18941888stuck, because Windows does not have an argc/argv-style API.
18951889
18961890=end original
18971891
18981892もしあなたが Windows を使っているのなら、この悩ましい問題を
18991893解決する方法は全くありません。
1900たとえ Perl が C<fork()> をエミュレートしたとしても、まだうまくいきません;
1894たとえ Perl が C<fork()> をエミュレートしたとしても、まだうまくいきません
19011895なぜならマイクロソフトは argc/argv 形式の API を提供していないからです。
19021896
19031897=head2 Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?
19041898
19051899(なぜ EOF(UNIX での^D、MS-DOS での^Z)を受け取った後で STDIN から読み込むことができないの?)
19061900
19071901=begin original
19081902
1909This happens only if your perl is compiled to use stdio instead of
1903Some stdio's set error and eof flags that need clearing. The
1910perlio, which is the default. Some (maybe all?) stdios set error and
1904POSIX module defines clearerr() that you can use. That is the
1911eof flags that you may need to clear. The L<POSIX> module defines
1905technically correct way to do it. Here are some less reliable
1912C<clearerr()> that you can use. That is the technically correct way to
1906workarounds:
1913do it. Here are some less reliable workarounds:
19141907
19151908=end original
19161909
1917これは、デフォルトである perlio ではなく、stdio を使ってコンパイルされ
1910一部の stdio ではエラーフラグと eof フラグがセットされ
1918perl でのみ起こります。
1919一部の(おそらく全ての?) stdio ではエラーフラグと eof フラグがセットされ、
19201911それをクリアする必要があるからです。
1921L<POSIX> モジュールはこのために使える C<clearerr()> を定義しています。
1912POSIX モジュールはこのために使える clearerr() を定義しています。
19221913これは、フラグをクリアするための技術的に正しい方法です。
19231914以下の方法はこれよりは信頼性にかけるやり方です:
19241915
19251916=over 4
19261917
19271918=item 1
19281919
19291920=begin original
19301921
19311922Try keeping around the seekpointer and go there, like this:
19321923
19331924=end original
19341925
19351926以下の例のように、シークポインタを保存しておいてそこへ移動します:
19361927
1937 my $where = tell($log_fh);
1928 $where = tell(LOG);
1938 seek($log_fh, $where, 0);
1929 seek(LOG, $where, 0);
19391930
19401931=item 2
19411932
19421933=begin original
19431934
19441935If that doesn't work, try seeking to a different part of the file and
19451936then back.
19461937
19471938=end original
19481939
19491940上のやりかたがだめなら、一度ファイルの別の部分にシークして、それから
19501941元の場所にシークするようにします。
19511942
19521943=item 3
19531944
19541945=begin original
19551946
19561947If that doesn't work, try seeking to a different part of
19571948the file, reading something, and then seeking back.
19581949
19591950=end original
19601951
19611952これでもだめなら、ファイルの別の部分にシークして何かを読み出し、それから
19621953元の場所にシークするようにします。
19631954
19641955=item 4
19651956
19661957=begin original
19671958
19681959If that doesn't work, give up on your stdio package and use sysread.
19691960
19701961=end original
19711962
19721963これでだめなら stdio パッケージで行うことはあきらめて、sysread を使います。
19731964
19741965=back
19751966
19761967=head2 How can I convert my shell script to perl?
19771968
19781969(私のシェルスクリプトを perl に変換するには?)
19791970
19801971=begin original
19811972
1982Learn Perl and rewrite it. Seriously, there's no simple converter.
1973Learn Perl and rewrite it. Seriously, there's no simple converter.
19831974Things that are awkward to do in the shell are easy to do in Perl, and
19841975this very awkwardness is what would make a shell->perl converter
1985nigh-on impossible to write. By rewriting it, you'll think about what
1976nigh-on impossible to write. By rewriting it, you'll think about what
19861977you're really trying to do, and hopefully will escape the shell's
19871978pipeline datastream paradigm, which while convenient for some matters,
19881979causes many inefficiencies.
19891980
19901981=end original
19911982
19921983Perl を学び、自分で書き直しましょう。
19931984まじめな話、単純なコンバータというものはありません。
19941985シェルで行うには不格好になるようなことも Perl では簡単に行うことができ、
19951986そして、このとても不格好なことがシェル→ perl コンバーターを作製することを
19961987ほとんど不可能なことにしているのです。
19971988自分で書き換えをすることで、あなたは自分が本当に試すべきことについて
19981989考えるようになり、シェルの(便利なときもあるものの多くの非効率を持ち込む)
19991990パイプラインデータストリームパラダイムから逃れることができるでしょう。
20001991
20011992=head2 Can I use perl to run a telnet or ftp session?
20021993
20031994(telnet や ftp のセッションを実行するために perl を使うことができますか?)
20041995
20051996=begin original
20061997
2007Try the L<Net::FTP>, L<TCP::Client>, and L<Net::Telnet> modules
1998Try the Net::FTP, TCP::Client, and Net::Telnet modules (available from
2008(available from CPAN).
1999CPAN). http://www.cpan.org/scripts/netstuff/telnet.emul.shar
2009L<http://www.cpan.org/scripts/netstuff/telnet.emul.shar> will also help
2000will also help for emulating the telnet protocol, but Net::Telnet is
2010for emulating the telnet protocol, but L<Net::Telnet> is quite
2001quite probably easier to use..
2011probably easier to use.
20122002
20132003=end original
20142004
2015L<Net::FTP>, L<TCP::Client>, L<Net::Telnet> といったモジュール(CPAN で
2005Net::FTP, TCP::Client, Net::Telnet といったモジュール(CPAN で入手可能です)を
2016入手可能です)を試してみてください。
2006試してみてください。
2017L<http://www.cpan.org/scripts/netstuff/telnet.emul.shar>
2007http://www.cpan.org/scripts/netstuff/telnet.emul.shar も telnet プロトコルを
2018telnet プロトコルをエミュレートする助けになるでしょうが、L<Net::Telnet>
2008エミュレートする助けになるでしょうが、Net::Telnet は使うのがとても簡単です。
2019使うのがとても簡単です。
20202009
20212010=begin original
20222011
20232012If all you want to do is pretend to be telnet but don't need
20242013the initial telnet handshaking, then the standard dual-process
20252014approach will suffice:
20262015
20272016=end original
20282017
20292018あなたのやりたいことが telnet のふりをすることであっても初期化時の
20302019telnet のハンドシェイクを必要としないのであれば、
20312020標準的な dual-process アプローチで十分でしょう。
20322021
2033 use IO::Socket; # new in 5.004
2022 use IO::Socket; # new in 5.004
2034 my $handle = IO::Socket::INET->new('www.perl.com:80')
2023 $handle = IO::Socket::INET->new('www.perl.com:80')
2035 or die "can't connect to port 80 on www.perl.com $!";
2024 or die "can't connect to port 80 on www.perl.com: $!";
2036 $handle->autoflush(1);
2025 $handle->autoflush(1);
2037 if (fork()) { # XXX: undef means failure
2026 if (fork()) { # XXX: undef means failure
2038 select($handle);
2027 select($handle);
2039 print while <STDIN>; # everything from stdin to socket
2028 print while <STDIN>; # everything from stdin to socket
2040 } else {
2029 } else {
2041 print while <$handle>; # everything from socket to stdout
2030 print while <$handle>; # everything from socket to stdout
2042 }
2031 }
2043 close $handle;
2032 close $handle;
2044 exit;
2033 exit;
20452034
20462035=head2 How can I write expect in Perl?
20472036
20482037( Perl で expect を書くには?)
20492038
20502039=begin original
20512040
2052Once upon a time, there was a library called F<chat2.pl> (part of the
2041Once upon a time, there was a library called chat2.pl (part of the
2053standard perl distribution), which never really got finished. If you
2042standard perl distribution), which never really got finished. If you
2054find it somewhere, I<don't use it>. These days, your best bet is to
2043find it somewhere, I<don't use it>. These days, your best bet is to
2055look at the L<Expect> module available from CPAN, which also requires two
2044look at the Expect module available from CPAN, which also requires two
2056other modules from CPAN, L<IO::Pty> and L<IO::Stty>.
2045other modules from CPAN, IO::Pty and IO::Stty.
20572046
20582047=end original
20592048
2060昔々、F<chat2.pl> と呼ばれたライブラリがありました(これは標準の perl
2049昔々、chat2.pl と呼ばれたライブラリがありました(これは標準の perl
2061配布キットに含まれます); 実際には完成していませんでした
2050配布キットに含まれます)。
2062もしこれをどこかで見つけても I<使ってはいけません>。
2051もしこれをどこかで見つけてもB<使ってはいけません>。
2063今日では、CPAN にある L<IO::Pty>L<IO::Stty> といった
2052今日では、CPAN にある IO::Pty や IO::Stty といった
2064ライブラリを使う L<Expect> を探すのが最善でしょう。
2053ライブラリを探すのが最善でしょう。
20652054
20662055=head2 Is there a way to hide perl's command line from programs such as "ps"?
20672056
20682057(“ps”のようなプログラムから、perl のコマンドラインを隠す方法はありますか?)
20692058
20702059=begin original
20712060
20722061First of all note that if you're doing this for security reasons (to
20732062avoid people seeing passwords, for example) then you should rewrite
20742063your program so that critical information is never given as an
2075argument. Hiding the arguments won't make your program completely
2064argument. Hiding the arguments won't make your program completely
20762065secure.
20772066
20782067=end original
20792068
20802069まず初めに、あなたが(たとえば他人がパスワードを除くのを避けるためなどの)
20812070セキュリティ上の理由でそれを行おうとしてるのであれば、
20822071重要な情報が引数として与えられることがないようにプログラムを
20832072書き直すべきだということに注意してください。
20842073引数を隠すことは、あなたのプログラムを完全に安全なものにすることは
20852074ありません。
20862075
20872076=begin original
20882077
20892078To actually alter the visible command line, you can assign to the
2090variable $0 as documented in L<perlvar>. This won't work on all
2079variable $0 as documented in L<perlvar>. This won't work on all
2091operating systems, though. Daemon programs like sendmail place their
2080operating systems, though. Daemon programs like sendmail place their
20922081state there, as in:
20932082
20942083=end original
20952084
20962085外部から見えるコマンドラインを実際に書き換えるために、L<perlvar> で
20972086説明されているように $0 という変数に代入することができます。
20982087ただし、これはすべてのオペレーティングシステムで実行できるというものでは
20992088ありません。
21002089sendmail のようなデーモンプログラムは以下の例のように状態を設定します:
21012090
2102 $0 = "orcus [accepting connections]";
2091 $0 = "orcus [accepting connections]";
21032092
2104=head2 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?
2093=head2 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?
21052094
21062095(perl スクリプトの中で、ディレクトリを変更したり環境変数を変更しました。なぜ、スクリプトを終了したときこれらの変更は無効になってしまうの? 変更が反映されるようにするには?)
21072096
21082097=over 4
21092098
21102099=item Unix
21112100
21122101=begin original
21132102
21142103In the strictest sense, it can't be done--the script executes as a
2115different process from the shell it was started from. Changes to a
2104different process from the shell it was started from. Changes to a
21162105process are not reflected in its parent--only in any children
2117created after the change. There is shell magic that may allow you to
2106created after the change. There is shell magic that may allow you to
2118fake it by C<eval()>ing the script's output in your shell; check out the
2107fake it by eval()ing the script's output in your shell; check out the
21192108comp.unix.questions FAQ for details.
21202109
21212110=end original
21222111
21232112もっとも厳密な意味で言うと、それはできません--スクリプトはそれを
21242113起動したシェルとは異なるプロセスで実行されるのです。
21252114あるプロセスに対する変更はその親に反映されることはありません
21262115--変更した後で生成された子プロセスに対してのみ反映されます。
2127あなたの使っているシェルにおいてスクリプトの出力を C<eval()> することによって、
2116あなたの使っているシェルにおいてスクリプトの出力をeval()することによって、
2128お望みのことをしたように見せかけるシェルマジック (shell magic)があります;
2117お望みのことをしたように見せかけるシェルマジック (shell magic)があります
21292118詳しくは comp.unix.questions FAQを調べてください。
21302119
21312120=back
21322121
21332122=head2 How do I close a process's filehandle without waiting for it to complete?
21342123
21352124(プロセスの完了を待つことなしにそのファイルハンドルをクローズするには?)
21362125
21372126=begin original
21382127
21392128Assuming your system supports such things, just send an appropriate signal
2140to the process (see L<perlfunc/"kill">). It's common to first send a TERM
2129to the process (see L<perlfunc/"kill">). It's common to first send a TERM
21412130signal, wait a little bit, and then send a KILL signal to finish it off.
21422131
21432132=end original
21442133
21452134あなたの使っているシステムがそういった機能をサポートしていると仮定すると、
21462135そのプロセスに対して適切なシグナルを送るだけです
21472136(L<perlfunc/"kill"> を参照してください)。
21482137最初にTERMシグナルを送り、ちょっとだけ待って、
21492138終了させるために KILL シグナルを送るというのが一般的なものです。
21502139
21512140=head2 How do I fork a daemon process?
21522141
21532142(デーモンプロセスを fork() するには?)
21542143
21552144=begin original
21562145
21572146If by daemon process you mean one that's detached (disassociated from
21582147its tty), then the following process is reported to work on most
2159Unixish systems. Non-Unix users should check their Your_OS::Process
2148Unixish systems. Non-Unix users should check their Your_OS::Process
21602149module for other solutions.
21612150
21622151=end original
21632152
21642153あなたのいうデーモンプロセスが detach されている(tty と
21652154結び付けられていない)ものであれば、以下の手順がほとんどの UNIX 的な
21662155システムで動作するということが報告されています。
21672156非 UNIX ユーザーは Your_OS::Process モジュールで他の解決策を
21682157あたるべきでしょう。
21692158
21702159=over 4
21712160
21722161=item *
21732162
21742163=begin original
21752164
2176Open /dev/tty and use the TIOCNOTTY ioctl on it. See L<tty(1)>
2165Open /dev/tty and use the TIOCNOTTY ioctl on it. See L<tty>
2177for details. Or better yet, you can just use the C<POSIX::setsid()>
2166for details. Or better yet, you can just use the POSIX::setsid()
21782167function, so you don't have to worry about process groups.
21792168
21802169=end original
21812170
21822171/dev/tty をオープンし、それに TIOCNOTTY ioctl を使います。
2183詳しくは L<tty(1)> を参照してください。
2172詳しくは L<tty> を参照してください。
2184そのほかもっと良いのは、C<POSIX::setsid()> 関数を使うことです; これによって
2173そのほかもっと良いのは、POSIX::setsid() 関数を使うことです
2185プロセスグループに関して思い煩う必要がなくなります。
2174これによってプロセスグループに関して思い煩う必要がなくなります。
21862175
21872176=item *
21882177
21892178=begin original
21902179
21912180Change directory to /
21922181
21932182=end original
21942183
21952184/ へディレクトリを変更します。
21962185
21972186=item *
21982187
21992188=begin original
22002189
22012190Reopen STDIN, STDOUT, and STDERR so they're not connected to the old
22022191tty.
22032192
22042193=end original
22052194
2206STDIN、STDOUT、STDERR を再オープンします; これでこれらのハンドルは以前の
2195STDIN、STDOUT、STDERR を再オープンします
2207tty とは結び付けらていない状態になります。
2196これでこれらのハンドルは以前の tty とは結び付けらていない状態になります。
22082197
22092198=item *
22102199
22112200=begin original
22122201
22132202Background yourself like this:
22142203
22152204=end original
22162205
22172206以下のようにしてバックグラウンドにします:
22182207
2219 fork && exit;
2208 fork && exit;
22202209
22212210=back
22222211
22232212=begin original
22242213
2225The L<Proc::Daemon> module, available from CPAN, provides a function to
2214The Proc::Daemon module, available from CPAN, provides a function to
22262215perform these actions for you.
22272216
22282217=end original
22292218
2230CPAN で入手できる L<Proc::Daemon> モジュールはこれらの操作を
2219CPAN で入手できる Proc::Daemon モジュールはこれらの操作を
22312220行ってくれる関数を提供しています。
22322221
22332222=head2 How do I find out if I'm running interactively or not?
22342223
22352224(自分が対話的に実行されているかどうかを知るには?)
22362225
22372226=begin original
22382227
2239(contributed by brian d foy)
2228Good question. Sometimes C<-t STDIN> and C<-t STDOUT> can give clues,
2229sometimes not.
22402230
22412231=end original
22422232
2243(brian d foy によって寄贈されました)
2233良い質問です。
2234C<-t STDIN> と C<-t STDOUT> が手掛かりを与えてくれるときもあるし、
2235そうでないときもあります。
22442236
2245=begin original
2237 if (-t STDIN && -t STDOUT) {
2238 print "Now what? ";
2239 }
22462240
2247This is a difficult question to answer, and the best answer is
2248only a guess.
2249
2250=end original
2251
2252これは答えるのが難しい質問で、最良の答えは単なる推測です。
2253
22542241=begin original
22552242
2256What do you really want to know? If you merely want to know if one of
2243On POSIX systems, you can test whether your own process group matches
2257your filehandles is connected to a terminal, you can try the C<-t>
2244the current process group of your controlling terminal as follows:
2258file test:
22592245
22602246=end original
22612247
2262本当知りいことは何でょう?
2248POSIX システムでは以下のようして、あなが端末を制御ているカレントの
2263単にファイハンドルの一つが端末と接続していかどかを知りたいら、
2249プロセスグープにマッチすうなプロセスグループを
2264C<-t> ファイルテスト試すことができます:
2250所有しているかどうか検査できます:
22652251
2266 if( -t STDOUT ) {
2252 use POSIX qw/getpgrp tcgetpgrp/;
2267 print "I'm connected to a terminal!\n";
2268 }
22692253
2270=begin original
2254 # Some POSIX systems, such as Linux, can be
2255 # without a /dev/tty at boot time.
2256 if (!open(TTY, "/dev/tty")) {
2257 print "no tty\n";
2258 } else {
2259 $tpgrp = tcgetpgrp(fileno(*TTY));
2260 $pgrp = getpgrp();
2261 if ($tpgrp == $pgrp) {
2262 print "foreground\n";
2263 } else {
2264 print "background\n";
2265 }
2266 }
22712267
2272However, you might be out of luck if you expect that means there is a
2273real person on the other side. With the L<Expect> module, another
2274program can pretend to be a person. The program might even come close
2275to passing the Turing test.
2276
2277=end original
2278
2279しかし、その先に実際の人間がいることを想定するなら、うまく
2280いかないかもしれません。
2281L<Expect> では、他のプログラムは人間である振りをします。
2282プログラムはチューリングテストに通過しそうになるかもしれません。
2283
2284=begin original
2285
2286The L<IO::Interactive> module does the best it can to give you an
2287answer. Its C<is_interactive> function returns an output filehandle;
2288that filehandle points to standard output if the module thinks the
2289session is interactive. Otherwise, the filehandle is a null handle
2290that simply discards the output:
2291
2292=end original
2293
2294L<IO::Interactive> モジュールが答えとして最良のものです。
2295この C<is_interactive> 関数は出力ファイルハンドルを返します;
2296このファイルハンドルは、モジュールがセッションが対話的であると考えた
2297場合には、標準出力を示します。
2298さもなければ、ファイルハンドルは、単に出力が捨てられる空のハンドルです:
2299
2300 use IO::Interactive;
2301
2302 print { is_interactive } "I might go to standard output!\n";
2303
2304=begin original
2305
2306This still doesn't guarantee that a real person is answering your
2307prompts or reading your output.
2308
2309=end original
2310
2311これままだ実際の人間がプロンプトに答えたり出力を読んだりするのが
2312実際の人間であることを保証しません。
2313
2314=begin original
2315
2316If you want to know how to handle automated testing for your
2317distribution, you can check the environment. The CPAN
2318Testers, for instance, set the value of C<AUTOMATED_TESTING>:
2319
2320=end original
2321
2322あなたの配布での自動化テストの扱う方法を知りたいなら、環境を
2323チェックしてください。
2324例えば、CPAN Testers は C<AUTOMATED_TESTING> の値を設定します:
2325
2326 unless( $ENV{AUTOMATED_TESTING} ) {
2327 print "Hello interactive tester!\n";
2328 }
2329
23302268=head2 How do I timeout a slow event?
23312269
23322270(遅いイベントをタイムアウトするには?)
23332271
23342272=begin original
23352273
2336Use the C<alarm()> function, probably in conjunction with a signal
2274Use the alarm() function, probably in conjunction with a signal
23372275handler, as documented in L<perlipc/"Signals"> and the section on
2338"Signals" in the Camel. You may instead use the more flexible
2276"Signals" in the Camel. You may instead use the more flexible
2339L<Sys::AlarmCall> module available from CPAN.
2277Sys::AlarmCall module available from CPAN.
23402278
23412279=end original
23422280
23432281L<perlipc/"Signals"> やらくだ本の "Signal" の章で説明されているように、
2344C<alarm()> 関数と、おそらくはシグナルハンドラーを組み合わせて使います。
2282alarm() 関数と、おそらくはシグナルハンドラーを組み合わせて使います。
23452283この代わりに、CPAN にあるより柔軟性のある
2346L<Sys::AlarmCall> モジュールを使うこともできます。
2284Sys::AlarmCall モジュールを使うこともできます。
23472285
23482286=begin original
23492287
2350The C<alarm()> function is not implemented on all versions of Windows.
2288The alarm() function is not implemented on all versions of Windows.
23512289Check the documentation for your specific version of Perl.
23522290
23532291=end original
23542292
2355C<alarm()> 関数は Windows の全てのバージョンで実装されているわけではありません。
2293alarm() 関数は Windows の全てのバージョンで実装されているわけではありません。
23562294あなたの特定のバージョンの Perl のドキュメントをチェックしてください。
23572295
23582296=head2 How do I set CPU limits?
23592297X<BSD::Resource> X<limit> X<CPU>
23602298
23612299(CPU のリミットを設定するには?)
23622300
23632301=begin original
23642302
23652303(contributed by Xho)
23662304
23672305=end original
23682306
23692307(Xho によって寄贈されました)
23702308
23712309=begin original
23722310
2373Use the L<BSD::Resource> module from CPAN. As an example:
2311Use the C<BSD::Resource> module from CPAN. As an example:
23742312
23752313=end original
23762314
2377CPAN にある L<BSD::Resource> モジュールを使います。
2315CPAN にある C<BSD::Resource> モジュールを使います。
23782316例として:
23792317
2380 use BSD::Resource;
2318 use BSD::Resource;
2381 setrlimit(RLIMIT_CPU,10,20) or die $!;
2319 setrlimit(RLIMIT_CPU,10,20) or die $!;
23822320
23832321=begin original
23842322
23852323This sets the soft and hard limits to 10 and 20 seconds, respectively.
23862324After 10 seconds of time spent running on the CPU (not "wall" time),
23872325the process will be sent a signal (XCPU on some systems) which, if not
2388trapped, will cause the process to terminate. If that signal is
2326trapped, will cause the process to terminate. If that signal is
23892327trapped, then after 10 more seconds (20 seconds in total) the process
23902328will be killed with a non-trappable signal.
23912329
23922330=end original
23932331
23942332これはソフト制限とハード制限をそれぞれ 10 秒と 20 秒にセットします。
23952333CPU が 10 秒の時間を消費後("wall" 時間ではありません)、プロセスはシグナル
23962334(システムによっては XCPU)を送り、トラップされなければ、これによりプロセスは
23972335終了します。
23982336シグナルがトラップされると、さらに 10 秒(合計 20 秒)経過後、プロセスは
23992337ブロック不可シグナルで kill されます。
24002338
24012339=begin original
24022340
2403See the L<BSD::Resource> and your systems documentation for the gory
2341See the C<BSD::Resource> and your systems documentation for the gory
24042342details.
24052343
24062344=end original
24072345
2408詳細については L<BSD::Resource> とあなたのシステムのドキュメントを
2346詳細については C<BSD::Resource> とあなたのシステムのドキュメントを
24092347参照してください。
24102348
24112349=head2 How do I avoid zombies on a Unix system?
24122350
24132351(UNIX システムでゾンビを回避するには?)
24142352
24152353=begin original
24162354
2417Use the reaper code from L<perlipc/"Signals"> to call C<wait()> when a
2355Use the reaper code from L<perlipc/"Signals"> to call wait() when a
24182356SIGCHLD is received, or else use the double-fork technique described
24192357in L<perlfaq8/"How do I start a process in the background?">.
24202358
24212359=end original
24222360
2423SIGCHLD を受け取ったときに C<wait()> を呼び出すように L<perlipc/"Signals"> に
2361SIGCHLD を受け取ったときに wait() を呼び出すように L<perlipc/"Signals"> に
24242362ある刈り取り機プログラム (reaper code) を使うか、
24252363L<perlfaq8/"How do I start a process in the background?"> で
24262364説明されている double-fork テクニックを使います。
24272365
24282366=head2 How do I use an SQL database?
24292367
24302368(SQL データベースを使うには?)
24312369
24322370=begin original
24332371
2434The L<DBI> module provides an abstract interface to most database
2372The DBI module provides an abstract interface to most database
24352373servers and types, including Oracle, DB2, Sybase, mysql, Postgresql,
2436ODBC, and flat files. The DBI module accesses each database type
2374ODBC, and flat files. The DBI module accesses each database type
2437through a database driver, or DBD. You can see a complete list of
2375through a database driver, or DBD. You can see a complete list of
2438available drivers on CPAN: L<http://www.cpan.org/modules/by-module/DBD/> .
2376available drivers on CPAN: http://www.cpan.org/modules/by-module/DBD/ .
2439You can read more about DBI on L<http://dbi.perl.org/> .
2377You can read more about DBI on http://dbi.perl.org .
24402378
24412379=end original
24422380
2443L<DBI> モジュールはほとんどのデータベースサーバと型に対する抽象
2381DBI モジュールはほとんどのデータベースサーバと型に対する抽象
24442382インターフェースを提供します; これには Oracle, DB2, Sybase, mysql,
24452383Postgresql, ODBC, フラットファイルを含みます。
24462384DBI モジュールは、DBD と呼ばれるデータベースドライバを通して各データベース
24472385型にアクセスします。
24482386利用可能なドライバの完全な一覧は CPAN
2449(L<http://www.cpan.org/modules/by-module/DBD/>) にあります。
2387(http://www.cpan.org/modules/by-module/DBD/) にあります。
2450DBI に関するさらなる情報は L<http://dbi.perl.org/> にあります。
2388DBI に関するさらなる情報は http://dbi.perl.org にあります。
24512389
24522390=begin original
24532391
2454Other modules provide more specific access: L<Win32::ODBC>, L<Alzabo>,
2392Other modules provide more specific access: Win32::ODBC, Alzabo, iodbc,
2455C<iodbc>, and others found on CPAN Search: L<http://search.cpan.org/> .
2393and others found on CPAN Search: http://search.cpan.org .
24562394
24572395=end original
24582396
2459その他のモジュールはより具体的なアクセスを提供します: L<Win32::ODBC>,
2397その他のモジュールはより具体的なアクセスを提供します: Win32::ODBC, Alzabo,
2460L<Alzabo>, C<iodbc> などは CPAN Search (L<http://search.cpan.org/>) で
2398iodbc などは CPAN Search (http://search.cpan.org) で見つけられます。
2461見つけられます。
24622399
24632400=head2 How do I make a system() exit on control-C?
24642401
24652402(コントロール-C で system() が exit するようにするには?)
24662403
24672404=begin original
24682405
2469You can't. You need to imitate the C<system()> call (see L<perlipc> for
2406You can't. You need to imitate the system() call (see L<perlipc> for
24702407sample code) and then have a signal handler for the INT signal that
2471passes the signal on to the subprocess. Or you can check for it:
2408passes the signal on to the subprocess. Or you can check for it:
24722409
24732410=end original
24742411
24752412できません。
2476C<system()> 呼び出しを模倣する必要があり(L<perlipc> のサンプルコードを
2413system() 呼び出しを模倣する必要があり(L<perlipc> のサンプルコードを
24772414参照してください)、サブプロセスでのシグナルを送る
24782415INT シグナルのためのシグナルハンドラーを持つ必要があります。
24792416あるいは、以下のようにしてチェックすることもできます:
24802417
2481 $rc = system($cmd);
2418 $rc = system($cmd);
2482 if ($rc & 127) { die "signal death" }
2419 if ($rc & 127) { die "signal death" }
24832420
24842421=head2 How do I open a file without blocking?
24852422
24862423(ブロックせずにファイルをオープンするには?)
24872424
24882425=begin original
24892426
24902427If you're lucky enough to be using a system that supports
24912428non-blocking reads (most Unixish systems do), you need only to use the
2492C<O_NDELAY> or C<O_NONBLOCK> flag from the C<Fcntl> module in conjunction with
2429O_NDELAY or O_NONBLOCK flag from the Fcntl module in conjunction with
2493C<sysopen()>:
2430sysopen():
24942431
24952432=end original
24962433
24972434幸運にもあなたの使っているシステムがノンブロッキング読み出し
2498(ほとんどの Unix 的システムはサポートしています)をサポートしているのであれば、
2435(ほとんどの UNIX 的システムはサポートしています)をサポートしているのであれば、
2499C<Fcntl> モジュールの C<O_NDELAY>C<O_NONBLOCK> というフラグを
2436Fcntl モジュールの O_NDELAY や O_NONBLOCK というフラグを
2500C<sysopen()> と一緒に使うだけです。
2437sysopne() と一緒に使うだけです。
25012438
2502 use Fcntl;
2439 use Fcntl;
2503 sysopen(my $fh, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644)
2440 sysopen(FH, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644)
2504 or die "can't open /foo/somefile: $!":
2441 or die "can't open /foo/somefile: $!":
25052442
25062443=head2 How do I tell the difference between errors from the shell and perl?
25072444
25082445(シェルからと perl からのエラーを見分けるには?)
25092446
25102447=begin original
25112448
25122449(answer contributed by brian d foy)
25132450
25142451=end original
25152452
25162453(答えは brian d foy によって寄贈されました)
25172454
25182455=begin original
25192456
25202457When you run a Perl script, something else is running the script for you,
2521and that something else may output error messages. The script might
2458and that something else may output error messages. The script might
2522emit its own warnings and error messages. Most of the time you cannot
2459emit its own warnings and error messages. Most of the time you cannot
25232460tell who said what.
25242461
25252462=end original
25262463
25272464あなたが Perl スクリプトを実行すると、他の誰かがあなたのためにスクリプトを
25282465動かします; そしてその他の誰かがエラーメッセージを出すことがあります。
25292466スクリプトも自身の警告やエラーメッセージを出すかもしれません。
25302467ほとんどの場合、それを言っているのが誰かは教えてもらえません。
25312468
25322469=begin original
25332470
25342471You probably cannot fix the thing that runs perl, but you can change how
25352472perl outputs its warnings by defining a custom warning and die functions.
25362473
25372474=end original
25382475
25392476あなたが perl を実行しているものを修正することはおそらくできませんが、
25402477独自の警告と die の関数を定義することで、perl がどのように警告を
25412478出力するかを変えることはできます。
25422479
25432480=begin original
25442481
25452482Consider this script, which has an error you may not notice immediately.
25462483
25472484=end original
25482485
25492486すぐには気付かないかもしれない誤りを含むこのスクリプトを考えてみてください。
25502487
2551 #!/usr/locl/bin/perl
2488 #!/usr/locl/bin/perl
25522489
2553 print "Hello World\n";
2490 print "Hello World\n";
25542491
25552492=begin original
25562493
25572494I get an error when I run this from my shell (which happens to be
2558bash). That may look like perl forgot it has a C<print()> function,
2495bash). That may look like perl forgot it has a print() function,
25592496but my shebang line is not the path to perl, so the shell runs the
25602497script, and I get the error.
25612498
25622499=end original
25632500
25642501これを私のシェル(たまたま bash でした)で実行するとエラーが表示されます。
2565これは perl が C<print()> 関数を忘れてしまったかのように見えますが、
2502これは perl が print() 関数を忘れてしまったかのように見えますが、
25662503#! 行の perl へのパスが間違っているので、シェルがこのスクリプトを実行し、
25672504エラーが表示されます。
25682505
2569 $ ./test
2506 $ ./test
2570 ./test: line 3: print: command not found
2507 ./test: line 3: print: command not found
25712508
25722509=begin original
25732510
25742511A quick and dirty fix involves a little bit of code, but this may be all
25752512you need to figure out the problem.
25762513
25772514=end original
25782515
25792516素早く汚い修正には少しコードが必要ですが、おそらく問題を見つけ出すのに
25802517必要なものが全てあるでしょう。
25812518
2582 #!/usr/bin/perl -w
2519 #!/usr/bin/perl -w
25832520
2584 BEGIN {
2521 BEGIN {
2585 $SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; };
2522 $SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; };
2586 $SIG{__DIE__} = sub{ print STDERR "Perl: ", @_; exit 1};
2523 $SIG{__DIE__} = sub{ print STDERR "Perl: ", @_; exit 1};
2587 }
2524 }
25882525
2589 $a = 1 + undef;
2526 $a = 1 + undef;
2590 $x / 0;
2527 $x / 0;
2591 __END__
2528 __END__
25922529
25932530=begin original
25942531
2595The perl message comes out with "Perl" in front. The C<BEGIN> block
2532The perl message comes out with "Perl" in front. The BEGIN block
25962533works at compile time so all of the compilation errors and warnings
25972534get the "Perl:" prefix too.
25982535
25992536=end original
26002537
26012538perl のメッセージは先頭に "Perl" を付けて出力されます。
2602C<BEGIN> ブロックはコンパイル時に動作するので、全てのコンパイル時のエラーと
2539BEGIN ブロックはコンパイル時に動作するので、全てのコンパイル時のエラーと
26032540警告にも "Perl:" の接頭辞がつきます。
26042541
2605 Perl: Useless use of division (/) in void context at ./test line 9.
2542 Perl: Useless use of division (/) in void context at ./test line 9.
2606 Perl: Name "main::a" used only once: possible typo at ./test line 8.
2543 Perl: Name "main::a" used only once: possible typo at ./test line 8.
2607 Perl: Name "main::x" used only once: possible typo at ./test line 9.
2544 Perl: Name "main::x" used only once: possible typo at ./test line 9.
2608 Perl: Use of uninitialized value in addition (+) at ./test line 8.
2545 Perl: Use of uninitialized value in addition (+) at ./test line 8.
2609 Perl: Use of uninitialized value in division (/) at ./test line 9.
2546 Perl: Use of uninitialized value in division (/) at ./test line 9.
2610 Perl: Illegal division by zero at ./test line 9.
2547 Perl: Illegal division by zero at ./test line 9.
2611 Perl: Illegal division by zero at -e line 3.
2548 Perl: Illegal division by zero at -e line 3.
26122549
26132550=begin original
26142551
26152552If I don't see that "Perl:", it's not from perl.
26162553
26172554=end original
26182555
26192556"Perl:" という文字がなければ、perl が出しているものではありません。
26202557
26212558=begin original
26222559
26232560You could also just know all the perl errors, and although there are
2624some people who may know all of them, you probably don't. However, they
2561some people who may know all of them, you probably don't. However, they
2625all should be in the L<perldiag> manpage. If you don't find the error in
2562all should be in the perldiag manpage. If you don't find the error in
26262563there, it probably isn't a perl error.
26272564
26282565=end original
26292566
26302567単に全ての perl のエラーを知るという方法もありますが、(全てのエラーを
26312568知っている人もいるかもしれませんが)おそらくあなたは知らないでしょう。
2632しかし、これら全ては L<perldiag> マニュアルページにあるはずです。
2569しかし、これら全ては perldiag マニュアルページにあるはずです。
26332570もしエラーがここになければ、それはおそらく perl のエラーではありません。
26342571
26352572=begin original
26362573
26372574Looking up every message is not the easiest way, so let perl to do it
2638for you. Use the diagnostics pragma with turns perl's normal messages
2575for you. Use the diagnostics pragma with turns perl's normal messages
26392576into longer discussions on the topic.
26402577
26412578=end original
26422579
26432580全てのメッセージを探すというのは最も簡単な方法ではないので、その作業は
26442581perl にさせます。
26452582perl の通常のメッセージを、より長い説明に変更する diagnostics プラグマを
26462583使います。
26472584
2648 use diagnostics;
2585 use diagnostics;
26492586
26502587=begin original
26512588
26522589If you don't get a paragraph or two of expanded discussion, it
26532590might not be perl's message.
26542591
26552592=end original
26562593
26572594段落一つか二つの追加の議論が表示されなければ、それは perl のメッセージでは
26582595ないでしょう。
26592596
26602597=head2 How do I install a module from CPAN?
26612598
26622599(モジュールを CPAN からインストールするには?)
26632600
26642601=begin original
26652602
2666(contributed by brian d foy)
2603The easiest way is to have a module also named CPAN do it for you.
2604This module comes with perl version 5.004 and later.
26672605
26682606=end original
26692607
2670(brian d foy によっ寄贈さました)
2608最も単純な方法は、その仕事をしる CPAN モジュールを入手することです。
2609このモジュールは、5.004 以降の perl に付属しています。
26712610
2672=begin original
2611 $ perl -MCPAN -e shell
26732612
2674The easiest way is to have a module also named CPAN do it for you by using
2613 cpan shell -- CPAN exploration and modules installation (v1.59_54)
2675the C<cpan> command that comes with Perl. You can give it a list of modules
2614 ReadLine support enabled
2676to install:
26772615
2678=end original
2616 cpan> install Some::Module
26792617
2680最も単純な方法は、Perl と同梱されている C<cpan> コマンドを使って、この仕事を
2681してくれる CPAN モジュールを入手することです。
2682インストールするモジュールの一覧を与えます:
2683
2684 $ cpan IO::Interactive Getopt::Whatever
2685
26862618=begin original
26872619
2688If you prefer C<CPANPLUS>, it's just as easy:
2620To manually install the CPAN module, or any well-behaved CPAN module
2621for that matter, follow these steps:
26892622
26902623=end original
26912624
2692C<CPANPLUS> が好みなら、はり簡単す:
2625CPAN モジュール問題に対して行儀よく振る舞う CPAN モジュールを手作業
2626インストールするには、以下のステップに従います:
26932627
2694 $ cpanp i IO::Interactive Getopt::Whatever
2628=over 4
26952629
2696If you want to install a distribution from the current directory, you can
2630=item 1
2697tell C<CPAN.pm> to install C<.> (the full stop):
26982631
2699 $ cpan .
2700
27012632=begin original
27022633
2703See the documentation for either of those commands to see what else
2634Unpack the source into a temporary area.
2704you can do.
27052635
27062636=end original
27072637
2708その他に出来ることについてはこれらのコマンドの文書参照てください
2638ソースファイル一時的な領域に展開ます
27092639
2710=begin original
2640=item 2
27112641
2712If you want to try to install a distribution by yourself, resolving
2642 perl Makefile.PL
2713all dependencies on your own, you follow one of two possible build
2714paths.
27152643
2716=end original
2644=item 3
27172645
2718全ての依存を自分で解決して、配布を自分自身でインストールしてみたい場合は、
2646 make
2719二つのビルド方法のどちらかに従います。
27202647
2721=begin original
2648=item 4
27222649
2723For distributions that use I<Makefile.PL>:
2650 make test
27242651
2725=end original
2652=item 5
27262653
2727I<Makefile.PL> を使う配布では:
2654 make install
27282655
2729 $ perl Makefile.PL
2656=back
2730 $ make test install
27312657
27322658=begin original
27332659
2734For distributions that use I<Build.PL>:
2660If your version of perl is compiled without dynamic loading, then you
2661just need to replace step 3 (B<make>) with B<make perl> and you will
2662get a new F<perl> binary with your extension linked in.
27352663
27362664=end original
27372665
2738I<Build.PL> を使う配布では:
2666あなたの使っている perl が、動的ローディングを使わずに
2667コンパイルされたものであれば、
2668ステップ 3 の (B<make>) を B<make perl> で置き換える必要があり、
2669エクステンションをリンクした新しい F<perl> の実行ファイルを
2670入手することになります。
27392671
2740 $ perl Build.PL
2741 $ ./Build test
2742 $ ./Build install
2743
27442672=begin original
27452673
2746Some distributions may need to link to libraries or other third-party
2674See L<ExtUtils::MakeMaker> for more details on building extensions.
2747code and their build and installation sequences may be more complicated.
2675See also the next question, "What's the difference between require
2748Check any I<README> or I<INSTALL> files that you may find.
2676and use?".
27492677
27502678=end original
27512679
2752一部の配布ではライブラリはその他のサードパーコーリンクが
2680エクスンションビルについて詳細は
2753必要であったり、ビルドとインストールの手順がもっと複雑な場合もあります
2681L<ExtUtils::MakeMaker> を参照して下さい
2754見付かった I<README> I<INSTALL> ファイルをチェックしてください。
2682次の質問 "What's the difference between require
2683and use?" も参照してください。
27552684
27562685=head2 What's the difference between require and use?
27572686
27582687(require と use の間の違いとは?)
27592688
27602689=begin original
27612690
27622691(contributed by brian d foy)
27632692
27642693=end original
27652694
27662695(brian d foy によって寄贈されました)
27672696
27682697=begin original
27692698
27702699Perl runs C<require> statement at run-time. Once Perl loads, compiles,
27712700and runs the file, it doesn't do anything else. The C<use> statement
27722701is the same as a C<require> run at compile-time, but Perl also calls the
27732702C<import> method for the loaded package. These two are the same:
27742703
27752704=end original
27762705
27772706Perl は C<require> 文を実行時に実行します。
27782707一旦 Perl がロードされ、コンパイルされ、ファイルが実行されると、
27792708他に何もしません。
27802709C<use> 文は C<require> と同じことをコンパイル時に行いますが、
27812710Perl はまたロードされたパッケージの C<import> メソッドを呼び出します。
27822711以下の二つは同じです:
27832712
2784 use MODULE qw(import list);
2713 use MODULE qw(import list);
27852714
2786 BEGIN {
2715 BEGIN {
2787 require MODULE;
2716 require MODULE;
2788 MODULE->import(import list);
2717 MODULE->import(import list);
2789 }
2718 }
27902719
27912720=begin original
27922721
27932722However, you can suppress the C<import> by using an explicit, empty
27942723import list. Both of these still happen at compile-time:
27952724
27962725=end original
27972726
27982727しかし、明示的に空のインポートリストを使うことで、C<import> を
27992728抑制できます。
28002729これらはどちらもやはりコンパイル時に起こります:
28012730
2802 use MODULE ();
2731 use MODULE ();
28032732
2804 BEGIN {
2733 BEGIN {
2805 require MODULE;
2734 require MODULE;
2806 }
2735 }
28072736
28082737=begin original
28092738
28102739Since C<use> will also call the C<import> method, the actual value
28112740for C<MODULE> must be a bareword. That is, C<use> cannot load files
28122741by name, although C<require> can:
28132742
28142743=end original
28152744
28162745C<use> は C<import> メソッドの呼び出しも行うので、
28172746C<MODULE> の実際の値は裸の単語でなければなりません。
28182747これは、C<use> は名前でファイルをロード出来ませんが、
28192748C<require> ではできます:
28202749
2821 require "$ENV{HOME}/lib/Foo.pm"; # no @INC searching!
2750 require "$ENV{HOME}/lib/Foo.pm"; # no @INC searching!
28222751
28232752=begin original
28242753
28252754See the entry for C<use> in L<perlfunc> for more details.
28262755
28272756=end original
28282757
28292758さらなる詳細については L<perlfunc> の C<use> の項目を参照してください。
28302759
28312760=head2 How do I keep my own module/library directory?
28322761
28332762(自分自身のモジュール/ライブラリディレクトリを持つには?)
28342763
28352764=begin original
28362765
28372766When you build modules, tell Perl where to install the modules.
28382767
28392768=end original
28402769
28412770モジュールを作成するときに、Perl にモジュールをインストールする場所を
28422771指定します。
28432772
28442773=begin original
28452774
2846If you want to install modules for your own use, the easiest way might
2847be L<local::lib>, which you can download from CPAN. It sets various
2848installation settings for you, and uses those same settings within
2849your programs.
2850
2851=end original
2852
2853自分自身が使うためにモジュールをインストールしたいなら、一番簡単な方法は、
2854CPAN からダウンロードできる L<local::lib> でしょう。
2855これは様々なインストール設定を行い、プログラム中で同じ設定を使います。
2856
2857=begin original
2858
2859If you want more flexibility, you need to configure your CPAN client
2860for your particular situation.
2861
2862=end original
2863
2864もっと柔軟性がほしいなら、CPAN クライアントを特定の状況に合わせて
2865設定する必要があります。
2866
2867=begin original
2868
28692775For C<Makefile.PL>-based distributions, use the INSTALL_BASE option
28702776when generating Makefiles:
28712777
28722778=end original
28732779
28742780C<Makefile.PL>-ベースの配布では、Makefile の生成時に INSTALL_BASE
28752781オプションを使ってください:
28762782
2877 perl Makefile.PL INSTALL_BASE=/mydir/perl
2783 perl Makefile.PL INSTALL_BASE=/mydir/perl
28782784
28792785=begin original
28802786
2881You can set this in your C<CPAN.pm> configuration so modules
2787You can set this in your CPAN.pm configuration so modules automatically install
2882automatically install in your private library directory when you use
2788in your private library directory when you use the CPAN.pm shell:
2883the CPAN.pm shell:
28842789
28852790=end original
28862791
28872792CPAN.pm シェルを使ったときに、自動的にモジュールを自分のプライベートな
28882793ディレクトリにインストールさせるように、CPAN.pm の設定を変更できます:
28892794
2890 % cpan
2795 % cpan
2891 cpan> o conf makepl_arg INSTALL_BASE=/mydir/perl
2796 cpan> o conf makepl_arg INSTALL_BASE=/mydir/perl
2892 cpan> o conf commit
2797 cpan> o conf commit
28932798
28942799=begin original
28952800
28962801For C<Build.PL>-based distributions, use the --install_base option:
28972802
28982803=end original
28992804
29002805C<Build.PL>-ベースの配布では、--install_base オプションを使ってください:
29012806
2902 perl Build.PL --install_base /mydir/perl
2807 perl Build.PL --install_base /mydir/perl
29032808
29042809=begin original
29052810
2906You can configure C<CPAN.pm> to automatically use this option too:
2811You can configure CPAN.pm to automatically use this option too:
29072812
29082813=end original
29092814
29102815自動的にこのオプションを使うために、CPAN.pm を設定することも出来ます:
29112816
2912 % cpan
2817 % cpan
2913 cpan> o conf mbuild_arg "--install_base /mydir/perl"
2818 cpan> o conf mbuild_arg --install_base /mydir/perl
2914 cpan> o conf commit
2819 cpan> o conf commit
29152820
29162821=begin original
29172822
29182823INSTALL_BASE tells these tools to put your modules into
2919F</mydir/perl/lib/perl5>. See L<How do I add a directory to my
2824F</mydir/perl/lib/perl5>. See L<How do I add a directory to my
29202825include path (@INC) at runtime?> for details on how to run your newly
2921installed modules.
2826installed moudles.
29222827
29232828=end original
29242829
29252830INSTALL_BASE はこれらのツールにモジュールを F</mydir/perl/lib/perl5> に
29262831置くように伝えます。
29272832新しくインストールしたモジュールを実行する方法についての詳細は
29282833L<How do I add a directory to my include path (@INC) at runtime?> を
29292834参照してください。
29302835
29312836=begin original
29322837
29332838There is one caveat with INSTALL_BASE, though, since it acts
2934differently from the PREFIX and LIB settings that older versions of
2839differently than the PREFIX and LIB settings that older versions of
2935L<ExtUtils::MakeMaker> advocated. INSTALL_BASE does not support
2840ExtUtils::MakeMaker advocated. INSTALL_BASE does not support
29362841installing modules for multiple versions of Perl or different
2937architectures under the same directory. You should consider whether you
2842architectures under the same directory. You should consider if you
2938really want that and, if you do, use the older PREFIX and LIB
2843really want that , and if you do, use the older PREFIX and LIB
2939settings. See the L<ExtUtils::Makemaker> documentation for more details.
2844settings. See the ExtUtils::Makemaker documentation for more details.
29402845
29412846=end original
29422847
29432848しかし、INSTALL_BASE には一つの問題点があります;
2944なぜなら、より古いバージョンの L<ExtUtils::MakeMaker> が推奨していた
2849なぜなら、より古いバージョンの ExtUtils::MakeMaker が推奨していた
29452850PREFIX および LIB とは異なった動作をするからです。
29462851INSTALL_BASE は、複数のバージョンの Perl や異なったアーキテクチャに
29472852対して同じディレクトリにモジュールをインストールすることには
29482853対応していません。
29492854もし本当にそれが必要でそうするなら、古い PREFIX と LIB の設定を
29502855考慮するべきです。
2951さらなる詳細については L<ExtUtils::Makemaker> の文書を参照してください。
2856さらなる詳細については ExtUtils::Makemaker の文書を参照してください。
29522857
29532858=head2 How do I add the directory my program lives in to the module/library search path?
29542859
29552860(私のプログラムの置いてある場所をモジュール/ライブラリの検索パスに追加するには?)
29562861
29572862=begin original
29582863
29592864(contributed by brian d foy)
29602865
29612866=end original
29622867
29632868(brian d foy によって寄贈されました)
29642869
29652870=begin original
29662871
29672872If you know the directory already, you can add it to C<@INC> as you would
29682873for any other directory. You might <use lib> if you know the directory
29692874at compile time:
29702875
29712876=end original
29722877
29732878すでにディレクトリが分かっている場合は、それを C<@INC> に追加できます。
29742879コンパイル時に分かっている場合は、<use lib> が使えます:
29752880
2976 use lib $directory;
2881 use lib $directory;
29772882
29782883=begin original
29792884
29802885The trick in this task is to find the directory. Before your script does
29812886anything else (such as a C<chdir>), you can get the current working
29822887directory with the C<Cwd> module, which comes with Perl:
29832888
29842889=end original
29852890
29862891このタスクの秘訣は、ディレクトリを探すことです。
29872892あなたのスクリプトが(C<chdir> などで)他の場所へ行く前に、Perl に
29882893同梱されている C<Cwd> モジュールを使って、カレントワーキングディレクトリを
29892894取得できます:
29902895
2991 BEGIN {
2896 BEGIN {
2992 use Cwd;
2897 use Cwd;
2993 our $directory = cwd;
2898 our $directory = cwd;
2994 }
2899 }
29952900
2996 use lib $directory;
2901 use lib $directory;
29972902
29982903=begin original
29992904
30002905You can do a similar thing with the value of C<$0>, which holds the
30012906script name. That might hold a relative path, but C<rel2abs> can turn
30022907it into an absolute path. Once you have the
30032908
30042909=end original
30052910
30062911似たようなことは、スクリプト名を記録している C<$0> でもできます。
30072912これは相対パスかもしれませんが、C<rel2abs> はこれを絶対パスに変更できます。
30082913以下のようにすると
30092914
3010 BEGIN {
2915 BEGIN {
3011 use File::Spec::Functions qw(rel2abs);
2916 use File::Spec::Functions qw(rel2abs);
3012 use File::Basename qw(dirname);
2917 use File::Basename qw(dirname);
30132918
3014 my $path = rel2abs( $0 );
2919 my $path = rel2abs( $0 );
3015 our $directory = dirname( $path );
2920 our $directory = dirname( $path );
3016 }
2921 }
30172922
3018 use lib $directory;
2923 use lib $directory;
30192924
30202925=begin original
30212926
3022The L<FindBin> module, which comes with Perl, might work. It finds the
2927The C<FindBin> module, which comes with Perl, might work. It finds the
30232928directory of the currently running script and puts it in C<$Bin>, which
30242929you can then use to construct the right library path:
30252930
30262931=end original
30272932
3028Perl に同梱されている L<FindBin> モジュールが働くでしょう。
2933Perl に同梱されている C<FindBin> モジュールが働くでしょう。
30292934これは現在実行しているスクリプトのディレクトリを見つけて C<$Bin> に
30302935設定し、正しいライブラリパスを構築するために使えるようにします:
30312936
3032 use FindBin qw($Bin);
2937 use FindBin qw($Bin);
30332938
3034=begin original
3035
3036You can also use L<local::lib> to do much of the same thing. Install
3037modules using L<local::lib>'s settings then use the module in your
3038program:
3039
3040=end original
3041
3042同じことをするのに L<local::lib> も使えます。
3043L<local::lib> の設定を使ってモジュールをインストールしてから、プログラムで
3044そのモジュールを使います:
3045
3046 use local::lib; # sets up a local lib at ~/perl5
3047
3048=begin original
3049
3050See the L<local::lib> documentation for more details.
3051
3052=end original
3053
3054さらなる詳細については L<local::lib> の文書を参照してください。
3055
30562939=head2 How do I add a directory to my include path (@INC) at runtime?
30572940
30582941(実行時にインクルードパス (@INC) にディレクトリを追加するには?)
30592942
30602943=begin original
30612944
30622945Here are the suggested ways of modifying your include path, including
30632946environment variables, run-time switches, and in-code statements:
30642947
30652948=end original
30662949
30672950環境変数、実行時スイッチ、コード内の文などを使って、インクルードパスを
30682951変更するためのお薦めの方法を挙げておきます:
30692952
30702953=over 4
30712954
3072=item the C<PERLLIB> environment variable
2955=item the PERLLIB environment variable
30732956
3074(C<PERLLIB> 環境変数)
2957(PERLLIB 環境変数)
30752958
3076 $ export PERLLIB=/path/to/my/dir
2959 $ export PERLLIB=/path/to/my/dir
3077 $ perl program.pl
2960 $ perl program.pl
30782961
3079=item the C<PERL5LIB> environment variable
2962=item the PERL5LIB environment variable
30802963
3081(C<PERL5LIB> 環境変数)
2964(PERL5LIB 環境変数)
30822965
3083 $ export PERL5LIB=/path/to/my/dir
2966 $ export PERL5LIB=/path/to/my/dir
3084 $ perl program.pl
2967 $ perl program.pl
30852968
3086=item the C<perl -Idir> command line flag
2969=item the perl -Idir command line flag
30872970
3088(C<perl -Idir> コマンドラインフラグ)
2971(perl -Idir コマンドラインフラグ)
30892972
3090 $ perl -I/path/to/my/dir program.pl
2973 $ perl -I/path/to/my/dir program.pl
30912974
3092=item the C<lib> pragma:
2975=item the use lib pragma:
30932976
3094(C<lib> プラグマ)
2977(use lib プラグマ)
30952978
3096 use lib "$ENV{HOME}/myown_perllib";
2979 use lib "$ENV{HOME}/myown_perllib";
30972980
3098=item the L<local::lib> module:
3099
3100 use local::lib;
3101
3102 use local::lib "~/myown_perllib";
3103
31042981=back
31052982
31062983=begin original
31072984
3108The last is particularly useful because it knows about machine-dependent
2985The last is particularly useful because it knows about machine
3109architectures. The C<lib.pm> pragmatic module was first
2986dependent architectures. The lib.pm pragmatic module was first
31102987included with the 5.002 release of Perl.
31112988
31122989=end original
31132990
3114最後のものが特に便利です; なぜなら、これはマシン依存のアーキテクチャを
2991最後のものが特に便利です
3115知っているからです。
2992なぜなら、これはマシン依存のアーキテクチャを知っているからです。
3116プラグマ的モジュール C<lib.pm> は Perl 5.002 で最初に導入されました。
2993プラグマ的モジュール(pragmatic module) lib.pm は Perl 5.002 で
2994最初に導入されました。
31172995
31182996=head2 What is socket.ph and where do I get it?
31192997
31202998(socket.ph とは一体何で、それはどこで入手できますか?)
31212999
31223000=begin original
31233001
31243002It's a Perl 4 style file defining values for system networking
3125constants. Sometimes it is built using L<h2ph> when Perl is installed,
3003constants. Sometimes it is built using h2ph when Perl is installed,
3126but other times it is not. Modern programs should use C<use Socket;>
3004but other times it is not. Modern programs C<use Socket;> instead.
3127instead.
31283005
31293006=end original
31303007
31313008それは Perl 4 スタイルのファイルで、ネットワーク使用のための
31323009値を定義しているものです。
3133これは Perl がインストールされたときに L<h2ph> を使って作成されることも
3010これは Perl がインストールされたときに h2ph を使って作成されることも
31343011ありますが、そうでないときもあります。
3135最近のプログラムでは代わりに C<use Socket> を使うべきです。
3012最近のやり方では C<use Socket> を代わりに使用します。
31363013
3014=head1 REVISION
3015
3016Revision: $Revision$
3017
3018Date: $Date$
3019
3020See L<perlfaq> for source control details and availability.
3021
31373022=head1 AUTHOR AND COPYRIGHT
31383023
3139Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and
3024Copyright (c) 1997-2009 Tom Christiansen, Nathan Torkington, and
31403025other authors as noted. All rights reserved.
31413026
31423027This documentation is free; you can redistribute it and/or modify it
31433028under the same terms as Perl itself.
31443029
31453030Irrespective of its distribution, all code examples in this file
3146are hereby placed into the public domain. You are permitted and
3031are hereby placed into the public domain. You are permitted and
31473032encouraged to use this code in your own programs for fun
3148or for profit as you see fit. A simple comment in the code giving
3033or for profit as you see fit. A simple comment in the code giving
31493034credit would be courteous but is not required.
31503035
31513036=begin meta
31523037
31533038Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp>
3154Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-5.14.1, 5.00150039)
3039Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-)
3155Status: completed
31563040
31573041=end meta