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