オープンソース・ソフトウェアの開発とダウンロード

CVS リポジトリの参照

Contents of /perldocjp/docs/modules/perlfaq-5.0150042/perlfaq7.pod

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Wed Nov 6 09:43:21 2013 UTC (10 years, 6 months ago) by argrath
Branch: MAIN
CVS Tags: HEAD
perlfaq-5.0150042

1
2 =encoding euc-jp
3
4 =head1 NAME
5
6 =begin original
7
8 perlfaq7 - General Perl Language Issues
9
10 =end original
11
12 perlfaq7 - Perl 言語一般に関することがら
13
14 =head1 DESCRIPTION
15
16 =begin original
17
18 This section deals with general Perl language issues that don't
19 clearly fit into any of the other sections.
20
21 =end original
22
23 このセクションでは、他のセクションにはきっちりとあてはまらないような
24 Perl 言語に関する一般的な事柄を扱います。
25
26 =head2 Can I get a BNF/yacc/RE for the Perl language?
27
28 (Perl のための BNF/yacc/RE は入手できますか?)
29
30 =begin original
31
32 There is no BNF, but you can paw your way through the yacc grammar in
33 perly.y in the source distribution if you're particularly brave. The
34 grammar relies on very smart tokenizing code, so be prepared to
35 venture into toke.c as well.
36
37 =end original
38
39 BNFはありませんが、もし多少の勇気を持ちあわせているのであれば
40 配布ソースに含まれている perly.y にある yacc 文法をいじくりまわすことが
41 できます。
42 その文法は非常に賢い字句解析ルーチンに依存したものなので、
43 toke.c を眺める準備もしておきましょう。
44
45 =begin original
46
47 In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF.
48 The work of parsing perl is distributed between yacc, the lexer, smoke
49 and mirrors."
50
51 =end original
52
53 reduce
54 Chaim Frenkel の言葉を借りればこうです:
55 「Perl の文法は BNF まで縮小することができない。
56 perl の構文解析の作業は yacc、字句解析器、煙と鏡とに分配される。」
57
58 =head2 What are all these $@%&* punctuation signs, and how do I know when to use them?
59
60 ($@%&* のマークはなんですか、これらをいつ使えばいいのかを知るにはどうすればいいですか?)
61
62 =begin original
63
64 They are type specifiers, as detailed in L<perldata>:
65
66 =end original
67
68 これらは型指定子(type specifiers)で、L<perldata> で説明されています:
69
70 =begin original
71
72 $ for scalar values (number, string or reference)
73 @ for arrays
74 % for hashes (associative arrays)
75 & for subroutines (aka functions, procedures, methods)
76 * for all types of that symbol name. In version 4 you used them like
77 pointers, but in modern perls you can just use references.
78
79 =end original
80
81 $ スカラ値(数値、文字列、リファレンス)に対するもの
82 @ 配列に対するもの
83 % ハッシュ(連想配列)に対するもの
84 & サブルーチン(またの名を関数、手続き、メソッド)に対するもの
85 * シンボル名に対する全ての型。バージョン 4 ではポインタのように
86 使われていましたが、新しい perl ではリファレンスが使えます。
87
88 =begin original
89
90 There are a couple of other symbols that
91 you're likely to encounter that aren't
92 really type specifiers:
93
94 =end original
95
96 実際には型指定子として見ることはないであろう二つのものが
97 この他にもあります:
98
99 =begin original
100
101 <> are used for inputting a record from a filehandle.
102 \ takes a reference to something.
103
104 =end original
105
106 <> あるファイルハンドルからレコードを入力するのに使われます。
107 \ なにかのリファレンスを取ります。
108
109 =begin original
110
111 Note that <FILE> is I<neither> the type specifier for files
112 nor the name of the handle. It is the C<< <> >> operator applied
113 to the handle FILE. It reads one line (well, record--see
114 L<perlvar/$E<sol>>) from the handle FILE in scalar context, or I<all> lines
115 in list context. When performing open, close, or any other operation
116 besides C<< <> >> on files, or even when talking about the handle, do
117 I<not> use the brackets. These are correct: C<eof(FH)>, C<seek(FH, 0,
118 2)> and "copying from STDIN to FILE".
119
120 =end original
121
122 <FILE> は、ファイルに対する型指定子にもハンドルの名前の
123 I<どちらでもない> ということに注意してください。
124 これはハンドル FILE に対する C<< <> >> 演算子です。
125 一行(そう、レコードですね -- L<perlvar/$E<sol>> を参照してください) を
126 FILE というハンドルからスカラコンテキストで読み出します;
127 リストコンテキストの場合は B<全ての> 行を読み出します。
128 ファイルの C<< <> >> に関係する open、close などの操作を行うときには、
129 ハンドルについて行っている場合であっても
130 アングルブラケットを使っては B<いけません>。
131 C<eof(FH)>, C<seek(FH, 0,2)> や "copying from STDIN to FILE" は
132 正しいものなのです。
133
134 =head2 Do I always/never have to quote my strings or use semicolons and commas?
135
136 (文字列ではクォートしたりセミコロンやカンマを使うのは常に必要/常に不要ですか?)
137
138 =begin original
139
140 Normally, a bareword doesn't need to be quoted, but in most cases
141 probably should be (and must be under C<use strict>). But a hash key
142 consisting of a simple word and the left-hand
143 operand to the C<< => >> operator both
144 count as though they were quoted:
145
146 =end original
147
148 通常は、裸の単語(barewords)はクォートする必要はありませんが、
149 ほとんど場合はクォートすべきでしょう(そして、C<use strcit> しているときは
150 しなければなりません)。
151 しかし、単純な単語から
152 構成されるハッシュと、C<< => >> 演算子の左側にあるオペランドは
153 両方ともクォートされているとみなされます:
154
155 This is like this
156 ------------ ---------------
157 $foo{line} $foo{'line'}
158 bar => stuff 'bar' => stuff
159
160 =begin original
161
162 The final semicolon in a block is optional, as is the final comma in a
163 list. Good style (see L<perlstyle>) says to put them in except for
164 one-liners:
165
166 =end original
167
168 ブロックの最後にあるセミコロンは、リストの最後にあるカンマと同じく
169 省略可能です。
170 良いスタイル(L<perlstyle> を参照)は一行野郎(one-liners)でなければ
171 それらを使うようにしましょうと言っています。
172
173 if ($whoops) { exit 1 }
174 my @nums = (1, 2, 3);
175
176 if ($whoops) {
177 exit 1;
178 }
179
180 my @lines = (
181 "There Beren came from mountains cold",
182 "And lost he wandered under leaves",
183 );
184
185 =head2 How do I skip some return values?
186
187 (戻り値の一部をスキップするには?)
188
189 =begin original
190
191 One way is to treat the return values as a list and index into it:
192
193 =end original
194
195 方法の一つは、戻り値をリストとみなして、それに添え字づけするというものです:
196
197 $dir = (getpwnam($user))[7];
198
199 =begin original
200
201 Another way is to use undef as an element on the left-hand-side:
202
203 =end original
204
205 もう一つのやりかたは、左辺の要素として undef を使うというものです:
206
207 ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
208
209 =begin original
210
211 You can also use a list slice to select only the elements that
212 you need:
213
214 =end original
215
216 必要な要素だけを選択するために、リストスライスも使えます:
217
218 ($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];
219
220 =head2 How do I temporarily block warnings?
221
222 (一時的に警告をブロックするには?)
223
224 =begin original
225
226 If you are running Perl 5.6.0 or better, the C<use warnings> pragma
227 allows fine control of what warnings are produced.
228 See L<perllexwarn> for more details.
229
230 =end original
231
232 Perl 5.6.0 以降を使っているなら、C<use warnings> プラグマで
233 どんな警告を生成するかをうまく制御できます。
234 詳細については L<perllexwarn> を参照してください。
235
236 {
237 no warnings; # temporarily turn off warnings
238 $x = $y + $z; # I know these might be undef
239 }
240
241 =begin original
242
243 Additionally, you can enable and disable categories of warnings.
244 You turn off the categories you want to ignore and you can still
245 get other categories of warnings. See L<perllexwarn> for the
246 complete details, including the category names and hierarchy.
247
248 =end original
249
250 さらに、警告の分野毎に警告を有効または無効にできます。
251 無視したいカテゴリを無効にしても、残りのカテゴリの警告は受けられます。
252 カテゴリ名と階層を含む、完全な詳細については L<perllexwarn> を
253 参照してください。
254
255 {
256 no warnings 'uninitialized';
257 $x = $y + $z;
258 }
259
260 =begin original
261
262 If you have an older version of Perl, the C<$^W> variable (documented
263 in L<perlvar>) controls runtime warnings for a block:
264
265 =end original
266
267 より古いバージョンの場合は、変数 C<$^W>(L<perlvar> に説明があります)は
268 実行時の警告のブロックを制御します:
269
270 =begin original
271
272 {
273 local $^W = 0; # temporarily turn off warnings
274 $x = $y + $z; # I know these might be undef
275 }
276
277 =end original
278
279 {
280 local $^W = 0; # 一時的に警告をオフにする
281 $a = $b + $c; # これらが undef かもしれないことを知っている
282 }
283
284 =begin original
285
286 Note that like all the punctuation variables, you cannot currently
287 use my() on C<$^W>, only local().
288
289 =end original
290
291 全ての句読点変数(punctuation variable)と同様、現時点では C<$^W> に対して
292 my() を使うことはできず、local() だけしか使えないということに
293 注意してください。
294
295 =head2 What's an extension?
296
297 (エクステンションてなんですか?)
298
299 =begin original
300
301 An extension is a way of calling compiled C code from Perl. Reading
302 L<perlxstut> is a good place to learn more about extensions.
303
304 =end original
305
306 エクステンションとは、Perl からコンパイル済みの C コードを呼び出すための
307 方法です。
308 エクステンションについて知るには L<perlxstut> を読むのが良いでしょう。
309
310 =head2 Why do Perl operators have different precedence than C operators?
311
312 (なぜ Perl の演算子は C の演算子とは異なった優先順位を持っているのでしょうか?)
313
314 =begin original
315
316 Actually, they don't. All C operators that Perl copies have the same
317 precedence in Perl as they do in C. The problem is with operators that C
318 doesn't have, especially functions that give a list context to everything
319 on their right, eg. print, chmod, exec, and so on. Such functions are
320 called "list operators" and appear as such in the precedence table in
321 L<perlop>.
322
323 =end original
324
325 実際はそうではありません。
326 Perl に持ち込まれた C の演算子はすべて、C と Perl とで同じ優先順位を
327 持っています。
328 問題は、C にはない演算子、特にその右辺に対してつねにリストコンテキストを
329 与える関数、例えば print、chmod、exce などです。
330 そういった関数は「リスト演算子」と呼ばれ、L<perlop> にある優先順位テーブルに
331 あります。
332
333 =begin original
334
335 A common mistake is to write:
336
337 =end original
338
339 ありがちな間違いは以下のようにのように書いてしまうことです:
340
341 unlink $file || die "snafu";
342
343 =begin original
344
345 This gets interpreted as:
346
347 =end original
348
349 これは以下のように解釈されます:
350
351 unlink ($file || die "snafu");
352
353 =begin original
354
355 To avoid this problem, either put in extra parentheses or use the
356 super low precedence C<or> operator:
357
358 =end original
359
360 この問題を避けるためには、余計な括弧をつけるかより優先順位の低い
361 C<or> 演算子を使うようにします:
362
363 (unlink $file) || die "snafu";
364 unlink $file or die "snafu";
365
366 =begin original
367
368 The "English" operators (C<and>, C<or>, C<xor>, and C<not>)
369 deliberately have precedence lower than that of list operators for
370 just such situations as the one above.
371
372 =end original
373
374 "English" 演算子(C<and>, C<or>, C<xor>, C<not>) は先に説明している
375 同じ働きをするリスト演算子よりも低い優先順位を故意に持たされています。
376
377 =begin original
378
379 Another operator with surprising precedence is exponentiation. It
380 binds more tightly even than unary minus, making C<-2**2> produce a
381 negative four and not a positive one. It is also right-associating, meaning
382 that C<2**3**2> is two raised to the ninth power, not eight squared.
383
384 =end original
385
386 もう一つの、びっくりするような優先順位を持っている演算子は
387 べき乗(exponentiation)です。
388 これは単項のマイナスよりも強く結び付くので、C<-2**2> はプラス 4 ではなく、
389 マイナス 4 を生成します。
390 この演算子は右結合するので、C<2**3**2> は 8 の 2 乗ではなく、
391 2 の 9 乗です。
392
393 =begin original
394
395 Although it has the same precedence as in C, Perl's C<?:> operator
396 produces an lvalue. This assigns $x to either $if_true or $if_false, depending
397 on the trueness of $maybe:
398
399 =end original
400
401 C と同じ優先順位を持っているにも関らず、Perl では C<?:> 演算子は
402 左辺値を作り出します。
403 以下の代入では、$maybe の値に応じて、$if_true か $if_false のいずれかに
404 $x の値を代入します:
405
406 ($maybe ? $if_true : $if_false) = $x;
407
408 =head2 How do I declare/create a structure?
409
410 (構造体を宣言したり生成するには?)
411
412 =begin original
413
414 In general, you don't "declare" a structure. Just use a (probably
415 anonymous) hash reference. See L<perlref> and L<perldsc> for details.
416 Here's an example:
417
418 =end original
419
420 一般的には、構造体を“宣言”することはありません。
421 単に(おそらくは無名の)ハッシュリファレンスを使うだけです。
422 詳しくは L<perlref>と L<perldsc> を参照してください。
423 例を挙げましょう:
424
425 $person = {}; # new anonymous hash
426 $person->{AGE} = 24; # set field AGE to 24
427 $person->{NAME} = "Nat"; # set field NAME to "Nat"
428
429 =begin original
430
431 If you're looking for something a bit more rigorous, try L<perltoot>.
432
433 =end original
434
435 もうちょっと正確ななにかを求めているのなら、
436 L<perltoot> に挑戦してみてください。
437
438 =head2 How do I create a module?
439
440 (モジュールを作成するには?)
441
442 =begin original
443
444 L<perlnewmod> is a good place to start, ignore the bits
445 about uploading to CPAN if you don't want to make your
446 module publicly available.
447
448 =end original
449
450 L<perlnewmod> が始めるのに良い場所です; 作成したモジュールを公開しようと
451 思っていない場合は CPAN へのアップロードに関する部分については
452 無視してください。
453
454 =begin original
455
456 L<ExtUtils::ModuleMaker> and L<Module::Starter> are also
457 good places to start. Many CPAN authors now use L<Dist::Zilla>
458 to automate as much as possible.
459
460 =end original
461
462 L<ExtUtils::ModuleMaker> と L<Module::Starter> も始めるのによい場所です。
463 多くの CPAN 作者は今ではできるだけ自動化するために L<Dist::Zilla> を
464 使っています。
465
466 =begin original
467
468 Detailed documentation about modules can be found at:
469 L<perlmod>, L<perlmodlib>, L<perlmodstyle>.
470
471 =end original
472
473 モジュールに関する詳しい文書は以下にあります:
474 L<perlmod>, L<perlmodlib>, L<perlmodstyle>。
475
476 =begin original
477
478 If you need to include C code or C library interfaces
479 use h2xs. h2xs will create the module distribution structure
480 and the initial interface files.
481 L<perlxs> and L<perlxstut> explain the details.
482
483 =end original
484
485 C コードや C ライブラリインターフェースを含める必要があるなら、
486 h2xs を使ってください。
487 h2xs はモジュール配布構造と書記インターフェースファイルを作成します。
488 L<perlxs> と L<perlxstut> は詳細を説明しています。
489
490 =head2 How do I adopt or take over a module already on CPAN?
491
492 (すでに CPAN にあるモジュールを引き継ぐには?)
493
494 =begin original
495
496 Ask the current maintainer to make you a co-maintainer or
497 transfer the module to you.
498
499 =end original
500
501 共同メンテナにしてもらうか、モジュールを引き渡してもらうように現在の
502 メンテナに訊ねてください。
503
504 =begin original
505
506 If you can not reach the author for some reason contact
507 the PAUSE admins at modules@perl.org who may be able to help,
508 but each case it treated seperatly.
509
510 =end original
511
512 もし何らかの理由で作者と連絡が取れない場合、助けることができるかも知れない
513 PAUSE 管理者である modules@perl.org に連絡してください; 但しどのように
514 扱われるかは個々の場合によります。
515
516 =over 4
517
518 =item *
519
520 =begin original
521
522 Get a login for the Perl Authors Upload Server (PAUSE) if you don't
523 already have one: L<http://pause.perl.org>
524
525 =end original
526
527 まだ持っていないなら、the Perl Authors Upload Server (PAUSE) のアカウントを
528 取ります: L<http://pause.perl.org>
529
530 =item *
531
532 =begin original
533
534 Write to modules@perl.org explaining what you did to contact the
535 current maintainer. The PAUSE admins will also try to reach the
536 maintainer.
537
538 =end original
539
540 現在のメンテナに連絡するためにしたことの説明を modules@perl.org に書きます。
541 PAUSE 管理者もメンテナに連絡を試みます。
542
543 =item *
544
545 =begin original
546
547 Post a public message in a heavily trafficked site announcing your
548 intention to take over the module.
549
550 =end original
551
552 モジュールを引き継ぎたいという意思を表明するために、トラフィックの多い
553 サイトに公的なメッセージを投稿します。
554
555 =item *
556
557 =begin original
558
559 Wait a bit. The PAUSE admins don't want to act too quickly in case
560 the current maintainer is on holiday. If there's no response to
561 private communication or the public post, a PAUSE admin can transfer
562 it to you.
563
564 =end original
565
566 しばらく待ちます。
567 PAUSE 管理者は、現在のメンテナが休暇中の場合に、あまりに性急に行動したいとは
568 思いません。
569 もし私的な通信や公的な投稿に反応がない場合、PAUSE 管理者はモジュールを
570 あなたに移行できます。
571
572 =back
573
574 =head2 How do I create a class?
575 X<class, creation> X<package>
576
577 (クラスを作るには?)
578
579 =begin original
580
581 (contributed by brian d foy)
582
583 =end original
584
585 (brian d foy によって寄贈されました)
586
587 =begin original
588
589 In Perl, a class is just a package, and methods are just subroutines.
590 Perl doesn't get more formal than that and lets you set up the package
591 just the way that you like it (that is, it doesn't set up anything for
592 you).
593
594 =end original
595
596 Perl では、クラスは単なるパッケージで、メソッドは単なるサブルーチンです。
597 Perl はそれ以上に形式的なことはしませんし、あなたの好きな方法でパッケージを
598 設定できるようにします (つまり、Perl はあなたのために何の設定もしません)。
599
600 =begin original
601
602 The Perl documentation has several tutorials that cover class
603 creation, including L<perlboot> (Barnyard Object Oriented Tutorial),
604 L<perltoot> (Tom's Object Oriented Tutorial), L<perlbot> (Bag o'
605 Object Tricks), and L<perlobj>.
606
607 =end original
608
609 Perl の文書には、
610 L<perlboot> (Barnyard Object Oriented Tutorial),
611 L<perltoot> (Tom's Object Oriented Tutorial), L<perlbot> (Bag o'
612 Object Tricks), L<perlobj> といった、クラス生成に対応する
613 いくつかのチュートリアルがあります。
614
615 =head2 How can I tell if a variable is tainted?
616
617 (変数が汚染されているかどうかを確かめるには?)
618
619 =begin original
620
621 You can use the tainted() function of the Scalar::Util module, available
622 from CPAN (or included with Perl since release 5.8.0).
623 See also L<perlsec/"Laundering and Detecting Tainted Data">.
624
625 =end original
626
627 CPAN にある (リリース 5.8.0 からは Perl に含まれている) Scalar::Util
628 モジュールの tainted() 関数が使えます。
629 L<perlsec/"Laundering and Detecting Tainted Data"> も参照してください。
630
631 =head2 What's a closure?
632
633 (クロージャ(closure)ってなんですか?)
634
635 =begin original
636
637 Closures are documented in L<perlref>.
638
639 =end original
640
641 クロージャは L<perlref> に説明があります。
642
643 =begin original
644
645 I<Closure> is a computer science term with a precise but
646 hard-to-explain meaning. Usually, closures are implemented in Perl as
647 anonymous subroutines with lasting references to lexical variables
648 outside their own scopes. These lexicals magically refer to the
649 variables that were around when the subroutine was defined (deep
650 binding).
651
652 =end original
653
654 B<クロージャ> は、きちんとした定義を持ったコンピュータ科学の用語ですが
655 その意味を説明するのはとても難しいのです。
656 クロージャは Perl では、そのスコープの外側でもレキシカル変数に対する
657 リファレンスを保持しつづける無名サブルーチンとして実装されています。
658 これらのレキシカルは、サブルーチンが定義されたときの変数に対して、
659 魔法のような参照(magically refer)を行います(深い束縛、deep binding)。
660
661 =begin original
662
663 Closures are most often used in programming languages where you can
664 have the return value of a function be itself a function, as you can
665 in Perl. Note that some languages provide anonymous functions but are
666 not capable of providing proper closures: the Python language, for
667 example. For more information on closures, check out any textbook on
668 functional programming. Scheme is a language that not only supports
669 but encourages closures.
670
671 =end original
672
673 クロージャは、Perl ができるような関数の戻り値として関数それ自身を返す関数を
674 持つことができるプログラミング言語でもっともよく使われます。
675 一部の言語では、無名関数を提供しているけれども適切なクロージャを提供する
676 能力はないということに注意してください: たとえば Python がそうです。
677 クロージャに関するより詳しいことは、関数言語に関するなんらかの教科書を
678 みてください。
679 Scheme はクロージャをサポートするだけでなく、それを推奨している言語です。
680
681 =begin original
682
683 Here's a classic non-closure function-generating function:
684
685 =end original
686
687 以下は、古典的な、クロージャではない関数を生成する関数です:
688
689 sub add_function_generator {
690 return sub { shift() + shift() };
691 }
692
693 my $add_sub = add_function_generator();
694 my $sum = $add_sub->(4,5); # $sum is 9 now.
695
696 =begin original
697
698 The anonymous subroutine returned by add_function_generator() isn't
699 technically a closure because it refers to no lexicals outside its own
700 scope. Using a closure gives you a I<function template> with some
701 customization slots left out to be filled later.
702
703 =end original
704
705 add_function_generator() が返した無名サブルーチンは技術的には
706 クロージャではありません; なぜなら、あれはスコープの外側で
707 参照するようなレキシカルがないからです。
708 クロージャを使うことによって、後で埋めることのできるカスタマイズ可能な
709 幾つかのスロットを持つ I<関数テンプレート> のように働きます。
710
711 =begin original
712
713 Contrast this with the following make_adder() function, in which the
714 returned anonymous function contains a reference to a lexical variable
715 outside the scope of that function itself. Such a reference requires
716 that Perl return a proper closure, thus locking in for all time the
717 value that the lexical had when the function was created.
718
719 =end original
720
721 それとは対照的に、次の関数 make_adder() では、関数自身のスコープの外側で
722 レキシカル変数に対するリファレンスを持つ無名関数を返します。
723 そのようなリファレンスは Perl が適切なクロージャを返すように要求するので、
724 その変数を参照するときはいつでも関数が生成されたときの
725 レキシカルが参照されます。
726
727 sub make_adder {
728 my $addpiece = shift;
729 return sub { shift() + $addpiece };
730 }
731
732 my $f1 = make_adder(20);
733 my $f2 = make_adder(555);
734
735 =begin original
736
737 Now C<< $f1->($n) >> is always 20 plus whatever $n you pass in, whereas
738 C<< $f2->($n) >> is always 555 plus whatever $n you pass in. The $addpiece
739 in the closure sticks around.
740
741 =end original
742
743 これで、C<< $f1->($n) >> は それに渡した $n に対して常に 20 を加え、
744 C<< $f2->($n) >> は渡された $n に常に 555 を加えます。
745 クロージャの中にある $addpiece が仕事をしています。
746
747 =begin original
748
749 Closures are often used for less esoteric purposes. For example, when
750 you want to pass in a bit of code into a function:
751
752 =end original
753
754 クロージャは、それほど難しくない状況でよく使われます。
755 たとえば、関数にちょっとしたコードを押しこみたいときがそうです:
756
757 my $line;
758 timeout( 30, sub { $line = <STDIN> } );
759
760 =begin original
761
762 If the code to execute had been passed in as a string,
763 C<< '$line = <STDIN>' >>, there would have been no way for the
764 hypothetical timeout() function to access the lexical variable
765 $line back in its caller's scope.
766
767 =end original
768
769 もし実行すべきコードが文字列として渡されていたのであれば、
770 C<< '$line = <STDIN>' >> としているところは、仮想的な timeout() 関数が
771 アクセスするレキシカル変数 $line を呼び出し元のスコープのものに戻す
772 手段がなくなってしまいます。
773
774 =begin original
775
776 Another use for a closure is to make a variable I<private> to a
777 named subroutine, e.g. a counter that gets initialized at creation
778 time of the sub and can only be modified from within the sub.
779 This is sometimes used with a BEGIN block in package files to make
780 sure a variable doesn't get meddled with during the lifetime of the
781 package:
782
783 =end original
784
785 もう一つのクロージャの使用法は、ある変数を名前つきサブルーチンで
786 I<プライベート> にすることです; 例えば、サブルーチンの作成時に初期化され、
787 サブルーチン内でのみ変更可能なカウンタです。
788 これは、パッケージの生存期間中に変数が干渉されることがないように、とき毒
789 パッケージファイルの BEGIN ブロックで使われます:
790
791 BEGIN {
792 my $id = 0;
793 sub next_id { ++$id }
794 }
795
796 =begin original
797
798 This is discussed in more detail in L<perlsub>; see the entry on
799 I<Persistent Private Variables>.
800
801 =end original
802
803 これは L<perlsub> でより詳しく議論されています;
804 I<Persistent Private Variables> のエントリを参照してください。
805
806 =head2 What is variable suicide and how can I prevent it?
807
808 (変数の自殺(variable suicide)って何で、それをどうすれば防げますか?)
809
810 =begin original
811
812 This problem was fixed in perl 5.004_05, so preventing it means upgrading
813 your version of perl. ;)
814
815 =end original
816
817 この問題は 5.004_05 で修正されたので、防ぐためには perl を
818 バージョンアップします。 ;)
819
820 =begin original
821
822 Variable suicide is when you (temporarily or permanently) lose the value
823 of a variable. It is caused by scoping through my() and local()
824 interacting with either closures or aliased foreach() iterator variables
825 and subroutine arguments. It used to be easy to inadvertently lose a
826 variable's value this way, but now it's much harder. Take this code:
827
828 =end original
829
830 変数の自殺とは、(一時的にしろ、恒久的にしろ)変数の値を失ったときのことを
831 指します。
832 これは、クロージャ、もしくは別名つけされた foreach イテレータ変数や
833 サブルーチンの引数と相互作用している my() や local() を通した
834 スコープによって引き起こされます。
835 以前はこのやり方で変数の値をうっかりとなくしてしまうように
836 使われがちでしたが、現在は非常に難しくなっています。
837 以下のコードを考えてみましょう:
838
839 my $f = 'foo';
840 sub T {
841 while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }
842 }
843
844 T;
845 print "Finally $f\n";
846
847 =begin original
848
849 If you are experiencing variable suicide, that C<my $f> in the subroutine
850 doesn't pick up a fresh copy of the C<$f> whose value is C<'foo'>. The
851 output shows that inside the subroutine the value of C<$f> leaks through
852 when it shouldn't, as in this output:
853
854 =end original
855
856 もし変数の自殺に遭遇したら、サブルーチン内の C<my $f> は、値が
857 C<'foo'> である C<$f> の最新のコピーをではありません。
858 この出力は、漏れてはいけないサブルーチン内の C<$f> の値が漏れていることを
859 示し、以下のようになります:
860
861 foobar
862 foobarbar
863 foobarbarbar
864 Finally foo
865
866 =begin original
867
868 The $f that has "bar" added to it three times should be a new C<$f>
869 C<my $f> should create a new lexical variable each time through the loop.
870 The expected output is:
871
872 =end original
873
874 "bar" を保持している $f は三回 new C<$f> されるべきものです (C<my $f> は、
875 ループが通る度に新たなレキシカル変数を生成すべきなのです)。
876 予想される出力は以下のものです:
877
878 foobar
879 foobar
880 foobar
881 Finally foo
882
883 =head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?
884
885 ({関数, ファイルハンドル, 配列, ハッシュ, メソッド, 正規表現} を渡したり返したりするには?)
886
887 =begin original
888
889 You need to pass references to these objects. See L<perlsub/"Pass by
890 Reference"> for this particular question, and L<perlref> for
891 information on references.
892
893 =end original
894
895 これらのオブジェクトのリファレンスを渡す必要があります。
896 L<perlsub/"Pass by Reference"> にある関連した質問と、
897 L<perlref> にあるリファレンスに関する情報を参照してください。
898
899 =over 4
900
901 =item Passing Variables and Functions
902
903 (変数や関数を渡す)
904
905 =begin original
906
907 Regular variables and functions are quite easy to pass: just pass in a
908 reference to an existing or anonymous variable or function:
909
910 =end original
911
912 普通の変数や関数はとても簡単に渡せます: 既に存在している変数や関数に対する
913 リファレンスか、無名変数や無名関数に対するリファレンスを渡せばよいのです。
914
915 func( \$some_scalar );
916
917 func( \@some_array );
918 func( [ 1 .. 10 ] );
919
920 func( \%some_hash );
921 func( { this => 10, that => 20 } );
922
923 func( \&some_func );
924 func( sub { $_[0] ** $_[1] } );
925
926 =item Passing Filehandles
927
928 =begin original
929
930 As of Perl 5.6, you can represent filehandles with scalar variables
931 which you treat as any other scalar.
932
933 =end original
934
935 Perl 5.6 から、他のスカラと同様にファイルハンドルもスカラ変数で扱えます。
936
937 open my $fh, $filename or die "Cannot open $filename! $!";
938 func( $fh );
939
940 sub func {
941 my $passed_fh = shift;
942
943 my $line = <$passed_fh>;
944 }
945
946 =begin original
947
948 Before Perl 5.6, you had to use the C<*FH> or C<\*FH> notations.
949 These are "typeglobs"--see L<perldata/"Typeglobs and Filehandles">
950 and especially L<perlsub/"Pass by Reference"> for more information.
951
952 =end original
953
954 Perl 5.6 より前では、C<*FH> や C<\*FH> といった記法を使う必要があります。
955 これらは「型グロブ」(typeglob)です;
956 L<perldata/"Typeglobs and Filehandles">) と
957 L<perlsub/"Pass by Reference"> に詳しい説明があります。
958
959 =item Passing Regexes
960
961 (正規表現を渡す)
962
963 =begin original
964
965 Here's an example of how to pass in a string and a regular expression
966 for it to match against. You construct the pattern with the C<qr//>
967 operator:
968
969 =end original
970
971 以下の例は、文字列とマッチングする正規表現を渡す方法です。
972 パターンを C<qr//> 演算子で構築します:
973
974 sub compare {
975 my ($val1, $regex) = @_;
976 my $retval = $val1 =~ /$regex/;
977 return $retval;
978 }
979 $match = compare("old McDonald", qr/d.*D/i);
980
981 =item Passing Methods
982
983 (メソッドを渡す)
984
985 =begin original
986
987 To pass an object method into a subroutine, you can do this:
988
989 =end original
990
991 サブルーチンにオブジェクトメソッドを渡すには、以下のようにできます:
992
993 call_a_lot(10, $some_obj, "methname")
994 sub call_a_lot {
995 my ($count, $widget, $trick) = @_;
996 for (my $i = 0; $i < $count; $i++) {
997 $widget->$trick();
998 }
999 }
1000
1001 =begin original
1002
1003 Or, you can use a closure to bundle up the object, its
1004 method call, and arguments:
1005
1006 =end original
1007
1008 オブジェクトとそのメソッド呼び出しと引数とをまとめるために
1009 クロージャを使うこともできます:
1010
1011 my $whatnot = sub { $some_obj->obfuscate(@args) };
1012 func($whatnot);
1013 sub func {
1014 my $code = shift;
1015 &$code();
1016 }
1017
1018 =begin original
1019
1020 You could also investigate the can() method in the UNIVERSAL class
1021 (part of the standard perl distribution).
1022
1023 =end original
1024
1025 UNIVERSAL クラスにある can() メソッドを試すこともできます
1026 (これは標準の perl 配布キットの一部です)。
1027
1028 =back
1029
1030 =head2 How do I create a static variable?
1031
1032 (静的変数を作るには?)
1033
1034 =begin original
1035
1036 (contributed by brian d foy)
1037
1038 =end original
1039
1040 (brian d foy によって寄贈されました)
1041
1042 =begin original
1043
1044 In Perl 5.10, declare the variable with C<state>. The C<state>
1045 declaration creates the lexical variable that persists between calls
1046 to the subroutine:
1047
1048 =end original
1049
1050 Perl 5.10 では、C<state> で変数を宣言します。
1051 C<state> 宣言は、サブルーチンの呼び出しの間で永続するレキシカル変数を
1052 作成します:
1053
1054 sub counter { state $count = 1; $count++ }
1055
1056 =begin original
1057
1058 You can fake a static variable by using a lexical variable which goes
1059 out of scope. In this example, you define the subroutine C<counter>, and
1060 it uses the lexical variable C<$count>. Since you wrap this in a BEGIN
1061 block, C<$count> is defined at compile-time, but also goes out of
1062 scope at the end of the BEGIN block. The BEGIN block also ensures that
1063 the subroutine and the value it uses is defined at compile-time so the
1064 subroutine is ready to use just like any other subroutine, and you can
1065 put this code in the same place as other subroutines in the program
1066 text (i.e. at the end of the code, typically). The subroutine
1067 C<counter> still has a reference to the data, and is the only way you
1068 can access the value (and each time you do, you increment the value).
1069 The data in chunk of memory defined by C<$count> is private to
1070 C<counter>.
1071
1072 =end original
1073
1074 スコープ外でレキシカル変数を使うことでスタティック変数を
1075 でっち上げることができます。
1076 この例では、サブルーチン C<counter> を定義し、そこでレキシカル変数
1077 C<$count> を使います。
1078 これを BEGIN ブロックで囲っているので、C<$count> はコンパイル時に
1079 定義されますが、BEGIN ブロックの終わりでスコープから外れます。
1080 BEGIN ブロックはまた、サブルーチンとそこで使われている値はコンパイル時に
1081 定義されるので、このサブルーチンはその他のどのサブルーチンからも
1082 使える準備が出来ていて、このコードをプログラムテキスト中の他のサブルーチンと
1083 同様に同じ場所(典型的には、コードの最後)にこのコードを置けることを保証します。
1084 サブルーチン C<counter> は未だにデータへのリファレンスを持っていて、
1085 これが値にアクセスできる唯一の方法です(そしてそうする度に、値が
1086 インクリメントされます)。
1087 C<$count> で定義されたメモリの塊にあるデータは C<counter> に
1088 プライベートです。
1089
1090 BEGIN {
1091 my $count = 1;
1092 sub counter { $count++ }
1093 }
1094
1095 my $start = counter();
1096
1097 .... # code that calls counter();
1098
1099 my $end = counter();
1100
1101 =begin original
1102
1103 In the previous example, you created a function-private variable
1104 because only one function remembered its reference. You could define
1105 multiple functions while the variable is in scope, and each function
1106 can share the "private" variable. It's not really "static" because you
1107 can access it outside the function while the lexical variable is in
1108 scope, and even create references to it. In this example,
1109 C<increment_count> and C<return_count> share the variable. One
1110 function adds to the value and the other simply returns the value.
1111 They can both access C<$count>, and since it has gone out of scope,
1112 there is no other way to access it.
1113
1114 =end original
1115
1116 前述の例では、関数でプライベートな変数を作っています;
1117 ただ一つの関数だけがこのリファレンスを覚えているからです。
1118 変数がスコープ内にある間に複数の関数を定義でき、各関数は「プライベート」
1119 変数を共有できます。
1120 レキシカル変数がスコープ内ではあるけれども関数の外側から変数にアクセスでき、
1121 それに対するリファレンスすら作成できるため、これは実際には
1122 「静的」変数ではありません。
1123 この例では、C<increment_count> と C<return_count> は変数を共有しています。
1124 片方の関数が値を加え、もう片方は単に値を返します。
1125 これらは両方とも C<$count> にアクセスでき、スコープ外から離れるため、
1126 これにアクセスするその他の方法はありません。
1127
1128 BEGIN {
1129 my $count = 1;
1130 sub increment_count { $count++ }
1131 sub return_count { $count }
1132 }
1133
1134 =begin original
1135
1136 To declare a file-private variable, you still use a lexical variable.
1137 A file is also a scope, so a lexical variable defined in the file
1138 cannot be seen from any other file.
1139
1140 =end original
1141
1142 ファイルプライベートな変数を宣言するには、やはりレキシカル変数が使えます。
1143 ファイルもスコープとなるので、あるファイルで定義されたレキシカル変数は
1144 その他のファイルからは見えません。
1145
1146 =begin original
1147
1148 See L<perlsub/"Persistent Private Variables"> for more information.
1149 The discussion of closures in L<perlref> may help you even though we
1150 did not use anonymous subroutines in this answer. See
1151 L<perlsub/"Persistent Private Variables"> for details.
1152
1153 =end original
1154
1155 さらなる情報については L<perlsub/"Persistent Private Variables"> を
1156 参照してください。
1157 L<perlref> でのクロージャに関する議論は、この答えには無名サブルーチンは
1158 使っていないですが、助けになるかもしれません。
1159 詳細については L<perlsub/"Persistent Private Variables"> を参照してください。
1160
1161 =head2 What's the difference between dynamic and lexical (static) scoping? Between local() and my()?
1162
1163 (動的スコープとレキシカル(または静的)スコープとの間の違いは? local() と my() との違いは?)
1164
1165 =begin original
1166
1167 C<local($x)> saves away the old value of the global variable C<$x>
1168 and assigns a new value for the duration of the subroutine I<which is
1169 visible in other functions called from that subroutine>. This is done
1170 at run-time, so is called dynamic scoping. local() always affects global
1171 variables, also called package variables or dynamic variables.
1172
1173 =end original
1174
1175 C<local($x)> は、グローバル変数 C<$x> の古い値を保存して、
1176 I<そのサブルーチンから呼び出された関数から参照できる> サブルーチンの
1177 存在する間の新たな値を代入します。
1178 これはコンパイル時ではなく、実行時に行われるので、動的スコープと呼ばれます。
1179 local() は常にグローバル変数に対して作用し、
1180 パッケージ変数とか動的変数と呼ばれることもあります。
1181
1182 =begin original
1183
1184 C<my($x)> creates a new variable that is only visible in the current
1185 subroutine. This is done at compile-time, so it is called lexical or
1186 static scoping. my() always affects private variables, also called
1187 lexical variables or (improperly) static(ly scoped) variables.
1188
1189 =end original
1190
1191 C<my($x)> は、現在のサブルーチンにおいてのみ参照できる変数を新たに
1192 生成します。
1193 これはコンパイル時に行われるので、レキシカルスコープだとか
1194 静的スコープと呼ばれます。
1195 my() はプライベート変数に作用し、レキシカル変数とか、(間違って)静的
1196 (スコープを持った)変数と呼ばれることもあります。
1197
1198 =begin original
1199
1200 For instance:
1201
1202 =end original
1203
1204 例を挙げましょう:
1205
1206 sub visible {
1207 print "var has value $var\n";
1208 }
1209
1210 sub dynamic {
1211 local $var = 'local'; # new temporary value for the still-global
1212 visible(); # variable called $var
1213 }
1214
1215 sub lexical {
1216 my $var = 'private'; # new private variable, $var
1217 visible(); # (invisible outside of sub scope)
1218 }
1219
1220 $var = 'global';
1221
1222 visible(); # prints global
1223 dynamic(); # prints local
1224 lexical(); # prints global
1225
1226 =begin original
1227
1228 Notice how at no point does the value "private" get printed. That's
1229 because $var only has that value within the block of the lexical()
1230 function, and it is hidden from the called subroutine.
1231
1232 =end original
1233
1234 "private"という値を出力させる方法がないということに注目してください。
1235 これは、$var が関数の lexical() なブロックの内側でのみその値を持っていて、
1236 そこから呼び出されたサブルーチンからは隠されてしまうからです。
1237
1238 =begin original
1239
1240 In summary, local() doesn't make what you think of as private, local
1241 variables. It gives a global variable a temporary value. my() is
1242 what you're looking for if you want private variables.
1243
1244 =end original
1245
1246 まとめると、local() はあなたがプライベートと考えるようなことをローカルな
1247 変数に対して行うようなことはありません。
1248 これはグローバル変数に一時的な値を与えるだけです。
1249 あなたがプライベート変数を必要としている場合、my() があなたが
1250 探し求めているものです。
1251
1252 =begin original
1253
1254 See L<perlsub/"Private Variables via my()"> and
1255 L<perlsub/"Temporary Values via local()"> for excruciating details.
1256
1257 =end original
1258
1259 このことをより詳しく説明している L<perlsub/"Private Variables via my()"> と
1260 L<perlsub/"Temporary Values via local()"> を参照してください。
1261
1262 =head2 How can I access a dynamic variable while a similarly named lexical is in scope?
1263
1264 (どうやれば同じ名前のレキシカル変数がスコープにあるときに動的変数にアクセスできますか?)
1265
1266 =begin original
1267
1268 If you know your package, you can just mention it explicitly, as in
1269 $Some_Pack::var. Note that the notation $::var is B<not> the dynamic $var
1270 in the current package, but rather the one in the "main" package, as
1271 though you had written $main::var.
1272
1273 =end original
1274
1275 自分のパッケージを知っているのなら、これを $Some_Pack::var と明示的に
1276 記述できます。
1277 $::var という記法はカレントパッケージにおける動的な $var ではなく、
1278 "main" パッケージの中にあるそれなのだということに注意してください。
1279
1280 use vars '$var';
1281 local $var = "global";
1282 my $var = "lexical";
1283
1284 print "lexical is $var\n";
1285 print "global is $main::var\n";
1286
1287 =begin original
1288
1289 Alternatively you can use the compiler directive our() to bring a
1290 dynamic variable into the current lexical scope.
1291
1292 =end original
1293
1294 あるいは、動的変数を現在のレキシカルスコープにもたらすために、コンパイラ
1295 指示子 our() を使えます。
1296
1297 require 5.006; # our() did not exist before 5.6
1298 use vars '$var';
1299
1300 local $var = "global";
1301 my $var = "lexical";
1302
1303 print "lexical is $var\n";
1304
1305 {
1306 our $var;
1307 print "global is $var\n";
1308 }
1309
1310 =head2 What's the difference between deep and shallow binding?
1311
1312 (深い束縛(deep binding)と浅い束縛(shallow binding)との間の違いとは?)
1313
1314 =begin original
1315
1316 In deep binding, lexical variables mentioned in anonymous subroutines
1317 are the same ones that were in scope when the subroutine was created.
1318 In shallow binding, they are whichever variables with the same names
1319 happen to be in scope when the subroutine is called. Perl always uses
1320 deep binding of lexical variables (i.e., those created with my()).
1321 However, dynamic variables (aka global, local, or package variables)
1322 are effectively shallowly bound. Consider this just one more reason
1323 not to use them. See the answer to L<"What's a closure?">.
1324
1325 =end original
1326
1327 深い束縛では、無名サブルーチンに置かれているレキシカル変数は
1328 そのサブルーチンが生成されたときのものと同一のものになります。
1329 浅い束縛では、これはそのサブルーチンが呼び出されたときのスコープに
1330 存在している同じ名前を持った変数のどれか、になります。
1331 Perl はレキシカル変数(つまり、my() によって作られるもの)に対しては
1332 常に深い束縛を使います。
1333 それに対し動的変数(つまりグローバル変数か、ローカル変数か、
1334 パッケージ変数)では、浅い束縛がなされます。
1335 こういったものを使わない理由がもう一つあります。
1336 その答えは L<"What's a closure?"> を参照してください。
1337
1338 =head2 Why doesn't "my($foo) = E<lt>$fhE<gt>;" work right?
1339
1340 (なぜ "my($foo) = E<lt>$fhE<gt>;" が正しく動作しないのでしょうか?)
1341
1342 =begin original
1343
1344 C<my()> and C<local()> give list context to the right hand side
1345 of C<=>. The <$fh> read operation, like so many of Perl's
1346 functions and operators, can tell which context it was called in and
1347 behaves appropriately. In general, the scalar() function can help.
1348 This function does nothing to the data itself (contrary to popular myth)
1349 but rather tells its argument to behave in whatever its scalar fashion is.
1350 If that function doesn't have a defined scalar behavior, this of course
1351 doesn't help you (such as with sort()).
1352
1353 =end original
1354
1355 C<my()> と C<local()> は C<=> の右辺に対してリストコンテキストを与えます。
1356 読み込み操作 <$fh> は Perlの関数や演算子の多くと同じくそれが呼び出されたときの
1357 コンテキストを見分けることができて、それに応じて適切に振る舞います。
1358 一般的には、scalar() 関数が助けになるでしょう。
1359 その関数は(一般的な神話とは反して)引数となるデータに対して
1360 何も行いませんが、引数がスカラとして振る舞うように指示します。
1361 関数のスカラ時の振る舞いが定義されていないのであれば、
1362 当然ながらこれはあなたの助けにはなりません(sort() がそうです)。
1363
1364 =begin original
1365
1366 To enforce scalar context in this particular case, however, you need
1367 merely omit the parentheses:
1368
1369 =end original
1370
1371 しかしながら特定のケースにおいては、スカラコンテキストを強制するために
1372 単に括弧を取り除く必要があります:
1373
1374 local($foo) = <$fh>; # WRONG
1375 local($foo) = scalar(<$fh>); # ok
1376 local $foo = <$fh>; # right
1377
1378 =begin original
1379
1380 You should probably be using lexical variables anyway, although the
1381 issue is the same here:
1382
1383 =end original
1384
1385 これと同じ問題があるものの、なんにしろレキシカル変数を使うべきでしょう。
1386
1387 my($foo) = <$fh>; # WRONG
1388 my $foo = <$fh>; # right
1389
1390 =head2 How do I redefine a builtin function, operator, or method?
1391
1392 (組み込みの関数や演算子、メソッドを再定義するには?)
1393
1394 =begin original
1395
1396 Why do you want to do that? :-)
1397
1398 =end original
1399
1400 なんだってそんなことをしたがるのですか? :-)
1401
1402 =begin original
1403
1404 If you want to override a predefined function, such as open(),
1405 then you'll have to import the new definition from a different
1406 module. See L<perlsub/"Overriding Built-in Functions">.
1407
1408 =end original
1409
1410 open() のようなあらかじめ定義されている関数をオーバーライドしたいのであれば、
1411 異なるモジュールから新しい定義をインポートする必要があります。
1412 L<perlsub/"Overriding Built-in Functions"> を参照してください。
1413
1414 =begin original
1415
1416 If you want to overload a Perl operator, such as C<+> or C<**>,
1417 then you'll want to use the C<use overload> pragma, documented
1418 in L<overload>.
1419
1420 =end original
1421
1422 C<+> や C<**> のような Perl の演算子をオーバーロードしたいのであれば
1423 L<overload> で説明されているような C<use overload> プラグマを
1424 使いたくなるでしょう。
1425
1426 =begin original
1427
1428 If you're talking about obscuring method calls in parent classes,
1429 see L<perltoot/"Overridden Methods">.
1430
1431 =end original
1432
1433 親クラスにおける不明瞭なメソッド呼び出しについて考えているのなら、
1434 L<perltoot/"Overridden Methods"> を参照してください。
1435
1436 =head2 What's the difference between calling a function as &foo and foo()?
1437
1438 (関数呼び出しを &foo で行ったときと foo() で行ったときとの違いはなんですか?)
1439
1440 =begin original
1441
1442 (contributed by brian d foy)
1443
1444 =end original
1445
1446 (brian d foy によって寄贈されました)
1447
1448 =begin original
1449
1450 Calling a subroutine as C<&foo> with no trailing parentheses ignores
1451 the prototype of C<foo> and passes it the current value of the argument
1452 list, C<@_>. Here's an example; the C<bar> subroutine calls C<&foo>,
1453 which prints its arguments list:
1454
1455 =end original
1456
1457 C<&foo> の形で、引き続くかっこなしでサブルーチンを呼び出すと、
1458 C<foo> のプロトタイプを無視して、引数リスト C<@_> の現在の値を渡します。
1459 以下は例です: C<bar> サブルーチンは、引数リストを表示する C<&foo> を
1460 呼び出します:
1461
1462 sub bar { &foo }
1463
1464 sub foo { print "Args in foo are: @_\n" }
1465
1466 bar( qw( a b c ) );
1467
1468 =begin original
1469
1470 When you call C<bar> with arguments, you see that C<foo> got the same C<@_>:
1471
1472 =end original
1473
1474 C<bar> を引数付きで呼び出した場合、C<foo> も同じ C<@_> を得ます:
1475
1476 Args in foo are: a b c
1477
1478 =begin original
1479
1480 Calling the subroutine with trailing parentheses, with or without arguments,
1481 does not use the current C<@_> and respects the subroutine prototype. Changing
1482 the example to put parentheses after the call to C<foo> changes the program:
1483
1484 =end original
1485
1486 サブルーチンをかっこ付きで呼び出した場合、引数のありなしに関わらず、
1487 現在の C<@_> は使わずに、サブルーチンのプロトタイプに従います。
1488 C<foo> を呼び出すときにかっこをつけるように例を変更します:
1489
1490 sub bar { &foo() }
1491
1492 sub foo { print "Args in foo are: @_\n" }
1493
1494 bar( qw( a b c ) );
1495
1496 =begin original
1497
1498 Now the output shows that C<foo> doesn't get the C<@_> from its caller.
1499
1500 =end original
1501
1502 今度は、C<foo> が呼び出し元から C<@_> を得ていないことが分かります:
1503
1504 Args in foo are:
1505
1506 =begin original
1507
1508 The main use of the C<@_> pass-through feature is to write subroutines
1509 whose main job it is to call other subroutines for you. For further
1510 details, see L<perlsub>.
1511
1512 =end original
1513
1514 C<@_> パススルー機能の主な利用法は、他のサブルーチンを呼び出すのが
1515 主な仕事であるサブルーチンを書くためです。
1516 更なる詳細については、L<perlsub> を参照してください。
1517
1518 =head2 How do I create a switch or case statement?
1519
1520 (switch 文や case 文を作るには?)
1521
1522 =begin original
1523
1524 In Perl 5.10, use the C<given-when> construct described in L<perlsyn>:
1525
1526 =end original
1527
1528 Perl 5.10 では、L<perlsyn> に記述されている C<given-when> 構造を
1529 使ってください:
1530
1531 use 5.010;
1532
1533 given ( $string ) {
1534 when( 'Fred' ) { say "I found Fred!" }
1535 when( 'Barney' ) { say "I found Barney!" }
1536 when( /Bamm-?Bamm/ ) { say "I found Bamm-Bamm!" }
1537 default { say "I don't recognize the name!" }
1538 };
1539
1540 =begin original
1541
1542 If one wants to use pure Perl and to be compatible with Perl versions
1543 prior to 5.10, the general answer is to use C<if-elsif-else>:
1544
1545 =end original
1546
1547 もしピュア Perl を使って、バージョン 5.10 より前と互換性を持たせたいなら、
1548 構造文を書くための一般的な答えは C<if-elsif-else> を使うことです:
1549
1550 for ($variable_to_test) {
1551 if (/pat1/) { } # do something
1552 elsif (/pat2/) { } # do something else
1553 elsif (/pat3/) { } # do something else
1554 else { } # default
1555 }
1556
1557 =begin original
1558
1559 Here's a simple example of a switch based on pattern matching,
1560 lined up in a way to make it look more like a switch statement.
1561 We'll do a multiway conditional based on the type of reference stored
1562 in $whatchamacallit:
1563
1564 =end original
1565
1566 以下の例は、パターンマッチングに基づいた単純な switch の例です。
1567 $whatchamacallit に格納されたリファレンスの型に基づいて多様なやり方の
1568 条件判断を行っています:
1569
1570 SWITCH: for (ref $whatchamacallit) {
1571
1572 /^$/ && die "not a reference";
1573
1574 /SCALAR/ && do {
1575 print_scalar($$ref);
1576 last SWITCH;
1577 };
1578
1579 /ARRAY/ && do {
1580 print_array(@$ref);
1581 last SWITCH;
1582 };
1583
1584 /HASH/ && do {
1585 print_hash(%$ref);
1586 last SWITCH;
1587 };
1588
1589 /CODE/ && do {
1590 warn "can't print function ref";
1591 last SWITCH;
1592 };
1593
1594 # DEFAULT
1595
1596 warn "User defined type skipped";
1597
1598 }
1599
1600 =begin original
1601
1602 See L<perlsyn> for other examples in this style.
1603
1604 =end original
1605
1606 このスタイルに関するその他の例については L<perlsyn> を参照してください。
1607
1608 =begin original
1609
1610 Sometimes you should change the positions of the constant and the variable.
1611 For example, let's say you wanted to test which of many answers you were
1612 given, but in a case-insensitive way that also allows abbreviations.
1613 You can use the following technique if the strings all start with
1614 different characters or if you want to arrange the matches so that
1615 one takes precedence over another, as C<"SEND"> has precedence over
1616 C<"STOP"> here:
1617
1618 =end original
1619
1620 定数や変数の位置を変えた方が良いことがあるかもしれません。
1621 たとえば、与えられたたくさんの答についてテストを行いたいとしましょう;
1622 この場合大小文字を無視することもできますし、略記することもあります。
1623 もし全て文字列が異なるキャラクターで始まっていたり、
1624 C<"SEND"> が C<"STOP"> より高い優先順位を持つように調整したいのなら
1625 マッチの順序をアレンジしたいのであれば以下に示すようなテクニックを
1626 使うことができます。
1627
1628 chomp($answer = <>);
1629 if ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" }
1630 elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" }
1631 elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" }
1632 elsif ("LIST" =~ /^\Q$answer/i) { print "Action is list\n" }
1633 elsif ("EDIT" =~ /^\Q$answer/i) { print "Action is edit\n" }
1634
1635 =begin original
1636
1637 A totally different approach is to create a hash of function references.
1638
1639 =end original
1640
1641 まったく異なるアプローチに、関数のリファレンスのハッシュを作成するという
1642 やり方があります。
1643
1644 my %commands = (
1645 "happy" => \&joy,
1646 "sad", => \&sullen,
1647 "done" => sub { die "See ya!" },
1648 "mad" => \&angry,
1649 );
1650
1651 print "How are you? ";
1652 chomp($string = <STDIN>);
1653 if ($commands{$string}) {
1654 $commands{$string}->();
1655 } else {
1656 print "No such command: $string\n";
1657 }
1658
1659 =begin original
1660
1661 Starting from Perl 5.8, a source filter module, C<Switch>, can also be
1662 used to get switch and case. Its use is now discouraged, because it's
1663 not fully compatible with the native switch of Perl 5.10, and because,
1664 as it's implemented as a source filter, it doesn't always work as intended
1665 when complex syntax is involved.
1666
1667 =end original
1668
1669 Perl 5.8 から、ソースフィルタモジュール C<Switch> も switch と case を
1670 使うために使えます。
1671 Perl 5.10 には完全互換のネイティブな switch があり、また、これはソース
1672 フィルタとして実装されているので、複雑な文法の場合はいつも想定通りに
1673 動くとは限らないからです。
1674
1675 =head2 How can I catch accesses to undefined variables, functions, or methods?
1676
1677 (どうすれば未定義な変数, 関数, メソッドに対するアクセスを捕捉できますか?)
1678
1679 =begin original
1680
1681 The AUTOLOAD method, discussed in L<perlsub/"Autoloading"> and
1682 L<perltoot/"AUTOLOAD: Proxy Methods">, lets you capture calls to
1683 undefined functions and methods.
1684
1685 =end original
1686
1687 L<perlsub/"Autoloading"> と L<perltoot/"AUTOLOAD: Proxy Methods"> で
1688 言及されている AUTOLOAD メソッドは、未定義な関数やメソッドに対する
1689 呼び出しを捕捉させてくれます。
1690
1691 =begin original
1692
1693 When it comes to undefined variables that would trigger a warning
1694 under C<use warnings>, you can promote the warning to an error.
1695
1696 =end original
1697
1698 C<use warnings> が有効なときに警告の引き金になるような未定義変数への
1699 アクセスがあったとき、警告をエラーに昇格させることができます。
1700
1701 use warnings FATAL => qw(uninitialized);
1702
1703 =head2 Why can't a method included in this same file be found?
1704
1705 (なぜ同じファイルにあるメソッドが見つけられないのでしょうか?)
1706
1707 =begin original
1708
1709 Some possible reasons: your inheritance is getting confused, you've
1710 misspelled the method name, or the object is of the wrong type. Check
1711 out L<perltoot> for details about any of the above cases. You may
1712 also use C<print ref($object)> to find out the class C<$object> was
1713 blessed into.
1714
1715 =end original
1716
1717 幾つかの理由が考えられます: あなたが継承したものが混乱していているか、
1718 メソッド名を間違えたか、あるいはオブジェクトの型が間違っていたか。
1719 上記の場合に関する詳細は L<perltoot> をチェックしてみてください。
1720 C<$object> が bless されているクラスは C<print ref($object)> として
1721 見分けることができます。
1722
1723 =begin original
1724
1725 Another possible reason for problems is that you've used the
1726 indirect object syntax (eg, C<find Guru "Samy">) on a class name
1727 before Perl has seen that such a package exists. It's wisest to make
1728 sure your packages are all defined before you start using them, which
1729 will be taken care of if you use the C<use> statement instead of
1730 C<require>. If not, make sure to use arrow notation (eg.,
1731 C<< Guru->find("Samy") >>) instead. Object notation is explained in
1732 L<perlobj>.
1733
1734 =end original
1735
1736 もう一つありうる理由は、Perl がパッケージを見いだす前にクラス名を使った
1737 間接オブジェクト構文(C<find Guru "Samy"> のようなもの)を使ったためでしょう。
1738 パッケージは、それを使うよりも前に全てが定義されているようにします。
1739 これは C<require> 文ではなく C<use> 文を使えば考慮されます。
1740 あるいは、代わりに矢印記法(arrow notation、
1741 C<< Guru->find("Samy") >> のようなもの)を使うようにしてください。
1742 オブジェクトの記法は L<perlobj> で説明されています。
1743
1744 =begin original
1745
1746 Make sure to read about creating modules in L<perlmod> and
1747 the perils of indirect objects in L<perlobj/"Method Invocation">.
1748
1749 =end original
1750
1751 モジュールの作り方については L<perlmod> を、間接オブジェクトの問題点に
1752 ついては L<perlobj/"WARNING"> を確認してください。
1753
1754 =head2 How can I find out my current or calling package?
1755
1756 (カレントのパッケージや呼び出しパッケージはどうすればわかりますか?)
1757
1758 =begin original
1759
1760 (contributed by brian d foy)
1761
1762 =end original
1763
1764 (brian d foy によって寄贈されました)
1765
1766 =begin original
1767
1768 To find the package you are currently in, use the special literal
1769 C<__PACKAGE__>, as documented in L<perldata>. You can only use the
1770 special literals as separate tokens, so you can't interpolate them
1771 into strings like you can with variables:
1772
1773 =end original
1774
1775 今いるパッケージを知るには、L<perldata> に記述されている特殊リテラル
1776 C<__PACKAGE__> を使ってください。
1777 特殊リテラルは独立したトークンとしてのみ使えるので、変数のように
1778 文字列に展開できません:
1779
1780 my $current_package = __PACKAGE__;
1781 print "I am in package $current_package\n";
1782
1783 =begin original
1784
1785 If you want to find the package calling your code, perhaps to give better
1786 diagnostics as L<Carp> does, use the C<caller> built-in:
1787
1788 =end original
1789
1790 あるコードを呼び出したパッケージを知りたい場合 (おそらく L<Carp> のように
1791 よりよい診断メッセージのためでしょう) C<caller> 組み込み関数を
1792 使ってください:
1793
1794 sub foo {
1795 my @args = ...;
1796 my( $package, $filename, $line ) = caller;
1797
1798 print "I was called from package $package\n";
1799 );
1800
1801 =begin original
1802
1803 By default, your program starts in package C<main>, so you will
1804 always be in some package.
1805
1806 =end original
1807
1808 デフォルトでは、プログラムはパッケージ C<main> で開始されるので、
1809 いつもなんらかのパッケージ内にいるはずです。
1810
1811 =begin original
1812
1813 This is different from finding out the package an object is blessed
1814 into, which might not be the current package. For that, use C<blessed>
1815 from L<Scalar::Util>, part of the Standard Library since Perl 5.8:
1816
1817 =end original
1818
1819 これはオブジェクトが bless されているパッケージを知ることとは違います;
1820 これは現在のパッケージではないかもしれません。
1821 この目的のためには、Perl 5.8 から標準ライブラリの一部となっている
1822 L<Scalar::Util> の C<blessed> を使ってください:
1823
1824 use Scalar::Util qw(blessed);
1825 my $object_package = blessed( $object );
1826
1827 =begin original
1828
1829 Most of the time, you shouldn't care what package an object is blessed
1830 into, however, as long as it claims to inherit from that class:
1831
1832 =end original
1833
1834 しかし、ほとんどの場合では、オブジェクトがそのクラスから継承していると
1835 主張している限りは、どのパッケージが bless したかを
1836 気にするべきではありません:
1837
1838 my $is_right_class = eval { $object->isa( $package ) }; # true or false
1839
1840 =begin original
1841
1842 And, with Perl 5.10 and later, you don't have to check for an
1843 inheritance to see if the object can handle a role. For that, you can
1844 use C<DOES>, which comes from C<UNIVERSAL>:
1845
1846 =end original
1847
1848 また、Perl 5.10 以降では、オブジェクトがロールを扱うかどうかを見るために
1849 継承をチェックする必要はありません。
1850 このために、C<UNIVERSAL> にある C<DOES> を使えます:
1851
1852 my $class_does_it = eval { $object->DOES( $role ) }; # true or false
1853
1854 =begin original
1855
1856 You can safely replace C<isa> with C<DOES> (although the converse is not true).
1857
1858 =end original
1859
1860 安全に C<isa> を C<DOES> に置き換えられます (しかし逆は真ではありません)。
1861
1862 =head2 How can I comment out a large block of Perl code?
1863
1864 (Perl プログラムの大きなブロックをコメントアウトするには?)
1865
1866 =begin original
1867
1868 (contributed by brian d foy)
1869
1870 =end original
1871
1872 (brian d foy によって寄贈されました)
1873
1874 =begin original
1875
1876 The quick-and-dirty way to comment out more than one line of Perl is
1877 to surround those lines with Pod directives. You have to put these
1878 directives at the beginning of the line and somewhere where Perl
1879 expects a new statement (so not in the middle of statements like the C<#>
1880 comments). You end the comment with C<=cut>, ending the Pod section:
1881
1882 =end original
1883
1884 Perl の複数行をコメントアウトするための、汚いけれども簡単な方法は、
1885 コメントアウトしたい部分を Pod 指示子で囲むことです。
1886 これらの指示子は、行の先頭、かつ、Perl が新しい文が始まると
1887 想定する場所に置く必要があります(従って、C<#> コメントのように文の途中には
1888 置けません)。
1889 Pod セクションの終了を示す C<=cut> でコメントを終了します:
1890
1891 =pod
1892
1893 my $object = NotGonnaHappen->new();
1894
1895 ignored_sub();
1896
1897 $wont_be_assigned = 37;
1898
1899 =cut
1900
1901 =begin original
1902
1903 The quick-and-dirty method only works well when you don't plan to
1904 leave the commented code in the source. If a Pod parser comes along,
1905 your multiline comment is going to show up in the Pod translation.
1906 A better way hides it from Pod parsers as well.
1907
1908 =end original
1909
1910 汚いけれども簡単な方法は、ソースにコメントされたコードを残す予定が
1911 ない(つまりいずれ消す)場合にのみうまく働きます。
1912 もし Pod パーサを使うと、複数行コメントは Pod によって表示されます。
1913 よりよい方法は、Pod パーサからも隠すことです。
1914
1915 =begin original
1916
1917 The C<=begin> directive can mark a section for a particular purpose.
1918 If the Pod parser doesn't want to handle it, it just ignores it. Label
1919 the comments with C<comment>. End the comment using C<=end> with the
1920 same label. You still need the C<=cut> to go back to Perl code from
1921 the Pod comment:
1922
1923 =end original
1924
1925 C<=begin> 指示子で、段落を特定の目的のためにマークできます。
1926 Pod パーサがこれを扱えない場合、単に無視されます。
1927 コメントは C<comment> でラベル付けします。
1928 コメントの終了は同じラベルで C<=end> を使います。
1929 Pod コメントから Perl コードに戻るにはやはり C<=cut> が必要です:
1930
1931 =begin comment
1932
1933 my $object = NotGonnaHappen->new();
1934
1935 ignored_sub();
1936
1937 $wont_be_assigned = 37;
1938
1939 =end comment
1940
1941 =cut
1942
1943 =begin original
1944
1945 For more information on Pod, check out L<perlpod> and L<perlpodspec>.
1946
1947 =end original
1948
1949 Pod に関するさらなる情報については、L<perlpod> と L<perlpodspec> を
1950 調べてください。
1951
1952 =head2 How do I clear a package?
1953
1954 (パッケージをクリアするには?)
1955
1956 =begin original
1957
1958 Use this code, provided by Mark-Jason Dominus:
1959
1960 =end original
1961
1962 Mark-Jason Dominus による以下のコードを使います:
1963
1964 sub scrub_package {
1965 no strict 'refs';
1966 my $pack = shift;
1967 die "Shouldn't delete main package"
1968 if $pack eq "" || $pack eq "main";
1969 my $stash = *{$pack . '::'}{HASH};
1970 my $name;
1971 foreach $name (keys %$stash) {
1972 my $fullname = $pack . '::' . $name;
1973 # Get rid of everything with that name.
1974 undef $$fullname;
1975 undef @$fullname;
1976 undef %$fullname;
1977 undef &$fullname;
1978 undef *$fullname;
1979 }
1980 }
1981
1982 =begin original
1983
1984 Or, if you're using a recent release of Perl, you can
1985 just use the Symbol::delete_package() function instead.
1986
1987 =end original
1988
1989 あるいは、あなたが使っている Perl が最近のリリースのものであれば、
1990 単に Symbol::delete_package() という関数を代わりに使うことができます。
1991
1992 =head2 How can I use a variable as a variable name?
1993
1994 (変数を変数名として使うには?)
1995
1996 =begin original
1997
1998 Beginners often think they want to have a variable contain the name
1999 of a variable.
2000
2001 =end original
2002
2003 初心者はしばしば変数名が入った変数を使いたいと考えます。
2004
2005 $fred = 23;
2006 $varname = "fred";
2007 ++$$varname; # $fred now 24
2008
2009 =begin original
2010
2011 This works I<sometimes>, but it is a very bad idea for two reasons.
2012
2013 =end original
2014
2015 これは I<時には> 動作します; しかしこれは二つの理由により悪い考えです。
2016
2017 =begin original
2018
2019 The first reason is that this technique I<only works on global
2020 variables>. That means that if $fred is a lexical variable created
2021 with my() in the above example, the code wouldn't work at all: you'd
2022 accidentally access the global and skip right over the private lexical
2023 altogether. Global variables are bad because they can easily collide
2024 accidentally and in general make for non-scalable and confusing code.
2025
2026 =end original
2027
2028 一つ目の理由は、このテクニックは I<グローバル変数でのみ動作する> からです。
2029 つまり、もし上記の例において $fred が my() で作成されたレキシカル変数の
2030 場合、このコードは全く動作しません; プライベートなレキシカル変数を飛ばして、
2031 思いがけずグローバル変数にアクセスすることになります。
2032 グローバル変数は、簡単に衝突し、一般に拡張性がなく、混乱するコードを
2033 作ることになるので、よくないものです。
2034
2035 =begin original
2036
2037 Symbolic references are forbidden under the C<use strict> pragma.
2038 They are not true references and consequently are not reference-counted
2039 or garbage-collected.
2040
2041 =end original
2042
2043 シンボリックリファレンスは C<use strict> プラグマの元では禁止されます。
2044 これは真のリファレンスではないので、リファレンスカウントに含まれず、
2045 ガベージゴレクションもされません。
2046
2047 =begin original
2048
2049 The other reason why using a variable to hold the name of another
2050 variable is a bad idea is that the question often stems from a lack of
2051 understanding of Perl data structures, particularly hashes. By using
2052 symbolic references, you are just using the package's symbol-table hash
2053 (like C<%main::>) instead of a user-defined hash. The solution is to
2054 use your own hash or a real reference instead.
2055
2056 =end original
2057
2058 変数に他の変数の名前を記録するというのがよくない考えであるという
2059 別の理由としては、このような疑問はしばしば Perl のデータ構造、
2060 特にハッシュに関する理解の不足から発生するからです。
2061 シンボリックリファレンスを使うことによって、ユーザー定義のハッシュの代わりに
2062 パッケージのシンボルテーブルハッシュ(C<%main::> など)を使うことができます。
2063 解決法は、代わりに自分自身のハッシュや真のリファレンスを使うことです。
2064
2065 $USER_VARS{"fred"} = 23;
2066 my $varname = "fred";
2067 $USER_VARS{$varname}++; # not $$varname++
2068
2069 =begin original
2070
2071 There we're using the %USER_VARS hash instead of symbolic references.
2072 Sometimes this comes up in reading strings from the user with variable
2073 references and wanting to expand them to the values of your perl
2074 program's variables. This is also a bad idea because it conflates the
2075 program-addressable namespace and the user-addressable one. Instead of
2076 reading a string and expanding it to the actual contents of your program's
2077 own variables:
2078
2079 =end original
2080
2081 ここではシンボリックリファレンスの代わりに %USER_VARS ハッシュを
2082 使っています。
2083 時々これはユーザーから文字列を変数へのリファレンスとして読み込んで、
2084 それを perl プログラムの変数の値として拡張することがあります。
2085 これもよくない考えです; なぜなら、プログラムが指定する名前空間とユーザーが
2086 指定する名前空間を融合させることになるからです。
2087 以下のように文字列を読み込んであなたのプログラムの変数の実際の内容の
2088 ために拡張するのではなく:
2089
2090 $str = 'this has a $fred and $barney in it';
2091 $str =~ s/(\$\w+)/$1/eeg; # need double eval
2092
2093 =begin original
2094
2095 it would be better to keep a hash around like %USER_VARS and have
2096 variable references actually refer to entries in that hash:
2097
2098 =end original
2099
2100 %USER_VARS のようなハッシュを保存し、このハッシュのエントリを参照する
2101 変数リファレンスを持つよりよい方法です:
2102
2103 $str =~ s/\$(\w+)/$USER_VARS{$1}/g; # no /e here at all
2104
2105 =begin original
2106
2107 That's faster, cleaner, and safer than the previous approach. Of course,
2108 you don't need to use a dollar sign. You could use your own scheme to
2109 make it less confusing, like bracketed percent symbols, etc.
2110
2111 =end original
2112
2113 これは前述の手法よりも、より高速で、より明快で、より安全です。
2114 もちろん、ドル記号を使う必要はありません。
2115 パーセント記号で囲むなどのより混乱しにくい独自のスキームを使えます。
2116
2117 $str = 'this has a %fred% and %barney% in it';
2118 $str =~ s/%(\w+)%/$USER_VARS{$1}/g; # no /e here at all
2119
2120 =begin original
2121
2122 Another reason that folks sometimes think they want a variable to
2123 contain the name of a variable is that they don't know how to build
2124 proper data structures using hashes. For example, let's say they
2125 wanted two hashes in their program: %fred and %barney, and that they
2126 wanted to use another scalar variable to refer to those by name.
2127
2128 =end original
2129
2130 人々が時々変数名が入った変数を欲しがるもう一つの理由は、
2131 ハッシュを使った適切なデータ構造を構築する方法を知らないからです。
2132 例えば、プログラムで %fred と %barney が必要で、
2133 さらにこれらを名前で参照するスカラへ変数が必要だとします。
2134
2135 $name = "fred";
2136 $$name{WIFE} = "wilma"; # set %fred
2137
2138 $name = "barney";
2139 $$name{WIFE} = "betty"; # set %barney
2140
2141 =begin original
2142
2143 This is still a symbolic reference, and is still saddled with the
2144 problems enumerated above. It would be far better to write:
2145
2146 =end original
2147
2148 これはやはりシンボリックリファレンスで、やはり上記の問題を抱えたままです。
2149 以下のように書けば遥かに改善します:
2150
2151 $folks{"fred"}{WIFE} = "wilma";
2152 $folks{"barney"}{WIFE} = "betty";
2153
2154 =begin original
2155
2156 And just use a multilevel hash to start with.
2157
2158 =end original
2159
2160 そして始めるのに単に多段ハッシュを使います。
2161
2162 =begin original
2163
2164 The only times that you absolutely I<must> use symbolic references are
2165 when you really must refer to the symbol table. This may be because it's
2166 something that one can't take a real reference to, such as a format name.
2167 Doing so may also be important for method calls, since these always go
2168 through the symbol table for resolution.
2169
2170 =end original
2171
2172 唯一あなたが完全にシンボリックリファレンスを I<使わなければならない> 場合は、
2173 シンボルテーブルに対するリファレンスが必要なときだけです。
2174 これは、フォーマット名といったものに対する真のリファレンスを得ることが
2175 できないからです。
2176 そうすることはメソッド呼び出しのためにも重要です; なぜなら名前解決のために
2177 シンボルテーブルを使うからです。
2178
2179 =begin original
2180
2181 In those cases, you would turn off C<strict 'refs'> temporarily so you
2182 can play around with the symbol table. For example:
2183
2184 =end original
2185
2186 これらの場合、一時的に C<strict 'refs'> にしてシンボルテーブルを
2187 使うようにできます。
2188 例えば:
2189
2190 @colors = qw(red blue green yellow orange purple violet);
2191 for my $name (@colors) {
2192 no strict 'refs'; # renege for the block
2193 *$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
2194 }
2195
2196 =begin original
2197
2198 All those functions (red(), blue(), green(), etc.) appear to be separate,
2199 but the real code in the closure actually was compiled only once.
2200
2201 =end original
2202
2203 これら(red(), blue(), green() など)の関数全ては別々に現れますが、
2204 クロージャの中の実際のコードは一回だけコンパイルされます。
2205
2206 =begin original
2207
2208 So, sometimes you might want to use symbolic references to manipulate
2209 the symbol table directly. This doesn't matter for formats, handles, and
2210 subroutines, because they are always global--you can't use my() on them.
2211 For scalars, arrays, and hashes, though--and usually for subroutines--
2212 you probably only want to use hard references.
2213
2214 =end original
2215
2216 シンボルテーブルを直接操作するためにシンボリックリファレンスを
2217 使いたくなることがあるかもしれません。
2218 これには、フォーマット、ハンドル、サブルーチンには関係ありません; これらは
2219 常にグローバルだからです--これらに my() を使うことはできません。
2220 おそらく、スカラ、配列、ハッシュのために--そして普通はサブルーチンの
2221 ために--だけ、ハードリファレンスが必要でしょう。
2222
2223 =head2 What does "bad interpreter" mean?
2224
2225 ("bad interpreter" とはどういう意味ですか?)
2226
2227 =begin original
2228
2229 (contributed by brian d foy)
2230
2231 =end original
2232
2233 (brian d foy によって寄贈されました)
2234
2235 =begin original
2236
2237 The "bad interpreter" message comes from the shell, not perl. The
2238 actual message may vary depending on your platform, shell, and locale
2239 settings.
2240
2241 =end original
2242
2243 "bad interpreter" というメッセージは perl ではなく、シェルが出力しています。
2244 実際のメッセージはプラットフォーム、シェル、ロケール設定によって
2245 様々です。
2246
2247 =begin original
2248
2249 If you see "bad interpreter - no such file or directory", the first
2250 line in your perl script (the "shebang" line) does not contain the
2251 right path to perl (or any other program capable of running scripts).
2252 Sometimes this happens when you move the script from one machine to
2253 another and each machine has a different path to perl--/usr/bin/perl
2254 versus /usr/local/bin/perl for instance. It may also indicate
2255 that the source machine has CRLF line terminators and the
2256 destination machine has LF only: the shell tries to find
2257 /usr/bin/perl<CR>, but can't.
2258
2259 =end original
2260
2261 "bad interpreter - no such file or directory" と表示されたら、perl
2262 スクリプトの最初の行 ("#!" 行) に perl (あるいはスクリプトを実行する
2263 機能のあるその他のプログラム) への正しいパスが含まれていません。
2264 スクリプトをあるマシンから、perl のパスが異なる -- 例えば /usr/bin/perl と
2265 /usr/local/bin/perl -- 他のマシンに移動させた場合に時々起こります。
2266 これはまた、元のマシンの行終端が CRLF で、移動先のマシンが LF のみの場合にも
2267 起こります; シェルが /usr/bin/perl<CR> を探そうとしますが、失敗します。
2268
2269 =begin original
2270
2271 If you see "bad interpreter: Permission denied", you need to make your
2272 script executable.
2273
2274 =end original
2275
2276 "bad interpreter: Permission denied" と表示されたら、スクリプトを
2277 実行可能にする必要があります。
2278
2279 =begin original
2280
2281 In either case, you should still be able to run the scripts with perl
2282 explicitly:
2283
2284 =end original
2285
2286 どちらの場合でも、明示的に perl でスクリプトを実行できるようにするべきです:
2287
2288 % perl script.pl
2289
2290 =begin original
2291
2292 If you get a message like "perl: command not found", perl is not in
2293 your PATH, which might also mean that the location of perl is not
2294 where you expect it so you need to adjust your shebang line.
2295
2296 =end original
2297
2298 "perl: command not found" のようなメッセージが出た場合、perl が PATH に
2299 ありません; つまりおそらくは perl の位置があなたの想定している
2300 場所ではないことも意味しているので、#! 行を調整する必要があります。
2301
2302 =head1 AUTHOR AND COPYRIGHT
2303
2304 Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and
2305 other authors as noted. All rights reserved.
2306
2307 This documentation is free; you can redistribute it and/or modify it
2308 under the same terms as Perl itself.
2309
2310 Irrespective of its distribution, all code examples in this file
2311 are hereby placed into the public domain. You are permitted and
2312 encouraged to use this code in your own programs for fun
2313 or for profit as you see fit. A simple comment in the code giving
2314 credit would be courteous but is not required.
2315
2316 =begin meta
2317
2318 Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp>
2319 Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-5.14.1, 5.00150039)
2320 Status: completed
2321
2322 =end meta
2323

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26