/
perlfaq7.pod
executable file
·2350 lines (1583 loc) · 67.4 KB
/
perlfaq7.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=encoding euc-jp
=head1 NAME
=begin original
perlfaq7 - General Perl Language Issues
=end original
perlfaq7 - Perl 言語一般に関することがら
=head1 DESCRIPTION
=begin original
This section deals with general Perl language issues that don't
clearly fit into any of the other sections.
=end original
このセクションでは、他のセクションにはきっちりとあてはまらないような
Perl 言語に関する一般的な事柄を扱います。
=head2 Can I get a BNF/yacc/RE for the Perl language?
(Perl のための BNF/yacc/RE は入手できますか?)
=begin original
There is no BNF, but you can paw your way through the yacc grammar in
perly.y in the source distribution if you're particularly brave. The
grammar relies on very smart tokenizing code, so be prepared to
venture into toke.c as well.
=end original
BNFはありませんが、もし多少の勇気を持ちあわせているのであれば
配布ソースに含まれている perly.y にある yacc 文法をいじくりまわすことが
できます。
その文法は非常に賢い字句解析ルーチンに依存したものなので、
toke.c を眺める準備もしておきましょう。
=begin original
In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF.
The work of parsing perl is distributed between yacc, the lexer, smoke
and mirrors."
=end original
reduce
Chaim Frenkel の言葉を借りればこうです:
「Perl の文法は BNF まで縮小することができない。
perl の構文解析の作業は yacc、字句解析器、煙と鏡とに分配される。」
=head2 What are all these $@%&* punctuation signs, and how do I know when to use them?
($@%&* のマークはなんですか? これらをいつ使えばいいのかを知るにはどうすればいいですか?)
=begin original
They are type specifiers, as detailed in L<perldata>:
=end original
これらは型指定子(type specifiers)で、L<perldata> で説明されています:
=begin original
$ for scalar values (number, string or reference)
@ for arrays
% for hashes (associative arrays)
& for subroutines (aka functions, procedures, methods)
* for all types of that symbol name. In version 4 you used them like
pointers, but in modern perls you can just use references.
=end original
$ スカラ値(数値、文字列、リファレンス)に対するもの
@ 配列に対するもの
% ハッシュ(連想配列)に対するもの
& サブルーチン(またの名を関数、手続き、メソッド)に対するもの
* シンボル名に対する全ての型。バージョン 4 ではポインタのように
使われていましたが、新しい perl ではリファレンスが使えます。
=begin original
There are a couple of other symbols that
you're likely to encounter that aren't
really type specifiers:
=end original
実際には型指定子として見ることはないであろう二つのものが
この他にもあります:
=begin original
<> are used for inputting a record from a filehandle.
\ takes a reference to something.
=end original
<> あるファイルハンドルからレコードを入力するのに使われます。
\ なにかのリファレンスを取ります。
=begin original
Note that <FILE> is I<neither> the type specifier for files
nor the name of the handle. It is the C<< <> >> operator applied
to the handle FILE. It reads one line (well, record--see
L<perlvar/$E<sol>>) from the handle FILE in scalar context, or I<all> lines
in list context. When performing open, close, or any other operation
besides C<< <> >> on files, or even when talking about the handle, do
I<not> use the brackets. These are correct: C<eof(FH)>, C<seek(FH, 0,
2)> and "copying from STDIN to FILE".
=end original
<FILE> は、ファイルに対する型指定子にもハンドルの名前の
I<どちらでもない> ということに注意してください。
これはハンドル FILE に対する C<< <> >> 演算子です。
一行(そう、レコードですね。L<perlvar/$E<sol>> を参照してください) を
FILE というハンドルからスカラコンテキストで読み出します。
リストコンテキストの場合は B<全ての> 行を読み出します。
ファイルの C<< <> >> に関係する open、close などの操作を行うときには、
ハンドルについて行っている場合であっても
アングルブラケットを使っては B<いけません>。
C<eof(FH)>, C<seek(FH, 0,2)> や "copying from STDIN to FILE" は
正しいものなのです。
=head2 Do I always/never have to quote my strings or use semicolons and commas?
(文字列では常にクォートする/決してクォートしない必要があるのでしょうか? また、セミコロンやカンマについては?)
=begin original
Normally, a bareword doesn't need to be quoted, but in most cases
probably should be (and must be under C<use strict>). But a hash key
consisting of a simple word and the left-hand
operand to the C<< => >> operator both
count as though they were quoted:
=end original
通常は、裸の単語(barewords)はクォートする必要はありませんが、
ほとんど場合はクォートすべきでしょう(そして、C<use strcit> しているときは
しなければなりません)。
しかし、単純な単語から
構成されるハッシュと、C<< => >> 演算子の左側にあるオペランドは
両方ともクォートされているとみなされます:
This is like this
------------ ---------------
$foo{line} $foo{'line'}
bar => stuff 'bar' => stuff
=begin original
The final semicolon in a block is optional, as is the final comma in a
list. Good style (see L<perlstyle>) says to put them in except for
one-liners:
=end original
ブロックの最後にあるセミコロンは、リストの最後にあるカンマと同じく
省略可能です。
良いスタイル(L<perlstyle> を参照)は一行野郎(one-liners)でなければ
それらを使うようにしましょうと言っています。
if ($whoops) { exit 1 }
@nums = (1, 2, 3);
if ($whoops) {
exit 1;
}
@lines = (
"There Beren came from mountains cold",
"And lost he wandered under leaves",
);
=head2 How do I skip some return values?
(戻り値の一部をスキップするには?)
=begin original
One way is to treat the return values as a list and index into it:
=end original
方法の一つは、戻り値をリストとみなして、それに添え字づけするというものです:
$dir = (getpwnam($user))[7];
=begin original
Another way is to use undef as an element on the left-hand-side:
=end original
もう一つのやりかたは、左辺の要素として undef を使うというものです:
($dev, $ino, undef, undef, $uid, $gid) = stat($file);
=begin original
You can also use a list slice to select only the elements that
you need:
=end original
必要な要素だけを選択するために、リストスライスも使えます:
($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];
=head2 How do I temporarily block warnings?
(一時的に警告をブロックするには?)
=begin original
If you are running Perl 5.6.0 or better, the C<use warnings> pragma
allows fine control of what warning are produced.
See L<perllexwarn> for more details.
=end original
Perl 5.6.0 以降を使っているなら、C<use warnings> プラグマで
どんな警告を生成するかをうまく制御できます。
詳細については L<perllexwarn> を参照してください。
{
no warnings; # temporarily turn off warnings
$a = $b + $c; # I know these might be undef
}
=begin original
Additionally, you can enable and disable categories of warnings.
You turn off the categories you want to ignore and you can still
get other categories of warnings. See L<perllexwarn> for the
complete details, including the category names and hierarchy.
=end original
さらに、警告の分野毎に警告を有効または無効にできます。
無視したいカテゴリを無効にしても、残りのカテゴリの警告は受けられます。
カテゴリ名と階層を含む、完全な詳細については L<perllexwarn> を
参照してください。
{
no warnings 'uninitialized';
$a = $b + $c;
}
=begin original
If you have an older version of Perl, the C<$^W> variable (documented
in L<perlvar>) controls runtime warnings for a block:
=end original
より古いバージョンの場合は、変数 C<$^W>(L<perlvar> に説明があります)は
実行時の警告のブロックを制御します:
=begin original
{
local $^W = 0; # temporarily turn off warnings
$a = $b + $c; # I know these might be undef
}
=end original
{
local $^W = 0; # 一時的に警告をオフにする
$a = $b + $c; # これらが undef かもしれないことを知っている
}
=begin original
Note that like all the punctuation variables, you cannot currently
use my() on C<$^W>, only local().
=end original
全ての句読点変数(punctuation variable)と同様、現時点では C<$^W> に対して
my() を使うことはできず、local() だけしか使えないということに
注意してください。
=head2 What's an extension?
(エクステンションてなんですか?)
=begin original
An extension is a way of calling compiled C code from Perl. Reading
L<perlxstut> is a good place to learn more about extensions.
=end original
エクステンションとは、Perl からコンパイル済みの C コードを呼び出すための
方法です。
エクステンションについて知るには L<perlxstut> を読むのが良いでしょう。
=head2 Why do Perl operators have different precedence than C operators?
(なぜ Perl の演算子は C の演算子とは異なった優先順位を持っているのでしょうか?)
=begin original
Actually, they don't. All C operators that Perl copies have the same
precedence in Perl as they do in C. The problem is with operators that C
doesn't have, especially functions that give a list context to everything
on their right, eg. print, chmod, exec, and so on. Such functions are
called "list operators" and appear as such in the precedence table in
L<perlop>.
=end original
実際はそうではありません。Perl に持ち込まれたCの演算子はすべて、
C と Perl とで同じ優先順位を持っています。
問題は、C にはない演算子、特にその右辺に対してつねにリストコンテキストを
与える関数、例えば print、chmod、exec などです。
そういった関数は「リスト演算子」と呼ばれ、
L<perlop> にある優先順位テーブルにあります。
=begin original
A common mistake is to write:
=end original
ありがちな間違いは以下のようにのように書いてしまうことです:
unlink $file || die "snafu";
=begin original
This gets interpreted as:
=end original
これは以下のように解釈されます:
unlink ($file || die "snafu");
=begin original
To avoid this problem, either put in extra parentheses or use the
super low precedence C<or> operator:
=end original
この問題を避けるためには、余計な括弧をつけるかより優先順位の低い
C<or> 演算子を使うようにします:
(unlink $file) || die "snafu";
unlink $file or die "snafu";
=begin original
The "English" operators (C<and>, C<or>, C<xor>, and C<not>)
deliberately have precedence lower than that of list operators for
just such situations as the one above.
=end original
"English" 演算子(C<and>, C<or>, C<xor>, C<not>) は先に説明している
同じ働きをするリスト演算子よりも低い優先順位を故意に持たされています。
=begin original
Another operator with surprising precedence is exponentiation. It
binds more tightly even than unary minus, making C<-2**2> produce a
negative not a positive four. It is also right-associating, meaning
that C<2**3**2> is two raised to the ninth power, not eight squared.
=end original
もう一つの、びっくりするような優先順位を持っている演算子は
べき乗(exponentiation)です。
これは単項のマイナスよりも強く結び付くので、C<-2**2> はプラス 4 ではなく、
マイナス 4 を生成します。
この演算子は右結合するので、C<2**3**2> は 8 の 2 乗ではなく、
2 の 9 乗です。
=begin original
Although it has the same precedence as in C, Perl's C<?:> operator
produces an lvalue. This assigns $x to either $a or $b, depending
on the trueness of $maybe:
=end original
C と同じ優先順位を持っているにも関らず、Perl では C<?:> 演算子は
左辺値を作り出します。
以下の代入では、$maybe の値に応じて、$a か $b のいずれかに
$x の値を代入します:
($maybe ? $a : $b) = $x;
=head2 How do I declare/create a structure?
(構造体を宣言したり生成するには?)
=begin original
In general, you don't "declare" a structure. Just use a (probably
anonymous) hash reference. See L<perlref> and L<perldsc> for details.
Here's an example:
=end original
一般的には、構造体を“宣言”することはありません。
単に(おそらくは無名の)ハッシュリファレンスを使うだけです。
詳しくは L<perlref>と L<perldsc> を参照してください。
例を挙げましょう:
$person = {}; # new anonymous hash
$person->{AGE} = 24; # set field AGE to 24
$person->{NAME} = "Nat"; # set field NAME to "Nat"
=begin original
If you're looking for something a bit more rigorous, try L<perltoot>.
=end original
もうちょっと正確ななにかを求めているのなら、
L<perltoot> に挑戦してみてください。
=head2 How do I create a module?
(モジュールを作成するには?)
=begin original
(contributed by brian d foy)
=end original
(brian d foy によって寄贈されました)
=begin original
L<perlmod>, L<perlmodlib>, L<perlmodstyle> explain modules
in all the gory details. L<perlnewmod> gives a brief
overview of the process along with a couple of suggestions
about style.
=end original
L<perlmod>, L<perlmodlib>, L<perlmodstyle> はモジュールに関する全ての
不愉快な詳細について説明しています。
L<perlnewmod> にはこのプロセスに関する大まかな概要と、スタイルに関する
いくつかの忠告があります。
=begin original
If you need to include C code or C library interfaces in
your module, you'll need h2xs. h2xs will create the module
distribution structure and the initial interface files
you'll need. L<perlxs> and L<perlxstut> explain the details.
=end original
もしモジュールに C コードや C ライブラリインターフェースを含めたいなら、
h2xs が必要です。
h2xs は必要になるモジュール配布構造と初期インターフェースファイルを
作成します。
L<perlxs> と L<perlxstut> は詳細を説明しています。
=begin original
If you don't need to use C code, other tools such as
ExtUtils::ModuleMaker and Module::Starter, can help you
create a skeleton module distribution.
=end original
C のコードを使う必要がないのなら、ExtUtils::ModuleMaker や
Module::Starter といったツールが、モジュール配布の骨格を作るのを
助けてくれます。
=begin original
You may also want to see Sam Tregar's "Writing Perl Modules
for CPAN" ( http://apress.com/book/bookDisplay.html?bID=14 )
which is the best hands-on guide to creating module
distributions.
=end original
モジュール配布を作成するための最良の実践型ガイドである、Sam Tregar による
"Writing Perl Modules for CPAN"
( http://apress.com/book/bookDisplay.html?bID=14 ) を見るのもよいでしょう。
=head2 How do I adopt or take over a module already on CPAN?
(すでに CPAN にあるモジュールを引き継ぐには?)
=begin original
(contributed by brian d foy)
=end original
(brian d foy によって寄贈されました)
=begin original
The easiest way to take over a module is to have the current
module maintainer either make you a co-maintainer or transfer
the module to you.
=end original
モジュールを引き継ぐのに一番簡単な方法は、現在のモジュールのメンテナに
連絡して、共同メンテナにしてもらうか、モジュールを引き渡してもらう
ことです。
=begin original
If you can't reach the author for some reason (e.g. email bounces),
the PAUSE admins at modules@perl.org can help. The PAUSE admins
treat each case individually.
=end original
もし何らかの理由(メールが返ってきたなど)で作者と連絡が取れない場合、
PAUSE 管理者である modules@perl.org が助けになるかもしれません。
PAUSE 管理者はそれぞれの場合を個別に扱います。
=over 4
=item *
=begin original
Get a login for the Perl Authors Upload Server (PAUSE) if you don't
already have one: http://pause.perl.org
=end original
まだ持っていないなら、the Perl Authors Upload Server (PAUSE) のアカウントを
取ります: http://pause.perl.org
=item *
=begin original
Write to modules@perl.org explaining what you did to contact the
current maintainer. The PAUSE admins will also try to reach the
maintainer.
=end original
現在のメンテナに連絡するためにしたことの説明を modules@perl.org に書きます。
PAUSE 管理者もメンテナに連絡を試みます。
=item *
=begin original
Post a public message in a heavily trafficked site announcing your
intention to take over the module.
=end original
モジュールを引き継ぎたいという意思を表明するために、トラフィックの多い
サイトに公的なメッセージを投稿します。
=item *
=begin original
Wait a bit. The PAUSE admins don't want to act too quickly in case
the current maintainer is on holiday. If there's no response to
private communication or the public post, a PAUSE admin can transfer
it to you.
=end original
しばらく待ちます。
PAUSE 管理者は、現在のメンテナが休暇中の場合に、あまりに性急に行動したいとは
思いません。
もし私的な通信や公的な投稿に反応がない場合、PAUSE 管理者はモジュールを
あなたに移行できます。
=back
=head2 How do I create a class?
X<class, creation> X<package>
(クラスを作るには?)
=begin original
(contributed by brian d foy)
=end original
(brian d foy によって寄贈されました)
=begin original
In Perl, a class is just a package, and methods are just subroutines.
Perl doesn't get more formal than that and lets you set up the package
just the way that you like it (that is, it doesn't set up anything for
you).
=end original
Perl では、クラスは単なるパッケージで、メソッドは単なるサブルーチンです。
Perl はそれ以上に形式的なことはしませんし、あなたの好きな方法でパッケージを
設定できるようにします (つまり、Perl はあなたのために何の設定もしません)。
=begin original
The Perl documentation has several tutorials that cover class
creation, including L<perlboot> (Barnyard Object Oriented Tutorial),
L<perltoot> (Tom's Object Oriented Tutorial), L<perlbot> (Bag o'
Object Tricks), and L<perlobj>.
=end original
Perl の文書には、
L<perlboot> (Barnyard Object Oriented Tutorial),
L<perltoot> (Tom's Object Oriented Tutorial), L<perlbot> (Bag o'
Object Tricks), L<perlobj> といった、クラス生成に対応する
いくつかのチュートリアルがあります。
=head2 How can I tell if a variable is tainted?
(変数が汚染されているかどうかを確かめるには?)
=begin original
You can use the tainted() function of the Scalar::Util module, available
from CPAN (or included with Perl since release 5.8.0).
See also L<perlsec/"Laundering and Detecting Tainted Data">.
=end original
CPAN にある (リリース 5.8.0 からは Perl に含まれている) Scalar::Util
モジュールの tainted() 関数が使えます。
L<perlsec/"Laundering and Detecting Tainted Data"> も参照してください。
=head2 What's a closure?
(クロージャ(closure)ってなんですか?)
=begin original
Closures are documented in L<perlref>.
=end original
クロージャは L<perlref> に説明があります。
=begin original
I<Closure> is a computer science term with a precise but
hard-to-explain meaning. Usually, closures are implemented in Perl as
anonymous subroutines with lasting references to lexical variables
outside their own scopes. These lexicals magically refer to the
variables that were around when the subroutine was defined (deep
binding).
=end original
B<クロージャ> は、きちんとした定義を持ったコンピュータ科学の用語ですが
その意味を説明するのはとても難しいのです。
クロージャは Perl では、そのスコープの外側でもレキシカル変数に対する
リファレンスを保持しつづける無名サブルーチンとして実装されています。
これらのレキシカルは、サブルーチンが定義されたときの変数に対して、
魔法のような参照(magically refer)を行います(深い束縛、deep binding)。
=begin original
Closures are most often used in programming languages where you can
have the return value of a function be itself a function, as you can
in Perl. Note that some languages provide anonymous functions but are
not capable of providing proper closures: the Python language, for
example. For more information on closures, check out any textbook on
functional programming. Scheme is a language that not only supports
but encourages closures.
=end original
クロージャは、Perl ができるような関数の戻り値として関数それ自身を返す関数を
持つことができるプログラミング言語でもっともよく使われます。
一部の言語では、無名関数を提供しているけれども適切なクロージャを提供する
能力はないということに注意してください。
たとえば Python がそうです。
クロージャに関するより詳しいことは、関数言語に関するなんらかの教科書を
みてください。
Scheme はクロージャをサポートするだけでなく、それを推奨している言語です。
=begin original
Here's a classic non-closure function-generating function:
=end original
以下は、古典的な、クロージャではない関数を生成する関数です:
sub add_function_generator {
return sub { shift() + shift() };
}
$add_sub = add_function_generator();
$sum = $add_sub->(4,5); # $sum is 9 now.
=begin original
The anonymous subroutine returned by add_function_generator() isn't
technically a closure because it refers to no lexicals outside its own
scope. Using a closure gives you a I<function template> with some
customization slots left out to be filled later.
=end original
add_function_generator() が返した無名サブルーチンは技術的には
クロージャではありません。
なぜなら、あれはスコープの外側で参照するようなレキシカルがないからです。
クロージャを使うことによって、後で埋めることのできるカスタマイズ可能な
幾つかのスロットを持つ B<関数テンプレート> のように働きます。
=begin original
Contrast this with the following make_adder() function, in which the
returned anonymous function contains a reference to a lexical variable
outside the scope of that function itself. Such a reference requires
that Perl return a proper closure, thus locking in for all time the
value that the lexical had when the function was created.
=end original
それとは対照的に、次の関数 make_adder() では、関数自身のスコープの外側で
レキシカル変数に対するリファレンスを持つ無名関数を返します。
そのようなリファレンスは Perl が適切なクロージャを返すように要求するので、
その変数を参照するときはいつでも関数が生成されたときの
レキシカルが参照されます。
sub make_adder {
my $addpiece = shift;
return sub { shift() + $addpiece };
}
$f1 = make_adder(20);
$f2 = make_adder(555);
=begin original
Now C<&$f1($n)> is always 20 plus whatever $n you pass in, whereas
C<&$f2($n)> is always 555 plus whatever $n you pass in. The $addpiece
in the closure sticks around.
=end original
これで、C<&$f1($n)> は それに渡した $n に対して常に 20 を加え、
C<&$f2($n)> は渡された $n に常に 555 を加えます。
クロージャの中にある $addpiece が仕事をしています。
=begin original
Closures are often used for less esoteric purposes. For example, when
you want to pass in a bit of code into a function:
=end original
クロージャは、それほど難しくない状況でよく使われます。
たとえば、関数にちょっとしたコードを押しこみたいときがそうです:
my $line;
timeout( 30, sub { $line = <STDIN> } );
=begin original
If the code to execute had been passed in as a string,
C<< '$line = <STDIN>' >>, there would have been no way for the
hypothetical timeout() function to access the lexical variable
$line back in its caller's scope.
=end original
もし実行すべきコードが文字列として渡されていたのであれば、
C<< '$line = <STDIN>' >> としているところは、仮想的な timeout() 関数が
アクセスするレキシカル変数 $line を呼び出し元のスコープのものに戻す
手段がなくなってしまいます。
=begin original
Another use for a closure is to make a variable I<private> to a
named subroutine, e.g. a counter that gets initialized at creation
time of the sub and can only be modified from within the sub.
This is sometimes used with a BEGIN block in package files to make
sure a variable doesn't get meddled with during the lifetime of the
package:
=end original
もう一つのクロージャの使用法は、ある変数を名前つきサブルーチンで
I<プライベート> にすることです; 例えば、サブルーチンの作成時に初期化され、
サブルーチン内でのみ変更可能なカウンタです。
これは、パッケージの生存期間中に変数が干渉されることがないように、とき毒
パッケージファイルの BEGIN ブロックで使われます:
BEGIN {
my $id = 0;
sub next_id { ++$id }
}
=begin original
This is discussed in more detail in L<perlsub>; see the entry on
I<Persistent Private Variables>.
=end original
これは L<perlsub> でより詳しく議論されています;
I<Persistent Private Variables> のエントリを参照してください。
=head2 What is variable suicide and how can I prevent it?
(変数の自殺(variable suicide)って何で、それをどうすれば防げますか?)
=begin original
This problem was fixed in perl 5.004_05, so preventing it means upgrading
your version of perl. ;)
=end original
この問題は 5.004_05 で修正されたので、防ぐためには perl を
バージョンアップします :)
=begin original
Variable suicide is when you (temporarily or permanently) lose the value
of a variable. It is caused by scoping through my() and local()
interacting with either closures or aliased foreach() iterator variables
and subroutine arguments. It used to be easy to inadvertently lose a
variable's value this way, but now it's much harder. Take this code:
=end original
変数の自殺とは、(一時的にしろ、恒久的にしろ)変数の値を失ったときのことを
指します。
これは、クロージャ、もしくは別名つけされた foreach イテレータ変数や
サブルーチンの引数と相互作用している my() や local() を通した
スコープによって引き起こされます。
以前はこのやり方で変数の値をうっかりとなくしてしまうように
使われがちでしたが、現在は非常に難しくなっています。
以下のコードを考えてみましょう:
my $f = 'foo';
sub T {
while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }
}
T;
print "Finally $f\n";
=begin original
If you are experiencing variable suicide, that C<my $f> in the subroutine
doesn't pick up a fresh copy of the C<$f> whose value is <foo>. The output
shows that inside the subroutine the value of C<$f> leaks through when it
shouldn't, as in this output:
=end original
もし変数の自殺に遭遇したら、サブルーチン内の C<my $f> は、値が C<foo> である
C<$f> の最新のコピーをではありません。
この出力は、漏れてはいけないサブルーチン内の C<$f> の値が漏れていることを
示し、以下のようになります:
foobar
foobarbar
foobarbarbar
Finally foo
=begin original
The $f that has "bar" added to it three times should be a new C<$f>
C<my $f> should create a new lexical variable each time through the loop.
The expected output is:
=end original
"bar" を保持している $f は三回 new C<$f> されるべきものです (C<my $f> は、
ループが通る度に新たなレキシカル変数を生成すべきなのです)。
予想される出力は以下のものです:
foobar
foobar
foobar
Finally foo
=head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?
({関数, ファイルハンドル, 配列, ハッシュ, メソッド, 正規表現} を渡したり返したりするには?)
=begin original
You need to pass references to these objects. See L<perlsub/"Pass by
Reference"> for this particular question, and L<perlref> for
information on references.
=end original
これらのオブジェクトのリファレンスを渡す必要があります。
L<perlsub/"Pass by Reference"> にある関連した質問と、
L<perlref> にあるリファレンスに関する情報を参照してください。
=over 4
=item Passing Variables and Functions
(変数や関数を渡す)
=begin original
Regular variables and functions are quite easy to pass: just pass in a
reference to an existing or anonymous variable or function:
=end original
普通の変数や関数はとても簡単に渡せます: 既に存在している変数や関数に対する
リファレンスか、無名変数や無名関数に対するリファレンスを渡せばよいのです。
func( \$some_scalar );
func( \@some_array );
func( [ 1 .. 10 ] );
func( \%some_hash );
func( { this => 10, that => 20 } );
func( \&some_func );
func( sub { $_[0] ** $_[1] } );
=item Passing Filehandles
=begin original
As of Perl 5.6, you can represent filehandles with scalar variables
which you treat as any other scalar.
=end original
Perl 5.6 から、他のスカラと同様にファイルハンドルもスカラ変数で扱えます。
open my $fh, $filename or die "Cannot open $filename! $!";
func( $fh );
sub func {
my $passed_fh = shift;
my $line = <$passed_fh>;
}
=begin original
Before Perl 5.6, you had to use the C<*FH> or C<\*FH> notations.
These are "typeglobs"--see L<perldata/"Typeglobs and Filehandles">
and especially L<perlsub/"Pass by Reference"> for more information.
=end original
Perl 5.6 より前では、C<*FH> や C<\*FH> といった記法を使う必要があります。
これらは“型グロブ”(typeglob)です。
L<perldata/"Typeglobs and Filehandles">) と L<perlsub/"Pass by Reference"> に
詳しい説明があります。
=item Passing Regexes
(正規表現を渡す)
=begin original
Here's an example of how to pass in a string and a regular expression
for it to match against. You construct the pattern with the C<qr//>
operator:
=end original
以下の例は、文字列とマッチングする正規表現を渡す方法です。
パターンを C<qr//> 演算子で構築します:
sub compare($$) {
my ($val1, $regex) = @_;
my $retval = $val1 =~ /$regex/;