1 |
|
2 |
=encoding euc-jp |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
=begin original |
7 |
|
8 |
perlfaq8 - System Interaction |
9 |
|
10 |
=end original |
11 |
|
12 |
perlfaq8 - システムとの相互作用 |
13 |
|
14 |
=head1 DESCRIPTION |
15 |
|
16 |
=begin original |
17 |
|
18 |
This section of the Perl FAQ covers questions involving operating |
19 |
system interaction. Topics include interprocess communication (IPC), |
20 |
control over the user-interface (keyboard, screen and pointing |
21 |
devices), and most anything else not related to data manipulation. |
22 |
|
23 |
=end original |
24 |
|
25 |
FAQ のこのセクションでは、オペレーティングシステムとの対話に関する質問を |
26 |
扱っています。 |
27 |
これにはプロセス間(IPC)、ユーザーインターフェース |
28 |
(キーボード、スクリーン、ポインティングデバイス)の制御、 |
29 |
その他データ操作に関連しないほとんどの事柄を含みます。 |
30 |
|
31 |
=begin original |
32 |
|
33 |
Read the FAQs and documentation specific to the port of perl to your |
34 |
operating system (eg, L<perlvms>, L<perlplan9>, ...). These should |
35 |
contain more detailed information on the vagaries of your perl. |
36 |
|
37 |
=end original |
38 |
|
39 |
あなたの使っているオペレーティングシステム向けの移植について特有のことは、 |
40 |
それに関する FAQ とドキュメント(L<perlvms>, L<perlplan9> など)を |
41 |
読んでください。 |
42 |
そこには、あなたの使う perl についてのより詳しい情報があります。 |
43 |
|
44 |
=head2 How do I find out which operating system I'm running under? |
45 |
|
46 |
(実行しているオペレーティングシステムを見分けるには?) |
47 |
|
48 |
=begin original |
49 |
|
50 |
The C<$^O> variable (C<$OSNAME> if you use C<English>) contains an |
51 |
indication of the name of the operating system (not its release |
52 |
number) that your perl binary was built for. |
53 |
|
54 |
=end original |
55 |
|
56 |
C<$^O> という変数(use C<Engish> をしていれば C<$OSNAME>)は、あなたの使っている |
57 |
perl の実行ファイルがビルドされたオペレーションシステムの名前 |
58 |
(リリース番号ではありません)の情報を持っています。 |
59 |
|
60 |
=head2 How come exec() doesn't return? |
61 |
X<exec> X<system> X<fork> X<open> X<pipe> |
62 |
|
63 |
(なぜ exec() は戻ってこないのでしょう?) |
64 |
|
65 |
=begin original |
66 |
|
67 |
(contributed by brian d foy) |
68 |
|
69 |
=end original |
70 |
|
71 |
(brian d foy によって寄贈されました) |
72 |
|
73 |
=begin original |
74 |
|
75 |
The C<exec> function's job is to turn your process into another |
76 |
command and never to return. If that's not what you want to do, don't |
77 |
use C<exec>. :) |
78 |
|
79 |
=end original |
80 |
|
81 |
C<exec> 関数の仕事は、あなたのプロセスを他のコマンドに切り替えて、 |
82 |
決して帰ってこないことです。 |
83 |
もしこれがあなたのしたいことでないなら、C<exec> を使わないでください。 :) |
84 |
|
85 |
=begin original |
86 |
|
87 |
If you want to run an external command and still keep your Perl process |
88 |
going, look at a piped C<open>, C<fork>, or C<system>. |
89 |
|
90 |
=end original |
91 |
|
92 |
もし外部コマンドを実行して、かつ Perl プロセスを実行したままにしたい場合は、 |
93 |
パイプ付きの C<open>, C<fork>, C<system> のいずれかを見てください。 |
94 |
|
95 |
=head2 How do I do fancy stuff with the keyboard/screen/mouse? |
96 |
|
97 |
(キーボード/画面/マウスの凝った機能を使うには?) |
98 |
|
99 |
=begin original |
100 |
|
101 |
How you access/control keyboards, screens, and pointing devices |
102 |
("mice") is system-dependent. Try the following modules: |
103 |
|
104 |
=end original |
105 |
|
106 |
キーボード、画面、ポインティングデバイス(“マウス”)にアクセスしたり |
107 |
それを制御することはシステム依存です。 |
108 |
以下のモジュールを試してみてください: |
109 |
|
110 |
=over 4 |
111 |
|
112 |
=item Keyboard |
113 |
|
114 |
Term::Cap Standard perl distribution |
115 |
Term::ReadKey CPAN |
116 |
Term::ReadLine::Gnu CPAN |
117 |
Term::ReadLine::Perl CPAN |
118 |
Term::Screen CPAN |
119 |
|
120 |
=item Screen |
121 |
|
122 |
Term::Cap Standard perl distribution |
123 |
Curses CPAN |
124 |
Term::ANSIColor CPAN |
125 |
|
126 |
=item Mouse |
127 |
|
128 |
Tk CPAN |
129 |
Wx CPAN |
130 |
Gtk2 CPAN |
131 |
Qt4 kdebindings4 package |
132 |
|
133 |
=back |
134 |
|
135 |
=begin original |
136 |
|
137 |
Some of these specific cases are shown as examples in other answers |
138 |
in this section of the perlfaq. |
139 |
|
140 |
=end original |
141 |
|
142 |
これらの一部の特殊なケースは、このセクションの他の回答の例として |
143 |
示されています。 |
144 |
|
145 |
=head2 How do I print something out in color? |
146 |
|
147 |
(色付きで何かを出力するには?) |
148 |
|
149 |
=begin original |
150 |
|
151 |
In general, you don't, because you don't know whether |
152 |
the recipient has a color-aware display device. If you |
153 |
know that they have an ANSI terminal that understands |
154 |
color, you can use the L<Term::ANSIColor> module from CPAN: |
155 |
|
156 |
=end original |
157 |
|
158 |
一般的にはできません; なぜなら、あなたはディスプレイデバイスに関する |
159 |
レシピについて何も知らないからです。 |
160 |
もしあなたがカラーの扱える ANSI ターミナルについて知っているのなら、 |
161 |
CPAN にある L<Term::ANSIColor> モジュールを使うことができます。 |
162 |
|
163 |
use Term::ANSIColor; |
164 |
print color("red"), "Stop!\n", color("reset"); |
165 |
print color("green"), "Go!\n", color("reset"); |
166 |
|
167 |
=begin original |
168 |
|
169 |
Or like this: |
170 |
|
171 |
=end original |
172 |
|
173 |
あるいは次のようにも書けます: |
174 |
|
175 |
use Term::ANSIColor qw(:constants); |
176 |
print RED, "Stop!\n", RESET; |
177 |
print GREEN, "Go!\n", RESET; |
178 |
|
179 |
=head2 How do I read just one key without waiting for a return key? |
180 |
|
181 |
(リターンキーを待たずにキーのデータを一つ読み取るには?) |
182 |
|
183 |
=begin original |
184 |
|
185 |
Controlling input buffering is a remarkably system-dependent matter. |
186 |
On many systems, you can just use the B<stty> command as shown in |
187 |
L<perlfunc/getc>, but as you see, that's already getting you into |
188 |
portability snags. |
189 |
|
190 |
=end original |
191 |
|
192 |
入力バッファを制御するのは非常にシステムに依存したやりかたです。 |
193 |
多くのシステムでは、L<perlfunc/getc> にあるように |
194 |
B<stty> コマンドを使うことができますが、今書いた通り |
195 |
移植性の問題があるのです。 |
196 |
|
197 |
open(TTY, "+</dev/tty") or die "no tty: $!"; |
198 |
system "stty cbreak </dev/tty >/dev/tty 2>&1"; |
199 |
$key = getc(TTY); # perhaps this works |
200 |
# OR ELSE |
201 |
sysread(TTY, $key, 1); # probably this does |
202 |
system "stty -cbreak </dev/tty >/dev/tty 2>&1"; |
203 |
|
204 |
=begin original |
205 |
|
206 |
The L<Term::ReadKey> module from CPAN offers an easy-to-use interface that |
207 |
should be more efficient than shelling out to B<stty> for each key. |
208 |
It even includes limited support for Windows. |
209 |
|
210 |
=end original |
211 |
|
212 |
CPAN にある L<Term::ReadKey> モジュールは、B<stty> をキー毎にシェルに |
213 |
送るよりもより効果的に行ってくれて使うのが簡単なインターフェースを |
214 |
提供します。 |
215 |
このモジュールは限定的ながら Windows にも対応しています。 |
216 |
|
217 |
use Term::ReadKey; |
218 |
ReadMode('cbreak'); |
219 |
$key = ReadKey(0); |
220 |
ReadMode('normal'); |
221 |
|
222 |
=begin original |
223 |
|
224 |
However, using the code requires that you have a working C compiler |
225 |
and can use it to build and install a CPAN module. Here's a solution |
226 |
using the standard L<POSIX> module, which is already on your system |
227 |
(assuming your system supports POSIX). |
228 |
|
229 |
=end original |
230 |
|
231 |
しかし、このコードを使うには C コンパイラを使えることが条件であり、かつ |
232 |
CPAN モジュールのビルドとインストールができなければなりません。 |
233 |
以下の例は、標準の L<POSIX> モジュールを使った解決策で、 |
234 |
あなたの使っているシステムが POSIX をサポートしていれば |
235 |
即使えるものです。 |
236 |
|
237 |
use HotKey; |
238 |
$key = readkey(); |
239 |
|
240 |
=begin original |
241 |
|
242 |
And here's the C<HotKey> module, which hides the somewhat mystifying calls |
243 |
to manipulate the POSIX termios structures. |
244 |
|
245 |
=end original |
246 |
|
247 |
そして以下の例は、C<HotKey> モジュールを使ったものです; これは POSIX の |
248 |
termio 構造体の操作を包み隠します。 |
249 |
|
250 |
# HotKey.pm |
251 |
package HotKey; |
252 |
|
253 |
use strict; |
254 |
use warnings; |
255 |
|
256 |
use parent 'Exporter'; |
257 |
our @EXPORT = qw(cbreak cooked readkey); |
258 |
|
259 |
use POSIX qw(:termios_h); |
260 |
my ($term, $oterm, $echo, $noecho, $fd_stdin); |
261 |
|
262 |
$fd_stdin = fileno(STDIN); |
263 |
$term = POSIX::Termios->new(); |
264 |
$term->getattr($fd_stdin); |
265 |
$oterm = $term->getlflag(); |
266 |
|
267 |
$echo = ECHO | ECHOK | ICANON; |
268 |
$noecho = $oterm & ~$echo; |
269 |
|
270 |
sub cbreak { |
271 |
$term->setlflag($noecho); # ok, so i don't want echo either |
272 |
$term->setcc(VTIME, 1); |
273 |
$term->setattr($fd_stdin, TCSANOW); |
274 |
} |
275 |
|
276 |
sub cooked { |
277 |
$term->setlflag($oterm); |
278 |
$term->setcc(VTIME, 0); |
279 |
$term->setattr($fd_stdin, TCSANOW); |
280 |
} |
281 |
|
282 |
sub readkey { |
283 |
my $key = ''; |
284 |
cbreak(); |
285 |
sysread(STDIN, $key, 1); |
286 |
cooked(); |
287 |
return $key; |
288 |
} |
289 |
|
290 |
END { cooked() } |
291 |
|
292 |
1; |
293 |
|
294 |
=head2 How do I check whether input is ready on the keyboard? |
295 |
|
296 |
(キーの入力待ちがあるかどうかチェックするには?) |
297 |
|
298 |
=begin original |
299 |
|
300 |
The easiest way to do this is to read a key in nonblocking mode with the |
301 |
L<Term::ReadKey> module from CPAN, passing it an argument of -1 to indicate |
302 |
not to block: |
303 |
|
304 |
=end original |
305 |
|
306 |
最も簡単な方法は、CPAN にある L<Term::ReadKey> に対して |
307 |
(ブロックを行わないという意味である)-1 を引数に渡して |
308 |
使用することによって非ブロックモードでキーを読み取るという方法です。 |
309 |
|
310 |
use Term::ReadKey; |
311 |
|
312 |
ReadMode('cbreak'); |
313 |
|
314 |
if (defined (my $char = ReadKey(-1)) ) { |
315 |
# input was waiting and it was $char |
316 |
} else { |
317 |
# no input was waiting |
318 |
} |
319 |
|
320 |
ReadMode('normal'); # restore normal tty settings |
321 |
|
322 |
=head2 How do I clear the screen? |
323 |
|
324 |
(画面をクリアするには?) |
325 |
|
326 |
=begin original |
327 |
|
328 |
(contributed by brian d foy) |
329 |
|
330 |
=end original |
331 |
|
332 |
(brian d foy によって寄贈されました) |
333 |
|
334 |
=begin original |
335 |
|
336 |
To clear the screen, you just have to print the special sequence |
337 |
that tells the terminal to clear the screen. Once you have that |
338 |
sequence, output it when you want to clear the screen. |
339 |
|
340 |
=end original |
341 |
|
342 |
画面をクリアするには、端末に画面をクリアするように伝える特殊な |
343 |
シーケンスを表示するだけです。 |
344 |
このシーケンスが分かれば、画面をクリアしたいときにこれを出力します。 |
345 |
|
346 |
=begin original |
347 |
|
348 |
You can use the L<Term::ANSIScreen> module to get the special |
349 |
sequence. Import the C<cls> function (or the C<:screen> tag): |
350 |
|
351 |
=end original |
352 |
|
353 |
この特殊なシーケンスを得るためには L<Term::ANSIScreen> モジュールが |
354 |
使えます。 |
355 |
C<cls> 関数 (あるいは C<:screen> タグ) をインポートします: |
356 |
|
357 |
use Term::ANSIScreen qw(cls); |
358 |
my $clear_screen = cls(); |
359 |
|
360 |
print $clear_screen; |
361 |
|
362 |
=begin original |
363 |
|
364 |
The L<Term::Cap> module can also get the special sequence if you want |
365 |
to deal with the low-level details of terminal control. The C<Tputs> |
366 |
method returns the string for the given capability: |
367 |
|
368 |
=end original |
369 |
|
370 |
端末制御のための低レベルな詳細を扱いたいなら、 |
371 |
L<Term::Cap> から特殊シーケンスを得られます。 |
372 |
C<Tputs> メソッドは、与えられた能力のための文字列を返します: |
373 |
|
374 |
use Term::Cap; |
375 |
|
376 |
my $terminal = Term::Cap->Tgetent( { OSPEED => 9600 } ); |
377 |
my $clear_string = $terminal->Tputs('cl'); |
378 |
|
379 |
print $clear_screen; |
380 |
|
381 |
=begin original |
382 |
|
383 |
On Windows, you can use the L<Win32::Console> module. After creating |
384 |
an object for the output filehandle you want to affect, call the |
385 |
C<Cls> method: |
386 |
|
387 |
=end original |
388 |
|
389 |
Windows では、L<Win32::Console> モジュールが使えます。 |
390 |
影響を与えたい出力ファイルハンドルのオブジェクトを作った後、 |
391 |
C<Cls> メソッドを呼び出します: |
392 |
|
393 |
Win32::Console; |
394 |
|
395 |
my $OUT = Win32::Console->new(STD_OUTPUT_HANDLE); |
396 |
my $clear_string = $OUT->Cls; |
397 |
|
398 |
print $clear_screen; |
399 |
|
400 |
=begin original |
401 |
|
402 |
If you have a command-line program that does the job, you can call |
403 |
it in backticks to capture whatever it outputs so you can use it |
404 |
later: |
405 |
|
406 |
=end original |
407 |
|
408 |
もしこの処理を行うコマンドラインプログラムがあるなら、そのプログラムが |
409 |
出力するものを得るために逆クォートで呼び出して、後でそれを使うことも |
410 |
できます: |
411 |
|
412 |
my $clear_string = `clear`; |
413 |
|
414 |
print $clear_string; |
415 |
|
416 |
=head2 How do I get the screen size? |
417 |
|
418 |
(画面サイズを得るには?) |
419 |
|
420 |
=begin original |
421 |
|
422 |
If you have L<Term::ReadKey> module installed from CPAN, |
423 |
you can use it to fetch the width and height in characters |
424 |
and in pixels: |
425 |
|
426 |
=end original |
427 |
|
428 |
CPAN にある L<Term::ReadKey> モジュールをインストールしているのなら、 |
429 |
文字やピクセルでの幅と高さを得ることができます: |
430 |
|
431 |
use Term::ReadKey; |
432 |
my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize(); |
433 |
|
434 |
=begin original |
435 |
|
436 |
This is more portable than the raw C<ioctl>, but not as |
437 |
illustrative: |
438 |
|
439 |
=end original |
440 |
|
441 |
以下の例は生の C<ioctl> よりも移植性がありますが、 |
442 |
あまりわかりやすい例ではありません: |
443 |
|
444 |
require 'sys/ioctl.ph'; |
445 |
die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; |
446 |
open(my $tty_fh, "+</dev/tty") or die "No tty: $!"; |
447 |
unless (ioctl($tty_fh, &TIOCGWINSZ, $winsize='')) { |
448 |
die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ; |
449 |
} |
450 |
my ($row, $col, $xpixel, $ypixel) = unpack('S4', $winsize); |
451 |
print "(row,col) = ($row,$col)"; |
452 |
print " (xpixel,ypixel) = ($xpixel,$ypixel)" if $xpixel || $ypixel; |
453 |
print "\n"; |
454 |
|
455 |
=head2 How do I ask the user for a password? |
456 |
|
457 |
(ユーザーにパスワードを尋ねるには?) |
458 |
|
459 |
=begin original |
460 |
|
461 |
(This question has nothing to do with the web. See a different |
462 |
FAQ for that.) |
463 |
|
464 |
=end original |
465 |
|
466 |
(この質問は web とは関係ありません。 |
467 |
それに関しては 別の FAQ を参照してください。) |
468 |
|
469 |
=begin original |
470 |
|
471 |
There's an example of this in L<perlfunc/crypt>). First, you put the |
472 |
terminal into "no echo" mode, then just read the password normally. |
473 |
You may do this with an old-style C<ioctl()> function, POSIX terminal |
474 |
control (see L<POSIX> or its documentation the Camel Book), or a call |
475 |
to the B<stty> program, with varying degrees of portability. |
476 |
|
477 |
=end original |
478 |
|
479 |
この例が L<perlfunc/crypt> にあります。 |
480 |
第一に、端末を“no echo”モードにし、それから通常通りにパスワードを |
481 |
読み込みます。 |
482 |
これを、古いスタイルの C<ioctl()> 関数を使ってできますし、あるいは |
483 |
POSIX の端末制御(L<POSIX> と、らくだ本を参照してください)を使うことも、 |
484 |
B<stty> プログラムを呼び出すことも可能です(ただしこれは移植性は劣ります)。 |
485 |
|
486 |
=begin original |
487 |
|
488 |
You can also do this for most systems using the L<Term::ReadKey> module |
489 |
from CPAN, which is easier to use and in theory more portable. |
490 |
|
491 |
=end original |
492 |
|
493 |
あるいはほとんどのシステムで、CPAN にある L<Term::ReadKey> を使って |
494 |
行うこともできます; これは使うのが簡単で、理論的にはより移植性があります。 |
495 |
|
496 |
use Term::ReadKey; |
497 |
|
498 |
ReadMode('noecho'); |
499 |
my $password = ReadLine(0); |
500 |
|
501 |
=head2 How do I read and write the serial port? |
502 |
|
503 |
(シリアルポートの読み書きを行うには?) |
504 |
|
505 |
=begin original |
506 |
|
507 |
This depends on which operating system your program is running on. In |
508 |
the case of Unix, the serial ports will be accessible through files in |
509 |
C</dev>; on other systems, device names will doubtless differ. |
510 |
Several problem areas common to all device interaction are the |
511 |
following: |
512 |
|
513 |
=end original |
514 |
|
515 |
これはプログラムを実行するオペレーティングシステムに依存します。 |
516 |
UNIX の場合、シリアルポートは C</dev> にあるファイルを通じてアクセスが |
517 |
可能です; 他のシステムでは、デバイス名は異なったものであることでしょう。 |
518 |
全てのデバイス操作に共通の問題点として以下のものが挙げられます: |
519 |
|
520 |
=over 4 |
521 |
|
522 |
=item lockfiles |
523 |
|
524 |
(ロックファイル) |
525 |
|
526 |
=begin original |
527 |
|
528 |
Your system may use lockfiles to control multiple access. Make sure |
529 |
you follow the correct protocol. Unpredictable behavior can result |
530 |
from multiple processes reading from one device. |
531 |
|
532 |
=end original |
533 |
|
534 |
あなたの使っているシステムは、多重アクセスを制御するためにロック |
535 |
ファイルを使用しているかもしれません。 |
536 |
正しい手順に従うようにしてください。 |
537 |
予測のつかない振る舞いは一つのデバイスに対する複数のプロセスの読み出しが |
538 |
原因かもしれません。 |
539 |
|
540 |
=item open mode |
541 |
|
542 |
(オープンモード) |
543 |
|
544 |
=begin original |
545 |
|
546 |
If you expect to use both read and write operations on the device, |
547 |
you'll have to open it for update (see L<perlfunc/"open"> for |
548 |
details). You may wish to open it without running the risk of |
549 |
blocking by using C<sysopen()> and C<O_RDWR|O_NDELAY|O_NOCTTY> from the |
550 |
L<Fcntl> module (part of the standard perl distribution). See |
551 |
L<perlfunc/"sysopen"> for more on this approach. |
552 |
|
553 |
=end original |
554 |
|
555 |
一つのデバイスに対して、読み込みと書き出しの両方の操作ができることを |
556 |
期待しているのなら、それを更新モード(詳しくは L<perlfunc/"open"> を |
557 |
参照)でオープンする必要があるでしょう。 |
558 |
C<sysopen()> と L<Fcntl> モジュールにある C<O_RDWR|O_NDELAY|O_NOCTTY> とを使って |
559 |
ブロッキングする危険性なしにオープンを実行したいと考えるかもしれません。 |
560 |
このアプローチに関する詳細は L<perlfunc/"sysopen"> を参照してください。 |
561 |
|
562 |
=item end of line |
563 |
|
564 |
(行の末尾) |
565 |
|
566 |
=begin original |
567 |
|
568 |
Some devices will be expecting a "\r" at the end of each line rather |
569 |
than a "\n". In some ports of perl, "\r" and "\n" are different from |
570 |
their usual (Unix) ASCII values of "\015" and "\012". You may have to |
571 |
give the numeric values you want directly, using octal ("\015"), hex |
572 |
("0x0D"), or as a control-character specification ("\cM"). |
573 |
|
574 |
=end original |
575 |
|
576 |
幾つかのデバイスでは、行の終端に "\n" ではなく "\r" を期待しています。 |
577 |
perl の移植の一部では、"\r" と "\n" は通常の(UNIX の) ASCII 値である |
578 |
"\015" と "\012" とは異なったものになっています。 |
579 |
八進表記 ("\015") や十六進表記("0x0D")、 |
580 |
あるいは制御文字指定 ("\cM") を使って |
581 |
直接数値を与える必要があるかもしれません。 |
582 |
|
583 |
=begin original |
584 |
|
585 |
print DEV "atv1\012"; # wrong, for some devices |
586 |
print DEV "atv1\015"; # right, for some devices |
587 |
|
588 |
=end original |
589 |
|
590 |
print DEV "atv1\012"; # 一部のデバイスにとっては間違い |
591 |
print DEV "atv1\015"; # 一部のデバイスにとっては正しい |
592 |
|
593 |
=begin original |
594 |
|
595 |
Even though with normal text files a "\n" will do the trick, there is |
596 |
still no unified scheme for terminating a line that is portable |
597 |
between Unix, DOS/Win, and Macintosh, except to terminate I<ALL> line |
598 |
ends with "\015\012", and strip what you don't need from the output. |
599 |
This applies especially to socket I/O and autoflushing, discussed |
600 |
next. |
601 |
|
602 |
=end original |
603 |
|
604 |
通常のテキストファイルでさえも、"\n" はいたずらを行う可能性があります; |
605 |
B<全ての行> を "\015\012" で終わらせ、出力から必要のないものを |
606 |
取り除くということを除いては、UNIX、DOS/Win、Macintosh との間で |
607 |
互換性のある行の終端方法は未だに統一されていません。 |
608 |
これは特にソケットの入出力や自動フラッシュで適用されます; これについては |
609 |
次に述べます。 |
610 |
|
611 |
=item flushing output |
612 |
|
613 |
=begin original |
614 |
|
615 |
If you expect characters to get to your device when you C<print()> them, |
616 |
you'll want to autoflush that filehandle. You can use C<select()> |
617 |
and the C<$|> variable to control autoflushing (see L<perlvar/$E<verbar>> |
618 |
and L<perlfunc/select>, or L<perlfaq5>, "How do I flush/unbuffer an |
619 |
output filehandle? Why must I do this?"): |
620 |
|
621 |
=end original |
622 |
|
623 |
デバイスに C<print()> した時にそのキャラクタが反映されるようにしたいのなら、 |
624 |
そのハンドルを自動フラッシュするようにしたいでしょう。 |
625 |
自動フラッシュを制御するのに、C<$|> という変数と、C<select()> を |
626 |
使うことができます(L<perlvar/$E<verbar>>とL<perlfunc/select> または |
627 |
L<perlfaq5> の "How do I flush/unbuffer an output filehandle? Why must |
628 |
I do this?" を参照してください)。 |
629 |
|
630 |
my $old_handle = select($dev_fh); |
631 |
$| = 1; |
632 |
select($old_handle); |
633 |
|
634 |
=begin original |
635 |
|
636 |
You'll also see code that does this without a temporary variable, as in |
637 |
|
638 |
=end original |
639 |
|
640 |
以下のように、これを一時変数を使わないで行うコードを見るかもしれません: |
641 |
|
642 |
select((select($deb_handle), $| = 1)[0]); |
643 |
|
644 |
=begin original |
645 |
|
646 |
Or if you don't mind pulling in a few thousand lines |
647 |
of code just because you're afraid of a little C<$|> variable: |
648 |
|
649 |
=end original |
650 |
|
651 |
C<$|> というような変数に戸惑いを感じていて、数千行のプログラムを |
652 |
引っ張りこむことを気にしないのなら以下のようにできます: |
653 |
|
654 |
use IO::Handle; |
655 |
$dev_fh->autoflush(1); |
656 |
|
657 |
=begin original |
658 |
|
659 |
As mentioned in the previous item, this still doesn't work when using |
660 |
socket I/O between Unix and Macintosh. You'll need to hard code your |
661 |
line terminators, in that case. |
662 |
|
663 |
=end original |
664 |
|
665 |
先のアイテムで述べたように、これでもまだ UNIX と Macintosh の間で |
666 |
ソケット I/O を使った場合にはうまく動作しません。 |
667 |
その場合には、行の終端をハードコーディングする必要があるでしょう。 |
668 |
|
669 |
=item non-blocking input |
670 |
|
671 |
=begin original |
672 |
|
673 |
If you are doing a blocking C<read()> or C<sysread()>, you'll have to |
674 |
arrange for an alarm handler to provide a timeout (see |
675 |
L<perlfunc/alarm>). If you have a non-blocking open, you'll likely |
676 |
have a non-blocking read, which means you may have to use a 4-arg |
677 |
C<select()> to determine whether I/O is ready on that device (see |
678 |
L<perlfunc/"select">. |
679 |
|
680 |
=end original |
681 |
|
682 |
ブロッキング C<read()> やブロッキング C<sysread()> を行うのであれば、 |
683 |
タイムアウトを実現するためにalarmハンドラーをアレンジする必要があるでしょう |
684 |
(L<perlfunc/alarm> を参照)。 |
685 |
もしノンブロッキング open を持っているのであれば、デバイスが入出力完了の |
686 |
状態であるかどうかを決定するために四つの引数を取る C<select()> を使う必要が |
687 |
あるであろうノンブロッキング read も同様に持っていることでしょう |
688 |
(L<perlfunc/"select">を参照)。 |
689 |
|
690 |
=back |
691 |
|
692 |
=begin original |
693 |
|
694 |
While trying to read from his caller-id box, the notorious Jamie |
695 |
Zawinski C<< <jwz@netscape.com> >>, after much gnashing of teeth and |
696 |
fighting with C<sysread>, C<sysopen>, POSIX's C<tcgetattr> business, |
697 |
and various other functions that go bump in the night, finally came up |
698 |
with this: |
699 |
|
700 |
=end original |
701 |
|
702 |
caller-id ボックスから読み出すことに挑戦したことで有名な |
703 |
Jamie Zawinski C<< <jwd@netscape.com> >> は、 |
704 |
C<sysread> や C<sysopen>、POSIX の C<tcgetattr> その他さまざまな関数を扱い |
705 |
悪戦苦闘しながら、最終的には以下のようものを作りました: |
706 |
|
707 |
sub open_modem { |
708 |
use IPC::Open2; |
709 |
my $stty = `/bin/stty -g`; |
710 |
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 |
712 |
# been opened on a pipe... |
713 |
system("/bin/stty $stty"); |
714 |
$_ = <MODEM_IN>; |
715 |
chomp; |
716 |
if ( !m/^Connected/ ) { |
717 |
print STDERR "$0: cu printed `$_' instead of `Connected'\n"; |
718 |
} |
719 |
} |
720 |
|
721 |
=head2 How do I decode encrypted password files? |
722 |
|
723 |
(暗号化されたパスワードファイルを復号化するには?) |
724 |
|
725 |
=begin original |
726 |
|
727 |
You spend lots and lots of money on dedicated hardware, but this is |
728 |
bound to get you talked about. |
729 |
|
730 |
=end original |
731 |
|
732 |
特別なハードウェアに非常に多額のお金を掛けてください; |
733 |
しかしこれはあなたが何について話しているかによります。 |
734 |
|
735 |
=begin original |
736 |
|
737 |
Seriously, you can't if they are Unix password files--the Unix |
738 |
password system employs one-way encryption. It's more like hashing |
739 |
than encryption. The best you can do is check whether something else |
740 |
hashes to the same string. You can't turn a hash back into the |
741 |
original string. Programs like Crack can forcibly (and intelligently) |
742 |
try to guess passwords, but don't (can't) guarantee quick success. |
743 |
|
744 |
=end original |
745 |
|
746 |
まじめな話をすると、UNIX のパスワードに対してのものならできません -- |
747 |
UNIX のパスワードシステムは一方向の暗号化を採用しています。 |
748 |
それは暗号化というよりはむしろハッシュ化といえるものです。 |
749 |
あなたがチェックできる最善の方法は、同じ文字列に対するハッシュかどうかを |
750 |
調べることです。 |
751 |
ハッシュを元の文字列に戻すことはできません。 |
752 |
Crack のようなプログラムは考えられるパスワードを力づくで(そして知的に) |
753 |
試しますが、即座に成功するものを生成することはしません(できません)。 |
754 |
|
755 |
=begin original |
756 |
|
757 |
If you're worried about users selecting bad passwords, you should |
758 |
proactively check when they try to change their password (by modifying |
759 |
L<passwd(1)>, for example). |
760 |
|
761 |
=end original |
762 |
|
763 |
もしあなたが、ユーザーが悪いパスワードを選択してしまうことを |
764 |
心配しているのであれば、ユーザーが(たとえば L<passwd(1)> を使って)自分の |
765 |
パスワードを変更しようとしたときに積極的にチェックをすべきでしょう。 |
766 |
|
767 |
=head2 How do I start a process in the background? |
768 |
|
769 |
(バックグラウンドでプロセスを起動するには?) |
770 |
|
771 |
=begin original |
772 |
|
773 |
(contributed by brian d foy) |
774 |
|
775 |
=end original |
776 |
|
777 |
(brian d foy によって寄贈されました) |
778 |
|
779 |
=begin original |
780 |
|
781 |
There's not a single way to run code in the background so you don't |
782 |
have to wait for it to finish before your program moves on to other |
783 |
tasks. Process management depends on your particular operating system, |
784 |
and many of the techniques are covered in L<perlipc>. |
785 |
|
786 |
=end original |
787 |
|
788 |
バックグラウンドでコードを実行するための単一の方法というのはないので、 |
789 |
あなたのプログラムが他のアスクに移動する前に終わるのを待つ必要は |
790 |
ありません。 |
791 |
プロセス管理は使用するオペレーティングシステムに依存していて、 |
792 |
多くのテクニックは L<perlipc> にあります。 |
793 |
|
794 |
=begin original |
795 |
|
796 |
Several CPAN modules may be able to help, including L<IPC::Open2> or |
797 |
L<IPC::Open3>, L<IPC::Run>, L<Parallel::Jobs>, |
798 |
L<Parallel::ForkManager>, L<POE>, L<Proc::Background>, and |
799 |
L<Win32::Process>. There are many other modules you might use, so |
800 |
check those namespaces for other options too. |
801 |
|
802 |
=end original |
803 |
|
804 |
L<IPC::Open2>, L<IPC::Open3>, L<IPC::Run>, L<Parallel::Jobs>, |
805 |
L<Parallel::ForkManager>, L<POE>, L<Proc::Background>, L<Win32::Process> |
806 |
といった CPAN モジュールが助けになるでしょう。 |
807 |
その他にも使えるかもしれない多くのモジュールがありますので、 |
808 |
これらの名前空間をチェックしてみてください。 |
809 |
|
810 |
=begin original |
811 |
|
812 |
If you are on a Unix-like system, you might be able to get away with a |
813 |
system call where you put an C<&> on the end of the command: |
814 |
|
815 |
=end original |
816 |
|
817 |
Unix 風のシステムを使っているなら、コマンドの最後に C<&> を置くことで |
818 |
システムコールから切り離すことができるかもしれません: |
819 |
|
820 |
system("cmd &") |
821 |
|
822 |
=begin original |
823 |
|
824 |
You can also try using C<fork>, as described in L<perlfunc> (although |
825 |
this is the same thing that many of the modules will do for you). |
826 |
|
827 |
=end original |
828 |
|
829 |
L<perlfunc> に記述されているように、C<fork> を使うこともできます |
830 |
(しかしこれは多くのモジュールがやってくれるのと同じことです)。 |
831 |
|
832 |
=over 4 |
833 |
|
834 |
=item STDIN, STDOUT, and STDERR are shared |
835 |
|
836 |
(STDIN, STDOUT, STDERR は共有されます) |
837 |
|
838 |
=begin original |
839 |
|
840 |
Both the main process and the backgrounded one (the "child" process) |
841 |
share the same STDIN, STDOUT and STDERR filehandles. If both try to |
842 |
access them at once, strange things can happen. You may want to close |
843 |
or reopen these for the child. You can get around this with |
844 |
C<open>ing a pipe (see L<perlfunc/"open">) but on some systems this |
845 |
means that the child process cannot outlive the parent. |
846 |
|
847 |
=end original |
848 |
|
849 |
主プロセスとバックグラウンドプロセス(子プロセス)で同じ STDIN, |
850 |
STDOUT, STDERR のファイルハンドルが共有されます。 |
851 |
両方のプロセスが同時にアクセスしようとすると、 |
852 |
おかしな事が発生するかもしれません。 |
853 |
子プロセス用のこれらのハンドルを、クローズしたり再オープンしたり |
854 |
したくなるかもしれません。 |
855 |
これは、パイプを C<open> することで行えますが、 |
856 |
一部のシステムにおいてはこれは子プロセスが親プロセスよりも |
857 |
長生きすることはできないということになります。 |
858 |
|
859 |
=item Signals |
860 |
|
861 |
(シグナル) |
862 |
|
863 |
=begin original |
864 |
|
865 |
You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too. |
866 |
SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is |
867 |
sent when you write to a filehandle whose child process has closed (an |
868 |
untrapped SIGPIPE can cause your program to silently die). This is |
869 |
not an issue with C<system("cmd&")>. |
870 |
|
871 |
=end original |
872 |
|
873 |
SIGCHLD シグナルを捕捉する必要があり、可能なら SIGPIPE も |
874 |
捕捉する必要があるでしょう。 |
875 |
SIGCHLD はバックグラウンドプロセスが終了したときに送られます。 |
876 |
SIGPIPE は既にクローズされている子プロセスを所有するファイルハンドルに |
877 |
書き込みを行ったときに送られます(トラップされていない SIGPIPE は、 |
878 |
あなたのプログラムを黙って終わらせてしまうかもしれません)。 |
879 |
これは C<system("cmd&")> を使ったときには起こりません。 |
880 |
|
881 |
=item Zombies |
882 |
|
883 |
(ゾンビ) |
884 |
|
885 |
=begin original |
886 |
|
887 |
You have to be prepared to "reap" the child process when it finishes. |
888 |
|
889 |
=end original |
890 |
|
891 |
子プロセスが終了したときにそれを“刈り取る”(reap) 準備をする必要があります。 |
892 |
|
893 |
$SIG{CHLD} = sub { wait }; |
894 |
|
895 |
$SIG{CHLD} = 'IGNORE'; |
896 |
|
897 |
=begin original |
898 |
|
899 |
You can also use a double fork. You immediately C<wait()> for your |
900 |
first child, and the init daemon will C<wait()> for your grandchild once |
901 |
it exits. |
902 |
|
903 |
=end original |
904 |
|
905 |
2 重 fork も使えます。 |
906 |
あなたは直ちに最初の子に対して C<wait()> し、init デーモンは孫が終了するのを |
907 |
C<wait()> します。 |
908 |
|
909 |
unless ($pid = fork) { |
910 |
unless (fork) { |
911 |
exec "what you really wanna do"; |
912 |
die "exec failed!"; |
913 |
} |
914 |
exit 0; |
915 |
} |
916 |
waitpid($pid, 0); |
917 |
|
918 |
=begin original |
919 |
|
920 |
See L<perlipc/"Signals"> for other examples of code to do this. |
921 |
Zombies are not an issue with C<system("prog &")>. |
922 |
|
923 |
=end original |
924 |
|
925 |
これを行うサンプルは L<perlipc/"Signals"> を参照してください。 |
926 |
ゾンビは C<system("prog &")> を使ったときには発生しません。 |
927 |
|
928 |
=back |
929 |
|
930 |
=head2 How do I trap control characters/signals? |
931 |
|
932 |
(制御文字やシグナルをトラップするには?) |
933 |
|
934 |
=begin original |
935 |
|
936 |
You don't actually "trap" a control character. Instead, that character |
937 |
generates a signal which is sent to your terminal's currently |
938 |
foregrounded process group, which you then trap in your process. |
939 |
Signals are documented in L<perlipc/"Signals"> and the |
940 |
section on "Signals" in the Camel. |
941 |
|
942 |
=end original |
943 |
|
944 |
実際には制御文字を“トラップ”できません。 |
945 |
その代わりに、その文字が生成して |
946 |
端末のフォアグラウンドプロセスに送られることになる |
947 |
シグナルがトラップできます。 |
948 |
シグナルは L<perlipc/"Signals"> とらくだ本の "Signal" の章に |
949 |
説明があります。 |
950 |
|
951 |
=begin original |
952 |
|
953 |
You can set the values of the C<%SIG> hash to be the functions you want |
954 |
to handle the signal. After perl catches the signal, it looks in C<%SIG> |
955 |
for a key with the same name as the signal, then calls the subroutine |
956 |
value for that key. |
957 |
|
958 |
=end original |
959 |
|
960 |
C<%SIG> ハッシュにシグナルを扱いたい関数をセットできます。 |
961 |
perl がシグナルを捕捉した後、perl はシグナルと同じ名前をキーとして C<%SIG> を |
962 |
見て、そのキーの値であるサブルーチンを呼び出します。 |
963 |
|
964 |
# as an anonymous subroutine |
965 |
|
966 |
$SIG{INT} = sub { syswrite(STDERR, "ouch\n", 5 ) }; |
967 |
|
968 |
# or a reference to a function |
969 |
|
970 |
$SIG{INT} = \&ouch; |
971 |
|
972 |
# or the name of the function as a string |
973 |
|
974 |
$SIG{INT} = "ouch"; |
975 |
|
976 |
=begin original |
977 |
|
978 |
Perl versions before 5.8 had in its C source code signal handlers which |
979 |
would catch the signal and possibly run a Perl function that you had set |
980 |
in C<%SIG>. This violated the rules of signal handling at that level |
981 |
causing perl to dump core. Since version 5.8.0, perl looks at C<%SIG> |
982 |
B<after> the signal has been caught, rather than while it is being caught. |
983 |
Previous versions of this answer were incorrect. |
984 |
|
985 |
=end original |
986 |
|
987 |
バージョン 5.8 より前の Perl には、シグナルを受け取って、可能なら C<%SIG> |
988 |
にセットされている Perl の関数を実行する C ソースコードのシグナルハンドラが |
989 |
ありました。 |
990 |
これはこのレベルでのシグナルハンドリングのルールに違反していて、perl の |
991 |
コアダンプを引き起こしていました。 |
992 |
バージョン 5.8.0 から、perl はシグナルを捕捉している間ではなく、シグナルを |
993 |
捕捉した B<後に> C<%SIG> を見ます。 |
994 |
この答えの以前のバージョンは間違っていました。 |
995 |
|
996 |
=head2 How do I modify the shadow password file on a Unix system? |
997 |
|
998 |
(UNIX システムのシャドウパスワードファイルを変更するには?) |
999 |
|
1000 |
=begin original |
1001 |
|
1002 |
If perl was installed correctly and your shadow library was written |
1003 |
properly, the C<getpw*()> functions described in L<perlfunc> should in |
1004 |
theory provide (read-only) access to entries in the shadow password |
1005 |
file. To change the file, make a new shadow password file (the format |
1006 |
varies from system to system--see L<passwd(1)> for specifics) and use |
1007 |
C<pwd_mkdb(8)> to install it (see L<pwd_mkdb(8)> for more details). |
1008 |
|
1009 |
=end original |
1010 |
|
1011 |
perl が正しくインストールされていて、かつ、シャドウライブラリが |
1012 |
きちんとインストールされていれば、L<perlfunc> で説明されている |
1013 |
C<getpw*()> 関数がシャドウパスワードファイルに対する(リードオンリーの) |
1014 |
アクセスを提供しています。 |
1015 |
ファイルを変更するには、新たなシャドウパスワードファイルを作成して |
1016 |
(フォーマットはシステム毎に異なります--詳しくは L<passwd(1)> |
1017 |
を参照してください)、C<pwd_mkdb(8)> を使ってそれをインストールします |
1018 |
(詳細は L<pwd_mkdb(8)> を参照のこと)。 |
1019 |
|
1020 |
=head2 How do I set the time and date? |
1021 |
|
1022 |
(日付や時刻を設定するには?) |
1023 |
|
1024 |
=begin original |
1025 |
|
1026 |
Assuming you're running under sufficient permissions, you should be |
1027 |
able to set the system-wide date and time by running the C<date(1)> |
1028 |
program. (There is no way to set the time and date on a per-process |
1029 |
basis.) This mechanism will work for Unix, MS-DOS, Windows, and NT; |
1030 |
the VMS equivalent is C<set time>. |
1031 |
|
1032 |
=end original |
1033 |
|
1034 |
あなたが十分な権限を持っているとすれば、C<date(1)> プログラムを実行すれば |
1035 |
システム全体の日付や時刻を設定できるはずです |
1036 |
(プロセス毎に日付や時刻を設定する方法はありません)。 |
1037 |
この機構は、UNIX、MS-DOS、Windows、NT でうまくいくでしょう。 |
1038 |
VMS には等価な C<set time> があります。 |
1039 |
|
1040 |
=begin original |
1041 |
|
1042 |
However, if all you want to do is change your time zone, you can |
1043 |
probably get away with setting an environment variable: |
1044 |
|
1045 |
=end original |
1046 |
|
1047 |
あなたのやりたいことがタイムゾーンの変更であるのなら、 |
1048 |
環境変数を変更することでそれができるでしょう。 |
1049 |
|
1050 |
$ENV{TZ} = "MST7MDT"; # Unixish |
1051 |
$ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms |
1052 |
system('trn', 'comp.lang.perl.misc'); |
1053 |
|
1054 |
=head2 How can I sleep() or alarm() for under a second? |
1055 |
X<Time::HiRes> X<BSD::Itimer> X<sleep> X<select> |
1056 |
|
1057 |
(一秒未満の時間に対する sleep() や alarm() をするには?) |
1058 |
|
1059 |
=begin original |
1060 |
|
1061 |
If you want finer granularity than the 1 second that the C<sleep()> |
1062 |
function provides, the easiest way is to use the C<select()> function as |
1063 |
documented in L<perlfunc/"select">. Try the L<Time::HiRes> and |
1064 |
the L<BSD::Itimer> modules (available from CPAN, and starting from |
1065 |
Perl 5.8 L<Time::HiRes> is part of the standard distribution). |
1066 |
|
1067 |
=end original |
1068 |
|
1069 |
C<sleep()> 関数が一秒未満の単位での動作をサポートすることを求めているのなら、 |
1070 |
L<perlfunc/"select"> にあるように、C<select()> を使うのが最も単純な方法です。 |
1071 |
L<Time::Hires> と L<BSD::Itimer> モジュール(CPAN から利用可能ですし、 |
1072 |
Perl 5.8 からは L<Time::HiRes> は標準配布の一部です)も |
1073 |
試してみてください。 |
1074 |
|
1075 |
=head2 How can I measure time under a second? |
1076 |
X<Time::HiRes> X<BSD::Itimer> X<sleep> X<select> |
1077 |
|
1078 |
(1 秒未満の時間を計るには?) |
1079 |
|
1080 |
=begin original |
1081 |
|
1082 |
(contributed by brian d foy) |
1083 |
|
1084 |
=end original |
1085 |
|
1086 |
(brian d foy によって寄贈されました) |
1087 |
|
1088 |
=begin original |
1089 |
|
1090 |
The L<Time::HiRes> module (part of the standard distribution as of |
1091 |
Perl 5.8) measures time with the C<gettimeofday()> system call, which |
1092 |
returns the time in microseconds since the epoch. If you can't install |
1093 |
L<Time::HiRes> for older Perls and you are on a Unixish system, you |
1094 |
may be able to call C<gettimeofday(2)> directly. See |
1095 |
L<perlfunc/syscall>. |
1096 |
|
1097 |
=end original |
1098 |
|
1099 |
(Perl 5.8 から標準配布の一部である) L<Time::HiRes> モジュールは、 |
1100 |
紀元からのマイクロ秒を返す C<gettimeofday()> システムコールを使って時間を |
1101 |
計測します。 |
1102 |
もし古い Perl を使っていて L<Time::HiRes> をインストールできず、 |
1103 |
Unixish を使っているなら、C<gettimeofday(2)> を直接使えます。 |
1104 |
L<perlfunc/syscall> を参照してください。 |
1105 |
|
1106 |
=head2 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) |
1107 |
|
1108 |
(atexit() や setjmp()/longjmp() をするには? (例外処理)) |
1109 |
|
1110 |
=begin original |
1111 |
|
1112 |
You can use the C<END> block to simulate C<atexit()>. Each package's |
1113 |
C<END> block is called when the program or thread ends. See the L<perlmod> |
1114 |
manpage for more details about C<END> blocks. |
1115 |
|
1116 |
=end original |
1117 |
|
1118 |
C<atexit()> をシミュレートするのに C<END> ブロックが使えます。 |
1119 |
それぞれのパッケージの C<END> ブロックは、プログラムやスレッドの終了時に |
1120 |
呼び出されます。 |
1121 |
C<END> ブロックに関するさらなる詳細については L<perlmod> を |
1122 |
参照してください。 |
1123 |
|
1124 |
=begin original |
1125 |
|
1126 |
For example, you can use this to make sure your filter program managed |
1127 |
to finish its output without filling up the disk: |
1128 |
|
1129 |
=end original |
1130 |
|
1131 |
例を挙げると、あなたのフィルタープログラムが出力を確実に |
1132 |
ディスクに送るようにするためには以下のようにできます: |
1133 |
|
1134 |
END { |
1135 |
close(STDOUT) || die "stdout close failed: $!"; |
1136 |
} |
1137 |
|
1138 |
=begin original |
1139 |
|
1140 |
The C<END> block isn't called when untrapped signals kill the program, |
1141 |
though, so if you use C<END> blocks you should also use |
1142 |
|
1143 |
=end original |
1144 |
|
1145 |
C<END> ブロックは、トラップされないシグナルがプログラムを強制終了させた |
1146 |
場合には呼び出されません; したがって、C<END> ブロックを使う場合には同時に |
1147 |
以下のようにするべきです: |
1148 |
|
1149 |
use sigtrap qw(die normal-signals); |
1150 |
|
1151 |
=begin original |
1152 |
|
1153 |
Perl's exception-handling mechanism is its C<eval()> operator. You |
1154 |
can use C<eval()> as C<setjmp> and C<die()> as C<longjmp>. For |
1155 |
details of this, see the section on signals, especially the time-out |
1156 |
handler for a blocking C<flock()> in L<perlipc/"Signals"> or the |
1157 |
section on "Signals" in I<Programming Perl>. |
1158 |
|
1159 |
=end original |
1160 |
|
1161 |
Perl の例外処理機構は C<eval()> 演算子です。 |
1162 |
C<setjmp> として C<eval()> を、C<longjmp> として C<die()> を使うことができます。 |
1163 |
これに関する詳細は、シグナルに関するセクション、 |
1164 |
特に L<perlipc/"Signals"> にあるブロッキング C<flock()> のための |
1165 |
タイムアウトハンドラーと、 I<Programming Perl> の "Signal" の章を参照してください。 |
1166 |
|
1167 |
=begin original |
1168 |
|
1169 |
If exception handling is all you're interested in, use one of the |
1170 |
many CPAN modules that handle exceptions, such as L<Try::Tiny>. |
1171 |
|
1172 |
=end original |
1173 |
|
1174 |
例外処理そのものに興味があるのなら、L<Try::Tiny> のような、例外を |
1175 |
扱うための多くの CPAN モジュールの一つを使ってください。 |
1176 |
|
1177 |
=begin original |
1178 |
|
1179 |
If you want the C<atexit()> syntax (and an C<rmexit()> as well), try the |
1180 |
C<AtExit> module available from CPAN. |
1181 |
|
1182 |
=end original |
1183 |
|
1184 |
C<atexit()> 構文(と、C<rmexit()>) が欲しいのなら、CPAN にある |
1185 |
C<AtExit> モジュールを試してみてください。 |
1186 |
|
1187 |
=head2 Why doesn't my sockets program work under System V (Solaris)? What does the error message "Protocol not supported" mean? |
1188 |
|
1189 |
(なぜ私のソケットプログラムはSystem V (Solaris) ではうまく動かないの? "Protocol not supported"というエラーメッセージの意味するところは?) |
1190 |
|
1191 |
=begin original |
1192 |
|
1193 |
Some Sys-V based systems, notably Solaris 2.X, redefined some of the |
1194 |
standard socket constants. Since these were constant across all |
1195 |
architectures, they were often hardwired into perl code. The proper |
1196 |
way to deal with this is to "use Socket" to get the correct values. |
1197 |
|
1198 |
=end original |
1199 |
|
1200 |
一部の System 5 ベースのシステム、特に Solaris 2.x では標準のソケット定数の |
1201 |
幾つかが再定義されています。 |
1202 |
これらの定数は全てのアーキテクチャに渡るものであったので、 |
1203 |
しばしば perl コードにハードコーディングされています。 |
1204 |
これに対処する適切な方法は正しい値を得るために "use Socket" とすることです。 |
1205 |
|
1206 |
=begin original |
1207 |
|
1208 |
Note that even though SunOS and Solaris are binary compatible, these |
1209 |
values are different. Go figure. |
1210 |
|
1211 |
=end original |
1212 |
|
1213 |
SunOS と Solaris とではバイナリ互換性があるにも関らず、 |
1214 |
これらの値が異なるということに注意してください。 |
1215 |
不思議なことです。 |
1216 |
|
1217 |
=head2 How can I call my system's unique C functions from Perl? |
1218 |
|
1219 |
(Perl から私のシステムに固有の C 関数を呼び出すには?) |
1220 |
|
1221 |
=begin original |
1222 |
|
1223 |
In most cases, you write an external module to do it--see the answer |
1224 |
to "Where can I learn about linking C with Perl? [h2xs, xsubpp]". |
1225 |
However, if the function is a system call, and your system supports |
1226 |
C<syscall()>, you can use the C<syscall> function (documented in |
1227 |
L<perlfunc>). |
1228 |
|
1229 |
=end original |
1230 |
|
1231 |
ほとんどの場合、その方法は外部モジュールを作るというものです -- |
1232 |
"Where can I learn about linking C with Perl? [h2xs, xsubpp]" |
1233 |
の回答を参照してください。 |
1234 |
ただし、その関数がシステムコールでありあなたの使っているシステムが |
1235 |
C<syscall()> をサポートしているのであれば、 |
1236 |
C<syscall> 関数(L<perlfunc> に説明があります)を使うことができます。 |
1237 |
|
1238 |
=begin original |
1239 |
|
1240 |
Remember to check the modules that came with your distribution, and |
1241 |
CPAN as well--someone may already have written a module to do it. On |
1242 |
Windows, try L<Win32::API>. On Macs, try L<Mac::Carbon>. If no module |
1243 |
has an interface to the C function, you can inline a bit of C in your |
1244 |
Perl source with L<Inline::C>. |
1245 |
|
1246 |
=end original |
1247 |
|
1248 |
一緒に配布されたモジュールや CPAN にあるモジュールをチェックすることを |
1249 |
忘れないでください--誰かが求めるモジュールを既に作っているかもしれません。 |
1250 |
Windows では、L<Win32::API> を試してください。 |
1251 |
Mac では、L<Mac::Carbon> を試してください。 |
1252 |
C 関数へのインターフェースを持つモジュールがなければ、 |
1253 |
L<Inline::C> を使って Perl のソースコードにちょっとした C のコードを |
1254 |
インライン化できます。 |
1255 |
|
1256 |
=head2 Where do I get the include files to do ioctl() or syscall()? |
1257 |
|
1258 |
(ioctl() や syscall() で使うための include ファイルはどこで入手できますか?) |
1259 |
|
1260 |
=begin original |
1261 |
|
1262 |
Historically, these would be generated by the L<h2ph> tool, part of the |
1263 |
standard perl distribution. This program converts C<cpp(1)> directives |
1264 |
in C header files to files containing subroutine definitions, like |
1265 |
C<SYS_getitimer()>, which you can use as arguments to your functions. |
1266 |
It doesn't work perfectly, but it usually gets most of the job done. |
1267 |
Simple files like F<errno.h>, F<syscall.h>, and F<socket.h> were fine, |
1268 |
but the hard ones like F<ioctl.h> nearly always need to be hand-edited. |
1269 |
Here's how to install the *.ph files: |
1270 |
|
1271 |
=end original |
1272 |
|
1273 |
伝統的に、これらのファイルは標準配布に含まれる L<h2ph> というツールによって |
1274 |
生成されるものです。 |
1275 |
このプログラムは C のヘッダーファイルにある C<cpp(1)> 指示子を |
1276 |
C<SYS_getitimer()> のような、関数に対する引数として使うことのできる |
1277 |
サブルーチン定義を含むファイルに変換するものです。 |
1278 |
これは完璧なものではありませんが、ほとんどの場合には十分な仕事を行います。 |
1279 |
F<errno.h>, F<syscall.h>, F<socket.h> のような単純なファイルはよいのですが、 |
1280 |
F<ioctl.h> のように難しいものはほとんど常に手で編集する必要があります。 |
1281 |
以下の手順は、*.ph ファイルをインストールするためのものです。 |
1282 |
|
1283 |
1. Become the super-user |
1284 |
2. cd /usr/include |
1285 |
3. h2ph *.h */*.h |
1286 |
|
1287 |
=begin original |
1288 |
|
1289 |
If your system supports dynamic loading, for reasons of portability and |
1290 |
sanity you probably ought to use L<h2xs> (also part of the standard perl |
1291 |
distribution). This tool converts C header files to Perl extensions. |
1292 |
See L<perlxstut> for how to get started with L<h2xs>. |
1293 |
|
1294 |
=end original |
1295 |
|
1296 |
あなたの使っているシステムが動的ローディングをサポートしているのであれば、 |
1297 |
移植性と健全性(sanity)のために、L<h2xs> を使うべきでしょう |
1298 |
(これも標準の perl 配布キットに含まれています)。 |
1299 |
このツールは、C のヘッダーファイルを Perl のエクステンションに変換します。 |
1300 |
L<h2xs> の使い方は L<perlxstut> を参照してください。 |
1301 |
|
1302 |
=begin original |
1303 |
|
1304 |
If your system doesn't support dynamic loading, you still probably |
1305 |
ought to use L<h2xs>. See L<perlxstut> and L<ExtUtils::MakeMaker> for |
1306 |
more information (in brief, just use B<make perl> instead of a plain |
1307 |
B<make> to rebuild perl with a new static extension). |
1308 |
|
1309 |
=end original |
1310 |
|
1311 |
あなたの使っているシステムが動的ローディングをサポートしていない |
1312 |
場合であっても、やはり L<h2xs> を使うべきでしょう。 |
1313 |
より詳しい情報は L<perlxstut> と L<ExtUtils::MakeMaker> を参照してください |
1314 |
(簡単に言うと、新しい静的エクステションを伴った perl を再ビルドするのに |
1315 |
通常の B<make> を使うのではなく、B<make perl> を使うだけです)。 |
1316 |
|
1317 |
=head2 Why do setuid perl scripts complain about kernel problems? |
1318 |
|
1319 |
(なぜ setuid された perl スクリプトはカーネルの問題について文句を言うのでしょうか?) |
1320 |
|
1321 |
=begin original |
1322 |
|
1323 |
Some operating systems have bugs in the kernel that make setuid |
1324 |
scripts inherently insecure. Perl gives you a number of options |
1325 |
(described in L<perlsec>) to work around such systems. |
1326 |
|
1327 |
=end original |
1328 |
|
1329 |
幾つかのオペレーティングシステムは、setuid スクリプトを本質的に |
1330 |
安全でなくするようなカーネルのバグを抱えています。 |
1331 |
Perl は、そういったシステムに対処して動作させるための幾つかのオプションを |
1332 |
持っています(L<perlsec> に説明があります)。 |
1333 |
|
1334 |
=head2 How can I open a pipe both to and from a command? |
1335 |
|
1336 |
(あるコマンドに対する双方向のパイプをオープンするには?) |
1337 |
|
1338 |
=begin original |
1339 |
|
1340 |
The L<IPC::Open2> module (part of the standard perl distribution) is |
1341 |
an easy-to-use approach that internally uses C<pipe()>, C<fork()>, and |
1342 |
C<exec()> to do the job. Make sure you read the deadlock warnings in |
1343 |
its documentation, though (see L<IPC::Open2>). See |
1344 |
L<perlipc/"Bidirectional Communication with Another Process"> and |
1345 |
L<perlipc/"Bidirectional Communication with Yourself"> |
1346 |
|
1347 |
=end original |
1348 |
|
1349 |
L<IPC::Open2> モジュール(標準の perl の配布に含まれています)は内部的に |
1350 |
C<pipe()>, C<fork()>, C<exec()> を使った使いやすい手法です。 |
1351 |
ただし、ドキュメントにあるデッドロックの警告をよく読んでください |
1352 |
(L<IPC::Open2>を参照)。 |
1353 |
L<perlipc/"Bidirectional Communication with Another Process"> と |
1354 |
L<perlipc/"Bidirectional Communication with Yourself"> も参照してください。 |
1355 |
|
1356 |
=begin original |
1357 |
|
1358 |
You may also use the L<IPC::Open3> module (part of the standard perl |
1359 |
distribution), but be warned that it has a different order of |
1360 |
arguments from L<IPC::Open2> (see L<IPC::Open3>). |
1361 |
|
1362 |
=end original |
1363 |
|
1364 |
L<IPC::Open3> モジュール(標準配布パッケージに含まれています)も使えますが、 |
1365 |
これは L<IPC::Open2> とは引数の順序が違うということに注意してください |
1366 |
(L<IPC::Open3> を参照してください)。 |
1367 |
|
1368 |
=head2 Why can't I get the output of a command with system()? |
1369 |
|
1370 |
(なぜ system() を使ったコマンドの出力を得ることができないのでしょうか?) |
1371 |
|
1372 |
=begin original |
1373 |
|
1374 |
You're confusing the purpose of C<system()> and backticks (``). C<system()> |
1375 |
runs a command and returns exit status information (as a 16 bit value: |
1376 |
the low 7 bits are the signal the process died from, if any, and |
1377 |
the high 8 bits are the actual exit value). Backticks (``) run a |
1378 |
command and return what it sent to STDOUT. |
1379 |
|
1380 |
=end original |
1381 |
|
1382 |
C<system()> の目的と逆クォートの目的を混同しているのでしょう。 |
1383 |
C<system()> はコマンドを実行して、終了ステータス情報(16bit 値として: |
1384 |
下位 7bit は(もしあれば)終了したプロセスからのシグナルで、上位 8bit は |
1385 |
実際の終了ステータス)を返します。 |
1386 |
逆クォートはコマンドを実行し、そのコマンドが STDOUT に送ったものを返します。 |
1387 |
|
1388 |
my $exit_status = system("mail-users"); |
1389 |
my $output_string = `ls`; |
1390 |
|
1391 |
=head2 How can I capture STDERR from an external command? |
1392 |
|
1393 |
(外部コマンドの STDERR を捕捉するには?) |
1394 |
|
1395 |
=begin original |
1396 |
|
1397 |
There are three basic ways of running external commands: |
1398 |
|
1399 |
=end original |
1400 |
|
1401 |
外部コマンドを実行する基本的なやり方が三つあります: |
1402 |
|
1403 |
system $cmd; # using system() |
1404 |
my $output = `$cmd`; # using backticks (``) |
1405 |
open (my $pipe_fh, "$cmd |"); # using open() |
1406 |
|
1407 |
=begin original |
1408 |
|
1409 |
With C<system()>, both STDOUT and STDERR will go the same place as the |
1410 |
script's STDOUT and STDERR, unless the C<system()> command redirects them. |
1411 |
Backticks and C<open()> read B<only> the STDOUT of your command. |
1412 |
|
1413 |
=end original |
1414 |
|
1415 |
C<system()> を使った場合の STDOUT と STDERR は、C<system()> コマンドが |
1416 |
リダイレクトを行っていない限りはそれぞれスクリプトが使っていた |
1417 |
STDOUT と STDERR へ出力されます。 |
1418 |
逆クォートと C<open()> はコマンドの STDOUT B<だけを> 読み込みます。 |
1419 |
|
1420 |
=begin original |
1421 |
|
1422 |
You can also use the C<open3()> function from L<IPC::Open3>. Benjamin |
1423 |
Goldberg provides some sample code: |
1424 |
|
1425 |
=end original |
1426 |
|
1427 |
L<IPC::Open3> の C<open3()> 関数も使えます。 |
1428 |
Benjamin Goldberg がいくつかのサンプルコードを提供しています: |
1429 |
|
1430 |
=begin original |
1431 |
|
1432 |
To capture a program's STDOUT, but discard its STDERR: |
1433 |
|
1434 |
=end original |
1435 |
|
1436 |
あるプログラムの STDOUT は捕捉したいが、STDERR は捨てたい場合: |
1437 |
|
1438 |
use IPC::Open3; |
1439 |
use File::Spec; |
1440 |
use Symbol qw(gensym); |
1441 |
open(NULL, ">", File::Spec->devnull); |
1442 |
my $pid = open3(gensym, \*PH, ">&NULL", "cmd"); |
1443 |
while( <PH> ) { } |
1444 |
waitpid($pid, 0); |
1445 |
|
1446 |
=begin original |
1447 |
|
1448 |
To capture a program's STDERR, but discard its STDOUT: |
1449 |
|
1450 |
=end original |
1451 |
|
1452 |
あるプログラムの STDERR は捕捉したいが、STDOUT は捨てたい場合: |
1453 |
|
1454 |
use IPC::Open3; |
1455 |
use File::Spec; |
1456 |
use Symbol qw(gensym); |
1457 |
open(NULL, ">", File::Spec->devnull); |
1458 |
my $pid = open3(gensym, ">&NULL", \*PH, "cmd"); |
1459 |
while( <PH> ) { } |
1460 |
waitpid($pid, 0); |
1461 |
|
1462 |
=begin original |
1463 |
|
1464 |
To capture a program's STDERR, and let its STDOUT go to our own STDERR: |
1465 |
|
1466 |
=end original |
1467 |
|
1468 |
プログラムの STDERR を捕捉してその STDOUT を自身の STDERR に送るには: |
1469 |
|
1470 |
use IPC::Open3; |
1471 |
use Symbol qw(gensym); |
1472 |
my $pid = open3(gensym, ">&STDERR", \*PH, "cmd"); |
1473 |
while( <PH> ) { } |
1474 |
waitpid($pid, 0); |
1475 |
|
1476 |
=begin original |
1477 |
|
1478 |
To read both a command's STDOUT and its STDERR separately, you can |
1479 |
redirect them to temp files, let the command run, then read the temp |
1480 |
files: |
1481 |
|
1482 |
=end original |
1483 |
|
1484 |
あるコマンドの STDOUT と STDERR を別々に読み込みたい場合、それをテンポラリ |
1485 |
ファイルにリダイレクトして、コマンドを実行して、テンポラリファイルから |
1486 |
読み込みます: |
1487 |
|
1488 |
use IPC::Open3; |
1489 |
use Symbol qw(gensym); |
1490 |
use IO::File; |
1491 |
local *CATCHOUT = IO::File->new_tmpfile; |
1492 |
local *CATCHERR = IO::File->new_tmpfile; |
1493 |
my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd"); |
1494 |
waitpid($pid, 0); |
1495 |
seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR; |
1496 |
while( <CATCHOUT> ) {} |
1497 |
while( <CATCHERR> ) {} |
1498 |
|
1499 |
=begin original |
1500 |
|
1501 |
But there's no real need for B<both> to be tempfiles... the following |
1502 |
should work just as well, without deadlocking: |
1503 |
|
1504 |
=end original |
1505 |
|
1506 |
しかし、実際には B<両方を> テンポラリファイルにするする必要はありません…。 |
1507 |
以下のものもデッドロックなしにうまく動きます: |
1508 |
|
1509 |
use IPC::Open3; |
1510 |
use Symbol qw(gensym); |
1511 |
use IO::File; |
1512 |
local *CATCHERR = IO::File->new_tmpfile; |
1513 |
my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd"); |
1514 |
while( <CATCHOUT> ) {} |
1515 |
waitpid($pid, 0); |
1516 |
seek CATCHERR, 0, 0; |
1517 |
while( <CATCHERR> ) {} |
1518 |
|
1519 |
=begin original |
1520 |
|
1521 |
And it'll be faster, too, since we can begin processing the program's |
1522 |
stdout immediately, rather than waiting for the program to finish. |
1523 |
|
1524 |
=end original |
1525 |
|
1526 |
そして、これはプログラムの終了を待つのではなく、プログラムの標準出力を直ちに |
1527 |
処理するので、より速いはずです。 |
1528 |
|
1529 |
=begin original |
1530 |
|
1531 |
With any of these, you can change file descriptors before the call: |
1532 |
|
1533 |
=end original |
1534 |
|
1535 |
これらのどれでも、呼び出しの前にファイル記述子を変更できます: |
1536 |
|
1537 |
open(STDOUT, ">logfile"); |
1538 |
system("ls"); |
1539 |
|
1540 |
=begin original |
1541 |
|
1542 |
or you can use Bourne shell file-descriptor redirection: |
1543 |
|
1544 |
=end original |
1545 |
|
1546 |
Bourne シェルのファイル記述子リダイレクションを使うこともできます: |
1547 |
|
1548 |
$output = `$cmd 2>some_file`; |
1549 |
open (PIPE, "cmd 2>some_file |"); |
1550 |
|
1551 |
=begin original |
1552 |
|
1553 |
You can also use file-descriptor redirection to make STDERR a |
1554 |
duplicate of STDOUT: |
1555 |
|
1556 |
=end original |
1557 |
|
1558 |
同様に、STDERR を STDOUT の複製にするためにファイル記述子 |
1559 |
リダイレクションを使うこともできます。 |
1560 |
|
1561 |
$output = `$cmd 2>&1`; |
1562 |
open (PIPE, "cmd 2>&1 |"); |
1563 |
|
1564 |
=begin original |
1565 |
|
1566 |
Note that you I<cannot> simply open STDERR to be a dup of STDOUT |
1567 |
in your Perl program and avoid calling the shell to do the redirection. |
1568 |
This doesn't work: |
1569 |
|
1570 |
=end original |
1571 |
|
1572 |
STDOUT の複製のために、Perl プログラムの中で単純に STDERR を |
1573 |
オープンすることは B<できない> ということと、リダイレクトのための |
1574 |
シェルの呼び出しを避けることはできないということに注意してください。 |
1575 |
以下の例はうまくいきません: |
1576 |
|
1577 |
open(STDERR, ">&STDOUT"); |
1578 |
$alloutput = `cmd args`; # stderr still escapes |
1579 |
|
1580 |
=begin original |
1581 |
|
1582 |
This fails because the C<open()> makes STDERR go to where STDOUT was |
1583 |
going at the time of the C<open()>. The backticks then make STDOUT go to |
1584 |
a string, but don't change STDERR (which still goes to the old |
1585 |
STDOUT). |
1586 |
|
1587 |
=end original |
1588 |
|
1589 |
これは C<open()> が、STDERR を(C<open()> が呼び出された時点で)STDOUT が |
1590 |
使っていた場所に対応するようにするので失敗します。 |
1591 |
その後で逆クォートは STDOUT(に出力された内容)を文字列にしますが、 |
1592 |
STDERR を変更することはしません(これは以前の STDOUT が指していたところです)。 |
1593 |
|
1594 |
=begin original |
1595 |
|
1596 |
Note that you I<must> use Bourne shell (C<sh(1)>) redirection syntax in |
1597 |
backticks, not C<csh(1)>! Details on why Perl's C<system()> and backtick |
1598 |
and pipe opens all use the Bourne shell are in the |
1599 |
F<versus/csh.whynot> article in the "Far More Than You Ever Wanted To |
1600 |
Know" collection in L<http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz> . To |
1601 |
capture a command's STDERR and STDOUT together: |
1602 |
|
1603 |
=end original |
1604 |
|
1605 |
backticlsの中では、C<csh(1)> のリダイレクト構文ではなく、Bourne shell |
1606 |
(C<sh(1)>) のリダイレクト構文を B<使わなければならない> ということに |
1607 |
注意してください! |
1608 |
なぜ Perl の C<system()>、逆クォート、パイプオープンの全てで Bourne シェルの |
1609 |
ものを使うかは L<http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz> にある |
1610 |
"Far More Than You Ever Wanted To Know" の F<versus/csh.whynot> という記事で |
1611 |
説明されています。 |
1612 |
あるコマンドの標準出力と標準エラー出力を両方とも捉えるには: |
1613 |
|
1614 |
$output = `cmd 2>&1`; # either with backticks |
1615 |
$pid = open(PH, "cmd 2>&1 |"); # or with an open pipe |
1616 |
while (<PH>) { } # plus a read |
1617 |
|
1618 |
=begin original |
1619 |
|
1620 |
To capture a command's STDOUT but discard its STDERR: |
1621 |
|
1622 |
=end original |
1623 |
|
1624 |
あるコマンドの標準出力を捉え、標準エラー出力を捨てるには: |
1625 |
|
1626 |
$output = `cmd 2>/dev/null`; # either with backticks |
1627 |
$pid = open(PH, "cmd 2>/dev/null |"); # or with an open pipe |
1628 |
while (<PH>) { } # plus a read |
1629 |
|
1630 |
=begin original |
1631 |
|
1632 |
To capture a command's STDERR but discard its STDOUT: |
1633 |
|
1634 |
=end original |
1635 |
|
1636 |
あるコマンドの標準エラー出力を捉え、標準出力を捨てるには: |
1637 |
|
1638 |
$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 |
1640 |
while (<PH>) { } # plus a read |
1641 |
|
1642 |
=begin original |
1643 |
|
1644 |
To exchange a command's STDOUT and STDERR in order to capture the STDERR |
1645 |
but leave its STDOUT to come out our old STDERR: |
1646 |
|
1647 |
=end original |
1648 |
|
1649 |
あるコマンドの標準エラー出力を捉えるために |
1650 |
標準出力と標準エラー出力を入れ替えるが、標準出力に |
1651 |
古い標準エラー出力に出るようにするには: |
1652 |
|
1653 |
$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 |
1655 |
while (<PH>) { } # plus a read |
1656 |
|
1657 |
=begin original |
1658 |
|
1659 |
To read both a command's STDOUT and its STDERR separately, it's easiest |
1660 |
to redirect them separately to files, and then read from those files |
1661 |
when the program is done: |
1662 |
|
1663 |
=end original |
1664 |
|
1665 |
標準出力と標準エラー出力の両方を分けて読み出すには、 |
1666 |
別々のファイルにリダイレクトしてしまって、 |
1667 |
その後でそのファイルをプログラムから読むというのが最も簡単な方法です: |
1668 |
|
1669 |
system("program args 1>program.stdout 2>program.stderr"); |
1670 |
|
1671 |
=begin original |
1672 |
|
1673 |
Ordering is important in all these examples. That's because the shell |
1674 |
processes file descriptor redirections in strictly left to right order. |
1675 |
|
1676 |
=end original |
1677 |
|
1678 |
これらの例では順序が重要です。 |
1679 |
なぜなら、シェルがリダイレクトのためのファイル記述子を処理する順序は |
1680 |
正確に左から右へという順になっているからです。 |
1681 |
|
1682 |
system("prog args 1>tmpfile 2>&1"); |
1683 |
system("prog args 2>&1 1>tmpfile"); |
1684 |
|
1685 |
=begin original |
1686 |
|
1687 |
The first command sends both standard out and standard error to the |
1688 |
temporary file. The second command sends only the old standard output |
1689 |
there, and the old standard error shows up on the old standard out. |
1690 |
|
1691 |
=end original |
1692 |
|
1693 |
最初のコマンドは標準出力と標準エラー出力の両方を一時ファイルに送ります。 |
1694 |
二番目のコマンドは古い標準出力だけをファイルへと送り、古い標準エラー出力は |
1695 |
古い標準出力へと送り出します。 |
1696 |
|
1697 |
=head2 Why doesn't open() return an error when a pipe open fails? |
1698 |
|
1699 |
(なぜ open() は パイプのオープンに失敗したときにエラーを返さないのでしょうか?) |
1700 |
|
1701 |
=begin original |
1702 |
|
1703 |
If the second argument to a piped C<open()> contains shell |
1704 |
metacharacters, perl C<fork()>s, then C<exec()>s a shell to decode the |
1705 |
metacharacters and eventually run the desired program. If the program |
1706 |
couldn't be run, it's the shell that gets the message, not Perl. All |
1707 |
your Perl program can find out is whether the shell itself could be |
1708 |
successfully started. You can still capture the shell's STDERR and |
1709 |
check it for error messages. See L<"How can I capture STDERR from an |
1710 |
external command?"> elsewhere in this document, or use the |
1711 |
L<IPC::Open3> module. |
1712 |
|
1713 |
=end original |
1714 |
|
1715 |
パイプされた C<open()> の第二引数にシェルのメタ文字が含まれている場合、 |
1716 |
perl は C<fork()> し、メタ文字をデコードするためにシェルを C<exec()> し、 |
1717 |
最終的に必要なプログラムを実行します。 |
1718 |
このプログラムが実行できない場合、メッセージを得るのはシェルであって、 |
1719 |
Perl ではありません。 |
1720 |
あなたの Perl プログラムがわかることはシェル自身が正しく開始されたかどうか |
1721 |
だけです。 |
1722 |
エラーメッセージのためにシェルの STDERR を捕捉してチェックするという方法は |
1723 |
あります。 |
1724 |
この文書の別の場所にある L<"How can I capture STDERR from an external |
1725 |
command?"> を参照するか、L<IPC::Open3> モジュールを使ってください。 |
1726 |
|
1727 |
=begin original |
1728 |
|
1729 |
If there are no shell metacharacters in the argument of C<open()>, Perl |
1730 |
runs the command directly, without using the shell, and can correctly |
1731 |
report whether the command started. |
1732 |
|
1733 |
=end original |
1734 |
|
1735 |
C<open()> の引数にシェルのメタ文字がないなら、Perl はコマンドをシェルを使わずに |
1736 |
直接実行し、コマンドが開始したかどうかを正しく報告できます。 |
1737 |
|
1738 |
=head2 What's wrong with using backticks in a void context? |
1739 |
|
1740 |
(無効コンテキストで逆クォートを使うことのなにが悪いのでしょうか?) |
1741 |
|
1742 |
=begin original |
1743 |
|
1744 |
Strictly speaking, nothing. Stylistically speaking, it's not a good |
1745 |
way to write maintainable code. Perl has several operators for |
1746 |
running external commands. Backticks are one; they collect the output |
1747 |
from the command for use in your program. The C<system> function is |
1748 |
another; it doesn't do this. |
1749 |
|
1750 |
=end original |
1751 |
|
1752 |
厳密に言えばありません。 |
1753 |
形式的に言えば、保守しやすいコードを書くための良い方法ではありません。 |
1754 |
Perl は外部コマンドを実行するためのいくつかの演算子があります。 |
1755 |
逆クォートはその一つです; これはコマンドからの出力を、自身のプログラムで |
1756 |
使うために収集します。 |
1757 |
C<system> 関数はもう一つのものです; これはコマンドからの出力を収集しません。 |
1758 |
|
1759 |
=begin original |
1760 |
|
1761 |
Writing backticks in your program sends a clear message to the readers |
1762 |
of your code that you wanted to collect the output of the command. |
1763 |
Why send a clear message that isn't true? |
1764 |
|
1765 |
=end original |
1766 |
|
1767 |
プログラム中に逆クォートを書くことで、そのコマンドの出力を使いたいという |
1768 |
はっきりしたメッセージをあなたのコードの読者に送ります。 |
1769 |
なぜ正しくないはっきりしたメッセージを送るのですか? |
1770 |
|
1771 |
=begin original |
1772 |
|
1773 |
Consider this line: |
1774 |
|
1775 |
=end original |
1776 |
|
1777 |
以下のような行を考えてみましょう: |
1778 |
|
1779 |
`cat /etc/termcap`; |
1780 |
|
1781 |
=begin original |
1782 |
|
1783 |
You forgot to check C<$?> to see whether the program even ran |
1784 |
correctly. Even if you wrote |
1785 |
|
1786 |
=end original |
1787 |
|
1788 |
プログラムが正しく実行されたかどうかを確認するために |
1789 |
C<$?> を見るのを忘れています。 |
1790 |
以下のように書いたとしても |
1791 |
|
1792 |
print `cat /etc/termcap`; |
1793 |
|
1794 |
=begin original |
1795 |
|
1796 |
this code could and probably should be written as |
1797 |
|
1798 |
=end original |
1799 |
|
1800 |
このコードは、以下のように書くべきでしょう |
1801 |
|
1802 |
system("cat /etc/termcap") == 0 |
1803 |
or die "cat program failed!"; |
1804 |
|
1805 |
=begin original |
1806 |
|
1807 |
which will echo the cat command's output as it is generated, instead |
1808 |
of waiting until the program has completed to print it out. It also |
1809 |
checks the return value. |
1810 |
|
1811 |
=end original |
1812 |
|
1813 |
これは、プログラムが出力を終えるのを待つのではなく、cat コマンドの出力が |
1814 |
生成される毎にエコーします。 |
1815 |
これはまたその戻り値のチェックも行います。 |
1816 |
|
1817 |
=begin original |
1818 |
|
1819 |
C<system> also provides direct control over whether shell wildcard |
1820 |
processing may take place, whereas backticks do not. |
1821 |
|
1822 |
=end original |
1823 |
|
1824 |
system() はまた、シェルのワイルドカード処理を行えるかどうかを直接 |
1825 |
制御しますが、逆クォートはそういった制御は行いません。 |
1826 |
|
1827 |
=head2 How can I call backticks without shell processing? |
1828 |
|
1829 |
(シェルの処理をせずに逆クォートを呼び出すには?) |
1830 |
|
1831 |
=begin original |
1832 |
|
1833 |
This is a bit tricky. You can't simply write the command |
1834 |
like this: |
1835 |
|
1836 |
=end original |
1837 |
|
1838 |
ちょっとトリッキーになります。 |
1839 |
単純に以下のようにはコマンドを書けません: |
1840 |
|
1841 |
@ok = `grep @opts '$search_string' @filenames`; |
1842 |
|
1843 |
=begin original |
1844 |
|
1845 |
As of Perl 5.8.0, you can use C<open()> with multiple arguments. |
1846 |
Just like the list forms of C<system()> and C<exec()>, no shell |
1847 |
escapes happen. |
1848 |
|
1849 |
=end original |
1850 |
|
1851 |
Perl 5.8.0 から、複数の引数の C<open()> を使えます。 |
1852 |
リスト形式の C<system()> や C<exec()> と同様に、シェルエスケープは |
1853 |
起きません。 |
1854 |
|
1855 |
open( GREP, "-|", 'grep', @opts, $search_string, @filenames ); |
1856 |
chomp(@ok = <GREP>); |
1857 |
close GREP; |
1858 |
|
1859 |
=begin original |
1860 |
|
1861 |
You can also: |
1862 |
|
1863 |
=end original |
1864 |
|
1865 |
以下のようにもできます: |
1866 |
|
1867 |
my @ok = (); |
1868 |
if (open(GREP, "-|")) { |
1869 |
while (<GREP>) { |
1870 |
chomp; |
1871 |
push(@ok, $_); |
1872 |
} |
1873 |
close GREP; |
1874 |
} else { |
1875 |
exec 'grep', @opts, $search_string, @filenames; |
1876 |
} |
1877 |
|
1878 |
=begin original |
1879 |
|
1880 |
Just as with C<system()>, no shell escapes happen when you C<exec()> a |
1881 |
list. Further examples of this can be found in L<perlipc/"Safe Pipe |
1882 |
Opens">. |
1883 |
|
1884 |
=end original |
1885 |
|
1886 |
C<system()> を使ったときと同じく、シェルエスケープは C<exec()> のリストに |
1887 |
対して行われません。 |
1888 |
さらなる例が L<perlipc/"Safe Pipe Opens"> にあります。 |
1889 |
|
1890 |
=begin original |
1891 |
|
1892 |
Note that if you're using Windows, no solution to this vexing issue is |
1893 |
even possible. Even though Perl emulates C<fork()>, you'll still be |
1894 |
stuck, because Windows does not have an argc/argv-style API. |
1895 |
|
1896 |
=end original |
1897 |
|
1898 |
もしあなたが Windows を使っているのなら、この悩ましい問題を |
1899 |
解決する方法は全くありません。 |
1900 |
たとえ Perl が C<fork()> をエミュレートしたとしても、まだうまくいきません; |
1901 |
なぜならマイクロソフトは argc/argv 形式の API を提供していないからです。 |
1902 |
|
1903 |
=head2 Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)? |
1904 |
|
1905 |
(なぜ EOF(UNIX での^D、MS-DOS での^Z)を受け取った後で STDIN から読み込むことができないの?) |
1906 |
|
1907 |
=begin original |
1908 |
|
1909 |
This happens only if your perl is compiled to use stdio instead of |
1910 |
perlio, which is the default. Some (maybe all?) stdios set error and |
1911 |
eof flags that you may need to clear. The L<POSIX> module defines |
1912 |
C<clearerr()> that you can use. That is the technically correct way to |
1913 |
do it. Here are some less reliable workarounds: |
1914 |
|
1915 |
=end original |
1916 |
|
1917 |
これは、デフォルトである perlio ではなく、stdio を使ってコンパイルされた |
1918 |
perl でのみ起こります。 |
1919 |
一部の(おそらく全ての?) stdio ではエラーフラグと eof フラグがセットされ、 |
1920 |
それをクリアする必要があるからです。 |
1921 |
L<POSIX> モジュールはこのために使える C<clearerr()> を定義しています。 |
1922 |
これは、フラグをクリアするための技術的に正しい方法です。 |
1923 |
以下の方法はこれよりは信頼性にかけるやり方です: |
1924 |
|
1925 |
=over 4 |
1926 |
|
1927 |
=item 1 |
1928 |
|
1929 |
=begin original |
1930 |
|
1931 |
Try keeping around the seekpointer and go there, like this: |
1932 |
|
1933 |
=end original |
1934 |
|
1935 |
以下の例のように、シークポインタを保存しておいてそこへ移動します: |
1936 |
|
1937 |
my $where = tell($log_fh); |
1938 |
seek($log_fh, $where, 0); |
1939 |
|
1940 |
=item 2 |
1941 |
|
1942 |
=begin original |
1943 |
|
1944 |
If that doesn't work, try seeking to a different part of the file and |
1945 |
then back. |
1946 |
|
1947 |
=end original |
1948 |
|
1949 |
上のやりかたがだめなら、一度ファイルの別の部分にシークして、それから |
1950 |
元の場所にシークするようにします。 |
1951 |
|
1952 |
=item 3 |
1953 |
|
1954 |
=begin original |
1955 |
|
1956 |
If that doesn't work, try seeking to a different part of |
1957 |
the file, reading something, and then seeking back. |
1958 |
|
1959 |
=end original |
1960 |
|
1961 |
これでもだめなら、ファイルの別の部分にシークして何かを読み出し、それから |
1962 |
元の場所にシークするようにします。 |
1963 |
|
1964 |
=item 4 |
1965 |
|
1966 |
=begin original |
1967 |
|
1968 |
If that doesn't work, give up on your stdio package and use sysread. |
1969 |
|
1970 |
=end original |
1971 |
|
1972 |
これでだめなら stdio パッケージで行うことはあきらめて、sysread を使います。 |
1973 |
|
1974 |
=back |
1975 |
|
1976 |
=head2 How can I convert my shell script to perl? |
1977 |
|
1978 |
(私のシェルスクリプトを perl に変換するには?) |
1979 |
|
1980 |
=begin original |
1981 |
|
1982 |
Learn Perl and rewrite it. Seriously, there's no simple converter. |
1983 |
Things that are awkward to do in the shell are easy to do in Perl, and |
1984 |
this very awkwardness is what would make a shell->perl converter |
1985 |
nigh-on impossible to write. By rewriting it, you'll think about what |
1986 |
you're really trying to do, and hopefully will escape the shell's |
1987 |
pipeline datastream paradigm, which while convenient for some matters, |
1988 |
causes many inefficiencies. |
1989 |
|
1990 |
=end original |
1991 |
|
1992 |
Perl を学び、自分で書き直しましょう。 |
1993 |
まじめな話、単純なコンバータというものはありません。 |
1994 |
シェルで行うには不格好になるようなことも Perl では簡単に行うことができ、 |
1995 |
そして、このとても不格好なことがシェル→ perl コンバーターを作製することを |
1996 |
ほとんど不可能なことにしているのです。 |
1997 |
自分で書き換えをすることで、あなたは自分が本当に試すべきことについて |
1998 |
考えるようになり、シェルの(便利なときもあるものの多くの非効率を持ち込む) |
1999 |
パイプラインデータストリームパラダイムから逃れることができるでしょう。 |
2000 |
|
2001 |
=head2 Can I use perl to run a telnet or ftp session? |
2002 |
|
2003 |
(telnet や ftp のセッションを実行するために perl を使うことができますか?) |
2004 |
|
2005 |
=begin original |
2006 |
|
2007 |
Try the L<Net::FTP>, L<TCP::Client>, and L<Net::Telnet> modules |
2008 |
(available from CPAN). |
2009 |
L<http://www.cpan.org/scripts/netstuff/telnet.emul.shar> will also help |
2010 |
for emulating the telnet protocol, but L<Net::Telnet> is quite |
2011 |
probably easier to use. |
2012 |
|
2013 |
=end original |
2014 |
|
2015 |
L<Net::FTP>, L<TCP::Client>, L<Net::Telnet> といったモジュール(CPAN で |
2016 |
入手可能です)を試してみてください。 |
2017 |
L<http://www.cpan.org/scripts/netstuff/telnet.emul.shar> も |
2018 |
telnet プロトコルをエミュレートする助けになるでしょうが、L<Net::Telnet> は |
2019 |
使うのがとても簡単です。 |
2020 |
|
2021 |
=begin original |
2022 |
|
2023 |
If all you want to do is pretend to be telnet but don't need |
2024 |
the initial telnet handshaking, then the standard dual-process |
2025 |
approach will suffice: |
2026 |
|
2027 |
=end original |
2028 |
|
2029 |
あなたのやりたいことが telnet のふりをすることであっても初期化時の |
2030 |
telnet のハンドシェイクを必要としないのであれば、 |
2031 |
標準的な dual-process アプローチで十分でしょう。 |
2032 |
|
2033 |
use IO::Socket; # new in 5.004 |
2034 |
my $handle = IO::Socket::INET->new('www.perl.com:80') |
2035 |
or die "can't connect to port 80 on www.perl.com $!"; |
2036 |
$handle->autoflush(1); |
2037 |
if (fork()) { # XXX: undef means failure |
2038 |
select($handle); |
2039 |
print while <STDIN>; # everything from stdin to socket |
2040 |
} else { |
2041 |
print while <$handle>; # everything from socket to stdout |
2042 |
} |
2043 |
close $handle; |
2044 |
exit; |
2045 |
|
2046 |
=head2 How can I write expect in Perl? |
2047 |
|
2048 |
( Perl で expect を書くには?) |
2049 |
|
2050 |
=begin original |
2051 |
|
2052 |
Once upon a time, there was a library called F<chat2.pl> (part of the |
2053 |
standard perl distribution), which never really got finished. If you |
2054 |
find it somewhere, I<don't use it>. These days, your best bet is to |
2055 |
look at the L<Expect> module available from CPAN, which also requires two |
2056 |
other modules from CPAN, L<IO::Pty> and L<IO::Stty>. |
2057 |
|
2058 |
=end original |
2059 |
|
2060 |
昔々、F<chat2.pl> と呼ばれたライブラリがありました(これは標準の perl |
2061 |
配布キットに含まれます); 実際には完成していませんでした。 |
2062 |
もしこれをどこかで見つけても I<使ってはいけません>。 |
2063 |
今日では、CPAN にある L<IO::Pty> や L<IO::Stty> といった |
2064 |
ライブラリを使う L<Expect> を探すのが最善でしょう。 |
2065 |
|
2066 |
=head2 Is there a way to hide perl's command line from programs such as "ps"? |
2067 |
|
2068 |
(“ps”のようなプログラムから、perl のコマンドラインを隠す方法はありますか?) |
2069 |
|
2070 |
=begin original |
2071 |
|
2072 |
First of all note that if you're doing this for security reasons (to |
2073 |
avoid people seeing passwords, for example) then you should rewrite |
2074 |
your program so that critical information is never given as an |
2075 |
argument. Hiding the arguments won't make your program completely |
2076 |
secure. |
2077 |
|
2078 |
=end original |
2079 |
|
2080 |
まず初めに、あなたが(たとえば他人がパスワードを除くのを避けるためなどの) |
2081 |
セキュリティ上の理由でそれを行おうとしてるのであれば、 |
2082 |
重要な情報が引数として与えられることがないようにプログラムを |
2083 |
書き直すべきだということに注意してください。 |
2084 |
引数を隠すことは、あなたのプログラムを完全に安全なものにすることは |
2085 |
ありません。 |
2086 |
|
2087 |
=begin original |
2088 |
|
2089 |
To actually alter the visible command line, you can assign to the |
2090 |
variable $0 as documented in L<perlvar>. This won't work on all |
2091 |
operating systems, though. Daemon programs like sendmail place their |
2092 |
state there, as in: |
2093 |
|
2094 |
=end original |
2095 |
|
2096 |
外部から見えるコマンドラインを実際に書き換えるために、L<perlvar> で |
2097 |
説明されているように $0 という変数に代入することができます。 |
2098 |
ただし、これはすべてのオペレーティングシステムで実行できるというものでは |
2099 |
ありません。 |
2100 |
sendmail のようなデーモンプログラムは以下の例のように状態を設定します: |
2101 |
|
2102 |
$0 = "orcus [accepting connections]"; |
2103 |
|
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? |
2105 |
|
2106 |
(perl スクリプトの中で、ディレクトリを変更したり環境変数を変更しました。なぜ、スクリプトを終了したときこれらの変更は無効になってしまうの? 変更が反映されるようにするには?) |
2107 |
|
2108 |
=over 4 |
2109 |
|
2110 |
=item Unix |
2111 |
|
2112 |
=begin original |
2113 |
|
2114 |
In the strictest sense, it can't be done--the script executes as a |
2115 |
different process from the shell it was started from. Changes to a |
2116 |
process are not reflected in its parent--only in any children |
2117 |
created after the change. There is shell magic that may allow you to |
2118 |
fake it by C<eval()>ing the script's output in your shell; check out the |
2119 |
comp.unix.questions FAQ for details. |
2120 |
|
2121 |
=end original |
2122 |
|
2123 |
もっとも厳密な意味で言うと、それはできません--スクリプトはそれを |
2124 |
起動したシェルとは異なるプロセスで実行されるのです。 |
2125 |
あるプロセスに対する変更はその親に反映されることはありません |
2126 |
--変更した後で生成された子プロセスに対してのみ反映されます。 |
2127 |
あなたの使っているシェルにおいてスクリプトの出力を C<eval()> することによって、 |
2128 |
お望みのことをしたように見せかけるシェルマジック (shell magic)があります; |
2129 |
詳しくは comp.unix.questions FAQを調べてください。 |
2130 |
|
2131 |
=back |
2132 |
|
2133 |
=head2 How do I close a process's filehandle without waiting for it to complete? |
2134 |
|
2135 |
(プロセスの完了を待つことなしにそのファイルハンドルをクローズするには?) |
2136 |
|
2137 |
=begin original |
2138 |
|
2139 |
Assuming your system supports such things, just send an appropriate signal |
2140 |
to the process (see L<perlfunc/"kill">). It's common to first send a TERM |
2141 |
signal, wait a little bit, and then send a KILL signal to finish it off. |
2142 |
|
2143 |
=end original |
2144 |
|
2145 |
あなたの使っているシステムがそういった機能をサポートしていると仮定すると、 |
2146 |
そのプロセスに対して適切なシグナルを送るだけです |
2147 |
(L<perlfunc/"kill"> を参照してください)。 |
2148 |
最初にTERMシグナルを送り、ちょっとだけ待って、 |
2149 |
終了させるために KILL シグナルを送るというのが一般的なものです。 |
2150 |
|
2151 |
=head2 How do I fork a daemon process? |
2152 |
|
2153 |
(デーモンプロセスを fork() するには?) |
2154 |
|
2155 |
=begin original |
2156 |
|
2157 |
If by daemon process you mean one that's detached (disassociated from |
2158 |
its tty), then the following process is reported to work on most |
2159 |
Unixish systems. Non-Unix users should check their Your_OS::Process |
2160 |
module for other solutions. |
2161 |
|
2162 |
=end original |
2163 |
|
2164 |
あなたのいうデーモンプロセスが detach されている(tty と |
2165 |
結び付けられていない)ものであれば、以下の手順がほとんどの UNIX 的な |
2166 |
システムで動作するということが報告されています。 |
2167 |
非 UNIX ユーザーは Your_OS::Process モジュールで他の解決策を |
2168 |
あたるべきでしょう。 |
2169 |
|
2170 |
=over 4 |
2171 |
|
2172 |
=item * |
2173 |
|
2174 |
=begin original |
2175 |
|
2176 |
Open /dev/tty and use the TIOCNOTTY ioctl on it. See L<tty(1)> |
2177 |
for details. Or better yet, you can just use the C<POSIX::setsid()> |
2178 |
function, so you don't have to worry about process groups. |
2179 |
|
2180 |
=end original |
2181 |
|
2182 |
/dev/tty をオープンし、それに TIOCNOTTY ioctl を使います。 |
2183 |
詳しくは L<tty(1)> を参照してください。 |
2184 |
そのほかもっと良いのは、C<POSIX::setsid()> 関数を使うことです; これによって |
2185 |
プロセスグループに関して思い煩う必要がなくなります。 |
2186 |
|
2187 |
=item * |
2188 |
|
2189 |
=begin original |
2190 |
|
2191 |
Change directory to / |
2192 |
|
2193 |
=end original |
2194 |
|
2195 |
/ へディレクトリを変更します。 |
2196 |
|
2197 |
=item * |
2198 |
|
2199 |
=begin original |
2200 |
|
2201 |
Reopen STDIN, STDOUT, and STDERR so they're not connected to the old |
2202 |
tty. |
2203 |
|
2204 |
=end original |
2205 |
|
2206 |
STDIN、STDOUT、STDERR を再オープンします; これでこれらのハンドルは以前の |
2207 |
tty とは結び付けらていない状態になります。 |
2208 |
|
2209 |
=item * |
2210 |
|
2211 |
=begin original |
2212 |
|
2213 |
Background yourself like this: |
2214 |
|
2215 |
=end original |
2216 |
|
2217 |
以下のようにしてバックグラウンドにします: |
2218 |
|
2219 |
fork && exit; |
2220 |
|
2221 |
=back |
2222 |
|
2223 |
=begin original |
2224 |
|
2225 |
The L<Proc::Daemon> module, available from CPAN, provides a function to |
2226 |
perform these actions for you. |
2227 |
|
2228 |
=end original |
2229 |
|
2230 |
CPAN で入手できる L<Proc::Daemon> モジュールはこれらの操作を |
2231 |
行ってくれる関数を提供しています。 |
2232 |
|
2233 |
=head2 How do I find out if I'm running interactively or not? |
2234 |
|
2235 |
(自分が対話的に実行されているかどうかを知るには?) |
2236 |
|
2237 |
=begin original |
2238 |
|
2239 |
(contributed by brian d foy) |
2240 |
|
2241 |
=end original |
2242 |
|
2243 |
(brian d foy によって寄贈されました) |
2244 |
|
2245 |
=begin original |
2246 |
|
2247 |
This is a difficult question to answer, and the best answer is |
2248 |
only a guess. |
2249 |
|
2250 |
=end original |
2251 |
|
2252 |
これは答えるのが難しい質問で、最良の答えは単なる推測です。 |
2253 |
|
2254 |
=begin original |
2255 |
|
2256 |
What do you really want to know? If you merely want to know if one of |
2257 |
your filehandles is connected to a terminal, you can try the C<-t> |
2258 |
file test: |
2259 |
|
2260 |
=end original |
2261 |
|
2262 |
本当に知りたいことは何でしょう? |
2263 |
単にファイルハンドルの一つが端末と接続しているかどうかを知りたいなら、 |
2264 |
C<-t> ファイルテストを試すことができます: |
2265 |
|
2266 |
if( -t STDOUT ) { |
2267 |
print "I'm connected to a terminal!\n"; |
2268 |
} |
2269 |
|
2270 |
=begin original |
2271 |
|
2272 |
However, you might be out of luck if you expect that means there is a |
2273 |
real person on the other side. With the L<Expect> module, another |
2274 |
program can pretend to be a person. The program might even come close |
2275 |
to passing the Turing test. |
2276 |
|
2277 |
=end original |
2278 |
|
2279 |
しかし、その先に実際の人間がいることを想定するなら、うまく |
2280 |
いかないかもしれません。 |
2281 |
L<Expect> では、他のプログラムは人間である振りをします。 |
2282 |
プログラムはチューリングテストに通過しそうになるかもしれません。 |
2283 |
|
2284 |
=begin original |
2285 |
|
2286 |
The L<IO::Interactive> module does the best it can to give you an |
2287 |
answer. Its C<is_interactive> function returns an output filehandle; |
2288 |
that filehandle points to standard output if the module thinks the |
2289 |
session is interactive. Otherwise, the filehandle is a null handle |
2290 |
that simply discards the output: |
2291 |
|
2292 |
=end original |
2293 |
|
2294 |
L<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 |
|
2306 |
This still doesn't guarantee that a real person is answering your |
2307 |
prompts or reading your output. |
2308 |
|
2309 |
=end original |
2310 |
|
2311 |
これままだ実際の人間がプロンプトに答えたり出力を読んだりするのが |
2312 |
実際の人間であることを保証しません。 |
2313 |
|
2314 |
=begin original |
2315 |
|
2316 |
If you want to know how to handle automated testing for your |
2317 |
distribution, you can check the environment. The CPAN |
2318 |
Testers, 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 |
|
2330 |
=head2 How do I timeout a slow event? |
2331 |
|
2332 |
(遅いイベントをタイムアウトするには?) |
2333 |
|
2334 |
=begin original |
2335 |
|
2336 |
Use the C<alarm()> function, probably in conjunction with a signal |
2337 |
handler, as documented in L<perlipc/"Signals"> and the section on |
2338 |
"Signals" in the Camel. You may instead use the more flexible |
2339 |
L<Sys::AlarmCall> module available from CPAN. |
2340 |
|
2341 |
=end original |
2342 |
|
2343 |
L<perlipc/"Signals"> やらくだ本の "Signal" の章で説明されているように、 |
2344 |
C<alarm()> 関数と、おそらくはシグナルハンドラーを組み合わせて使います。 |
2345 |
この代わりに、CPAN にあるより柔軟性のある |
2346 |
L<Sys::AlarmCall> モジュールを使うこともできます。 |
2347 |
|
2348 |
=begin original |
2349 |
|
2350 |
The C<alarm()> function is not implemented on all versions of Windows. |
2351 |
Check the documentation for your specific version of Perl. |
2352 |
|
2353 |
=end original |
2354 |
|
2355 |
C<alarm()> 関数は Windows の全てのバージョンで実装されているわけではありません。 |
2356 |
あなたの特定のバージョンの Perl のドキュメントをチェックしてください。 |
2357 |
|
2358 |
=head2 How do I set CPU limits? |
2359 |
X<BSD::Resource> X<limit> X<CPU> |
2360 |
|
2361 |
(CPU のリミットを設定するには?) |
2362 |
|
2363 |
=begin original |
2364 |
|
2365 |
(contributed by Xho) |
2366 |
|
2367 |
=end original |
2368 |
|
2369 |
(Xho によって寄贈されました) |
2370 |
|
2371 |
=begin original |
2372 |
|
2373 |
Use the L<BSD::Resource> module from CPAN. As an example: |
2374 |
|
2375 |
=end original |
2376 |
|
2377 |
CPAN にある L<BSD::Resource> モジュールを使います。 |
2378 |
例として: |
2379 |
|
2380 |
use BSD::Resource; |
2381 |
setrlimit(RLIMIT_CPU,10,20) or die $!; |
2382 |
|
2383 |
=begin original |
2384 |
|
2385 |
This sets the soft and hard limits to 10 and 20 seconds, respectively. |
2386 |
After 10 seconds of time spent running on the CPU (not "wall" time), |
2387 |
the process will be sent a signal (XCPU on some systems) which, if not |
2388 |
trapped, will cause the process to terminate. If that signal is |
2389 |
trapped, then after 10 more seconds (20 seconds in total) the process |
2390 |
will be killed with a non-trappable signal. |
2391 |
|
2392 |
=end original |
2393 |
|
2394 |
これはソフト制限とハード制限をそれぞれ 10 秒と 20 秒にセットします。 |
2395 |
CPU が 10 秒の時間を消費後("wall" 時間ではありません)、プロセスはシグナル |
2396 |
(システムによっては XCPU)を送り、トラップされなければ、これによりプロセスは |
2397 |
終了します。 |
2398 |
シグナルがトラップされると、さらに 10 秒(合計 20 秒)経過後、プロセスは |
2399 |
ブロック不可シグナルで kill されます。 |
2400 |
|
2401 |
=begin original |
2402 |
|
2403 |
See the L<BSD::Resource> and your systems documentation for the gory |
2404 |
details. |
2405 |
|
2406 |
=end original |
2407 |
|
2408 |
詳細については L<BSD::Resource> とあなたのシステムのドキュメントを |
2409 |
参照してください。 |
2410 |
|
2411 |
=head2 How do I avoid zombies on a Unix system? |
2412 |
|
2413 |
(UNIX システムでゾンビを回避するには?) |
2414 |
|
2415 |
=begin original |
2416 |
|
2417 |
Use the reaper code from L<perlipc/"Signals"> to call C<wait()> when a |
2418 |
SIGCHLD is received, or else use the double-fork technique described |
2419 |
in L<perlfaq8/"How do I start a process in the background?">. |
2420 |
|
2421 |
=end original |
2422 |
|
2423 |
SIGCHLD を受け取ったときに C<wait()> を呼び出すように L<perlipc/"Signals"> に |
2424 |
ある刈り取り機プログラム (reaper code) を使うか、 |
2425 |
L<perlfaq8/"How do I start a process in the background?"> で |
2426 |
説明されている double-fork テクニックを使います。 |
2427 |
|
2428 |
=head2 How do I use an SQL database? |
2429 |
|
2430 |
(SQL データベースを使うには?) |
2431 |
|
2432 |
=begin original |
2433 |
|
2434 |
The L<DBI> module provides an abstract interface to most database |
2435 |
servers and types, including Oracle, DB2, Sybase, mysql, Postgresql, |
2436 |
ODBC, and flat files. The DBI module accesses each database type |
2437 |
through a database driver, or DBD. You can see a complete list of |
2438 |
available drivers on CPAN: L<http://www.cpan.org/modules/by-module/DBD/> . |
2439 |
You can read more about DBI on L<http://dbi.perl.org/> . |
2440 |
|
2441 |
=end original |
2442 |
|
2443 |
L<DBI> モジュールはほとんどのデータベースサーバと型に対する抽象 |
2444 |
インターフェースを提供します; これには Oracle, DB2, Sybase, mysql, |
2445 |
Postgresql, ODBC, フラットファイルを含みます。 |
2446 |
DBI モジュールは、DBD と呼ばれるデータベースドライバを通して各データベース |
2447 |
型にアクセスします。 |
2448 |
利用可能なドライバの完全な一覧は CPAN |
2449 |
(L<http://www.cpan.org/modules/by-module/DBD/>) にあります。 |
2450 |
DBI に関するさらなる情報は L<http://dbi.perl.org/> にあります。 |
2451 |
|
2452 |
=begin original |
2453 |
|
2454 |
Other modules provide more specific access: L<Win32::ODBC>, L<Alzabo>, |
2455 |
C<iodbc>, and others found on CPAN Search: L<http://search.cpan.org/> . |
2456 |
|
2457 |
=end original |
2458 |
|
2459 |
その他のモジュールはより具体的なアクセスを提供します: L<Win32::ODBC>, |
2460 |
L<Alzabo>, C<iodbc> などは CPAN Search (L<http://search.cpan.org/>) で |
2461 |
見つけられます。 |
2462 |
|
2463 |
=head2 How do I make a system() exit on control-C? |
2464 |
|
2465 |
(コントロール-C で system() が exit するようにするには?) |
2466 |
|
2467 |
=begin original |
2468 |
|
2469 |
You can't. You need to imitate the C<system()> call (see L<perlipc> for |
2470 |
sample code) and then have a signal handler for the INT signal that |
2471 |
passes the signal on to the subprocess. Or you can check for it: |
2472 |
|
2473 |
=end original |
2474 |
|
2475 |
できません。 |
2476 |
C<system()> 呼び出しを模倣する必要があり(L<perlipc> のサンプルコードを |
2477 |
参照してください)、サブプロセスでのシグナルを送る |
2478 |
INT シグナルのためのシグナルハンドラーを持つ必要があります。 |
2479 |
あるいは、以下のようにしてチェックすることもできます: |
2480 |
|
2481 |
$rc = system($cmd); |
2482 |
if ($rc & 127) { die "signal death" } |
2483 |
|
2484 |
=head2 How do I open a file without blocking? |
2485 |
|
2486 |
(ブロックせずにファイルをオープンするには?) |
2487 |
|
2488 |
=begin original |
2489 |
|
2490 |
If you're lucky enough to be using a system that supports |
2491 |
non-blocking reads (most Unixish systems do), you need only to use the |
2492 |
C<O_NDELAY> or C<O_NONBLOCK> flag from the C<Fcntl> module in conjunction with |
2493 |
C<sysopen()>: |
2494 |
|
2495 |
=end original |
2496 |
|
2497 |
幸運にもあなたの使っているシステムがノンブロッキング読み出し |
2498 |
(ほとんどの Unix 的システムはサポートしています)をサポートしているのであれば、 |
2499 |
C<Fcntl> モジュールの C<O_NDELAY> や C<O_NONBLOCK> というフラグを |
2500 |
C<sysopen()> と一緒に使うだけです。 |
2501 |
|
2502 |
use Fcntl; |
2503 |
sysopen(my $fh, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644) |
2504 |
or die "can't open /foo/somefile: $!": |
2505 |
|
2506 |
=head2 How do I tell the difference between errors from the shell and perl? |
2507 |
|
2508 |
(シェルからと perl からのエラーを見分けるには?) |
2509 |
|
2510 |
=begin original |
2511 |
|
2512 |
(answer contributed by brian d foy) |
2513 |
|
2514 |
=end original |
2515 |
|
2516 |
(答えは brian d foy によって寄贈されました) |
2517 |
|
2518 |
=begin original |
2519 |
|
2520 |
When you run a Perl script, something else is running the script for you, |
2521 |
and that something else may output error messages. The script might |
2522 |
emit its own warnings and error messages. Most of the time you cannot |
2523 |
tell who said what. |
2524 |
|
2525 |
=end original |
2526 |
|
2527 |
あなたが Perl スクリプトを実行すると、他の誰かがあなたのためにスクリプトを |
2528 |
動かします; そしてその他の誰かがエラーメッセージを出すことがあります。 |
2529 |
スクリプトも自身の警告やエラーメッセージを出すかもしれません。 |
2530 |
ほとんどの場合、それを言っているのが誰かは教えてもらえません。 |
2531 |
|
2532 |
=begin original |
2533 |
|
2534 |
You probably cannot fix the thing that runs perl, but you can change how |
2535 |
perl outputs its warnings by defining a custom warning and die functions. |
2536 |
|
2537 |
=end original |
2538 |
|
2539 |
あなたが perl を実行しているものを修正することはおそらくできませんが、 |
2540 |
独自の警告と die の関数を定義することで、perl がどのように警告を |
2541 |
出力するかを変えることはできます。 |
2542 |
|
2543 |
=begin original |
2544 |
|
2545 |
Consider this script, which has an error you may not notice immediately. |
2546 |
|
2547 |
=end original |
2548 |
|
2549 |
すぐには気付かないかもしれない誤りを含むこのスクリプトを考えてみてください。 |
2550 |
|
2551 |
#!/usr/locl/bin/perl |
2552 |
|
2553 |
print "Hello World\n"; |
2554 |
|
2555 |
=begin original |
2556 |
|
2557 |
I get an error when I run this from my shell (which happens to be |
2558 |
bash). That may look like perl forgot it has a C<print()> function, |
2559 |
but my shebang line is not the path to perl, so the shell runs the |
2560 |
script, and I get the error. |
2561 |
|
2562 |
=end original |
2563 |
|
2564 |
これを私のシェル(たまたま bash でした)で実行するとエラーが表示されます。 |
2565 |
これは perl が C<print()> 関数を忘れてしまったかのように見えますが、 |
2566 |
#! 行の perl へのパスが間違っているので、シェルがこのスクリプトを実行し、 |
2567 |
エラーが表示されます。 |
2568 |
|
2569 |
$ ./test |
2570 |
./test: line 3: print: command not found |
2571 |
|
2572 |
=begin original |
2573 |
|
2574 |
A quick and dirty fix involves a little bit of code, but this may be all |
2575 |
you need to figure out the problem. |
2576 |
|
2577 |
=end original |
2578 |
|
2579 |
素早く汚い修正には少しコードが必要ですが、おそらく問題を見つけ出すのに |
2580 |
必要なものが全てあるでしょう。 |
2581 |
|
2582 |
#!/usr/bin/perl -w |
2583 |
|
2584 |
BEGIN { |
2585 |
$SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; }; |
2586 |
$SIG{__DIE__} = sub{ print STDERR "Perl: ", @_; exit 1}; |
2587 |
} |
2588 |
|
2589 |
$a = 1 + undef; |
2590 |
$x / 0; |
2591 |
__END__ |
2592 |
|
2593 |
=begin original |
2594 |
|
2595 |
The perl message comes out with "Perl" in front. The C<BEGIN> block |
2596 |
works at compile time so all of the compilation errors and warnings |
2597 |
get the "Perl:" prefix too. |
2598 |
|
2599 |
=end original |
2600 |
|
2601 |
perl のメッセージは先頭に "Perl" を付けて出力されます。 |
2602 |
C<BEGIN> ブロックはコンパイル時に動作するので、全てのコンパイル時のエラーと |
2603 |
警告にも "Perl:" の接頭辞がつきます。 |
2604 |
|
2605 |
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. |
2607 |
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. |
2609 |
Perl: Use of uninitialized value in division (/) at ./test line 9. |
2610 |
Perl: Illegal division by zero at ./test line 9. |
2611 |
Perl: Illegal division by zero at -e line 3. |
2612 |
|
2613 |
=begin original |
2614 |
|
2615 |
If I don't see that "Perl:", it's not from perl. |
2616 |
|
2617 |
=end original |
2618 |
|
2619 |
"Perl:" という文字がなければ、perl が出しているものではありません。 |
2620 |
|
2621 |
=begin original |
2622 |
|
2623 |
You could also just know all the perl errors, and although there are |
2624 |
some people who may know all of them, you probably don't. However, they |
2625 |
all should be in the L<perldiag> manpage. If you don't find the error in |
2626 |
there, it probably isn't a perl error. |
2627 |
|
2628 |
=end original |
2629 |
|
2630 |
単に全ての perl のエラーを知るという方法もありますが、(全てのエラーを |
2631 |
知っている人もいるかもしれませんが)おそらくあなたは知らないでしょう。 |
2632 |
しかし、これら全ては L<perldiag> マニュアルページにあるはずです。 |
2633 |
もしエラーがここになければ、それはおそらく perl のエラーではありません。 |
2634 |
|
2635 |
=begin original |
2636 |
|
2637 |
Looking up every message is not the easiest way, so let perl to do it |
2638 |
for you. Use the diagnostics pragma with turns perl's normal messages |
2639 |
into longer discussions on the topic. |
2640 |
|
2641 |
=end original |
2642 |
|
2643 |
全てのメッセージを探すというのは最も簡単な方法ではないので、その作業は |
2644 |
perl にさせます。 |
2645 |
perl の通常のメッセージを、より長い説明に変更する diagnostics プラグマを |
2646 |
使います。 |
2647 |
|
2648 |
use diagnostics; |
2649 |
|
2650 |
=begin original |
2651 |
|
2652 |
If you don't get a paragraph or two of expanded discussion, it |
2653 |
might not be perl's message. |
2654 |
|
2655 |
=end original |
2656 |
|
2657 |
段落一つか二つの追加の議論が表示されなければ、それは perl のメッセージでは |
2658 |
ないでしょう。 |
2659 |
|
2660 |
=head2 How do I install a module from CPAN? |
2661 |
|
2662 |
(モジュールを CPAN からインストールするには?) |
2663 |
|
2664 |
=begin original |
2665 |
|
2666 |
(contributed by brian d foy) |
2667 |
|
2668 |
=end original |
2669 |
|
2670 |
(brian d foy によって寄贈されました) |
2671 |
|
2672 |
=begin original |
2673 |
|
2674 |
The easiest way is to have a module also named CPAN do it for you by using |
2675 |
the C<cpan> command that comes with Perl. You can give it a list of modules |
2676 |
to install: |
2677 |
|
2678 |
=end original |
2679 |
|
2680 |
最も単純な方法は、Perl と同梱されている C<cpan> コマンドを使って、この仕事を |
2681 |
してくれる CPAN モジュールを入手することです。 |
2682 |
インストールするモジュールの一覧を与えます: |
2683 |
|
2684 |
$ cpan IO::Interactive Getopt::Whatever |
2685 |
|
2686 |
=begin original |
2687 |
|
2688 |
If you prefer C<CPANPLUS>, it's just as easy: |
2689 |
|
2690 |
=end original |
2691 |
|
2692 |
C<CPANPLUS> が好みなら、やはり簡単です: |
2693 |
|
2694 |
$ cpanp i IO::Interactive Getopt::Whatever |
2695 |
|
2696 |
If you want to install a distribution from the current directory, you can |
2697 |
tell C<CPAN.pm> to install C<.> (the full stop): |
2698 |
|
2699 |
$ cpan . |
2700 |
|
2701 |
=begin original |
2702 |
|
2703 |
See the documentation for either of those commands to see what else |
2704 |
you can do. |
2705 |
|
2706 |
=end original |
2707 |
|
2708 |
その他に出来ることについてはこれらのコマンドの文書を参照してください。 |
2709 |
|
2710 |
=begin original |
2711 |
|
2712 |
If you want to try to install a distribution by yourself, resolving |
2713 |
all dependencies on your own, you follow one of two possible build |
2714 |
paths. |
2715 |
|
2716 |
=end original |
2717 |
|
2718 |
全ての依存を自分で解決して、配布を自分自身でインストールしてみたい場合は、 |
2719 |
二つのビルド方法のどちらかに従います。 |
2720 |
|
2721 |
=begin original |
2722 |
|
2723 |
For distributions that use I<Makefile.PL>: |
2724 |
|
2725 |
=end original |
2726 |
|
2727 |
I<Makefile.PL> を使う配布では: |
2728 |
|
2729 |
$ perl Makefile.PL |
2730 |
$ make test install |
2731 |
|
2732 |
=begin original |
2733 |
|
2734 |
For distributions that use I<Build.PL>: |
2735 |
|
2736 |
=end original |
2737 |
|
2738 |
I<Build.PL> を使う配布では: |
2739 |
|
2740 |
$ perl Build.PL |
2741 |
$ ./Build test |
2742 |
$ ./Build install |
2743 |
|
2744 |
=begin original |
2745 |
|
2746 |
Some distributions may need to link to libraries or other third-party |
2747 |
code and their build and installation sequences may be more complicated. |
2748 |
Check any I<README> or I<INSTALL> files that you may find. |
2749 |
|
2750 |
=end original |
2751 |
|
2752 |
一部の配布ではライブラリはその他のサードパーティのコードへのリンクが |
2753 |
必要であったり、ビルドとインストールの手順がもっと複雑な場合もあります。 |
2754 |
見付かった I<README> や I<INSTALL> ファイルをチェックしてください。 |
2755 |
|
2756 |
=head2 What's the difference between require and use? |
2757 |
|
2758 |
(require と use の間の違いとは?) |
2759 |
|
2760 |
=begin original |
2761 |
|
2762 |
(contributed by brian d foy) |
2763 |
|
2764 |
=end original |
2765 |
|
2766 |
(brian d foy によって寄贈されました) |
2767 |
|
2768 |
=begin original |
2769 |
|
2770 |
Perl runs C<require> statement at run-time. Once Perl loads, compiles, |
2771 |
and runs the file, it doesn't do anything else. The C<use> statement |
2772 |
is the same as a C<require> run at compile-time, but Perl also calls the |
2773 |
C<import> method for the loaded package. These two are the same: |
2774 |
|
2775 |
=end original |
2776 |
|
2777 |
Perl は C<require> 文を実行時に実行します。 |
2778 |
一旦 Perl がロードされ、コンパイルされ、ファイルが実行されると、 |
2779 |
他に何もしません。 |
2780 |
C<use> 文は C<require> と同じことをコンパイル時に行いますが、 |
2781 |
Perl はまたロードされたパッケージの C<import> メソッドを呼び出します。 |
2782 |
以下の二つは同じです: |
2783 |
|
2784 |
use MODULE qw(import list); |
2785 |
|
2786 |
BEGIN { |
2787 |
require MODULE; |
2788 |
MODULE->import(import list); |
2789 |
} |
2790 |
|
2791 |
=begin original |
2792 |
|
2793 |
However, you can suppress the C<import> by using an explicit, empty |
2794 |
import list. Both of these still happen at compile-time: |
2795 |
|
2796 |
=end original |
2797 |
|
2798 |
しかし、明示的に空のインポートリストを使うことで、C<import> を |
2799 |
抑制できます。 |
2800 |
これらはどちらもやはりコンパイル時に起こります: |
2801 |
|
2802 |
use MODULE (); |
2803 |
|
2804 |
BEGIN { |
2805 |
require MODULE; |
2806 |
} |
2807 |
|
2808 |
=begin original |
2809 |
|
2810 |
Since C<use> will also call the C<import> method, the actual value |
2811 |
for C<MODULE> must be a bareword. That is, C<use> cannot load files |
2812 |
by name, although C<require> can: |
2813 |
|
2814 |
=end original |
2815 |
|
2816 |
C<use> は C<import> メソッドの呼び出しも行うので、 |
2817 |
C<MODULE> の実際の値は裸の単語でなければなりません。 |
2818 |
これは、C<use> は名前でファイルをロード出来ませんが、 |
2819 |
C<require> ではできます: |
2820 |
|
2821 |
require "$ENV{HOME}/lib/Foo.pm"; # no @INC searching! |
2822 |
|
2823 |
=begin original |
2824 |
|
2825 |
See the entry for C<use> in L<perlfunc> for more details. |
2826 |
|
2827 |
=end original |
2828 |
|
2829 |
さらなる詳細については L<perlfunc> の C<use> の項目を参照してください。 |
2830 |
|
2831 |
=head2 How do I keep my own module/library directory? |
2832 |
|
2833 |
(自分自身のモジュール/ライブラリディレクトリを持つには?) |
2834 |
|
2835 |
=begin original |
2836 |
|
2837 |
When you build modules, tell Perl where to install the modules. |
2838 |
|
2839 |
=end original |
2840 |
|
2841 |
モジュールを作成するときに、Perl にモジュールをインストールする場所を |
2842 |
指定します。 |
2843 |
|
2844 |
=begin original |
2845 |
|
2846 |
If you want to install modules for your own use, the easiest way might |
2847 |
be L<local::lib>, which you can download from CPAN. It sets various |
2848 |
installation settings for you, and uses those same settings within |
2849 |
your programs. |
2850 |
|
2851 |
=end original |
2852 |
|
2853 |
自分自身が使うためにモジュールをインストールしたいなら、一番簡単な方法は、 |
2854 |
CPAN からダウンロードできる L<local::lib> でしょう。 |
2855 |
これは様々なインストール設定を行い、プログラム中で同じ設定を使います。 |
2856 |
|
2857 |
=begin original |
2858 |
|
2859 |
If you want more flexibility, you need to configure your CPAN client |
2860 |
for your particular situation. |
2861 |
|
2862 |
=end original |
2863 |
|
2864 |
もっと柔軟性がほしいなら、CPAN クライアントを特定の状況に合わせて |
2865 |
設定する必要があります。 |
2866 |
|
2867 |
=begin original |
2868 |
|
2869 |
For C<Makefile.PL>-based distributions, use the INSTALL_BASE option |
2870 |
when generating Makefiles: |
2871 |
|
2872 |
=end original |
2873 |
|
2874 |
C<Makefile.PL>-ベースの配布では、Makefile の生成時に INSTALL_BASE |
2875 |
オプションを使ってください: |
2876 |
|
2877 |
perl Makefile.PL INSTALL_BASE=/mydir/perl |
2878 |
|
2879 |
=begin original |
2880 |
|
2881 |
You can set this in your C<CPAN.pm> configuration so modules |
2882 |
automatically install in your private library directory when you use |
2883 |
the CPAN.pm shell: |
2884 |
|
2885 |
=end original |
2886 |
|
2887 |
CPAN.pm シェルを使ったときに、自動的にモジュールを自分のプライベートな |
2888 |
ディレクトリにインストールさせるように、CPAN.pm の設定を変更できます: |
2889 |
|
2890 |
% cpan |
2891 |
cpan> o conf makepl_arg INSTALL_BASE=/mydir/perl |
2892 |
cpan> o conf commit |
2893 |
|
2894 |
=begin original |
2895 |
|
2896 |
For C<Build.PL>-based distributions, use the --install_base option: |
2897 |
|
2898 |
=end original |
2899 |
|
2900 |
C<Build.PL>-ベースの配布では、--install_base オプションを使ってください: |
2901 |
|
2902 |
perl Build.PL --install_base /mydir/perl |
2903 |
|
2904 |
=begin original |
2905 |
|
2906 |
You can configure C<CPAN.pm> to automatically use this option too: |
2907 |
|
2908 |
=end original |
2909 |
|
2910 |
自動的にこのオプションを使うために、CPAN.pm を設定することも出来ます: |
2911 |
|
2912 |
% cpan |
2913 |
cpan> o conf mbuild_arg "--install_base /mydir/perl" |
2914 |
cpan> o conf commit |
2915 |
|
2916 |
=begin original |
2917 |
|
2918 |
INSTALL_BASE tells these tools to put your modules into |
2919 |
F</mydir/perl/lib/perl5>. See L<How do I add a directory to my |
2920 |
include path (@INC) at runtime?> for details on how to run your newly |
2921 |
installed modules. |
2922 |
|
2923 |
=end original |
2924 |
|
2925 |
INSTALL_BASE はこれらのツールにモジュールを F</mydir/perl/lib/perl5> に |
2926 |
置くように伝えます。 |
2927 |
新しくインストールしたモジュールを実行する方法についての詳細は |
2928 |
L<How do I add a directory to my include path (@INC) at runtime?> を |
2929 |
参照してください。 |
2930 |
|
2931 |
=begin original |
2932 |
|
2933 |
There is one caveat with INSTALL_BASE, though, since it acts |
2934 |
differently from the PREFIX and LIB settings that older versions of |
2935 |
L<ExtUtils::MakeMaker> advocated. INSTALL_BASE does not support |
2936 |
installing modules for multiple versions of Perl or different |
2937 |
architectures under the same directory. You should consider whether you |
2938 |
really want that and, if you do, use the older PREFIX and LIB |
2939 |
settings. See the L<ExtUtils::Makemaker> documentation for more details. |
2940 |
|
2941 |
=end original |
2942 |
|
2943 |
しかし、INSTALL_BASE には一つの問題点があります; |
2944 |
なぜなら、より古いバージョンの L<ExtUtils::MakeMaker> が推奨していた |
2945 |
PREFIX および LIB とは異なった動作をするからです。 |
2946 |
INSTALL_BASE は、複数のバージョンの Perl や異なったアーキテクチャに |
2947 |
対して同じディレクトリにモジュールをインストールすることには |
2948 |
対応していません。 |
2949 |
もし本当にそれが必要でそうするなら、古い PREFIX と LIB の設定を |
2950 |
考慮するべきです。 |
2951 |
さらなる詳細については L<ExtUtils::Makemaker> の文書を参照してください。 |
2952 |
|
2953 |
=head2 How do I add the directory my program lives in to the module/library search path? |
2954 |
|
2955 |
(私のプログラムの置いてある場所をモジュール/ライブラリの検索パスに追加するには?) |
2956 |
|
2957 |
=begin original |
2958 |
|
2959 |
(contributed by brian d foy) |
2960 |
|
2961 |
=end original |
2962 |
|
2963 |
(brian d foy によって寄贈されました) |
2964 |
|
2965 |
=begin original |
2966 |
|
2967 |
If you know the directory already, you can add it to C<@INC> as you would |
2968 |
for any other directory. You might <use lib> if you know the directory |
2969 |
at compile time: |
2970 |
|
2971 |
=end original |
2972 |
|
2973 |
すでにディレクトリが分かっている場合は、それを C<@INC> に追加できます。 |
2974 |
コンパイル時に分かっている場合は、<use lib> が使えます: |
2975 |
|
2976 |
use lib $directory; |
2977 |
|
2978 |
=begin original |
2979 |
|
2980 |
The trick in this task is to find the directory. Before your script does |
2981 |
anything else (such as a C<chdir>), you can get the current working |
2982 |
directory with the C<Cwd> module, which comes with Perl: |
2983 |
|
2984 |
=end original |
2985 |
|
2986 |
このタスクの秘訣は、ディレクトリを探すことです。 |
2987 |
あなたのスクリプトが(C<chdir> などで)他の場所へ行く前に、Perl に |
2988 |
同梱されている C<Cwd> モジュールを使って、カレントワーキングディレクトリを |
2989 |
取得できます: |
2990 |
|
2991 |
BEGIN { |
2992 |
use Cwd; |
2993 |
our $directory = cwd; |
2994 |
} |
2995 |
|
2996 |
use lib $directory; |
2997 |
|
2998 |
=begin original |
2999 |
|
3000 |
You can do a similar thing with the value of C<$0>, which holds the |
3001 |
script name. That might hold a relative path, but C<rel2abs> can turn |
3002 |
it into an absolute path. Once you have the |
3003 |
|
3004 |
=end original |
3005 |
|
3006 |
似たようなことは、スクリプト名を記録している C<$0> でもできます。 |
3007 |
これは相対パスかもしれませんが、C<rel2abs> はこれを絶対パスに変更できます。 |
3008 |
以下のようにすると |
3009 |
|
3010 |
BEGIN { |
3011 |
use File::Spec::Functions qw(rel2abs); |
3012 |
use File::Basename qw(dirname); |
3013 |
|
3014 |
my $path = rel2abs( $0 ); |
3015 |
our $directory = dirname( $path ); |
3016 |
} |
3017 |
|
3018 |
use lib $directory; |
3019 |
|
3020 |
=begin original |
3021 |
|
3022 |
The L<FindBin> module, which comes with Perl, might work. It finds the |
3023 |
directory of the currently running script and puts it in C<$Bin>, which |
3024 |
you can then use to construct the right library path: |
3025 |
|
3026 |
=end original |
3027 |
|
3028 |
Perl に同梱されている L<FindBin> モジュールが働くでしょう。 |
3029 |
これは現在実行しているスクリプトのディレクトリを見つけて C<$Bin> に |
3030 |
設定し、正しいライブラリパスを構築するために使えるようにします: |
3031 |
|
3032 |
use FindBin qw($Bin); |
3033 |
|
3034 |
=begin original |
3035 |
|
3036 |
You can also use L<local::lib> to do much of the same thing. Install |
3037 |
modules using L<local::lib>'s settings then use the module in your |
3038 |
program: |
3039 |
|
3040 |
=end original |
3041 |
|
3042 |
同じことをするのに L<local::lib> も使えます。 |
3043 |
L<local::lib> の設定を使ってモジュールをインストールしてから、プログラムで |
3044 |
そのモジュールを使います: |
3045 |
|
3046 |
use local::lib; # sets up a local lib at ~/perl5 |
3047 |
|
3048 |
=begin original |
3049 |
|
3050 |
See the L<local::lib> documentation for more details. |
3051 |
|
3052 |
=end original |
3053 |
|
3054 |
さらなる詳細については L<local::lib> の文書を参照してください。 |
3055 |
|
3056 |
=head2 How do I add a directory to my include path (@INC) at runtime? |
3057 |
|
3058 |
(実行時にインクルードパス (@INC) にディレクトリを追加するには?) |
3059 |
|
3060 |
=begin original |
3061 |
|
3062 |
Here are the suggested ways of modifying your include path, including |
3063 |
environment variables, run-time switches, and in-code statements: |
3064 |
|
3065 |
=end original |
3066 |
|
3067 |
環境変数、実行時スイッチ、コード内の文などを使って、インクルードパスを |
3068 |
変更するためのお薦めの方法を挙げておきます: |
3069 |
|
3070 |
=over 4 |
3071 |
|
3072 |
=item the C<PERLLIB> environment variable |
3073 |
|
3074 |
(C<PERLLIB> 環境変数) |
3075 |
|
3076 |
$ export PERLLIB=/path/to/my/dir |
3077 |
$ perl program.pl |
3078 |
|
3079 |
=item the C<PERL5LIB> environment variable |
3080 |
|
3081 |
(C<PERL5LIB> 環境変数) |
3082 |
|
3083 |
$ export PERL5LIB=/path/to/my/dir |
3084 |
$ perl program.pl |
3085 |
|
3086 |
=item the C<perl -Idir> command line flag |
3087 |
|
3088 |
(C<perl -Idir> コマンドラインフラグ) |
3089 |
|
3090 |
$ perl -I/path/to/my/dir program.pl |
3091 |
|
3092 |
=item the C<lib> pragma: |
3093 |
|
3094 |
(C<lib> プラグマ) |
3095 |
|
3096 |
use lib "$ENV{HOME}/myown_perllib"; |
3097 |
|
3098 |
=item the L<local::lib> module: |
3099 |
|
3100 |
use local::lib; |
3101 |
|
3102 |
use local::lib "~/myown_perllib"; |
3103 |
|
3104 |
=back |
3105 |
|
3106 |
=begin original |
3107 |
|
3108 |
The last is particularly useful because it knows about machine-dependent |
3109 |
architectures. The C<lib.pm> pragmatic module was first |
3110 |
included with the 5.002 release of Perl. |
3111 |
|
3112 |
=end original |
3113 |
|
3114 |
最後のものが特に便利です; なぜなら、これはマシン依存のアーキテクチャを |
3115 |
知っているからです。 |
3116 |
プラグマ的モジュール C<lib.pm> は Perl 5.002 で最初に導入されました。 |
3117 |
|
3118 |
=head2 What is socket.ph and where do I get it? |
3119 |
|
3120 |
(socket.ph とは一体何で、それはどこで入手できますか?) |
3121 |
|
3122 |
=begin original |
3123 |
|
3124 |
It's a Perl 4 style file defining values for system networking |
3125 |
constants. Sometimes it is built using L<h2ph> when Perl is installed, |
3126 |
but other times it is not. Modern programs should use C<use Socket;> |
3127 |
instead. |
3128 |
|
3129 |
=end original |
3130 |
|
3131 |
それは Perl 4 スタイルのファイルで、ネットワーク使用のための |
3132 |
値を定義しているものです。 |
3133 |
これは Perl がインストールされたときに L<h2ph> を使って作成されることも |
3134 |
ありますが、そうでないときもあります。 |
3135 |
最近のプログラムでは代わりに C<use Socket> を使うべきです。 |
3136 |
|
3137 |
=head1 AUTHOR AND COPYRIGHT |
3138 |
|
3139 |
Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and |
3140 |
other authors as noted. All rights reserved. |
3141 |
|
3142 |
This documentation is free; you can redistribute it and/or modify it |
3143 |
under the same terms as Perl itself. |
3144 |
|
3145 |
Irrespective of its distribution, all code examples in this file |
3146 |
are hereby placed into the public domain. You are permitted and |
3147 |
encouraged to use this code in your own programs for fun |
3148 |
or for profit as you see fit. A simple comment in the code giving |
3149 |
credit would be courteous but is not required. |
3150 |
|
3151 |
=begin meta |
3152 |
|
3153 |
Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp> |
3154 |
Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-5.14.1, 5.00150039) |
3155 |
Status: completed |
3156 |
|
3157 |
=end meta |
3158 |
|