perltrap > 5.24.1 との差分

perltrap 5.24.1 と 5.6.1 の差分

1
21=encoding euc-jp
32
43=head1 NAME
54
6=begin original
5perltrap - Perlの不注意による罠
76
8perltrap - Perl traps for the unwary
9
10=end original
11
12perltrap - 不注意による Perl の罠
13
147=head1 DESCRIPTION
158
169=begin original
1710
1811The biggest trap of all is forgetting to C<use warnings> or use the B<-w>
19switch; see L<warnings> and L<perlrun>. The second biggest trap is not
12switch; see L<perllexwarn> and L<perlrun>. The second biggest trap is not
2013making your entire program runnable under C<use strict>. The third biggest
2114trap is not reading the list of changes in this version of Perl; see
2215L<perldelta>.
2316
2417=end original
2518
26最も大きな罠とは、C<use warnings> あるいは B<-w> スイッチを使うのを
19最も大きな罠とは、C<use warnings> あるいは B<-w> スイッチを
27忘れてしまうということです; L<warnings> と L<perlrun> を
20使うのを忘れてしまうということです
28参照してください。
21L<perllexwarn> と L<perlrun> を参照してください。
29二番目に大きな罠とは、あなたのプログラム全体を C<use strict> の元で
22二番目に大きな罠とは、あなたのプログラム全体を
30実行しないということです。
23C<use strict> の元で実行しないということです。
31三番目の罠は、このバージョンの Perl での変更点を読まないということです;
24三番目の罠は、Perl のこのバージョンでの変更点を読まないということです
3225L<perldelta> を参照してください。
3326
3427=head2 Awk Traps
3528
3629(awk の罠)
3730
38=begin original
31B<awk> に慣れた方は、以下のようなことに特に注意してください:
3932
40Accustomed B<awk> users should take special note of the following:
33=over 4
4134
42=end original
35=item *
4336
44B<awk> に慣れた方は、以下のようなことに特に注意してください:
37Englishモジュールを
4538
46=over 4
39 use English;
4740
41のようにしてロードすれば、B<awk> でそうであったように
42(C<$/> のような)特殊変数を($RS のような)名前で参照することができます。
43詳しくは L<perlvar> を参照してください。
44
4845=item *
4946
50=begin original
47Perlでは、すべての単純文(simple statement)の末尾にセミコロンが必要です
48(ブロックの最後に置かれたときを除きます)。
49改行は文の区切りとはなりません。
5150
52A Perl program executes only once, not once for each input line. You can
51=item *
53do an implicit loop with C<-n> or C<-p>.
5452
55=end original
53C<if> や C<while> ではカーリーブレースが必要です。
5654
57Perl のプログラムは、1 度だけ実行されます; 入力行毎ではありません。
55=item *
58C<-n> や C<-p> を使って暗黙のループを使えます。
5956
57Perlでは、変数は“$”"か“@”か“%”で始まります。
58
6059=item *
6160
62=begin original
61配列の添え字は 0 から始まります。substr() や index() での文字列の位置も
62同様です。
6363
64The English module, loaded via
64=item *
6565
66=end original
66配列の添え字が数値であるか、文字列であるかを決めなければなりません。
6767
68Englishモジュールを
68=item *
6969
70 use English;
70ハッシュ(連想配列)の値は、単に参照するだけでは存在することに
71なりません。
7172
72=begin original
73=item *
7374
74allows you to refer to special variables (like C<$/>) with names (like
75比較を文字列によって行うのか、数値によって行うのかを
75$RS), as though they were in B<awk>; see L<perlvar> for details.
76決めなければなりません。
7677
77=end original
78=item *
7879
79のようにしてロードすれば、B<awk> そうであったように
80入力を読み込むだけは split は行われません。
80(C<$/> ような)特殊変数を($RS のような)名前参照することができます;
81配列へsplit は自分行います
81詳しくL<perlvar> を参照しください。
82また、split() 演算子の引数B<awk> のものと異なっています
8283
8384=item *
8485
85=begin original
86通常、カレント行は $0 ではなく $_ にあります。
87一般的に、改行は取り除かれません
88($0 には実行しているプログラムの名前があります)。
89L<perlvar> を参照してください。
8690
87Semicolons are required after all simple statements in Perl (except
91=item *
88at the end of a block). Newline is not a statement delimiter.
8992
90=end original
93$<I<digit>> はフィールドを参照しません。これは
94直前に行ったパターンマッチングの部分文字列を参照します。
9195
92Perl では、すべての単純文(simple statement)の末尾にセミコロンが必要です
96=item *
93(ブロックの最後に置かれたときを除きます)。
94改行は文の区切りとはなりません。
9597
98print() 文は、C<$,> や C<$\> に値を設定しない限りフィールド区切り子や
99レコード区切り子を付加しません。English モジュールを使っていれば、
100$OFS や $ORS に対して設定することでも OK です。
101
96102=item *
97103
98=begin original
104ファイルに対して出力する前には、そのファイルをあらかじめオープンして
105おかなければなりません。
99106
100Curly brackets are required on C<if>s and C<while>s.
107=item *
101108
102=end original
109範囲演算子は“..”であって、カンマではありません。カンマ演算子は
110Cと同じような振る舞いをします。
103111
104C<if> や C<while> では中かっこが必要です。
112=item *
105113
114マッチ演算子は“=~”であって、“~”ではありません
115(“~”はCと同様に、1の補数を取る演算子です)。
116
106117=item *
107118
108=begin original
119べき乗の演算子は“**”であって、“^”ではありません。
120“^”はCと同様、XOR演算子です
121(B<awk> が基本的に C と非互換であることにお気付きかもしれませんね)。
109122
110Variables begin with "$", "@" or "%" in Perl.
123=item *
111124
112=end original
125連接演算子は“.”であって、空文字列ではありません
126(空文字列を使ってしまうと
127C</pat/ /pat/> が、その三番目のスラッシュが除算演算子と解釈されてしまうので
128正しく解析できなくなります。
129Perl の字句解析器は "/", "?", ">" といった演算子に対して
130多少文脈依存となっています。実際、"." 自身も数値の始まりとなる
131可能性もあります)
113132
114Perl では、変数は "$" か "@" か "%" で始まります。
133=item *
115134
135キーワード C<next>, C<exit>, C<continue> の振る舞いが異なります。
136
116137=item *
117138
118=begin original
139以下の変数の働きが異なります。
119140
120Arrays index from 0. Likewise string positions in substr() and
141 Awk Perl
121index().
142 ARGC scalar @ARGV (compare with $#ARGV)
143 ARGV[0] $0
144 FILENAME $ARGV
145 FNR $. - something
146 FS (whatever you like)
147 NF $#Fld, or some such
148 NR $.
149 OFMT $#
150 OFS $,
151 ORS $\
152 RLENGTH length($&)
153 RS $/
154 RSTART length($`)
155 SUBSEP $;
122156
123=end original
157=item *
124158
125配列の添え字は 0 から始ります。
159$RS に正規表現をセットすることはできせん。できるのは文字列だけです。
126substr() や index() での文字列の位置も同様です。
127160
128161=item *
129162
163妙だと思ったときには awk の構文を a2p に通して、
164出力されたものを見てみましょう。
165
166=back
167
168=head2 C Traps
169
170(C の罠)
171
130172=begin original
131173
132You have to decide whether your array has numeric or string indices.
174Cerebral C programmers should take note of the following:
133175
134176=end original
135177
136配列添え字が数値あるか、文字列であるかを決めなければなりません。
178知的な C プログラマは以下ことに注意すべきす:
137179
180=over 4
181
138182=item *
139183
140=begin original
184C<if> や C<while> にはカーリーブレースが必要です。
141185
142Hash values do not spring into existence upon mere reference.
186=item *
143187
144=end original
188C<else if> ではなく、C<elsif> を使わなければなりません。
145189
146ハッシュ(連想配列)の値は、単に参照するだけでは存在することになりません。
147
148190=item *
149191
150=begin original
192C のC<break> と C<continue> は、Perlではそれぞれ C<last> と
193C<next> となります。
194C とは異なり、これらは C<do { } while> 構文では I<使えません>。
151195
152You have to decide whether you want to use string or numeric
196=item *
153comparisons.
154197
155=end original
198switch 文はありません(が、その場(on the fly)で作り上げることは簡単です)。
156199
157比較を文字列によって行うのか、数値によって行うのかを
200=item *
158決めなければなりません。
159201
202Perl では、変数は“$”か“@”か“%”で始まります。
203
160204=item *
161205
162=begin original
206コメントの始まりは、“#”であり、“/*”ではありません。
163207
164Reading an input line does not split it for you. You get to split it
208=item *
165to an array yourself. And the split() operator has different
166arguments than B<awk>'s.
167209
168=end original
210なにかのアドレスを得ることはできません。Perl には似たような演算子である
211バックスラッシュがありますが、これは参照を生成します。
169212
170入力を読み込むだけでは split は行われません。
213=item *
171配列への split は自分で行います。
172また、split() 演算子の引数は B<awk> のものと異なっています。
173214
215C<ARGV> は大文字でなければなりません。C<$ARGV[0]> が C での C<argv[1]> に相当し、
216C<argv[0]> にあたるものは C<$0> です。
217
174218=item *
175219
176220=begin original
177221
178The current input line is normally in $_, not $0. It generally does
222System calls such as link(), unlink(), rename(), etc. return nonzero for
179not have the newline stripped. ($0 is the name of the program
223success, not 0. (system(), however, returns zero for success.)
180executed.) See L<perlvar>.
181224
182225=end original
183226
184通常、カレント行は $0 ではなく $_ あります。
227link(), unlink(), rename() などのシステムコールは、成功時
185一般的に改行取り除かれせん
2280 ではなく非 0 の値を返します。(但しsystem() 成功時に 0 を返し)
186($0 には実行しているプログラムの名前があります)。
187L<perlvar> を参照してください。
188229
189230=item *
190231
232シグナルハンドラは、シグナル番号ではなくシグナル名を扱います。
233使用できるシグナル名は、kill -l として確かめてください。
234
235=back
236
237=head2 Sed Traps
238
239(sed の罠)
240
191241=begin original
192242
193$<I<digit>> does not refer to fields--it refers to substrings matched
243Seasoned B<sed> programmers should take note of the following:
194by the last match pattern.
195244
196245=end original
197246
198$<I<digit>>フィールドを参照しません--れは直前行った
247熟練した B<sed> プログラマ以下の注意すべきです:
199パターンマッチングの部分文字列を参照します。
200248
249=over 4
250
201251=item *
202252
203=begin original
253置換における後方参照には、“\”ではなく“$”を使います。
204254
205The print() statement does not add field and record separators unless
255=item *
206you set C<$,> and C<$\>. You can set $OFS and $ORS if you're using
207the English module.
208256
209=end original
257"(", ")", "|" といったパターンマッチのメタキャラクタは、その直前に
258バックスラッシュを置く必要はありません。
210259
211print() 文は、C<$,> や C<$\> に値を設定しない限りフィールド区切り子や
212レコード区切り子を付加しません。
213English モジュールを使っていれば、$OFS や $ORS に対して
214設定することもできます。
215
216260=item *
217261
262範囲演算子は C<...> であって、カンマではありません。
263
264=back
265
266=head2 Shell Traps
267
268(shell の罠)
269
218270=begin original
219271
220You must open your files before you print to them.
272Sharp shell programmers should take note of the following:
221273
222274=end original
223275
224ファイに対して出力する前に、そファイルをあらかじめオープンして
276鋭いシェプログラマ以下ことに注意すべきです:
225おかなければなりません。
226277
278=over 4
279
227280=item *
228281
229=begin original
282バッククォート演算子は、コマンド内にシングルクォートがあっても
283変数の展開を行ないます。
230284
231The range operator is "..", not comma. The comma operator works as in
285=item *
232C.
233286
234=end original
287バッククォート演算子は B<csh> とは違って、返された値を変換しません。
235288
236範囲演算子は ".." であって、カンマではありません。
237カンマ演算子は C と同じような振る舞いをします。
238
239289=item *
240290
241=begin original
291シェル (特に B<csh>) は、コマンドラインごとに何段階もの置換を行ないます。
292Perl はダブルクォート、バッククォート、
293アングルブラケット、検索パターンといった特定の構造でだけ置換を行ないます。
242294
243The match operator is "=~", not "~". ("~" is the one's complement
295=item *
244operator, as in C.)
245296
246=end original
297シェルは一度に少しずつ解釈を行ないます。
298Perl は実行前にプログラム全体をコンパイルします
299(コンパイル時に実行される C<BEGIN> ブロックを除く)。
247300
248マッチ演算子は "=~" であって、"~" ではありません。
301=item *
249("~" はCと同様に、1 の補数を取る演算子です。)
250302
303引数は $1, $2 などではなく、@ARGV から得られます。
304
251305=item *
252306
307環境変数は、自動的には独立したスカラー変数として利用できるように
308なりません。
309
310=back
311
312=head2 Perl Traps
313
314(Perl の罠)
315
253316=begin original
254317
255The exponentiation operator is "**", not "^". "^" is the XOR
318Practicing Perl Programmers should take note of the following:
256operator, as in C. (You know, one could get the feeling that B<awk> is
257basically incompatible with C.)
258319
259320=end original
260321
261べき乗の演算子は "**" であって、"^" でありません。
322実践的な Perl プログラマ以下のことに注意すべきです:
262"^" は C と同様、XOR 演算子です。
263(B<awk> が基本的に C と非互換であることにお気付きかもしれませんね。)
264323
324=over 4
325
265326=item *
266327
267=begin original
328多くの演算子がリストコンテキストとスカラーコンテキストとで
329振る舞いが変わることを忘れないでください。
330詳しくは L<perldata> を参照してください。
268331
269The concatenation operator is ".", not the null string. (Using the
332=item *
270null string would render C</pat/ /pat/> unparsable, because the third slash
271would be interpreted as a division operator--the tokenizer is in fact
272slightly context sensitive for operators like "/", "?", and ">".
273And in fact, "." itself can be the beginning of a number.)
274333
275=end original
334裸の単語、特に全てが小文字のものはできる限り使わないでください。
335見た目だけではその「裸の単語」が関数なのか、
336文字列なのかが判断できません。文字列にはクォートを、
337関数呼び出しには括弧をつければ、迷うこともないでしょう。
276338
277連接演算子は "." であって、空文字列ではありません。
278(空文字列を使ってしまうと C</pat/ /pat/> が、その 3 番目のスラッシュが
279除算演算子と解釈されてしまうので正しく解析できなくなります--
280Perl の字句解析器は "/", "?", ">" といった演算子に対して
281多少文脈依存となっています。
282実際、"." 自身も数値の始まりとなる可能性もあります。)
283
284339=item *
285340
286341=begin original
287342
288The C<next>, C<exit>, and C<continue> keywords work differently.
343You cannot discern from mere inspection which builtins
344are unary operators (like chop() and chdir())
345and which are list operators (like print() and unlink()).
346(Unless prototyped, user-defined subroutines can B<only> be list
347operators, never unary ones.) See L<perlop> and L<perlsub>.
289348
290349=end original
291350
292キーワード C<next>, C<exit>, C<continue> 振る舞いが異ります。
351組込み関数のどれが(chop() chdir())よう単項演算子で、
352どれが(print() や unlink())のような
353リスト演算子であるかは見ただけではわかりません
354(プロトタイプがなければ、ユーザー定義サブルーチンは
355リスト演算子として B<のみ> 定義でき、単項演算子にはできません)。
356L<perlop> と L<perlsub> を参照してください。
293357
294358=item *
295359
296=begin original
360いくつかの関数が $_ や @ARGV などをデフォルトにしていますが、
361同じことを期待する他の関数がデフォルトになっていないことを覚えるのに、
362辛いタイピングが必要でしょう。
297363
298The following variables work differently:
299364
300=end original
365=item *
301366
302以下の変数の働きが異りま
367<FH> 構造はファイルハンドルではく、そのハンドルに対る行読み込みの
368操作(readline operation)です。
369while ループの条件式の中にこのファイル読み込みだけがあった場合には
370読み込まれたデータは $_ に代入されます。
303371
304 Awk Perl
372 while (<FH>) { }
305 ARGC scalar @ARGV (compare with $#ARGV)
373 while (defined($_ = <FH>)) { }..
306 ARGV[0] $0
374 <FH>; # データは捨てられる!
307 FILENAME $ARGV
308 FNR $. - something
309 FS (whatever you like)
310 NF $#Fld, or some such
311 NR $.
312 OFMT $#
313 OFS $,
314 ORS $\
315 RLENGTH length($&)
316 RS $/
317 RSTART length($`)
318 SUBSEP $;
319375
320376=item *
321377
322=begin original
378C<=~> が必要なところで c<=> を使わない、ということを忘れないでください。
379これら二つの構造はかなり違います。
323380
324You cannot set $RS to a pattern, only a string.
381 $x = /foo/;
382 $x =~ /foo/;
325383
326=end original
384=item *
327385
328$RS に正規表現セットすことはません; できるのは文字列だけです
386C<do {}> 構造は、ループ制御行えような本当のループはありません。
329387
330388=item *
331389
332=begin original
390ローカル変数は、my() で済むところではこれで済ませること
391(使えない場所については、L<perlform> を参照してください)。
392local() を使えばグローバル変数に対するローカルな値を与えますが、
393動的スコープの不慮の副作用の可能性は、そのままです。
333394
334When in doubt, run the B<awk> construct through B<a2p> and see what it
395=item *
335gives you.
336396
337=end original
397モジュールにある export された変数を局所化すると、その export された
398値は変更されません。
399ローカル名は新しい値の別名(alias)となりますが、
400外部名は元々の値の別名のままです。
338401
339妙だと思ったときには B<awk> の構文を B<a2p> に通して、出力されたものを
340見てみましょう。
341
342402=back
343403
344=head2 C/C++ Traps
404=head2 Perl4 to Perl5 Traps
345405
346(C/++ の罠)
406(Perl4 から Perl5 への罠)
347407
348408=begin original
349409
350Cerebral C and C++ programmers should take note of the following:
410Practicing Perl4 Programmers should take note of the following
411Perl4-to-Perl5 specific traps.
351412
352413=end original
353414
354的な C と C++ のプログラマは以下のことに注意すべきです:
415実践的な Perl4 プログラマは
416以下に挙げる Perl4 と Perl5 の違いに特有な罠に気をつけた
417方が良いでしょう。
355418
419以下は順不同のリストです。
420
356421=over 4
357422
358=item *
423=item Discontinuance, Deprecation, and BugFix traps
359424
425修正された perl4 のバグや、なくなった perl4 の仕様、
426perl5 で仕様の変わったもの。
427
428=item 構文解析に関する罠
429
430新しい構文解析器によって引き起こされるもの。
431
432=item 数値に関する罠
433
360434=begin original
361435
362Curly brackets are required on C<if>'s and C<while>'s.
436Traps having to do with numerical or mathematical operators.
363437
364438=end original
365439
366C<if> C<while> は中かっこが必要です。
440数値算術演算子る罠
367441
368=item *
442=item General data type traps
369443
370=begin original
444perlの標準的なデータ型に内包される罠。
371445
372You must use C<elsif> rather than C<else if>.
446=item Context Traps - scalar, list contexts
373447
374=end original
448リスト内のコンテキストや、スカラー文/宣言に関する罠。
375449
376C<else if> ではなく、C<elsif> を使わなければなりません。
450=item 優先順位の罠
377451
378=item *
452構文解析、評価、コードの実行の優先順に関係した罠。
379453
380=begin original
454=item General Regular Expression Traps using s///, etc.
381455
382The C<break> and C<continue> keywords from C become in Perl C<last>
456パターンマッチングの仕様に関する罠。
383and C<next>, respectively. Unlike in C, these do I<not> work within a
384C<do { } while> construct. See L<perlsyn/"Loop Control">.
385457
386=end original
458=item Subroutine, Signal, Sorting Traps
387459
388C の C<break> と C<continue> はPerl ではそれぞれ C<last> と
460シグナル及びシグナルハンドラ一般的なサブルーチン、ソート、
389C<next> となります
461ソートのためのサブルーチンに関連した罠
390C とは異なり、これらは C<do { } while> 構文では I<使えません>。
391L<perlsyn/"Loop Control"> を参照してください。
392462
393=item *
463=item OS Traps
394464
395=begin original
465OS特有の罠。
396466
397The switch statement is called C<given>/C<when> and only available in
467=item DBM Traps
398perl 5.10 or newer. See L<perlsyn/"Switch Statements">.
399468
400=end original
469C<dbmopen()> の使用や、dbm の実装に関連した罠。
401470
402switch 文は C<given>/C<when> と呼ばれ、perl 5.10 以降でのみ利用可能です。
471=item Unclassified Traps
403L<perlsyn/"Switch Statements"> を参照してください。
404472
405=item *
473その他の罠。
406474
475=back
476
407477=begin original
408478
409Variables begin with "$", "@" or "%" in Perl.
479If you find an example of a conversion trap that is not listed here,
480please submit it to <F<perlbug@perl.org>> for inclusion.
481Also note that at least some of these can be caught with the
482C<use warnings> pragma or the B<-w> switch.
410483
411484=end original
412485
413Perl では数は "$" か "@" か "%" で始まります。
486もしここ挙げたリストにないような換の罠の
487例を見つけたら、それを <F<perlbug@perl.org>> まで送ってください。
488また、少なくともこれらのいくつかは C<use warnings> プラグマか
489B<-w> スイッチで捕捉できることに注意してください。
414490
415=item *
491=head2 Discontinuance, Deprecation, and BugFix traps
416492
417=begin original
493perl4 から、なくなったり修正されたことがら。
418494
419Comments begin with "#", not "/*" or "//". Perl may interpret C/C++
495=over 4
420comments as division operators, unterminated regular expressions or
421the defined-or operator.
422496
423=end original
497=item * Discontinuance
424498
425コメントの始ま、"#" であり、"/*" "//" ありません。
499“_”で始まるシンボルもはmain パッケージに強制的に結び付けられること
426Perl C/C++ のコメントを除算演算子、終端していない正規表現、
500C<$_> 自身(と C<@-> など)を除いてはくなりました。
427定義性和演算子として解釈するかもしれません。
428501
429=item *
502 package test;
503 $_legacy = 1;
430504
431=begin original
505 package main;
506 print "\$_legacy is ",$_legacy,"\n";
432507
433You can't take the address of anything, although a similar operator
508 # perl4 の出力: $_legacy is 1
434in Perl is the backslash, which creates a reference.
509 # perl5 の出力: $_legacy is
435510
436=end original
511=item * Deprecation
437512
438なにかのアドレスを得ることはできません; Perl は似たような演算子である
513ダブルコロン、変数名の中パッケージセパレーターなるようりました。
439バックスラッシュがありますが、リファレンスを生成します。
514のため、以下の例で perl4 と perl5 とでは振る舞いが変わります。
515これはパッケージが実在してないためです。
440516
441=item *
517 $a=1;$b=2;$c=3;$var=4;
518 print "$a::$b::$c ";
519 print "$var::abc::xyz\n";
442520
521 # perl4 の出力: 1::2::3 4::abc::xyz
522 # perl5 出力: 3
523
524C<::> は今では、(これがバグとして分類すべきかどうかはともかく)
525パッケージデリミタとしてみなされるようになっています
526(ここでは古いパッケージデリミタの ' を使っています)。
527 $x = 10 ;
528 print "x=${'x}\n" ;
529
530 # perl4 の出力: x=10
531 # perl5 の出力: Can't find string terminator "'" anywhere before EOF
532
443533=begin original
444534
445C<ARGV> must be capitalized. C<$ARGV[0]> is C's C<argv[1]>, and C<argv[0]>
535You can avoid this problem, and remain compatible with perl4, if you
446ends up in C<$0>.
536always explicitly include the package name:
447537
448538=end original
449539
450C<ARGV> は大文字ればなりません。
540常にパッケージ名を含めること、この問題を避つつ、perl4 との
451C<$ARGV[0]> C の C<argv[1]> に相当し、C<argv[0]> にあたるものは
541互換性を維持することができます:
452C<$0> です。
453542
454=item *
543 $x = 10 ;
544 print "x=${main'x}\n" ;
455545
456=begin original
546C<$:> の解析については、優先順位の罠 も参照してください。
457547
458System calls such as link(), unlink(), rename(), etc. return nonzero for
548=item * BugFix
459success, not 0. (system(), however, returns zero for success.)
460549
461=end original
550C<splice()> の第 2、第 3 引数は
551(ラクダ本にある通り)リストコンテキストではなく
552スカラーコンテキストで評価されるようになりました。
462553
463link(), unlink(), rename() などテムコールは、成功時に
554 sub sub1{return(0,2) } # 要素二つトを返す
4640 ではなく非 0を返しま
555 sub sub2{ return(1,2,3)} # 要素三つリストを返す
465(但し、system() は成功時に 0 を返します。)
556 @a1 = ("a","b","c","d","e");
557 @a2 = splice(@a1,&sub1,&sub2);
558 print join(' ',@a2),"\n";
466559
467=item *
560 # perl4 の出力: a b
561 # perl5 の出力: c d e
468562
563=item * Discontinuance
564
565最適化によってなくなってしまうようなブロックの中に
566飛び込むC<goto> は使えなくなりました。ちぇっ。
567
568 goto marker1;
569
570 for(1){
571 marker1:
572 print "Here I is!\n";
573 }
574
575 # perl4 prints: Here I is!
576 # perl5 errors: Can't "goto" into the middle of a foreach loop
577
578=item * Discontinuance
579
580空白を変数の名前や、クォート構造の区切りに使うことは
581構文的に正当なものではなくなりました。ちぇっ、ちぇっ。
582
583 $a = ("foo bar");
584 $b = q baz ;
585 print "a is $a, b is $b\n";
586
587 # perl4 の出力: a is foo bar, b is baz
588 # perl5 errors: Bareword found where operator expected
589
590=item * Discontinuance
591
592古い while/if BLOCK BLOCK の構文は、もはやサポートされていません。
593
594 if { 1 } {
595 print "True!";
596 }
597 else {
598 print "False!";
599 }
600
601 # perl4 の出力: True!
602 # perl5 errors: syntax error at test.pl line 1, near "if {"
603
604=item * BugFix
605
606C<**> 演算子の優先順位は、単項のマイナスよりも高くなりました。
607これは以前からドキュメントにはそうあったのですが、実際は違っていました。
608
609 print -4**2,"\n";
610
611 # perl4 の出力: 16
612 # perl5 の出力: -16
613
614=item * Discontinuance
615
616配列ではないリストに対する繰り返しで
617あるときの C<foreach{}> の意味が変わりました。
618以前はそういったリストはテンポラリな配列に代入されていましたが、
619現在はそうではありません(効率上の理由です)。
620これは、現在では値のコピーに対して繰り返しをするのではなく
621実際の値に対して繰り返しをするということです。
622
623 @list = ('ab','abc','bcd','def');
624 foreach $var (grep(/ab/,@list)){
625 $var = 1;
626 }
627 print (join(':',@list));
628
629 # perl4 の出力: ab:abc:bcd:def
630 # perl5 の出力: 1:1:bcd:def
631
632perl4 と同じようにするには、自分で陽にテンポラリの配列へと
633コピーしてからその配列に対して繰り返しを行います。例えば、
634
635 foreach $var (grep(/ab/,@list)){
636
637この部分を以下のように変更します。
638
639 foreach $var (@tmp = grep(/ab/,@list)){
640
641そうしないと、$var を変更したときに @list の値に影響が出ます
642(これはループ変数に C<$_> を使っていて、かつ、
643C<$_> を局所化していないようなサブルーチンを
644ループの中で呼ぶようなときに良く起こります)。
645
646=item * Discontinuance
647
648引数なしの C<split> の振る舞いは、C<split /\s+/>
649($_ が空白から始まっているときに先頭のフィールドが空になる)
650 のときと同じというものから、C<split ' '>
651($_ が空白から始まっているときに先頭のフィールドが空にならない)
652のときと同じものになりました。
653
654 $_ = ' hi mom';
655 print join(':', split);
656
657 # perl4 の出力: :hi:mom
658 # perl5 の出力: hi:mom
659
660=item * BugFix
661
662perl4 では、B<-e> スイッチにアタッチされたテキストは無視されて、常に
663後に続く引数からプログラムが取り出されていました。さらに、
664B<-e> スイッチの後に引数を渡さない場合も受け付けていました。
665これらの振る舞いは両方とも修正されました。
666
667 perl -e'print "attached to -e"' 'print "separate arg"'
668
669 # perl4 の出力: separate arg
670 # perl5 の出力: attached to -e
671
672 perl -e
673
674 # perl4 prints:
675 # perl5 dies: No code specified for -e.
676
677=item * Discontinuance
678
679perl4 では C<push> の戻り値はドキュメントに書かれていませんでしたが、
680実際には対象となるリストに最後にpushした値が返されていました。
681Perl5 では、C<push> の戻り値はドキュメントに明記され、かつそれは
682perl4 から変更されました。これは push した後のリストにある要素の数を
683返します。
684
685 @x = ('existing');
686 print push(@x, 'first new', 'second new');
687
688 # perl4 の出力: second new
689 # perl5 の出力: 3
690
691=item * Deprecation
692
693一部のエラーメッセージが異なっています。
694
695=item * Discontinuance
696
469697=begin original
470698
471Signal handlers deal with signal names, not numbers. Use C<kill -l>
699In Perl 4, if in list context the delimiters to the first argument of
472to find their names on your system.
700C<split()> were C<??>, the result would be placed in C<@_> as well as
701being returned. Perl 5 has more respect for your subroutine arguments.
473702
474703=end original
475704
476シグナルハンドラは、シグナル番号はなくシグナル名を扱います。
705Perl 4 では、リストコンテキスト C<split()> の最初の引数の
477使用できるシグナル名は、kill -l として確かめてくださ
706デリミタが C<??>った場合、返れる結果が C<@_> にも設定されました
707Perl 5 ではサブルーチンの引数により多くの敬意を払います。
478708
709=item * Discontinuance
710
711幾つかのバグがうかつにも修正されているかもしれません :-)
712
479713=back
480714
481=head2 JavaScript Traps
715=head2 Parsing Traps
482716
483(JavaScript の罠)
717(パースの罠)
484718
485719=begin original
486720
487Judicious JavaScript programmers should take note of the following:
721Perl4-to-Perl5 traps from having to do with parsing.
488722
489723=end original
490724
491思慮深い JavaScript プログラマは以下のこに注意すべきです:
725パースに関する Perl4 Perl5 の違いの罠です
492726
493727=over 4
494728
495=item *
729=item * Parsing
496730
497=begin original
731. と = の間にあるスペースに注意。
498732
499In Perl, binary C<+> is always addition. C<$string1 + $string2> converts
733 $string . = "more string";
500both strings to numbers and then adds them. To concatenate two strings,
734 print $string;
501use the C<.> operator.
502735
503=end original
736 # perl4 の出力: more string
737 # perl5 の出力: syntax error at - line 1, near ". ="
504738
505Perl では、二項の C<+> は常に加算です。
739=item * Parsing
506C<$string1 + $string2> は両方の文字列を数値に変換してから加算します。
507二つの文字列を結合するには、C<.> 演算子を使ってください。
508740
509=item *
741perl5 では構文解析が改良されました。
510742
743 sub foo {}
744 &foo
745 print("hello, world\n");
746
747 # perl4 の出力: hello, world
748 # perl5 の出力: syntax error
749
750=item * Parsing
751
752“それが関数に見えるのなら、それは関数だ”のルール。
753
754 print
755 ($foo == 1) ? "is one\n" : "is zero\n";
756
757 # perl4 の出力: is zero
758 # perl5 の警告: "Useless use of a constant in void context" if using -w
759
760=item * Parsing
761
762C<$#array> 構造の文字列展開で名前の周りにブレースがあるときには
763違いがあります。
764
765 @a = (1..3);
766 print "${#a}";
767
768 # perl4 prints: 2
769 # perl5 fails with syntax error
770
771 @ = (1..3);
772 print "$#{a}";
773
774 # perl4 prints: {a}
775 # perl5 prints: 2
776
777=back
778
779=head2 Numerical Traps
780
781(数値の罠)
782
511783=begin original
512784
513The C<+> unary operator doesn't do anything in Perl. It exists to avoid
785Perl4-to-Perl5 traps having to do with numerical operators,
514syntactic ambiguities.
786operands, or output from same.
515787
516788=end original
517789
518C<+> 単項演算子は Perl では何もしません。
790同じものに対する数値演算子、オペランド、出力に関する
519これは文法的な曖昧さを避けるために存在してす。
791Perl4 と Perl5 の違の罠です。
520792
521=item *
793=over 5
522794
795=item * Numerical
796
523797=begin original
524798
525Unlike C<for...in>, Perl's C<for> (also spelled C<foreach>) does not allow
799Formatted output and significant digits
526the left-hand side to be an arbitrary expression. It must be a variable:
527800
528801=end original
529802
530C<for...in> 異なりPerl C<for> (C<foreach> と書くこともあります) は
803書式指定された出力と、最下位数字。
531左側に任意の式を置くことはできません。
532これは変数でなければなりません:
533804
534 for my $variable (keys %hash) {
805 print 7.373504 - 0, "\n";
535 ...
806 printf "%20.18f\n", 7.373504 - 0;
536 }
537807
808 # Perl4 の出力:
809 7.375039999999996141
810 7.37503999999999614
811
812 # Perl5 の出力:
813 7.373504
814 7.37503999999999614
815
816=item * Numerical
817
818ここに記述されていた項目は削除されました。
819以前あったものでは、オートインクリメント演算子が符号付き整数の限界を越えたことを
820検知しないということを紹介していました。
821これはバージョン 5.003_04 で修正されましたが、大きな整数を取り扱うことに
822不安を感じているのであれば
823
824 use Math::BigInt;
825
826を使うことを考慮してください。
827
828=item * Numerical
829
830数値の等価性の比較の結果を代入しても、perl ではその比較の結果が
831偽 (0) であったときにはうまくいきません。
832論理比較は、現在 0 ではなく null を返します。
833
834 $p = ($test == 1);
835 print $p,"\n";
836
837 # perl4 の出力: 0
838 # perl5 出力:
839
840この新しい仕様の別の例は L<"General Regular Expression Traps using s///, etc.">
841を参照してください。
842
843=item * Bitwise string ops
844
538845=begin original
539846
540Furthermore, don't forget the C<keys> in there, as
847When bitwise operators which can operate upon either numbers or
541C<foreach my $kv (%hash) {}> iterates over the keys and values, and is
848strings (C<& | ^ ~>) are given only strings as arguments, perl4 would
542generally not useful ($kv would be a key, then a value, and so on).
849treat the operands as bitstrings so long as the program contained a call
850to the C<vec()> function. perl5 treats the string operands as bitstrings.
851(See L<perlop/Bitwise String Operators> for more details.)
543852
544853=end original
545854
546さらに、ここで C<keys> を忘れいでください; C<foreach my $kv (%hash) {}> は
855数値としても文字列としても操作可能ビット操作演算子 (C<& | ^ ~>)
547キー値に対して反復するので、一般的には有用ではありません ($kv はキーになり
856文字列のみを引数として与えられた場合
548次に値になりという形になります)
857perl4 はプログラムが C<vec()> 関数の呼び出しを含んでいればオペランドを
858ビット文字列として扱います。
859perl5 は文字列オペランドをビット文字列として扱います
860(さらなる詳細については L<perlop/Bitwise String Operators> を参照してください)。
549861
550=item *
862 $fred = "10";
863 $barney = "12";
864 $betty = $fred & $barney;
865 print "$betty\n";
866 # Uncomment the next line to change perl4's behavior
867 # ($dummy) = vec("dummy", 0, 0);
551868
869 # Perl4 prints:
870 8
871
872 # Perl5 prints:
873 10
874
875 # If vec() is used anywhere in the program, both print:
876 10
877
878=back
879
880=head2 General data type traps
881
882(一般的なデータ型の罠)
883
552884=begin original
553885
554To iterate over the indices of an array, use C<foreach my $i (0 .. $#array)
886Perl4-to-Perl5 traps involving most data-types, and their usage
555{}>. C<foreach my $v (@array) {}> iterates over the values.
887within certain expressions and/or context.
556888
557889=end original
558890
559配列インックスに対して反復するには
891ほとんどのデータ型とそれらの特定の表現やコンテキストでの
560C<foreach my $i (0 .. $#array) {}> を使ってください。
892使い方に関する Perl4 Perl5 の違の罠です
561C<foreach my $v (@array) {}> は値に対して反復します。
562893
563=item *
894=over 5
564895
896=item * (配列)
897
565898=begin original
566899
567Perl requires braces following C<if>, C<while>, C<foreach>, etc.
900Negative array subscripts now count from the end of the array.
568901
569902=end original
570903
571Perl は C<if>, C<while>, C<foreach> などに引き続いて中かっこ必要す。
904配列の添え字あったとき、それは配列の終端から数えられるように
905なりました。
572906
573=item *
907 @a = (1, 2, 3, 4, 5);
908 print "The third element of the array is $a[3] also expressed as $a[-2] \n";
574909
910 # perl4 の出力: The third element of the array is 4 also expressed as
911 # perl5 の出力: The third element of the array is 4 also expressed as 4
912
913=item * (配列)
914
915C<$#array> に(それまでよりも)小さな値を設定したときには、余計な
916配列要素は捨てられ、さらにそれが元に戻せないようになりました。
917
918 @a = (a,b,c,d,e);
919 print "Before: ",join('',@a);
920 $#a =1;
921 print ", After: ",join('',@a);
922 $#a =3;
923 print ", Recovered: ",join('',@a),"\n";
924
925 # perl4 の出力: Before: abcde, After: ab, Recovered: abcd
926 # perl5 の出力: Before: abcde, After: ab, Recovered: ab
927
928=item * (ハッシュ)
929
930ハッシュは使われる前に定義されます。
931
932 local($s,@a,%h);
933 die "scalar \$s defined" if defined($s);
934 die "array \@a defined" if defined(@a);
935 die "hash \%h defined" if defined(%h);
936
937 # perl4 の出力:
938 # perl5 dies: hash %h defined
939
575940=begin original
576941
577In Perl, C<else if> is spelled C<elsif>.
942Perl will now generate a warning when it sees defined(@a) and
943defined(%h).
578944
579945=end original
580946
581Perl 、C<else if> は C<elsif> と書きます。
947Perl は defined(@a) defined(%h) に警告を出ようになりました
582948
583=item *
949=item * (グロブ)
584950
951ある変数から別の変数へのグロブの代入は、
952代入された変数が代入の後で局所化されているときには
953失敗します。
954
955 @a = ("This is Perl 4");
956 *b = *a;
957 local(@a);
958 print @b,"\n";
959
960 # perl4 の出力: This is Perl 4
961 # perl5 の出力:
962
963=item * (グロブ)
964
585965=begin original
586966
587C<? :> has higher precedence than assignment. In JavaScript, one can
967Assigning C<undef> to a glob has no effect in Perl 5. In Perl 4
588write:
968it undefines the associated scalar (but may have other side effects
969including SEGVs). Perl 5 will also warn if C<undef> is assigned to a
970typeglob. (Note that assigning C<undef> to a typeglob is different
971than calling the C<undef> function on a typeglob (C<undef *foo>), which
972has quite a few effects.
589973
590974=end original
591975
592C<? :> 代入より高い優先順位を持ち
976C<undef> のグロブへの代入は Perl5 では何の影響も及ぼしせん
593JavaScript では、以下のように書て:
977Perl4 では結び付られたスカラーを undefine します
978(しかし SEGV を含め、なんらかの副作用があるかもしれません)。
979Perl 5 はまた型グロブに C<undef> を代入すると警告されます。
980(型グロブに C<undef> を代入するのは、型グロブに対して
981C<undef> 関数を呼び出すのとは違います)。いくつかの効果があります。
594982
595 condition ? do_something() : variable = 3
983 $foo = "bar";
984 *foo = undef;
985 print $foo;
596986
987 # perl4 prints:
988 # perl4 warns: "Use of uninitialized variable" if using -w
989 # perl5 prints: bar
990 # perl5 warns: "Undefined value assigned to typeglob" if using -w
991
992=item * (スカラー文字列)
993
994(文字列に対する)単項の符号反転の意味が変わりました。
995この変更は戻り値と、マジックインクリメントの両方に影響します。
996
997 $x = "aaa";
998 print ++$x," : ";
999 print -$x," : ";
1000 print ++$x,"\n";
1001
1002 # perl4 の出力: aab : -0 : 1
1003 # perl5 の出力: aab : -aab : aac
1004
1005=item * (定数)
1006
1007perl4 では定数を変更してしまいます。
1008
1009 $foo = "x";
1010 &mod($foo);
1011 for ($x = 0; $x < 3; $x++) {
1012 &mod("a");
1013 }
1014 sub mod {
1015 print "before: $_[0]";
1016 $_[0] = "m";
1017 print " after: $_[0]\n";
1018 }
1019
1020 # perl4:
1021 # before: x after: m
1022 # before: a after: m
1023 # before: m after: m
1024 # before: m after: m
1025
1026 # Perl5:
1027 # before: x after: m
1028 # Modification of a read-only value attempted at foo.pl line 12.
1029 # before: a
1030
1031=item * (スカラー)
1032
1033perl4 と perl5 とで全然違う動作:
1034
1035 print "$x", defined $x
1036
1037 # perl 4: 1
1038 # perl 5: <no output, $x is not called into existence>
1039
1040=item * (変数の自殺)
1041
1042perl5 では、変数の自殺(variable sucide)の振る舞いはより
1043首尾一貫したものとなりました。
1044perl4 ではスカラーのみがそうであるような振る舞いを、
1045perl5 ではハッシュとスカラーで示します。
1046
1047 $aGlobal{ "aKey" } = "global value";
1048 print "MAIN:", $aGlobal{"aKey"}, "\n";
1049 $GlobalLevel = 0;
1050 &test( *aGlobal );
1051
1052 sub test {
1053 local( *theArgument ) = @_;
1054 local( %aNewLocal ); # perl 4 != 5.001l,m
1055 $aNewLocal{"aKey"} = "this should never appear";
1056 print "SUB: ", $theArgument{"aKey"}, "\n";
1057 $aNewLocal{"aKey"} = "level $GlobalLevel"; # 出力すべきもの
1058 $GlobalLevel++;
1059 if( $GlobalLevel<4 ) {
1060 &test( *aNewLocal );
1061 }
1062 }
1063
1064 # Perl4:
1065 # MAIN:global value
1066 # SUB: global value
1067 # SUB: level 0
1068 # SUB: level 1
1069 # SUB: level 2
1070
1071 # Perl5:
1072 # MAIN:global value
1073 # SUB: global value
1074 # SUB: this should never appear
1075 # SUB: this should never appear
1076 # SUB: this should never appear
1077
1078=back
1079
1080=head2 Context Traps - scalar, list contexts
1081
1082(コンテキストの罠 - スカラーコンテキストとリストコンテキスト)
1083
1084=over 5
1085
1086=item * (リストコンテキスト)
1087
5971088=begin original
5981089
599and the variable is only assigned if the condition is false. In Perl, you
1090The elements of argument lists for formats are now evaluated in list
600need parentheses:
1091context. This means you can interpolate list values now.
6011092
6021093=end original
6031094
604条件が偽の場合のみ変数に代入されま
1095formatる引数リストの要素は、リストコンテキストで
605Perl では、かっこ必要です:
1096評価されるようになりました。これは、今やリストの値
1097展開できるようになったということです。
6061098
607 $condition ? do_something() : ($variable = 3);
1099 @fmt = ("foo","bar","baz");
1100 format STDOUT=
1101 @<<<<< @||||| @>>>>>
1102 @fmt;
1103 .
1104 write;
6081105
1106 # perl4 errors: Please use commas to separate fields in file
1107 # perl5 の出力: foo bar baz
1108
1109=item * (スカラーコンテキスト)
1110
1111C<caller()> 関数は呼び出し元がなく、スカラーコンテキストで呼ばれた
1112場合には偽を返すようになりました。これによりライブラリファイルが
1113(自分が)require されたのかを判断することができます。
1114
1115 caller() ? (print "You rang?\n") : (print "Got a 0\n");
1116
1117 # perl4 errors: There is no caller
1118 # perl5 の出力: Got a 0
1119
1120=item * (スカラーコンテキスト)
1121
1122スカラーコンテキストにあるカンマ演算子は、その引数に対して
1123スカラーコンテキストを与えるようになりました。
1124
1125 @y= ('a','b','c');
1126 $x = (1, 2, @y);
1127 print "x = $x\n";
1128
1129 # Perl4 prints: x = c # Thinks list context interpolates list
1130 # Perl5 prints: x = 3 # Knows scalar uses length of list
1131
1132=item * (list, builtin)
1133
6091134=begin original
6101135
611Or just use C<if>.
1136C<sprintf()> is prototyped as ($;@), so its first argument is given scalar
1137context. Thus, if passed an array, it will probably not do what you want,
1138unlike Perl 4:
6121139
6131140=end original
6141141
615または単に C<if> を使ってください。
1142C<sprintf()> のプロトタイプは ($;@) なので、最初の引数は
1143スカラコンテキストです。従って、配列を渡すと、
1144Perl 4 とは違っておそらくあなたの望まない結果になるでしょう。
6161145
617=item *
1146 @z = ('%s%s', 'foo', 'bar');
1147 $x = sprintf(@z);
1148 print $x;
6181149
1150 # perl4 prints: foobar
1151 # perl5 prints: 3
1152
6191153=begin original
6201154
621Perl requires semicolons to separate statements.
1155C<printf()> works the same as it did in Perl 4, though:
6221156
6231157=end original
6241158
625Perl は文の分割セミコロンが必要で
1159しかし、C<printf()> のほうは Perl 4 と同じよう動きま:
6261160
627=item *
1161 @z = ('%s%s', 'foo', 'bar');
1162 printf STDOUT (@z);
6281163
1164 # perl4 の出力: foobar
1165 # perl5 の出力: foobar
1166
1167=back
1168
1169=head2 Precedence Traps
1170
1171(評価順序の罠)
1172
6291173=begin original
6301174
631Variables declared with C<my> only affect code I<after> the declaration.
1175Perl4-to-Perl5 traps involving precedence order.
632You cannot write C<$x = 1; my $x;> and expect the first assignment to
633affect the same variable. It will instead assign to an C<$x> declared
634previously in an outer scope, or to a global variable.
6351176
6361177=end original
6371178
638C<my> で宣言された変数は宣言の I<後> み効果を持ちます。
1179評価順序に関する Perl4 Perl5 違いの罠です。
639C<$x = 1; my $x;> と書いて最初の代入が同じ変数に影響すると
640想定することはできません。
641これは外側のスコープ、あるいはグローバル変数の C<$x> に代入されます。
6421180
6431181=begin original
6441182
645Note also that the variable is not visible until the following
1183Perl 4 has almost the same precedence rules as Perl 5 for the operators
646I<statement>. This means that in C<my $x = 1 + $x> the second $x refers
1184that they both have. Perl 4 however, seems to have had some
647to one declared previously.
1185inconsistencies that made the behavior differ from what was documented.
6481186
6491187=end original
6501188
651また、変数I<文> まで有効にならないこにも注意しください。
1189Perl4 ほとんど演算子で Perl5同じ優先順位を持っています
652これは、C<my $x = 1 + $x> すると 2 番目の $x 前に宣言されたものを
1190しかし Perl4 では、ドキュメントとは少々異なるような
653参照すということです。
1191一貫性に欠けものがあります。
6541192
655=item *
1193=over 5
6561194
1195=item * Precedence
1196
6571197=begin original
6581198
659C<my> variables are scoped to the current block, not to the current
1199LHS vs. RHS when both sides are getting an op.
660function. If you write C<{my $x;} $x;>, the second C<$x> does not refer to
661the one declared inside the block.
6621200
6631201=end original
6641202
665C<my> 変数のスコープ現在の関数ではく現在のブロックです
1203左辺と右辺両方とも演算子を取るようにりました
666C<{my $x;} $x;> と書くと、2 番目の C<$x> はブロックの内側で宣言された変数を
667参照できません。
6681204
669=item *
1205 @arr = ( 'left', 'right' );
1206 $a{shift @arr} = shift @arr;
1207 print join( ' ', keys %a );
6701208
671=begin original
1209 # perl4 の出力: left
1210 # perl5 の出力: right
6721211
673An object's members cannot be made accessible as variables. The closest
1212=item * Precedence
674Perl equivalent to C<with(object) { method() }> is C<for>, which can alias
675C<$_> to the object:
6761213
677=end original
1214以下の例は、優先順位の関係で意味エラー(semantic error)となるようになりました。
6781215
679オブジェクトのメンバは変数としてアクセスできません。
1216 @list = (1,2,3,4,5);
680C<with(object) { method() }> Perl での一番近い等価物は C<for> で、
1217 %map = ("a",1,"b",2,"c",3,"d",4);
681これは C<$_> がオブジェク別名となります:
1218 $n = shift @list + 2; # リストの最初の要素に2を加える
1219 print "n is $n, ";
1220 $m = keys %map + 2; # ハッシュにあるアイテムの数+2
1221 print "m is $m\n";
6821222
683 for ($object) {
1223 # perl4 出力: n is 3, m is 6
684 $_->method;
1224 # perl5 errors and fails to compile
685 }
6861225
687=item *
1226=item * Precedence
6881227
1228代入演算子の優先順位は、代入と同じとなりました。
1229perl4 は間違って、この優先順位が関連演算子と同じものに
1230なっています。このため、式の中にあるのと同じように括弧でくくらなければ
1231ならなくなりました。
1232
1233 /foo/ ? ($a += 2) : ($a -= 2);
1234
1235下のようにしてしまうと、
1236
1237 /foo/ ? $a += 2 : $a -= 2
1238
1239これは
1240
1241 (/foo/ ? $a += 2 : $a) -= 2;
1242
1243のように間違って解析されてしまいます。
1244その一方で、
1245
1246 $a += /foo/ ? 1 : 2;
1247
1248これは C プログラマーが期待するであろう動作になりました。
1249
1250=item * Precedence
1251
1252 open FOO || die;
1253
1254これはもはや正しくなくなりました。ここではファイルハンドルを
1255括弧で括る必要があります。さもなければ、perl5 はこういった文を
1256そのデフォルトの優先順位のまま放っておきます(以下の例参照)。
1257
1258 open(FOO || die);
1259
1260 # perl4 opens or dies
1261 # perl5 opens FOO, dying only if 'FOO' is false, i.e. never
1262
1263=item * Precedence
1264
1265perl4 は特殊変数 C<$:> の優先順位に、perl5 が C<$::> を main パッケージと
1266みなすのと同じ優先順位を与えます。
1267
1268 $a = "x"; print "$::a";
1269
1270 # perl 4 の出力: -:a
1271 # perl 5 の出力: x
1272
1273=item * Precedence
1274
1275perl には、ファイルテスト演算子と代入演算子を組み合わせたときに
1276優先順位のバグがありました。したがって、Perl4 の優先順位テーブルでは
1277C<-e $foo .= "q"> は
1278C<((-e $foo) .= "q")> と解釈すべきなのに実際には
1279C<((-e $foo) .= "q")> と解釈していました。
1280Perl5 ではドキュメントにある通りの優先順位です。
1281
1282 -e $foo .= "q"
1283
1284 # perl4 の出力: no output
1285 # perl5 の出力: Can't modify -e in concatenation
1286
1287=item * Precedence
1288
6891289=begin original
6901290
691The object or class on which a method is called is passed as one of the
1291In perl4, keys(), each() and values() were special high-precedence operators
692method's arguments, not as a separate C<this> value.
1292that operated on a single hash, but in perl5, they are regular named unary
1293operators. As documented, named unary operators have lower precedence
1294than the arithmetic and concatenation operators C<+ - .>, but the perl4
1295variants of these operators actually bind tighter than C<+ - .>.
1296Thus, for:
6931297
6941298=end original
6951299
696メソッドが呼び出されたオブジェクトまたクラスは独立した C<this> 値でなく
1300perl4 では、key()、each()、values()シングルハッシュに対する
697メソッドの引数の一つとて渡されます
1301特別に高い評価順序を持った演算子で
1302しかし perl5 では、これらの演算子は通常の名前付き単項演算子となりました。
1303ドキュメントにある通り、名前付き単項演算子は、C<+ - .> のような
1304数学演算子や連結演算子よりも低い優先順位を持っています。
1305しかし、perl4 ではこれらの演算子よりも key() などのほうが強く演算対象と
1306結び付いていたのです。
1307したがって、以下の例のようになります:
6981308
1309 %foo = 1..10;
1310 print keys %foo - 1
1311
1312 # perl4 prints: 4
1313 # perl5 prints: Type of arg 1 to keys must be hash (not subtraction)
1314
1315この perl4 の振る舞いは便利であるかもしれませんが、一貫性に欠けます。
1316
6991317=back
7001318
701=head2 Sed Traps
1319=head2 General Regular Expression Traps using s///, etc.
7021320
703(sed の罠)
1321(s/// などを使ったときの一般的な正規表現の罠)
7041322
7051323=begin original
7061324
707Seasoned B<sed> programmers should take note of the following:
1325All types of RE traps.
7081326
7091327=end original
7101328
711熟練した B<sed> プログラマは以下のこと注意べきです:
1329正規表現る全てのタイプの罠。
7121330
713=over 4
1331=over 5
7141332
715=item *
1333=item * 正規表現
7161334
717=begin original
1335C<s'$lhs'$rhs'> はもはやいずれの辺にあっても展開されなくなりました。
1336以前は $lhs は展開し、$rhs を展開しませんでした。
1337(文字列にあるリテラルの '$' にはいまでもマッチしません)。
7181338
719A Perl program executes only once, not once for each input line. You can
1339 $a=1;$b=2;
720do an implicit loop with C<-n> or C<-p>.
1340 $string = '1 2 $a $b';
1341 $string =~ s'$a'$b';
1342 print $string,"\n";
7211343
722=end original
1344 # perl4 の出力: $b 2 $a $b
1345 # perl5 の出力: 1 2 $a $b
7231346
724Perl のプログラムは、1 度だけ実行されます; 入力行毎ではありません。
1347=item * 正規表現
725C<-n> や C<-p> を使って暗黙のループを使えます。
7261348
727=item *
1349C<m//g> は、その状態を正規表現ではなく検索対象の文字列に
1350結び付けるようになりました
1351(sub に対するブロックのスコープが残っているのであれば、
1352検索文字列の状態は失われます)。
7281353
729=begin original
1354 $_ = "ababab";
1355 while(m/ab/g){
1356 &doit("blah");
1357 }
1358 sub doit{local($_) = shift; print "Got $_ "}
7301359
731Backreferences in substitutions use "$" rather than "\".
1360 # perl4 prints: Got blah Got blah Got blah Got blah
1361 # perl5 の出力: infinite loop blah...
7321362
733=end original
1363=item * 正規表現
7341364
735置換における後方参照には"\" ではなく "$" 使います。
1365現在のところC<m//o> 量指定子無名サブルーチンの中にある正規表現で
1366使った場合、I<すべての> クロージャーはそのような無名サブルーチンから、
1367そういったクロージャの中で一番最初に使われたものの中で
1368コンパイルされたかのような正規表現を生成します。
7361369
737=item *
1370 sub build_match {
1371 my($left,$right) = @_;
1372 return sub { $_[0] =~ /$left stuff $right/o; };
1373 }
1374 $good = build_match('foo','bar');
1375 $bad = build_match('baz','blarch');
1376 print $good->('foo stuff bar') ? "ok\n" : "not ok\n";
1377 print $bad->('baz stuff blarch') ? "ok\n" : "not ok\n";
1378 print $bad->('foo stuff bar') ? "not ok\n" : "ok\n";
7381379
739=begin original
1380For most builds of Perl5, this will print:
1381ok
1382not ok
1383not ok
7401384
741The pattern matching metacharacters "(", ")", and "|" do not have backslashes
1385この例の場合、build_match() は常にC<最初>に build_match() が呼ばれたときの
742in front.
1386$left と $right の内容にマッチするようなサブルーチンを返します。
1387呼び出されたその時点での値ではありません。
7431388
744=end original
1389=item * 正規表現
7451390
746"(", ")", "|" といったパターンマッチのメタキャラクタはその直前に
1391マッチング中で括弧が使われなかった場合perl4 では
747クスラシュを置く必要はありせん。
1392C<$+> には C<$&> と同じようにマチした全体がセトされすが、
1393Perl5 ではそうではありません。
7481394
749=item *
1395 "abcdef" =~ /b.*e/;
1396 print "\$+ = $+\n";
7501397
751=begin original
1398 # perl4 の出力: bcde
1399 # perl5 の出力:
7521400
753The range operator is C<...>, rather than comma.
1401=item * 正規表現
7541402
755=end original
1403置換は、失敗したときには空文字列を返すようになりました。
7561404
757範囲演算子は C<...> であって、カンマではありません。
1405 $string = "test";
1406 $value = ($string =~ s/foo//);
1407 print $value, "\n";
7581408
759=back
1409 # perl4 の出力: 0
1410 # perl5 の出力:
7601411
761=head2 Shell Traps
1412この新しい仕様に関しては L<数値に関する罠> も参照してください。
7621413
763(shell の罠)
1414=item * 正規表現
7641415
765=begin original
1416C<s`lhs`rhs`> (バッククォートの使用)は通常の置換となり、
1417バッククォートの展開は行われなくなりました。
7661418
767Sharp shell programmers should take note of the following:
1419 $string = "";
1420 $string =~ s`^`hostname`;
1421 print $string, "\n";
7681422
769=end original
1423 # perl4 の出力: <the local hostname>
1424 # perl5 の出力: hostname
7701425
771鋭いシェルプログラマは以下のことに注意すべきです:
1426=item * 正規表現
7721427
773=over 4
1428正規表現中の変数の使用に関する構文解析がより厳密になりました。
7741429
775=item *
1430 s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;
7761431
1432 # perl4: compiles w/o error
1433 # perl5: with Scalar found where operator expected ..., near "$opt$plus"
1434
7771435=begin original
7781436
779The backtick operator does variable interpolation without regard to
1437an added component of this example, apparently from the same script, is
780the presence of single quotes in the command.
1438the actual value of the s'd string after the substitution.
1439C<[$opt]> is a character class in perl4 and an array subscript in perl5
7811440
7821441=end original
7831442
784バックォー演算子は、コマンド内にシングルクォートがあっても
1443同じスリプでこの例に付け加えことは、
785変数展開を行ないます。
1444置換後文字列の実際の値です。
1445C<[$opt]> は perl4 ではキャラクタクラスであり、perl5 では
1446配列の添え字となります。
7861447
787=item *
1448 $grpc = 'a';
1449 $opt = 'r';
1450 $_ = 'bar';
1451 s/^([^$grpc]*$grpc[$opt]?)/foo/;
1452 print ;
7881453
789=begin original
1454 # perl4 の出力: foo
1455 # perl5 の出力: foobar
7901456
791The backtick operator does no translation of the return value, unlike B<csh>.
1457=item * Regular Expression
7921458
793=end original
1459perl5 では、C<m?x?> は C<?x?> と同様に一回だけマッチします。
1460perl4 では、C</x/> や C<m!x!> と同じように何度でもマッチします。
7941461
795バッククォート演算子は B<csh> とは違って、返された値を変換しません。
1462 $test = "once";
1463 sub match { $test =~ m?once?; }
1464 &match();
1465 if( &match() ) {
1466 # m?x? が二回以上マッチ
1467 print "perl4\n";
1468 } else {
1469 # m?x? が一回だけマッチ
1470 print "perl5\n";
1471 }
7961472
797=item *
1473 # perl4 の出力: perl4
1474 # perl5 の出力: perl5
7981475
1476=back
1477
1478=head2 Subroutine, Signal, Sorting Traps
1479
1480(サブルーチン、シグナル、ソートの罠)
1481
7991482=begin original
8001483
801Shells (especially B<csh>) do several levels of substitution on each
1484The general group of Perl4-to-Perl5 traps having to do with
802command line. Perl does substitution in only certain constructs
1485Signals, Sorting, and their related subroutines, as well as
803such as double quotes, backticks, angle brackets, and search patterns.
1486general subroutine traps. Includes some OS-Specific traps.
8041487
8051488=end original
8061489
807シェル (特に B<csh>) は、コマンドラインごと何段階もの置換を行ないます。
1490Perl4 Perl5 の違いの罠分類される一般的なもの
808Perl はダブクォートバッククォート、山かっこ、検索パターンといった
1491シグナル、ート、そしてそれらに関連する
809特定の構造だけ置換を行なます。
1492サブルーチンくつかの OS 固有の罠を含めた
1493サブルーチンの罠と同じ様なものです。
8101494
811=item *
1495=over 5
8121496
813=begin original
1497=item * (Signals)
8141498
815Shells interpret scripts a little bit at a time. Perl compiles the
1499文字列のように見える裸の単語は、その名前が使うよりも前に
816entire program before executing it (except for C<BEGIN> blocks, which
1500サブルーチンの名前として定義されている場合にはサブルーチンとして
817execute at compile time).
1501みなされます。
8181502
819=end original
1503 sub SeeYa { warn"Hasta la vista, baby!" }
1504 $SIG{'TERM'} = SeeYa;
1505 print "SIGTERM is now $SIG{'TERM'}\n";
8201506
821シェルは一度に少しずつ解釈を行ないます。
1507 # perl4 prints: SIGTERM is now main'SeeYa
822Perl は実行前にプログラム全体をコンパイルします
1508 # perl5 prints: SIGTERM is now main::1 (and warns "Hasta la vista, baby!")
823(コンパイル時に実行される C<BEGIN> ブロックを除く)。
8241509
825=item *
1510B<-w> を使って、これを発見できます。
8261511
827=begin original
1512=item * (Sort Subroutine)
8281513
829The arguments are available via @ARGV, not $1, $2, etc.
1514reverse はもはやソートのサブルーチンの名前としては
1515使えなくなりました。
8301516
831=end original
1517 sub reverse{ print "yup "; $a <=> $b }
1518 print sort reverse (2,1,3);
8321519
833引数は $1, $2 などではなく、@ARGV から得られます。
1520 # perl4 prints: yup yup 123
1521 # perl5 prints: 123
1522 # perl5 warns (if using -w): Ambiguous call resolved as CORE::reverse()
8341523
835=item *
1524=item * warn() won't let you specify a filehandle.
8361525
837=begin original
1526常に STDERR に出力していたにもかかわらず、perl4 では warn() はファイルハンドルの
1527指定を必要としていましたが、perl5 では必要なくなりました。
8381528
839The environment is not automatically made available as separate scalar
1529 warn STDERR "Foo!";
840variables.
8411530
842=end original
1531 # perl4 の出力: Foo!
1532 # perl5 の出力: String found where operator expected
8431533
844環境変数は、自動的には独立したスカラ変数として利用できるようになりません。
1534=back
8451535
846=item *
1536=head2 OS Traps
8471537
1538(OS の罠)
1539
1540=over 5
1541
1542=item * (SysV)
1543
8481544=begin original
8491545
850The shell's C<test> uses "=", "!=", "<" etc for string comparisons and "-eq",
1546Under HPUX, and some other SysV OSes, one had to reset any signal handler,
851"-ne", "-lt" etc for numeric comparisons. This is the reverse of Perl, which
1547within the signal handler function, each time a signal was handled with
852uses C<eq>, C<ne>, C<lt> for string comparisons, and C<==>, C<!=> C<< < >> etc
1548perl4. With perl5, the reset is now done correctly. Any code relying
853for numeric comparisons.
1549on the handler _not_ being reset will have to be reworked.
8541550
8551551=end original
8561552
857シェルC<test> "=", "!=", "<" などを文字列比較に使い"-eq", "-ne",
1553HPUX 及び一部SysV OS ではperl4 のときには
858"-lt" などを数値比較に使いま
1554シグナルが発生る度にそのシグナルハンドラ関数の中で
859は Perl とは逆す; Perl では C<eq>, C<ne>, C<lt> を文字列比較に使い、
1555シグナルハンドラを再設定しなけばなりませんした。
860C<==>, C<!=> C<< < >> など数値比較使い
1556perl5 では、この再設定正しく行うようなりした
1557ハンドラを再設定しないということに依存したプログラムは
1558作業しなおす必要があります。
8611559
15605.002 以降の perl では、SysV のときには sigaction() を使います。
1561
1562 sub gotit {
1563 print "Got @_... ";
1564 }
1565 $SIG{'INT'} = 'gotit';
1566
1567 $| = 1;
1568 $pid = fork;
1569 if ($pid) {
1570 kill('INT', $pid);
1571 sleep(1);
1572 kill('INT', $pid);
1573 } else {
1574 while (1) {sleep(10);}
1575 }
1576
1577 # perl4 (HPUX) prints: Got INT...
1578 # perl5 (HPUX) prints: Got INT... Got INT...
1579
1580=item * (SysV)
1581
1582SysV OS では、
1583アペンドモード(C<<< >> >>>)でオープンしたファイルに対する
1584C<seek()> は fopen() マニュアルページにあるように正しく動作するように
1585なりました。例えば、アペンドモードでファイルをオープンした場合には
1586既にファイルにある情報を上書きすることはできません。
1587
1588 open(TEST,">>seek.test");
1589 $start = tell TEST ;
1590 foreach(1 .. 9){
1591 print TEST "$_ ";
1592 }
1593 $end = tell TEST ;
1594 seek(TEST,$start,0);
1595 print TEST "18 characters here";
1596
1597 # perl4 (solaris) seek.test has: 18 characters here
1598 # perl5 (solaris) seek.test has: 1 2 3 4 5 6 7 8 9 18 characters here
1599
1600
1601
8621602=back
8631603
864=head2 Perl Traps
1604=head2 Interpolation Traps
8651605
866(Perl の罠)
1606(展開の罠)
8671607
8681608=begin original
8691609
870Practicing Perl Programmers should take note of the following:
1610Perl4-to-Perl5 traps having to do with how things get interpolated
1611within certain expressions, statements, contexts, or whatever.
8711612
8721613=end original
8731614
874実践的な Perl プログラマは以下こと注意すべきです:
1615Perl4 Perl5違いの罠は、式や文、コンテキストなどにおける
1616展開に関するものがあります。
8751617
876=over 4
1618=over 5
8771619
878=item *
1620=item * Interpolation
8791621
880=begin original
1622ダブルクォートで囲まれた文字列にある @ は常に配列に展開されます。
8811623
882Remember that many operations behave differently in a list
1624 print "To: someone@somewhere.com\n";
883context than they do in a scalar one. See L<perldata> for details.
8841625
885=end original
1626 # perl < 5.6.1, error : In string, @somewhere now must be written as \@somewhere
1627 # perl >= 5.6.1, warning : Possible unintended interpolation of @somewhere in string
8861628
887多くの演算子がリストコンテキストとスカラコンテキストとで
1629=item * Interpolation
888振る舞いが変わることを忘れないでください。
889詳しくは L<perldata> を参照してください。
8901630
891=item *
1631ダブルクォートで括られた文字列がエスケープされていない $ や @ で
1632終了することがなくなりました。
8921633
893=begin original
1634 $foo = "foo$";
1635 $bar = "bar@";
1636 print "foo is $foo, bar is $bar\n";
8941637
895Avoid barewords if you can, especially all lowercase ones.
1638 # perl4 の出力: foo is foo$, bar is bar@
896You can't tell by just looking at it whether a bareword is
1639 # perl5 errors: Final $ should be \$ or $name
897a function or a string. By using quotes on strings and
898parentheses on function calls, you won't ever get them confused.
8991640
900=end original
1641注意:perl5 は、$bar の終端にある @ については“エラーにしません”
9011642
902裸の単語、特に全てが小文字のものはできる限り使わないでください。
1643=item * Interpolation
903見た目だけではその「裸の単語」が関数なのか、
904文字列なのかが判断できません。
905文字列にはクォートを、関数呼び出しには括弧をつければ、
906迷うこともないでしょう。
9071644
908=item *
1645Perl はダブルクォート中にあるブレースの内側の任意の式を
1646評価するようになりました(一般的には、C<$> や C<@> に続いて
1647開きのブレースがきたときです)。
9091648
1649 @www = "buz";
1650 $foo = "foo";
1651 $bar = "bar";
1652 sub foo { return "bar" };
1653 print "|@{w.w.w}|${main'foo}|";
1654
1655 # perl4 の出力: |@{w.w.w}|foo|
1656 # perl5 の出力: |buz|bar|
1657
1658C<use strict;> を使って、perl5 におけるこのような罠を避けることが
1659できることに注意してください。
1660
1661=item * Interpolation
1662
9101663=begin original
9111664
912You cannot discern from mere inspection which builtins
1665The construct "this is $$x" used to interpolate the pid at that point, but
913are unary operators (like chop() and chdir())
1666now tries to dereference $x. C<$$> by itself still works fine, however.
914and which are list operators (like print() and unlink()).
915(Unless prototyped, user-defined subroutines can B<only> be list
916operators, never unary ones.) See L<perlop> and L<perlsub>.
9171667
9181668=end original
9191669
920組込み関数のどれが(chop() や chdir())のような単項演算子で
1670"this is $$x" はプロセス ID を展開するようっていましたが
921どれが(print() や unlink())のような
1671今は $x の参照外し(dereference)を試みます。
922リスト演算子あるか見ただけはわかりません。
1672それも C<$$> 自身もきちと動作します
923(プロトタイプがなければ、ユーザー定義サブルーチンは
924リスト演算子として B<のみ> 定義でき、単項演算子にはできません。)
925L<perlop> と L<perlsub> を参照してください。
9261673
927=item *
1674 $s = "a reference";
1675 $x = *s;
1676 print "this is $$x\n";
9281677
929=begin original
1678 # perl4 の出力: this is XXXx (XXX はカレントプロセスID)
1679 # perl5 prints: this is a reference
9301680
931People have a hard time remembering that some functions
1681=item * Interpolation
932default to $_, or @ARGV, or whatever, but that others which
933you might expect to do not.
9341682
935=end original
1683C<eval "EXPR"> を使ったその場(on the fly)でのハッシュの生成は
1684ハッシュの名前を指定する C<$> がプロテクトされているか、もしくは
1685両方のカーリーブレースがプロテクトされていることを要求します。
1686両方のカーリーブレースがプロテクトされている場合には、perl4 と
1687perl5 の結果は同じです。
9361688
937いくつかの関数が $_ や @ARGV などをデフォルトにしていますが、
938同じことを期待する他の関数がデフォルトになっていないことを覚えるのに、
939辛いタイピングが必要でしょう。
9401689
941=item *
1690 $hashname = "foobar";
1691 $key = "baz";
1692 $value = 1234;
1693 eval "\$$hashname{'$key'} = q|$value|";
1694 (defined($foobar{'baz'})) ? (print "Yup") : (print "Nope");
9421695
943=begin original
1696 # perl4 の出力: Yup
1697 # perl5 の出力: Nope
9441698
945The <FH> construct is not the name of the filehandle, it is a readline
1699以下のものを:
946operation on that handle. The data read is assigned to $_ only if the
947file read is the sole condition in a while loop:
9481700
949=end original
1701 eval "\$$hashname{'$key'} = q|$value|";
9501702
951<FH> 構造はファイルハンドルではなく、そハンドルする行読み込みの
1703よう変更すると:
952操作(readline operation)です。
953while ループの条件式の中にこのファイル読み込みだけがあった場合には
954読み込まれたデータは $_ に代入されます。
9551704
956 while (<FH>) { }
1705 eval "\$\$hashname{'$key'} = q|$value|";
957 while (defined($_ = <FH>)) { }..
958 <FH>; # data discarded!
9591706
960=item *
1707結果はこうなります:
9611708
962=begin original
1709 # perl4 の出力: Nope
1710 # perl5 の出力: Yup
9631711
964Remember not to use C<=> when you need C<=~>;
1712以下のように変更した場合には
965these two constructs are quite different:
9661713
967=end original
1714 eval "\$$hashname\{'$key'\} = q|$value|";
9681715
969C<=~> が必要なとろで C<=> を使わない、といことを忘れいでください;
1716結果はこうなります。
970これら二つの構造はかなり違います:
9711717
972 $x = /foo/;
1718 # perl4 の出力: Yup
973 $x =~ /foo/;
1719 # perl5 の出力: Yup
1720 # これは両方のバージョンで同じ結果となります
9741721
975=item *
1722=item * Interpolation
9761723
977=begin original
1724以前のバージョンにあったバグに依存しているような perl4 プログラム。
9781725
979The C<do {}> construct isn't a real loop that you can use
1726 perl -e '$bar=q/not/; print "This is $foo{$bar} perl5"'
980loop control on.
9811727
982=end original
1728 # perl4 の出力: This is not perl5
1729 # perl5 の出力: This is perl5
9831730
984C<do {}> 構造は、ループ制御を行えるような本当のループではありません。
1731=item * Interpolation
9851732
986=item *
1733配列の参照についても注意する必要があります。
9871734
1735 print "$foo{"
1736
1737 perl 4 の出力: {
1738 perl 5 の出力: syntax error
1739
1740=item * Interpolation
1741
1742同様に、以下のようなことも注意してください:
1743
1744 $foo = "baz";
1745 print "\$$foo{bar}\n";
1746
1747 # perl4 prints: $baz{bar}
1748 # perl5 の出力: $
1749
1750perl5 は存在しない C<$foo{bar}> を探しに行きますが、perl4 は
1751$foo を“baz”に展開しただけで満足します。
1752C<eval> でもこの事に注意してください。
1753
1754=item * Interpolation
1755
1756C<eval> に C<qq()> された文字列を渡した場合
1757
1758 eval qq(
1759 foreach \$y (keys %\$x\) {
1760 \$count++;
1761 }
1762 );
1763
1764 # perl4 runs this ok
1765 # perl5 prints: Can't find string terminator ")"
1766
1767=back
1768
1769=head2 DBM Traps
1770
9881771=begin original
9891772
990Use C<my()> for local variables whenever you can get away with
1773General DBM traps.
991it (but see L<perlform> for where you can't).
992Using C<local()> actually gives a local value to a global
993variable, which leaves you open to unforeseen side-effects
994of dynamic scoping.
9951774
9961775=end original
9971776
998ローカル変数は、C<my()> で済むところではこれで済ませてください
1777DBMに関する一般的な罠。
999(使えない場所については、L<perlform> を参照してください)。
1000C<local()> を使えばグローバル変数に対するローカルな値を与えますが、
1001動的スコープの不慮の副作用の可能性は、そのままです。
10021778
1003=item *
1779=over 5
10041780
1781=item * DBM
1782
1783perl4 で(もしくは他の dbm/ndbm ツールで)作成した dbm データベースを、
1784perl5 の元で同じスクリプトで扱おうとすると失敗します。
1785perl5 の作成において、拡張 dbm の実装を C<tie> しないときに
1786C<dbmopen()> がデフォルトで使用する関数のために
1787(perl4 のときと同じ)同じ dbm/ndbm をリンクしていなければなりません。
1788
1789 dbmopen (%dbm, "file", undef);
1790 print "ok\n";
1791
1792 # perl4 の出力: ok
1793 # perl5 の出力: ok (IFF linked with -ldbm or -lndbm)
1794
1795
1796=item * DBM
1797
1798key/value のサイズに関する制限を超えたときに発生するエラーにより、
1799perl5 では即座にプログラムから exit します。
1800
1801 dbmopen(DB, "testdb",0600) || die "couldn't open db! $!";
1802 $DB{'trap'} = "x" x 1024; # ほとんどの dbm/ndbmには大きすぎる値
1803 print "YUP\n";
1804
1805 # perl4 の出力
1806 dbm store returned -1, errno 28, key "trap" at - line 3.
1807 YUP
1808
1809 # perl5 の出力
1810 dbm store returned -1, errno 28, key "trap" at - line 3.
1811
1812=back
1813
1814=head2 Unclassified Traps
1815
1816(未分類の罠)
1817
10051818=begin original
10061819
1007If you localize an exported variable in a module, its exported value will
1820Everything else.
1008not change. The local name becomes an alias to a new value but the
1009external name is still an alias for the original.
10101821
10111822=end original
10121823
1013モジュールにある export された変数を局所化すると、その export された
1824その他全て。
1014値は変更されません。
1015ローカル名は新しい値の別名(alias)となりますが、
1016外部名は元々の値の別名のままです。
10171825
1826=over 5
1827
1828=item * C<require>/C<do> の戻り値を使ったときの罠
1829
1830doit.pl というファイルが
1831
1832 sub foo {
1833 $rc = do "./do.pl";
1834 return 8;
1835 }
1836 print &foo, "\n";
1837
1838であって、do.pl が以下のような内容だったとすると、
1839
1840 return 3;
1841
1842doit.pl の実行結果はこうなります。
1843
1844 # perl 4 の出力: 3 (サブルーチンからすぐに抜けます)
1845 # perl 5 の出力: 8
1846
1847C<do> を C<require> に置き換えても同じ振る舞いとなります。
1848
1849=item * 空文字列に LIMIT を指定して C<split> を使った場合
1850
1851 $string = '';
1852 @list = split(/foo/, $string, 2)
1853
1854Perl4 は空文字列を持った一要素のリストを返しますが、Perl5 は
1855空リストを返します。
1856
10181857=back
10191858
10201859=begin original
10211860
10221861As always, if any of these are ever officially declared as bugs,
10231862they'll be fixed and removed.
10241863
10251864=end original
10261865
10271866いつものように、バグとして公式に宣言されたものがあれば、
10281867それは修正されて取り除かれるでしょう。
1029
1030=begin meta
1031
1032Translate: KIMURA Koichi (5.005_03)
1033Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-)
1034Status: completed
1035
1036=end meta