perltrap >
5.24.1
との差分
perltrap 5.24.1 と 5.6.1 の差分
1 | ||
2 | 1 | =encoding euc-jp |
3 | 2 | |
4 | 3 | =head1 NAME |
5 | 4 | |
6 | ||
5 | perltrap - Perlの不注意による罠 | |
7 | 6 | |
8 | perltrap - Perl traps for the unwary | |
9 | ||
10 | =end original | |
11 | ||
12 | perltrap - 不注意による Perl の罠 | |
13 | ||
14 | 7 | =head1 DESCRIPTION |
15 | 8 | |
16 | 9 | =begin original |
17 | 10 | |
18 | 11 | The biggest trap of all is forgetting to C<use warnings> or use the B<-w> |
19 | switch; see L<warn | |
12 | switch; see L<perllexwarn> and L<perlrun>. The second biggest trap is not | |
20 | 13 | making your entire program runnable under C<use strict>. The third biggest |
21 | 14 | trap is not reading the list of changes in this version of Perl; see |
22 | 15 | L<perldelta>. |
23 | 16 | |
24 | 17 | =end original |
25 | 18 | |
26 | 最も大きな罠とは、C<use warnings> あるいは B<-w> スイッチを | |
19 | 最も大きな罠とは、C<use warnings> あるいは B<-w> スイッチを | |
27 | 忘れてしまうということです | |
20 | 使うのを忘れてしまうということです。 | |
28 | 参照してください。 | |
21 | L<perllexwarn> と L<perlrun> を参照してください。 | |
29 | 二番目に大きな罠とは、あなたのプログラム全体を | |
22 | 二番目に大きな罠とは、あなたのプログラム全体を | |
30 | 実行しないということです。 | |
23 | C<use strict> の元で実行しないということです。 | |
31 | 三番目の罠は、このバージョン | |
24 | 三番目の罠は、Perl のこのバージョンでの変更点を読まないということです。 | |
32 | 25 | L<perldelta> を参照してください。 |
33 | 26 | |
34 | 27 | =head2 Awk Traps |
35 | 28 | |
36 | 29 | (awk の罠) |
37 | 30 | |
38 | ||
31 | B<awk> に慣れた方は、以下のようなことに特に注意してください: | |
39 | 32 | |
40 | ||
33 | =over 4 | |
41 | 34 | |
42 | =e | |
35 | =item * | |
43 | 36 | |
44 | ||
37 | Englishモジュールを | |
45 | 38 | |
46 | ||
39 | use English; | |
47 | 40 | |
41 | のようにしてロードすれば、B<awk> でそうであったように | |
42 | (C<$/> のような)特殊変数を($RS のような)名前で参照することができます。 | |
43 | 詳しくは L<perlvar> を参照してください。 | |
44 | ||
48 | 45 | =item * |
49 | 46 | |
50 | ||
47 | Perlでは、すべての単純文(simple statement)の末尾にセミコロンが必要です | |
48 | (ブロックの最後に置かれたときを除きます)。 | |
49 | 改行は文の区切りとはなりません。 | |
51 | 50 | |
52 | ||
51 | =item * | |
53 | do an implicit loop with C<-n> or C<-p>. | |
54 | 52 | |
55 | ||
53 | C<if> や C<while> ではカーリーブレースが必要です。 | |
56 | 54 | |
57 | ||
55 | =item * | |
58 | C<-n> や C<-p> を使って暗黙のループを使えます。 | |
59 | 56 | |
57 | Perlでは、変数は“$”"か“@”か“%”で始まります。 | |
58 | ||
60 | 59 | =item * |
61 | 60 | |
62 | ||
61 | 配列の添え字は 0 から始まります。substr() や index() での文字列の位置も | |
62 | 同様です。 | |
63 | 63 | |
64 | ||
64 | =item * | |
65 | 65 | |
66 | ||
66 | 配列の添え字が数値であるか、文字列であるかを決めなければなりません。 | |
67 | 67 | |
68 | ||
68 | =item * | |
69 | 69 | |
70 | ||
70 | ハッシュ(連想配列)の値は、単に参照するだけでは存在することに | |
71 | なりません。 | |
71 | 72 | |
72 | = | |
73 | =item * | |
73 | 74 | |
74 | ||
75 | 比較を文字列によって行うのか、数値によって行うのかを | |
75 | ||
76 | 決めなければなりません。 | |
76 | 77 | |
77 | =e | |
78 | =item * | |
78 | 79 | |
79 | ||
80 | 入力を読み込むだけでは split は行われません。 | |
80 | ||
81 | 配列への split は自分で行います。 | |
81 | ||
82 | また、split() 演算子の引数は B<awk> のものと異なっています。 | |
82 | 83 | |
83 | 84 | =item * |
84 | 85 | |
85 | ||
86 | 通常、カレント行は $0 ではなく $_ にあります。 | |
87 | 一般的に、改行は取り除かれません | |
88 | ($0 には実行しているプログラムの名前があります)。 | |
89 | L<perlvar> を参照してください。 | |
86 | 90 | |
87 | ||
91 | =item * | |
88 | at the end of a block). Newline is not a statement delimiter. | |
89 | 92 | |
90 | ||
93 | $<I<digit>> はフィールドを参照しません。これは | |
94 | 直前に行ったパターンマッチングの部分文字列を参照します。 | |
91 | 95 | |
92 | ||
96 | =item * | |
93 | (ブロックの最後に置かれたときを除きます)。 | |
94 | 改行は文の区切りとはなりません。 | |
95 | 97 | |
98 | print() 文は、C<$,> や C<$\> に値を設定しない限りフィールド区切り子や | |
99 | レコード区切り子を付加しません。English モジュールを使っていれば、 | |
100 | $OFS や $ORS に対して設定することでも OK です。 | |
101 | ||
96 | 102 | =item * |
97 | 103 | |
98 | ||
104 | ファイルに対して出力する前には、そのファイルをあらかじめオープンして | |
105 | おかなければなりません。 | |
99 | 106 | |
100 | ||
107 | =item * | |
101 | 108 | |
102 | ||
109 | 範囲演算子は“..”であって、カンマではありません。カンマ演算子は | |
110 | Cと同じような振る舞いをします。 | |
103 | 111 | |
104 | ||
112 | =item * | |
105 | 113 | |
114 | マッチ演算子は“=~”であって、“~”ではありません | |
115 | (“~”はCと同様に、1の補数を取る演算子です)。 | |
116 | ||
106 | 117 | =item * |
107 | 118 | |
108 | ||
119 | べき乗の演算子は“**”であって、“^”ではありません。 | |
120 | “^”はCと同様、XOR演算子です | |
121 | (B<awk> が基本的に C と非互換であることにお気付きかもしれませんね)。 | |
109 | 122 | |
110 | ||
123 | =item * | |
111 | 124 | |
112 | ||
125 | 連接演算子は“.”であって、空文字列ではありません | |
126 | (空文字列を使ってしまうと | |
127 | C</pat/ /pat/> が、その三番目のスラッシュが除算演算子と解釈されてしまうので | |
128 | 正しく解析できなくなります。 | |
129 | Perl の字句解析器は "/", "?", ">" といった演算子に対して | |
130 | 多少文脈依存となっています。実際、"." 自身も数値の始まりとなる | |
131 | 可能性もあります) | |
113 | 132 | |
114 | ||
133 | =item * | |
115 | 134 | |
135 | キーワード C<next>, C<exit>, C<continue> の振る舞いが異なります。 | |
136 | ||
116 | 137 | =item * |
117 | 138 | |
118 | ||
139 | 以下の変数の働きが異なります。 | |
119 | 140 | |
120 | ||
141 | Awk Perl | |
121 | ||
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 $; | |
122 | 156 | |
123 | =e | |
157 | =item * | |
124 | 158 | |
125 | ||
159 | $RS に正規表現をセットすることはできません。できるのは文字列だけです。 | |
126 | substr() や index() での文字列の位置も同様です。 | |
127 | 160 | |
128 | 161 | =item * |
129 | 162 | |
163 | 妙だと思ったときには awk の構文を a2p に通して、 | |
164 | 出力されたものを見てみましょう。 | |
165 | ||
166 | =back | |
167 | ||
168 | =head2 C Traps | |
169 | ||
170 | (C の罠) | |
171 | ||
130 | 172 | =begin original |
131 | 173 | |
132 | ||
174 | Cerebral C programmers should take note of the following: | |
133 | 175 | |
134 | 176 | =end original |
135 | 177 | |
136 | ||
178 | 知的な C プログラマは以下のことに注意すべきです: | |
137 | 179 | |
180 | =over 4 | |
181 | ||
138 | 182 | =item * |
139 | 183 | |
140 | ||
184 | C<if> や C<while> にはカーリーブレースが必要です。 | |
141 | 185 | |
142 | ||
186 | =item * | |
143 | 187 | |
144 | ||
188 | C<else if> ではなく、C<elsif> を使わなければなりません。 | |
145 | 189 | |
146 | ハッシュ(連想配列)の値は、単に参照するだけでは存在することになりません。 | |
147 | ||
148 | 190 | =item * |
149 | 191 | |
150 | ||
192 | C のC<break> と C<continue> は、Perlではそれぞれ C<last> と | |
193 | C<next> となります。 | |
194 | C とは異なり、これらは C<do { } while> 構文では I<使えません>。 | |
151 | 195 | |
152 | ||
196 | =item * | |
153 | comparisons. | |
154 | 197 | |
155 | ||
198 | switch 文はありません(が、その場(on the fly)で作り上げることは簡単です)。 | |
156 | 199 | |
157 | ||
200 | =item * | |
158 | 決めなければなりません。 | |
159 | 201 | |
202 | Perl では、変数は“$”か“@”か“%”で始まります。 | |
203 | ||
160 | 204 | =item * |
161 | 205 | |
162 | ||
206 | コメントの始まりは、“#”であり、“/*”ではありません。 | |
163 | 207 | |
164 | ||
208 | =item * | |
165 | to an array yourself. And the split() operator has different | |
166 | arguments than B<awk>'s. | |
167 | 209 | |
168 | ||
210 | なにかのアドレスを得ることはできません。Perl には似たような演算子である | |
211 | バックスラッシュがありますが、これは参照を生成します。 | |
169 | 212 | |
170 | ||
213 | =item * | |
171 | 配列への split は自分で行います。 | |
172 | また、split() 演算子の引数は B<awk> のものと異なっています。 | |
173 | 214 | |
215 | C<ARGV> は大文字でなければなりません。C<$ARGV[0]> が C での C<argv[1]> に相当し、 | |
216 | C<argv[0]> にあたるものは C<$0> です。 | |
217 | ||
174 | 218 | =item * |
175 | 219 | |
176 | 220 | =begin original |
177 | 221 | |
178 | ||
222 | System calls such as link(), unlink(), rename(), etc. return nonzero for | |
179 | not | |
223 | success, not 0. (system(), however, returns zero for success.) | |
180 | executed.) See L<perlvar>. | |
181 | 224 | |
182 | 225 | =end original |
183 | 226 | |
184 | ||
227 | link(), unlink(), rename() などのシステムコールは、成功時に | |
185 | ||
228 | 0 ではなく非 0 の値を返します。(但し、system() は成功時に 0 を返します。) | |
186 | ($0 には実行しているプログラムの名前があります)。 | |
187 | L<perlvar> を参照してください。 | |
188 | 229 | |
189 | 230 | =item * |
190 | 231 | |
232 | シグナルハンドラは、シグナル番号ではなくシグナル名を扱います。 | |
233 | 使用できるシグナル名は、kill -l として確かめてください。 | |
234 | ||
235 | =back | |
236 | ||
237 | =head2 Sed Traps | |
238 | ||
239 | (sed の罠) | |
240 | ||
191 | 241 | =begin original |
192 | 242 | |
193 | ||
243 | Seasoned B<sed> programmers should take note of the following: | |
194 | by the last match pattern. | |
195 | 244 | |
196 | 245 | =end original |
197 | 246 | |
198 | ||
247 | 熟練した B<sed> プログラマは以下のことに注意すべきです: | |
199 | パターンマッチングの部分文字列を参照します。 | |
200 | 248 | |
249 | =over 4 | |
250 | ||
201 | 251 | =item * |
202 | 252 | |
203 | ||
253 | 置換における後方参照には、“\”ではなく“$”を使います。 | |
204 | 254 | |
205 | ||
255 | =item * | |
206 | you set C<$,> and C<$\>. You can set $OFS and $ORS if you're using | |
207 | the English module. | |
208 | 256 | |
209 | ||
257 | "(", ")", "|" といったパターンマッチのメタキャラクタは、その直前に | |
258 | バックスラッシュを置く必要はありません。 | |
210 | 259 | |
211 | print() 文は、C<$,> や C<$\> に値を設定しない限りフィールド区切り子や | |
212 | レコード区切り子を付加しません。 | |
213 | English モジュールを使っていれば、$OFS や $ORS に対して | |
214 | 設定することもできます。 | |
215 | ||
216 | 260 | =item * |
217 | 261 | |
262 | 範囲演算子は C<...> であって、カンマではありません。 | |
263 | ||
264 | =back | |
265 | ||
266 | =head2 Shell Traps | |
267 | ||
268 | (shell の罠) | |
269 | ||
218 | 270 | =begin original |
219 | 271 | |
220 | ||
272 | Sharp shell programmers should take note of the following: | |
221 | 273 | |
222 | 274 | =end original |
223 | 275 | |
224 | ||
276 | 鋭いシェルプログラマは以下のことに注意すべきです: | |
225 | おかなければなりません。 | |
226 | 277 | |
278 | =over 4 | |
279 | ||
227 | 280 | =item * |
228 | 281 | |
229 | ||
282 | バッククォート演算子は、コマンド内にシングルクォートがあっても | |
283 | 変数の展開を行ないます。 | |
230 | 284 | |
231 | ||
285 | =item * | |
232 | C. | |
233 | 286 | |
234 | ||
287 | バッククォート演算子は B<csh> とは違って、返された値を変換しません。 | |
235 | 288 | |
236 | 範囲演算子は ".." であって、カンマではありません。 | |
237 | カンマ演算子は C と同じような振る舞いをします。 | |
238 | ||
239 | 289 | =item * |
240 | 290 | |
241 | ||
291 | シェル (特に B<csh>) は、コマンドラインごとに何段階もの置換を行ないます。 | |
292 | Perl はダブルクォート、バッククォート、 | |
293 | アングルブラケット、検索パターンといった特定の構造でだけ置換を行ないます。 | |
242 | 294 | |
243 | ||
295 | =item * | |
244 | operator, as in C.) | |
245 | 296 | |
246 | ||
297 | シェルは一度に少しずつ解釈を行ないます。 | |
298 | Perl は実行前にプログラム全体をコンパイルします | |
299 | (コンパイル時に実行される C<BEGIN> ブロックを除く)。 | |
247 | 300 | |
248 | ||
301 | =item * | |
249 | ("~" はCと同様に、1 の補数を取る演算子です。) | |
250 | 302 | |
303 | 引数は $1, $2 などではなく、@ARGV から得られます。 | |
304 | ||
251 | 305 | =item * |
252 | 306 | |
307 | 環境変数は、自動的には独立したスカラー変数として利用できるように | |
308 | なりません。 | |
309 | ||
310 | =back | |
311 | ||
312 | =head2 Perl Traps | |
313 | ||
314 | (Perl の罠) | |
315 | ||
253 | 316 | =begin original |
254 | 317 | |
255 | ||
318 | Practicing Perl Programmers should take note of the following: | |
256 | operator, as in C. (You know, one could get the feeling that B<awk> is | |
257 | basically incompatible with C.) | |
258 | 319 | |
259 | 320 | =end original |
260 | 321 | |
261 | ||
322 | 実践的な Perl プログラマは以下のことに注意すべきです: | |
262 | "^" は C と同様、XOR 演算子です。 | |
263 | (B<awk> が基本的に C と非互換であることにお気付きかもしれませんね。) | |
264 | 323 | |
324 | =over 4 | |
325 | ||
265 | 326 | =item * |
266 | 327 | |
267 | ||
328 | 多くの演算子がリストコンテキストとスカラーコンテキストとで | |
329 | 振る舞いが変わることを忘れないでください。 | |
330 | 詳しくは L<perldata> を参照してください。 | |
268 | 331 | |
269 | ||
332 | =item * | |
270 | null string would render C</pat/ /pat/> unparsable, because the third slash | |
271 | would be interpreted as a division operator--the tokenizer is in fact | |
272 | slightly context sensitive for operators like "/", "?", and ">". | |
273 | And in fact, "." itself can be the beginning of a number.) | |
274 | 333 | |
275 | ||
334 | 裸の単語、特に全てが小文字のものはできる限り使わないでください。 | |
335 | 見た目だけではその「裸の単語」が関数なのか、 | |
336 | 文字列なのかが判断できません。文字列にはクォートを、 | |
337 | 関数呼び出しには括弧をつければ、迷うこともないでしょう。 | |
276 | 338 | |
277 | 連接演算子は "." であって、空文字列ではありません。 | |
278 | (空文字列を使ってしまうと C</pat/ /pat/> が、その 3 番目のスラッシュが | |
279 | 除算演算子と解釈されてしまうので正しく解析できなくなります-- | |
280 | Perl の字句解析器は "/", "?", ">" といった演算子に対して | |
281 | 多少文脈依存となっています。 | |
282 | 実際、"." 自身も数値の始まりとなる可能性もあります。) | |
283 | ||
284 | 339 | =item * |
285 | 340 | |
286 | 341 | =begin original |
287 | 342 | |
288 | ||
343 | You cannot discern from mere inspection which builtins | |
344 | are unary operators (like chop() and chdir()) | |
345 | and which are list operators (like print() and unlink()). | |
346 | (Unless prototyped, user-defined subroutines can B<only> be list | |
347 | operators, never unary ones.) See L<perlop> and L<perlsub>. | |
289 | 348 | |
290 | 349 | =end original |
291 | 350 | |
292 | ||
351 | 組込み関数のどれが(chop() や chdir())のような単項演算子で、 | |
352 | どれが(print() や unlink())のような | |
353 | リスト演算子であるかは見ただけではわかりません | |
354 | (プロトタイプがなければ、ユーザー定義サブルーチンは | |
355 | リスト演算子として B<のみ> 定義でき、単項演算子にはできません)。 | |
356 | L<perlop> と L<perlsub> を参照してください。 | |
293 | 357 | |
294 | 358 | =item * |
295 | 359 | |
296 | ||
360 | いくつかの関数が $_ や @ARGV などをデフォルトにしていますが、 | |
361 | 同じことを期待する他の関数がデフォルトになっていないことを覚えるのに、 | |
362 | 辛いタイピングが必要でしょう。 | |
297 | 363 | |
298 | The following variables work differently: | |
299 | 364 | |
300 | =e | |
365 | =item * | |
301 | 366 | |
302 | ||
367 | <FH> 構造はファイルハンドルではなく、そのハンドルに対する行読み込みの | |
368 | 操作(readline operation)です。 | |
369 | while ループの条件式の中にこのファイル読み込みだけがあった場合には | |
370 | 読み込まれたデータは $_ に代入されます。 | |
303 | 371 | |
304 | | |
372 | while (<FH>) { } | |
305 | | |
373 | while (defined($_ = <FH>)) { }.. | |
306 | | |
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 $; | |
319 | 375 | |
320 | 376 | =item * |
321 | 377 | |
322 | = | |
378 | C<=~> が必要なところで c<=> を使わない、ということを忘れないでください。 | |
379 | これら二つの構造はかなり違います。 | |
323 | 380 | |
324 | ||
381 | $x = /foo/; | |
382 | $x =~ /foo/; | |
325 | 383 | |
326 | =e | |
384 | =item * | |
327 | 385 | |
328 | ||
386 | C<do {}> 構造は、ループ制御を行えるような本当のループではありません。 | |
329 | 387 | |
330 | 388 | =item * |
331 | 389 | |
332 | ||
390 | ローカル変数は、my() で済むところではこれで済ませること | |
391 | (使えない場所については、L<perlform> を参照してください)。 | |
392 | local() を使えばグローバル変数に対するローカルな値を与えますが、 | |
393 | 動的スコープの不慮の副作用の可能性は、そのままです。 | |
333 | 394 | |
334 | ||
395 | =item * | |
335 | gives you. | |
336 | 396 | |
337 | ||
397 | モジュールにある export された変数を局所化すると、その export された | |
398 | 値は変更されません。 | |
399 | ローカル名は新しい値の別名(alias)となりますが、 | |
400 | 外部名は元々の値の別名のままです。 | |
338 | 401 | |
339 | 妙だと思ったときには B<awk> の構文を B<a2p> に通して、出力されたものを | |
340 | 見てみましょう。 | |
341 | ||
342 | 402 | =back |
343 | 403 | |
344 | =head2 | |
404 | =head2 Perl4 to Perl5 Traps | |
345 | 405 | |
346 | ( | |
406 | (Perl4 から Perl5 への罠) | |
347 | 407 | |
348 | 408 | =begin original |
349 | 409 | |
350 | ||
410 | Practicing Perl4 Programmers should take note of the following | |
411 | Perl4-to-Perl5 specific traps. | |
351 | 412 | |
352 | 413 | =end original |
353 | 414 | |
354 | ||
415 | 実践的な Perl4 プログラマは | |
416 | 以下に挙げる Perl4 と Perl5 の違いに特有な罠に気をつけた | |
417 | 方が良いでしょう。 | |
355 | 418 | |
419 | 以下は順不同のリストです。 | |
420 | ||
356 | 421 | =over 4 |
357 | 422 | |
358 | =item | |
423 | =item Discontinuance, Deprecation, and BugFix traps | |
359 | 424 | |
425 | 修正された perl4 のバグや、なくなった perl4 の仕様、 | |
426 | perl5 で仕様の変わったもの。 | |
427 | ||
428 | =item 構文解析に関する罠 | |
429 | ||
430 | 新しい構文解析器によって引き起こされるもの。 | |
431 | ||
432 | =item 数値に関する罠 | |
433 | ||
360 | 434 | =begin original |
361 | 435 | |
362 | ||
436 | Traps having to do with numerical or mathematical operators. | |
363 | 437 | |
364 | 438 | =end original |
365 | 439 | |
366 | ||
440 | 数値や算術演算子に関する罠。 | |
367 | 441 | |
368 | =item | |
442 | =item General data type traps | |
369 | 443 | |
370 | ||
444 | perlの標準的なデータ型に内包される罠。 | |
371 | 445 | |
372 | ||
446 | =item Context Traps - scalar, list contexts | |
373 | 447 | |
374 | ||
448 | リスト内のコンテキストや、スカラー文/宣言に関する罠。 | |
375 | 449 | |
376 | ||
450 | =item 優先順位の罠 | |
377 | 451 | |
378 | ||
452 | 構文解析、評価、コードの実行の優先順に関係した罠。 | |
379 | 453 | |
380 | = | |
454 | =item General Regular Expression Traps using s///, etc. | |
381 | 455 | |
382 | ||
456 | パターンマッチングの仕様に関する罠。 | |
383 | and C<next>, respectively. Unlike in C, these do I<not> work within a | |
384 | C<do { } while> construct. See L<perlsyn/"Loop Control">. | |
385 | 457 | |
386 | =e | |
458 | =item Subroutine, Signal, Sorting Traps | |
387 | 459 | |
388 | ||
460 | シグナル及びシグナルハンドラ、一般的なサブルーチン、ソート、 | |
389 | ||
461 | ソートのためのサブルーチンに関連した罠。 | |
390 | C とは異なり、これらは C<do { } while> 構文では I<使えません>。 | |
391 | L<perlsyn/"Loop Control"> を参照してください。 | |
392 | 462 | |
393 | =item | |
463 | =item OS Traps | |
394 | 464 | |
395 | ||
465 | OS特有の罠。 | |
396 | 466 | |
397 | ||
467 | =item DBM Traps | |
398 | perl 5.10 or newer. See L<perlsyn/"Switch Statements">. | |
399 | 468 | |
400 | ||
469 | C<dbmopen()> の使用や、dbm の実装に関連した罠。 | |
401 | 470 | |
402 | ||
471 | =item Unclassified Traps | |
403 | L<perlsyn/"Switch Statements"> を参照してください。 | |
404 | 472 | |
405 | ||
473 | その他の罠。 | |
406 | 474 | |
475 | =back | |
476 | ||
407 | 477 | =begin original |
408 | 478 | |
409 | ||
479 | If you find an example of a conversion trap that is not listed here, | |
480 | please submit it to <F<perlbug@perl.org>> for inclusion. | |
481 | Also note that at least some of these can be caught with the | |
482 | C<use warnings> pragma or the B<-w> switch. | |
410 | 483 | |
411 | 484 | =end original |
412 | 485 | |
413 | ||
486 | もしここで挙げたリストにはないような変換の罠の | |
487 | 例を見つけたら、それを <F<perlbug@perl.org>> まで送ってください。 | |
488 | また、少なくともこれらのいくつかは C<use warnings> プラグマか | |
489 | B<-w> スイッチで捕捉できることに注意してください。 | |
414 | 490 | |
415 | =ite | |
491 | =head2 Discontinuance, Deprecation, and BugFix traps | |
416 | 492 | |
417 | ||
493 | perl4 から、なくなったり修正されたことがら。 | |
418 | 494 | |
419 | ||
495 | =over 4 | |
420 | comments as division operators, unterminated regular expressions or | |
421 | the defined-or operator. | |
422 | 496 | |
423 | =e | |
497 | =item * Discontinuance | |
424 | 498 | |
425 | ||
499 | “_”で始まるシンボルはもはや main パッケージに強制的に結び付けられることは、 | |
426 | ||
500 | C<$_> 自身(と C<@-> など)を除いてはなくなりました。 | |
427 | 定義性和演算子として解釈するかもしれません。 | |
428 | 501 | |
429 | ||
502 | package test; | |
503 | $_legacy = 1; | |
430 | 504 | |
431 | ||
505 | package main; | |
506 | print "\$_legacy is ",$_legacy,"\n"; | |
432 | 507 | |
433 | ||
508 | # perl4 の出力: $_legacy is 1 | |
434 | ||
509 | # perl5 の出力: $_legacy is | |
435 | 510 | |
436 | =e | |
511 | =item * Deprecation | |
437 | 512 | |
438 | ||
513 | ダブルコロンは、変数名の中でパッケージセパレーターになるようになりました。 | |
439 | ||
514 | このため、以下の例では perl4 と perl5 とでは振る舞いが変わります。 | |
515 | これはパッケージが実在してないためです。 | |
440 | 516 | |
441 | ||
517 | $a=1;$b=2;$c=3;$var=4; | |
518 | print "$a::$b::$c "; | |
519 | print "$var::abc::xyz\n"; | |
442 | 520 | |
521 | # perl4 の出力: 1::2::3 4::abc::xyz | |
522 | # perl5 出力: 3 | |
523 | ||
524 | C<::> は今では、(これがバグとして分類すべきかどうかはともかく) | |
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 | ||
443 | 533 | =begin original |
444 | 534 | |
445 | ||
535 | You can avoid this problem, and remain compatible with perl4, if you | |
446 | ||
536 | always explicitly include the package name: | |
447 | 537 | |
448 | 538 | =end original |
449 | 539 | |
450 | ||
540 | 常にパッケージ名を含めることで、この問題を避けつつ、perl4 との | |
451 | ||
541 | 互換性を維持することができます: | |
452 | C<$0> です。 | |
453 | 542 | |
454 | = | |
543 | $x = 10 ; | |
544 | print "x=${main'x}\n" ; | |
455 | 545 | |
456 | ||
546 | C<$:> の解析については、優先順位の罠 も参照してください。 | |
457 | 547 | |
458 | ||
548 | =item * BugFix | |
459 | success, not 0. (system(), however, returns zero for success.) | |
460 | 549 | |
461 | ||
550 | C<splice()> の第 2、第 3 引数は | |
551 | (ラクダ本にある通り)リストコンテキストではなく | |
552 | スカラーコンテキストで評価されるようになりました。 | |
462 | 553 | |
463 | ||
554 | sub sub1{return(0,2) } # 要素二つのリストを返す | |
464 | ||
555 | sub sub2{ return(1,2,3)} # 要素三つのリストを返す | |
465 | ||
556 | @a1 = ("a","b","c","d","e"); | |
557 | @a2 = splice(@a1,&sub1,&sub2); | |
558 | print join(' ',@a2),"\n"; | |
466 | 559 | |
467 | ||
560 | # perl4 の出力: a b | |
561 | # perl5 の出力: c d e | |
468 | 562 | |
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 | ||
606 | C<**> 演算子の優先順位は、単項のマイナスよりも高くなりました。 | |
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 | ||
632 | perl4 と同じようにするには、自分で陽にテンポラリの配列へと | |
633 | コピーしてからその配列に対して繰り返しを行います。例えば、 | |
634 | ||
635 | foreach $var (grep(/ab/,@list)){ | |
636 | ||
637 | この部分を以下のように変更します。 | |
638 | ||
639 | foreach $var (@tmp = grep(/ab/,@list)){ | |
640 | ||
641 | そうしないと、$var を変更したときに @list の値に影響が出ます | |
642 | (これはループ変数に C<$_> を使っていて、かつ、 | |
643 | C<$_> を局所化していないようなサブルーチンを | |
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 | ||
662 | perl4 では、B<-e> スイッチにアタッチされたテキストは無視されて、常に | |
663 | 後に続く引数からプログラムが取り出されていました。さらに、 | |
664 | B<-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 | ||
679 | perl4 では C<push> の戻り値はドキュメントに書かれていませんでしたが、 | |
680 | 実際には対象となるリストに最後にpushした値が返されていました。 | |
681 | Perl5 では、C<push> の戻り値はドキュメントに明記され、かつそれは | |
682 | perl4 から変更されました。これは 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 | ||
469 | 697 | =begin original |
470 | 698 | |
471 | ||
699 | In Perl 4, if in list context the delimiters to the first argument of | |
472 | t | |
700 | C<split()> were C<??>, the result would be placed in C<@_> as well as | |
701 | being returned. Perl 5 has more respect for your subroutine arguments. | |
473 | 702 | |
474 | 703 | =end original |
475 | 704 | |
476 | ||
705 | Perl 4 では、リストコンテキストで C<split()> の最初の引数の | |
477 | ||
706 | デリミタが C<??> だった場合、返される結果が C<@_> にも設定されました。 | |
707 | Perl 5 ではサブルーチンの引数により多くの敬意を払います。 | |
478 | 708 | |
709 | =item * Discontinuance | |
710 | ||
711 | 幾つかのバグがうかつにも修正されているかもしれません :-) | |
712 | ||
479 | 713 | =back |
480 | 714 | |
481 | =head2 | |
715 | =head2 Parsing Traps | |
482 | 716 | |
483 | ( | |
717 | (パースの罠) | |
484 | 718 | |
485 | 719 | =begin original |
486 | 720 | |
487 | ||
721 | Perl4-to-Perl5 traps from having to do with parsing. | |
488 | 722 | |
489 | 723 | =end original |
490 | 724 | |
491 | ||
725 | パースに関する Perl4 と Perl5 の違いの罠です。 | |
492 | 726 | |
493 | 727 | =over 4 |
494 | 728 | |
495 | =item * | |
729 | =item * Parsing | |
496 | 730 | |
497 | = | |
731 | . と = の間にあるスペースに注意。 | |
498 | 732 | |
499 | ||
733 | $string . = "more string"; | |
500 | ||
734 | print $string; | |
501 | use the C<.> operator. | |
502 | 735 | |
503 | ||
736 | # perl4 の出力: more string | |
737 | # perl5 の出力: syntax error at - line 1, near ". =" | |
504 | 738 | |
505 | ||
739 | =item * Parsing | |
506 | C<$string1 + $string2> は両方の文字列を数値に変換してから加算します。 | |
507 | 二つの文字列を結合するには、C<.> 演算子を使ってください。 | |
508 | 740 | |
509 | ||
741 | perl5 では構文解析が改良されました。 | |
510 | 742 | |
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 | ||
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 | ||
762 | C<$#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 | ||
511 | 783 | =begin original |
512 | 784 | |
513 | ||
785 | Perl4-to-Perl5 traps having to do with numerical operators, | |
514 | ||
786 | operands, or output from same. | |
515 | 787 | |
516 | 788 | =end original |
517 | 789 | |
518 | ||
790 | 同じものに対する数値演算子、オペランド、出力に関する | |
519 | ||
791 | Perl4 と Perl5 の違いの罠です。 | |
520 | 792 | |
521 | = | |
793 | =over 5 | |
522 | 794 | |
795 | =item * Numerical | |
796 | ||
523 | 797 | =begin original |
524 | 798 | |
525 | ||
799 | Formatted output and significant digits | |
526 | the left-hand side to be an arbitrary expression. It must be a variable: | |
527 | 800 | |
528 | 801 | =end original |
529 | 802 | |
530 | ||
803 | 書式指定された出力と、最下位の数字。 | |
531 | 左側に任意の式を置くことはできません。 | |
532 | これは変数でなければなりません: | |
533 | 804 | |
534 | | |
805 | print 7.373504 - 0, "\n"; | |
535 | ||
806 | printf "%20.18f\n", 7.373504 - 0; | |
536 | } | |
537 | 807 | |
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 | ||
538 | 845 | =begin original |
539 | 846 | |
540 | ||
847 | When bitwise operators which can operate upon either numbers or | |
541 | C< | |
848 | strings (C<& | ^ ~>) are given only strings as arguments, perl4 would | |
542 | ||
849 | treat the operands as bitstrings so long as the program contained a call | |
850 | to the C<vec()> function. perl5 treats the string operands as bitstrings. | |
851 | (See L<perlop/Bitwise String Operators> for more details.) | |
543 | 852 | |
544 | 853 | =end original |
545 | 854 | |
546 | ||
855 | 数値としても文字列としても操作可能なビット操作演算子 (C<& | ^ ~>) が | |
547 | ||
856 | 文字列のみを引数として与えられた場合、 | |
548 | ||
857 | perl4 は、プログラムが C<vec()> 関数の呼び出しを含んでいればオペランドを | |
858 | ビット文字列として扱います。 | |
859 | perl5 は文字列オペランドをビット文字列として扱います | |
860 | (さらなる詳細については L<perlop/Bitwise String Operators> を参照してください)。 | |
549 | 861 | |
550 | ||
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); | |
551 | 868 | |
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 | ||
552 | 884 | =begin original |
553 | 885 | |
554 | ||
886 | Perl4-to-Perl5 traps involving most data-types, and their usage | |
555 | ||
887 | within certain expressions and/or context. | |
556 | 888 | |
557 | 889 | =end original |
558 | 890 | |
559 | ||
891 | ほとんどのデータ型と、それらの特定の表現やコンテキストでの | |
560 | ||
892 | 使い方に関する Perl4 と Perl5 の違いの罠です。 | |
561 | C<foreach my $v (@array) {}> は値に対して反復します。 | |
562 | 893 | |
563 | = | |
894 | =over 5 | |
564 | 895 | |
896 | =item * (配列) | |
897 | ||
565 | 898 | =begin original |
566 | 899 | |
567 | ||
900 | Negative array subscripts now count from the end of the array. | |
568 | 901 | |
569 | 902 | =end original |
570 | 903 | |
571 | ||
904 | 配列の添え字が負であったとき、それは配列の終端から数えられるように | |
905 | なりました。 | |
572 | 906 | |
573 | = | |
907 | @a = (1, 2, 3, 4, 5); | |
908 | print "The third element of the array is $a[3] also expressed as $a[-2] \n"; | |
574 | 909 | |
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 | ||
915 | C<$#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 | ||
575 | 940 | =begin original |
576 | 941 | |
577 | ||
942 | Perl will now generate a warning when it sees defined(@a) and | |
943 | defined(%h). | |
578 | 944 | |
579 | 945 | =end original |
580 | 946 | |
581 | Perl | |
947 | Perl は defined(@a) と defined(%h) に警告を出すようになりました。 | |
582 | 948 | |
583 | =item * | |
949 | =item * (グロブ) | |
584 | 950 | |
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 | ||
585 | 965 | =begin original |
586 | 966 | |
587 | C< | |
967 | Assigning C<undef> to a glob has no effect in Perl 5. In Perl 4 | |
588 | ||
968 | it undefines the associated scalar (but may have other side effects | |
969 | including SEGVs). Perl 5 will also warn if C<undef> is assigned to a | |
970 | typeglob. (Note that assigning C<undef> to a typeglob is different | |
971 | than calling the C<undef> function on a typeglob (C<undef *foo>), which | |
972 | has quite a few effects. | |
589 | 973 | |
590 | 974 | =end original |
591 | 975 | |
592 | C< | |
976 | C<undef> のグロブへの代入は Perl5 では何の影響も及ぼしません。 | |
593 | ||
977 | Perl4 では結び付けられたスカラーを undefine します | |
978 | (しかし SEGV を含め、なんらかの副作用があるかもしれません)。 | |
979 | Perl 5 はまた型グロブに C<undef> を代入すると警告されます。 | |
980 | (型グロブに C<undef> を代入するのは、型グロブに対して | |
981 | C<undef> 関数を呼び出すのとは違います)。いくつかの効果があります。 | |
594 | 982 | |
595 | | |
983 | $foo = "bar"; | |
984 | *foo = undef; | |
985 | print $foo; | |
596 | 986 | |
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 | ||
1007 | perl4 では定数を変更してしまいます。 | |
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 | ||
1033 | perl4 と 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 | ||
1042 | perl5 では、変数の自殺(variable sucide)の振る舞いはより | |
1043 | 首尾一貫したものとなりました。 | |
1044 | perl4 ではスカラーのみがそうであるような振る舞いを、 | |
1045 | perl5 ではハッシュとスカラーで示します。 | |
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 | ||
597 | 1088 | =begin original |
598 | 1089 | |
599 | ||
1090 | The elements of argument lists for formats are now evaluated in list | |
600 | nee | |
1091 | context. This means you can interpolate list values now. | |
601 | 1092 | |
602 | 1093 | =end original |
603 | 1094 | |
604 | ||
1095 | formatに対する引数リストの要素は、リストコンテキストで | |
605 | ||
1096 | 評価されるようになりました。これは、今やリストの値が | |
1097 | 展開できるようになったということです。 | |
606 | 1098 | |
607 | | |
1099 | @fmt = ("foo","bar","baz"); | |
1100 | format STDOUT= | |
1101 | @<<<<< @||||| @>>>>> | |
1102 | @fmt; | |
1103 | . | |
1104 | write; | |
608 | 1105 | |
1106 | # perl4 errors: Please use commas to separate fields in file | |
1107 | # perl5 の出力: foo bar baz | |
1108 | ||
1109 | =item * (スカラーコンテキスト) | |
1110 | ||
1111 | C<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 | ||
609 | 1134 | =begin original |
610 | 1135 | |
611 | ||
1136 | C<sprintf()> is prototyped as ($;@), so its first argument is given scalar | |
1137 | context. Thus, if passed an array, it will probably not do what you want, | |
1138 | unlike Perl 4: | |
612 | 1139 | |
613 | 1140 | =end original |
614 | 1141 | |
615 | ||
1142 | C<sprintf()> のプロトタイプは ($;@) なので、最初の引数は | |
1143 | スカラコンテキストです。従って、配列を渡すと、 | |
1144 | Perl 4 とは違っておそらくあなたの望まない結果になるでしょう。 | |
616 | 1145 | |
617 | = | |
1146 | @z = ('%s%s', 'foo', 'bar'); | |
1147 | $x = sprintf(@z); | |
1148 | print $x; | |
618 | 1149 | |
1150 | # perl4 prints: foobar | |
1151 | # perl5 prints: 3 | |
1152 | ||
619 | 1153 | =begin original |
620 | 1154 | |
621 | ||
1155 | C<printf()> works the same as it did in Perl 4, though: | |
622 | 1156 | |
623 | 1157 | =end original |
624 | 1158 | |
625 | Perl | |
1159 | しかし、C<printf()> のほうは Perl 4 と同じように動きます: | |
626 | 1160 | |
627 | = | |
1161 | @z = ('%s%s', 'foo', 'bar'); | |
1162 | printf STDOUT (@z); | |
628 | 1163 | |
1164 | # perl4 の出力: foobar | |
1165 | # perl5 の出力: foobar | |
1166 | ||
1167 | =back | |
1168 | ||
1169 | =head2 Precedence Traps | |
1170 | ||
1171 | (評価順序の罠) | |
1172 | ||
629 | 1173 | =begin original |
630 | 1174 | |
631 | ||
1175 | Perl4-to-Perl5 traps involving precedence order. | |
632 | You cannot write C<$x = 1; my $x;> and expect the first assignment to | |
633 | affect the same variable. It will instead assign to an C<$x> declared | |
634 | previously in an outer scope, or to a global variable. | |
635 | 1176 | |
636 | 1177 | =end original |
637 | 1178 | |
638 | ||
1179 | 評価順序に関する Perl4 と Perl5 の違いの罠です。 | |
639 | C<$x = 1; my $x;> と書いて最初の代入が同じ変数に影響すると | |
640 | 想定することはできません。 | |
641 | これは外側のスコープ、あるいはグローバル変数の C<$x> に代入されます。 | |
642 | 1180 | |
643 | 1181 | =begin original |
644 | 1182 | |
645 | ||
1183 | Perl 4 has almost the same precedence rules as Perl 5 for the operators | |
646 | ||
1184 | that they both have. Perl 4 however, seems to have had some | |
647 | ||
1185 | inconsistencies that made the behavior differ from what was documented. | |
648 | 1186 | |
649 | 1187 | =end original |
650 | 1188 | |
651 | ||
1189 | Perl4 はほとんどの演算子で Perl5 と同じ優先順位を持っています。 | |
652 | ||
1190 | しかし Perl4 では、ドキュメントとは少々異なるような | |
653 | ||
1191 | 一貫性に欠けるものがあります。 | |
654 | 1192 | |
655 | = | |
1193 | =over 5 | |
656 | 1194 | |
1195 | =item * Precedence | |
1196 | ||
657 | 1197 | =begin original |
658 | 1198 | |
659 | ||
1199 | LHS vs. RHS when both sides are getting an op. | |
660 | function. If you write C<{my $x;} $x;>, the second C<$x> does not refer to | |
661 | the one declared inside the block. | |
662 | 1200 | |
663 | 1201 | =end original |
664 | 1202 | |
665 | ||
1203 | 左辺と右辺は両方とも演算子を取るようになりました。 | |
666 | C<{my $x;} $x;> と書くと、2 番目の C<$x> はブロックの内側で宣言された変数を | |
667 | 参照できません。 | |
668 | 1204 | |
669 | =it | |
1205 | @arr = ( 'left', 'right' ); | |
1206 | $a{shift @arr} = shift @arr; | |
1207 | print join( ' ', keys %a ); | |
670 | 1208 | |
671 | ||
1209 | # perl4 の出力: left | |
1210 | # perl5 の出力: right | |
672 | 1211 | |
673 | ||
1212 | =item * Precedence | |
674 | Perl equivalent to C<with(object) { method() }> is C<for>, which can alias | |
675 | C<$_> to the object: | |
676 | 1213 | |
677 | ||
1214 | 以下の例は、優先順位の関係で意味エラー(semantic error)となるようになりました。 | |
678 | 1215 | |
679 | ||
1216 | @list = (1,2,3,4,5); | |
680 | ||
1217 | %map = ("a",1,"b",2,"c",3,"d",4); | |
681 | ||
1218 | $n = shift @list + 2; # リストの最初の要素に2を加える | |
1219 | print "n is $n, "; | |
1220 | $m = keys %map + 2; # ハッシュにあるアイテムの数+2 | |
1221 | print "m is $m\n"; | |
682 | 1222 | |
683 | | |
1223 | # perl4 出力: n is 3, m is 6 | |
684 | ||
1224 | # perl5 errors and fails to compile | |
685 | } | |
686 | 1225 | |
687 | =item * | |
1226 | =item * Precedence | |
688 | 1227 | |
1228 | 代入演算子の優先順位は、代入と同じとなりました。 | |
1229 | perl4 は間違って、この優先順位が関連演算子と同じものに | |
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 | ||
1265 | perl4 は特殊変数 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 | ||
1275 | perl には、ファイルテスト演算子と代入演算子を組み合わせたときに | |
1276 | 優先順位のバグがありました。したがって、Perl4 の優先順位テーブルでは | |
1277 | C<-e $foo .= "q"> は | |
1278 | C<((-e $foo) .= "q")> と解釈すべきなのに実際には | |
1279 | C<((-e $foo) .= "q")> と解釈していました。 | |
1280 | Perl5 ではドキュメントにある通りの優先順位です。 | |
1281 | ||
1282 | -e $foo .= "q" | |
1283 | ||
1284 | # perl4 の出力: no output | |
1285 | # perl5 の出力: Can't modify -e in concatenation | |
1286 | ||
1287 | =item * Precedence | |
1288 | ||
689 | 1289 | =begin original |
690 | 1290 | |
691 | ||
1291 | In perl4, keys(), each() and values() were special high-precedence operators | |
692 | ||
1292 | that operated on a single hash, but in perl5, they are regular named unary | |
1293 | operators. As documented, named unary operators have lower precedence | |
1294 | than the arithmetic and concatenation operators C<+ - .>, but the perl4 | |
1295 | variants of these operators actually bind tighter than C<+ - .>. | |
1296 | Thus, for: | |
693 | 1297 | |
694 | 1298 | =end original |
695 | 1299 | |
696 | ||
1300 | perl4 では、key()、each()、values() はシングルハッシュに対する | |
697 | ||
1301 | 特別に高い評価順序を持った演算子でした。 | |
1302 | しかし perl5 では、これらの演算子は通常の名前付き単項演算子となりました。 | |
1303 | ドキュメントにある通り、名前付き単項演算子は、C<+ - .> のような | |
1304 | 数学演算子や連結演算子よりも低い優先順位を持っています。 | |
1305 | しかし、perl4 ではこれらの演算子よりも key() などのほうが強く演算対象と | |
1306 | 結び付いていたのです。 | |
1307 | したがって、以下の例のようになります: | |
698 | 1308 | |
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 | ||
699 | 1317 | =back |
700 | 1318 | |
701 | =head2 | |
1319 | =head2 General Regular Expression Traps using s///, etc. | |
702 | 1320 | |
703 | (s | |
1321 | (s/// などを使ったときの一般的な正規表現の罠) | |
704 | 1322 | |
705 | 1323 | =begin original |
706 | 1324 | |
707 | ||
1325 | All types of RE traps. | |
708 | 1326 | |
709 | 1327 | =end original |
710 | 1328 | |
711 | ||
1329 | 正規表現に関する全てのタイプの罠。 | |
712 | 1330 | |
713 | =over | |
1331 | =over 5 | |
714 | 1332 | |
715 | =item * | |
1333 | =item * 正規表現 | |
716 | 1334 | |
717 | ||
1335 | C<s'$lhs'$rhs'> はもはやいずれの辺にあっても展開されなくなりました。 | |
1336 | 以前は $lhs は展開し、$rhs を展開しませんでした。 | |
1337 | (文字列にあるリテラルの '$' にはいまでもマッチしません)。 | |
718 | 1338 | |
719 | ||
1339 | $a=1;$b=2; | |
720 | ||
1340 | $string = '1 2 $a $b'; | |
1341 | $string =~ s'$a'$b'; | |
1342 | print $string,"\n"; | |
721 | 1343 | |
722 | ||
1344 | # perl4 の出力: $b 2 $a $b | |
1345 | # perl5 の出力: 1 2 $a $b | |
723 | 1346 | |
724 | ||
1347 | =item * 正規表現 | |
725 | C<-n> や C<-p> を使って暗黙のループを使えます。 | |
726 | 1348 | |
727 | ||
1349 | C<m//g> は、その状態を正規表現ではなく検索対象の文字列に | |
1350 | 結び付けるようになりました | |
1351 | (sub に対するブロックのスコープが残っているのであれば、 | |
1352 | 検索文字列の状態は失われます)。 | |
728 | 1353 | |
729 | = | |
1354 | $_ = "ababab"; | |
1355 | while(m/ab/g){ | |
1356 | &doit("blah"); | |
1357 | } | |
1358 | sub doit{local($_) = shift; print "Got $_ "} | |
730 | 1359 | |
731 | ||
1360 | # perl4 prints: Got blah Got blah Got blah Got blah | |
1361 | # perl5 の出力: infinite loop blah... | |
732 | 1362 | |
733 | =e | |
1363 | =item * 正規表現 | |
734 | 1364 | |
735 | ||
1365 | 現在のところ、C<m//o> 量指定子を無名サブルーチンの中にある正規表現で | |
1366 | 使った場合、I<すべての> クロージャーはそのような無名サブルーチンから、 | |
1367 | そういったクロージャの中で一番最初に使われたものの中で | |
1368 | コンパイルされたかのような正規表現を生成します。 | |
736 | 1369 | |
737 | ||
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"; | |
738 | 1379 | |
739 | ||
1380 | For most builds of Perl5, this will print: | |
1381 | ok | |
1382 | not ok | |
1383 | not ok | |
740 | 1384 | |
741 | ||
1385 | この例の場合、build_match() は常にC<最初>に build_match() が呼ばれたときの | |
742 | ||
1386 | $left と $right の内容にマッチするようなサブルーチンを返します。 | |
1387 | 呼び出されたその時点での値ではありません。 | |
743 | 1388 | |
744 | =e | |
1389 | =item * 正規表現 | |
745 | 1390 | |
746 | ||
1391 | マッチングの中で括弧が使われなかった場合、perl4 では | |
747 | ||
1392 | C<$+> には C<$&> と同じようにマッチした全体がセットされますが、 | |
1393 | Perl5 ではそうではありません。 | |
748 | 1394 | |
749 | ||
1395 | "abcdef" =~ /b.*e/; | |
1396 | print "\$+ = $+\n"; | |
750 | 1397 | |
751 | ||
1398 | # perl4 の出力: bcde | |
1399 | # perl5 の出力: | |
752 | 1400 | |
753 | ||
1401 | =item * 正規表現 | |
754 | 1402 | |
755 | ||
1403 | 置換は、失敗したときには空文字列を返すようになりました。 | |
756 | 1404 | |
757 | ||
1405 | $string = "test"; | |
1406 | $value = ($string =~ s/foo//); | |
1407 | print $value, "\n"; | |
758 | 1408 | |
759 | ||
1409 | # perl4 の出力: 0 | |
1410 | # perl5 の出力: | |
760 | 1411 | |
761 | ||
1412 | この新しい仕様に関しては L<数値に関する罠> も参照してください。 | |
762 | 1413 | |
763 | ||
1414 | =item * 正規表現 | |
764 | 1415 | |
765 | ||
1416 | C<s`lhs`rhs`> (バッククォートの使用)は通常の置換となり、 | |
1417 | バッククォートの展開は行われなくなりました。 | |
766 | 1418 | |
767 | ||
1419 | $string = ""; | |
1420 | $string =~ s`^`hostname`; | |
1421 | print $string, "\n"; | |
768 | 1422 | |
769 | ||
1423 | # perl4 の出力: <the local hostname> | |
1424 | # perl5 の出力: hostname | |
770 | 1425 | |
771 | ||
1426 | =item * 正規表現 | |
772 | 1427 | |
773 | ||
1428 | 正規表現中の変数の使用に関する構文解析がより厳密になりました。 | |
774 | 1429 | |
775 | ||
1430 | s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o; | |
776 | 1431 | |
1432 | # perl4: compiles w/o error | |
1433 | # perl5: with Scalar found where operator expected ..., near "$opt$plus" | |
1434 | ||
777 | 1435 | =begin original |
778 | 1436 | |
779 | ||
1437 | an added component of this example, apparently from the same script, is | |
780 | the | |
1438 | the actual value of the s'd string after the substitution. | |
1439 | C<[$opt]> is a character class in perl4 and an array subscript in perl5 | |
781 | 1440 | |
782 | 1441 | =end original |
783 | 1442 | |
784 | ||
1443 | 同じスクリプトでこの例に付け加えことは、 | |
785 | ||
1444 | 置換後の文字列の実際の値です。 | |
1445 | C<[$opt]> は perl4 ではキャラクタクラスであり、perl5 では | |
1446 | 配列の添え字となります。 | |
786 | 1447 | |
787 | = | |
1448 | $grpc = 'a'; | |
1449 | $opt = 'r'; | |
1450 | $_ = 'bar'; | |
1451 | s/^([^$grpc]*$grpc[$opt]?)/foo/; | |
1452 | print ; | |
788 | 1453 | |
789 | ||
1454 | # perl4 の出力: foo | |
1455 | # perl5 の出力: foobar | |
790 | 1456 | |
791 | ||
1457 | =item * Regular Expression | |
792 | 1458 | |
793 | ||
1459 | perl5 では、C<m?x?> は C<?x?> と同様に一回だけマッチします。 | |
1460 | perl4 では、C</x/> や C<m!x!> と同じように何度でもマッチします。 | |
794 | 1461 | |
795 | ||
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 | } | |
796 | 1472 | |
797 | ||
1473 | # perl4 の出力: perl4 | |
1474 | # perl5 の出力: perl5 | |
798 | 1475 | |
1476 | =back | |
1477 | ||
1478 | =head2 Subroutine, Signal, Sorting Traps | |
1479 | ||
1480 | (サブルーチン、シグナル、ソートの罠) | |
1481 | ||
799 | 1482 | =begin original |
800 | 1483 | |
801 | ||
1484 | The general group of Perl4-to-Perl5 traps having to do with | |
802 | ||
1485 | Signals, Sorting, and their related subroutines, as well as | |
803 | ||
1486 | general subroutine traps. Includes some OS-Specific traps. | |
804 | 1487 | |
805 | 1488 | =end original |
806 | 1489 | |
807 | ||
1490 | Perl4 と Perl5 の違いの罠に分類される一般的なものは | |
808 | ||
1491 | シグナル、ソート、そしてそれらに関連する | |
809 | ||
1492 | サブルーチンで、いくつかの OS 固有の罠を含めた | |
1493 | サブルーチンの罠と同じ様なものです。 | |
810 | 1494 | |
811 | = | |
1495 | =over 5 | |
812 | 1496 | |
813 | = | |
1497 | =item * (Signals) | |
814 | 1498 | |
815 | ||
1499 | 文字列のように見える裸の単語は、その名前が使うよりも前に | |
816 | ||
1500 | サブルーチンの名前として定義されている場合にはサブルーチンとして | |
817 | ||
1501 | みなされます。 | |
818 | 1502 | |
819 | ||
1503 | sub SeeYa { warn"Hasta la vista, baby!" } | |
1504 | $SIG{'TERM'} = SeeYa; | |
1505 | print "SIGTERM is now $SIG{'TERM'}\n"; | |
820 | 1506 | |
821 | ||
1507 | # perl4 prints: SIGTERM is now main'SeeYa | |
822 | ||
1508 | # perl5 prints: SIGTERM is now main::1 (and warns "Hasta la vista, baby!") | |
823 | (コンパイル時に実行される C<BEGIN> ブロックを除く)。 | |
824 | 1509 | |
825 | ||
1510 | B<-w> を使って、これを発見できます。 | |
826 | 1511 | |
827 | = | |
1512 | =item * (Sort Subroutine) | |
828 | 1513 | |
829 | ||
1514 | reverse はもはやソートのサブルーチンの名前としては | |
1515 | 使えなくなりました。 | |
830 | 1516 | |
831 | ||
1517 | sub reverse{ print "yup "; $a <=> $b } | |
1518 | print sort reverse (2,1,3); | |
832 | 1519 | |
833 | ||
1520 | # perl4 prints: yup yup 123 | |
1521 | # perl5 prints: 123 | |
1522 | # perl5 warns (if using -w): Ambiguous call resolved as CORE::reverse() | |
834 | 1523 | |
835 | =item * | |
1524 | =item * warn() won't let you specify a filehandle. | |
836 | 1525 | |
837 | ||
1526 | 常に STDERR に出力していたにもかかわらず、perl4 では warn() はファイルハンドルの | |
1527 | 指定を必要としていましたが、perl5 では必要なくなりました。 | |
838 | 1528 | |
839 | ||
1529 | warn STDERR "Foo!"; | |
840 | variables. | |
841 | 1530 | |
842 | ||
1531 | # perl4 の出力: Foo! | |
1532 | # perl5 の出力: String found where operator expected | |
843 | 1533 | |
844 | ||
1534 | =back | |
845 | 1535 | |
846 | = | |
1536 | =head2 OS Traps | |
847 | 1537 | |
1538 | (OS の罠) | |
1539 | ||
1540 | =over 5 | |
1541 | ||
1542 | =item * (SysV) | |
1543 | ||
848 | 1544 | =begin original |
849 | 1545 | |
850 | ||
1546 | Under HPUX, and some other SysV OSes, one had to reset any signal handler, | |
851 | ||
1547 | within the signal handler function, each time a signal was handled with | |
852 | ||
1548 | perl4. With perl5, the reset is now done correctly. Any code relying | |
853 | ||
1549 | on the handler _not_ being reset will have to be reworked. | |
854 | 1550 | |
855 | 1551 | =end original |
856 | 1552 | |
857 | ||
1553 | HPUX 及び一部の SysV OS では、perl4 のときには | |
858 | ||
1554 | シグナルが発生する度にそのシグナルハンドラ関数の中で | |
859 | ||
1555 | シグナルハンドラを再設定しなければなりませんでした。 | |
860 | ||
1556 | perl5 では、この再設定を正しく行うようになりました。 | |
1557 | ハンドラを再設定しないということに依存したプログラムは | |
1558 | 作業しなおす必要があります。 | |
861 | 1559 | |
1560 | 5.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 | ||
1582 | SysV OS では、 | |
1583 | アペンドモード(C<<< >> >>>)でオープンしたファイルに対する | |
1584 | C<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 | ||
862 | 1602 | =back |
863 | 1603 | |
864 | =head2 | |
1604 | =head2 Interpolation Traps | |
865 | 1605 | |
866 | ( | |
1606 | (展開の罠) | |
867 | 1607 | |
868 | 1608 | =begin original |
869 | 1609 | |
870 | Pr | |
1610 | Perl4-to-Perl5 traps having to do with how things get interpolated | |
1611 | within certain expressions, statements, contexts, or whatever. | |
871 | 1612 | |
872 | 1613 | =end original |
873 | 1614 | |
874 | ||
1615 | Perl4 と Perl5 の違いの罠には、式や文、コンテキストなどにおける | |
1616 | 展開に関するものがあります。 | |
875 | 1617 | |
876 | =over | |
1618 | =over 5 | |
877 | 1619 | |
878 | =item * | |
1620 | =item * Interpolation | |
879 | 1621 | |
880 | ||
1622 | ダブルクォートで囲まれた文字列にある @ は常に配列に展開されます。 | |
881 | 1623 | |
882 | ||
1624 | print "To: someone@somewhere.com\n"; | |
883 | context than they do in a scalar one. See L<perldata> for details. | |
884 | 1625 | |
885 | ||
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 | |
886 | 1628 | |
887 | ||
1629 | =item * Interpolation | |
888 | 振る舞いが変わることを忘れないでください。 | |
889 | 詳しくは L<perldata> を参照してください。 | |
890 | 1630 | |
891 | ||
1631 | ダブルクォートで括られた文字列がエスケープされていない $ や @ で | |
1632 | 終了することがなくなりました。 | |
892 | 1633 | |
893 | = | |
1634 | $foo = "foo$"; | |
1635 | $bar = "bar@"; | |
1636 | print "foo is $foo, bar is $bar\n"; | |
894 | 1637 | |
895 | ||
1638 | # perl4 の出力: foo is foo$, bar is bar@ | |
896 | ||
1639 | # perl5 errors: Final $ should be \$ or $name | |
897 | a function or a string. By using quotes on strings and | |
898 | parentheses on function calls, you won't ever get them confused. | |
899 | 1640 | |
900 | ||
1641 | 注意:perl5 は、$bar の終端にある @ については“エラーにしません” | |
901 | 1642 | |
902 | ||
1643 | =item * Interpolation | |
903 | 見た目だけではその「裸の単語」が関数なのか、 | |
904 | 文字列なのかが判断できません。 | |
905 | 文字列にはクォートを、関数呼び出しには括弧をつければ、 | |
906 | 迷うこともないでしょう。 | |
907 | 1644 | |
908 | ||
1645 | Perl はダブルクォート中にあるブレースの内側の任意の式を | |
1646 | 評価するようになりました(一般的には、C<$> や C<@> に続いて | |
1647 | 開きのブレースがきたときです)。 | |
909 | 1648 | |
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 | ||
1658 | C<use strict;> を使って、perl5 におけるこのような罠を避けることが | |
1659 | できることに注意してください。 | |
1660 | ||
1661 | =item * Interpolation | |
1662 | ||
910 | 1663 | =begin original |
911 | 1664 | |
912 | ||
1665 | The construct "this is $$x" used to interpolate the pid at that point, but | |
913 | ||
1666 | now tries to dereference $x. C<$$> by itself still works fine, however. | |
914 | and which are list operators (like print() and unlink()). | |
915 | (Unless prototyped, user-defined subroutines can B<only> be list | |
916 | operators, never unary ones.) See L<perlop> and L<perlsub>. | |
917 | 1667 | |
918 | 1668 | =end original |
919 | 1669 | |
920 | ||
1670 | "this is $$x" はプロセス ID を展開するようになっていましたが、 | |
921 | ||
1671 | 今は $x の参照外し(dereference)を試みます。 | |
922 | ||
1672 | それでも C<$$> 自身は今でもきちんと動作します。 | |
923 | (プロトタイプがなければ、ユーザー定義サブルーチンは | |
924 | リスト演算子として B<のみ> 定義でき、単項演算子にはできません。) | |
925 | L<perlop> と L<perlsub> を参照してください。 | |
926 | 1673 | |
927 | = | |
1674 | $s = "a reference"; | |
1675 | $x = *s; | |
1676 | print "this is $$x\n"; | |
928 | 1677 | |
929 | ||
1678 | # perl4 の出力: this is XXXx (XXX はカレントプロセスID) | |
1679 | # perl5 prints: this is a reference | |
930 | 1680 | |
931 | ||
1681 | =item * Interpolation | |
932 | default to $_, or @ARGV, or whatever, but that others which | |
933 | you might expect to do not. | |
934 | 1682 | |
935 | ||
1683 | C<eval "EXPR"> を使ったその場(on the fly)でのハッシュの生成は | |
1684 | ハッシュの名前を指定する C<$> がプロテクトされているか、もしくは | |
1685 | 両方のカーリーブレースがプロテクトされていることを要求します。 | |
1686 | 両方のカーリーブレースがプロテクトされている場合には、perl4 と | |
1687 | perl5 の結果は同じです。 | |
936 | 1688 | |
937 | いくつかの関数が $_ や @ARGV などをデフォルトにしていますが、 | |
938 | 同じことを期待する他の関数がデフォルトになっていないことを覚えるのに、 | |
939 | 辛いタイピングが必要でしょう。 | |
940 | 1689 | |
941 | ||
1690 | $hashname = "foobar"; | |
1691 | $key = "baz"; | |
1692 | $value = 1234; | |
1693 | eval "\$$hashname{'$key'} = q|$value|"; | |
1694 | (defined($foobar{'baz'})) ? (print "Yup") : (print "Nope"); | |
942 | 1695 | |
943 | ||
1696 | # perl4 の出力: Yup | |
1697 | # perl5 の出力: Nope | |
944 | 1698 | |
945 | ||
1699 | 以下のものを: | |
946 | operation on that handle. The data read is assigned to $_ only if the | |
947 | file read is the sole condition in a while loop: | |
948 | 1700 | |
949 | ||
1701 | eval "\$$hashname{'$key'} = q|$value|"; | |
950 | 1702 | |
951 | ||
1703 | 次のように変更すると: | |
952 | 操作(readline operation)です。 | |
953 | while ループの条件式の中にこのファイル読み込みだけがあった場合には | |
954 | 読み込まれたデータは $_ に代入されます。 | |
955 | 1704 | |
956 | | |
1705 | eval "\$\$hashname{'$key'} = q|$value|"; | |
957 | while (defined($_ = <FH>)) { }.. | |
958 | <FH>; # data discarded! | |
959 | 1706 | |
960 | ||
1707 | 結果はこうなります: | |
961 | 1708 | |
962 | ||
1709 | # perl4 の出力: Nope | |
1710 | # perl5 の出力: Yup | |
963 | 1711 | |
964 | ||
1712 | 以下のように変更した場合には | |
965 | these two constructs are quite different: | |
966 | 1713 | |
967 | ||
1714 | eval "\$$hashname\{'$key'\} = q|$value|"; | |
968 | 1715 | |
969 | ||
1716 | 結果はこうなります。 | |
970 | これら二つの構造はかなり違います: | |
971 | 1717 | |
972 | | |
1718 | # perl4 の出力: Yup | |
973 | | |
1719 | # perl5 の出力: Yup | |
1720 | # これは両方のバージョンで同じ結果となります | |
974 | 1721 | |
975 | =item * | |
1722 | =item * Interpolation | |
976 | 1723 | |
977 | ||
1724 | 以前のバージョンにあったバグに依存しているような perl4 プログラム。 | |
978 | 1725 | |
979 | ||
1726 | perl -e '$bar=q/not/; print "This is $foo{$bar} perl5"' | |
980 | loop control on. | |
981 | 1727 | |
982 | ||
1728 | # perl4 の出力: This is not perl5 | |
1729 | # perl5 の出力: This is perl5 | |
983 | 1730 | |
984 | ||
1731 | =item * Interpolation | |
985 | 1732 | |
986 | ||
1733 | 配列の参照についても注意する必要があります。 | |
987 | 1734 | |
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 | ||
1750 | perl5 は存在しない C<$foo{bar}> を探しに行きますが、perl4 は | |
1751 | $foo を“baz”に展開しただけで満足します。 | |
1752 | C<eval> でもこの事に注意してください。 | |
1753 | ||
1754 | =item * Interpolation | |
1755 | ||
1756 | C<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 | ||
988 | 1771 | =begin original |
989 | 1772 | |
990 | ||
1773 | General DBM traps. | |
991 | it (but see L<perlform> for where you can't). | |
992 | Using C<local()> actually gives a local value to a global | |
993 | variable, which leaves you open to unforeseen side-effects | |
994 | of dynamic scoping. | |
995 | 1774 | |
996 | 1775 | =end original |
997 | 1776 | |
998 | ||
1777 | DBMに関する一般的な罠。 | |
999 | (使えない場所については、L<perlform> を参照してください)。 | |
1000 | C<local()> を使えばグローバル変数に対するローカルな値を与えますが、 | |
1001 | 動的スコープの不慮の副作用の可能性は、そのままです。 | |
1002 | 1778 | |
1003 | = | |
1779 | =over 5 | |
1004 | 1780 | |
1781 | =item * DBM | |
1782 | ||
1783 | perl4 で(もしくは他の dbm/ndbm ツールで)作成した dbm データベースを、 | |
1784 | perl5 の元で同じスクリプトで扱おうとすると失敗します。 | |
1785 | perl5 の作成において、拡張 dbm の実装を C<tie> しないときに | |
1786 | C<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 | ||
1798 | key/value のサイズに関する制限を超えたときに発生するエラーにより、 | |
1799 | perl5 では即座にプログラムから 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 | ||
1005 | 1818 | =begin original |
1006 | 1819 | |
1007 | ||
1820 | Everything else. | |
1008 | not change. The local name becomes an alias to a new value but the | |
1009 | external name is still an alias for the original. | |
1010 | 1821 | |
1011 | 1822 | =end original |
1012 | 1823 | |
1013 | ||
1824 | その他全て。 | |
1014 | 値は変更されません。 | |
1015 | ローカル名は新しい値の別名(alias)となりますが、 | |
1016 | 外部名は元々の値の別名のままです。 | |
1017 | 1825 | |
1826 | =over 5 | |
1827 | ||
1828 | =item * C<require>/C<do> の戻り値を使ったときの罠 | |
1829 | ||
1830 | doit.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 | ||
1842 | doit.pl の実行結果はこうなります。 | |
1843 | ||
1844 | # perl 4 の出力: 3 (サブルーチンからすぐに抜けます) | |
1845 | # perl 5 の出力: 8 | |
1846 | ||
1847 | C<do> を C<require> に置き換えても同じ振る舞いとなります。 | |
1848 | ||
1849 | =item * 空文字列に LIMIT を指定して C<split> を使った場合 | |
1850 | ||
1851 | $string = ''; | |
1852 | @list = split(/foo/, $string, 2) | |
1853 | ||
1854 | Perl4 は空文字列を持った一要素のリストを返しますが、Perl5 は | |
1855 | 空リストを返します。 | |
1856 | ||
1018 | 1857 | =back |
1019 | 1858 | |
1020 | 1859 | =begin original |
1021 | 1860 | |
1022 | 1861 | As always, if any of these are ever officially declared as bugs, |
1023 | 1862 | they'll be fixed and removed. |
1024 | 1863 | |
1025 | 1864 | =end original |
1026 | 1865 | |
1027 | 1866 | いつものように、バグとして公式に宣言されたものがあれば、 |
1028 | 1867 | それは修正されて取り除かれるでしょう。 |
1029 | ||
1030 | =begin meta | |
1031 | ||
1032 | Translate: KIMURA Koichi (5.005_03) | |
1033 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-) | |
1034 | Status: completed | |
1035 | ||
1036 | =end meta |