perlsyn >
5.40.0
との差分
perlsyn 5.40.0 と 5.20.1 の差分
1 | 1 | |
2 | =encoding u | |
2 | =encoding euc-jp | |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | X<syntax> |
6 | 6 | |
7 | 7 | =begin original |
8 | 8 | |
9 | perlsyn - Perl syntax | |
9 | perlsyn - Perl syntax | |
10 | 10 | |
11 | 11 | =end original |
12 | 12 | |
13 | perlsyn - Perl の文法 | |
13 | perlsyn - Perl の文法 | |
14 | 14 | |
15 | 15 | =head1 DESCRIPTION |
16 | 16 | |
17 | 17 | =begin original |
18 | 18 | |
19 | 19 | A Perl program consists of a sequence of declarations and statements |
20 | 20 | which run from the top to the bottom. Loops, subroutines, and other |
21 | 21 | control structures allow you to jump around within the code. |
22 | 22 | |
23 | 23 | =end original |
24 | 24 | |
25 | 25 | Perl プログラムは、宣言と文の並びから構成され、上から下へと実行されます。 |
26 | 26 | ループ、サブルーチン、その他の制御機構でコードの色々なところに |
27 | 27 | ジャンプできます。 |
28 | 28 | |
29 | 29 | =begin original |
30 | 30 | |
31 | 31 | Perl is a B<free-form> language: you can format and indent it however |
32 | 32 | you like. Whitespace serves mostly to separate tokens, unlike |
33 | 33 | languages like Python where it is an important part of the syntax, |
34 | 34 | or Fortran where it is immaterial. |
35 | 35 | |
36 | 36 | =end original |
37 | 37 | |
38 | 38 | Perl は B<自由書式> 言語です: 好きなように整形したりインデントしたり |
39 | 39 | できます。 |
40 | 40 | 空白が文法の重要な要素である Python や、意味のない Fortran のような言語と |
41 | 41 | 異なり、空白はほとんどトークンの分割の役目です。 |
42 | 42 | |
43 | 43 | =begin original |
44 | 44 | |
45 | 45 | Many of Perl's syntactic elements are B<optional>. Rather than |
46 | 46 | requiring you to put parentheses around every function call and |
47 | 47 | declare every variable, you can often leave such explicit elements off |
48 | 48 | and Perl will figure out what you meant. This is known as B<Do What I |
49 | 49 | Mean>, abbreviated B<DWIM>. It allows programmers to be B<lazy> and to |
50 | 50 | code in a style with which they are comfortable. |
51 | 51 | |
52 | 52 | =end original |
53 | 53 | |
54 | 54 | Perl の多くの文法要素は B<省略可能> です。 |
55 | 55 | 全ての関数をかっこで括ったり、全ての変数を宣言したりすることを |
56 | 56 | 要求するのではなく、しばしばそのような明示的な要素を置いておいて、 |
57 | 57 | Perl にあなたが意味しているところを見つけ出させることができます。 |
58 | 58 | これは B<Do What I Mean> と知られ、頭文字を取って B<DWIM> と呼ばれます。 |
59 | 59 | これによって、プログラマを B<怠惰> にでき、彼らが快適だと思うスタイルで |
60 | 60 | コーディングできるようにします。 |
61 | 61 | |
62 | 62 | =begin original |
63 | 63 | |
64 | 64 | Perl B<borrows syntax> and concepts from many languages: awk, sed, C, |
65 | 65 | Bourne Shell, Smalltalk, Lisp and even English. Other |
66 | 66 | languages have borrowed syntax from Perl, particularly its regular |
67 | 67 | expression extensions. So if you have programmed in another language |
68 | 68 | you will see familiar pieces in Perl. They often work the same, but |
69 | 69 | see L<perltrap> for information about how they differ. |
70 | 70 | |
71 | 71 | =end original |
72 | 72 | |
73 | 73 | Perl は、awk, sed, C, Bourne Shell, Smalltalk, Lisp, 果ては英語といった、 |
74 | 74 | 多くの言語からコンセプトと B<文法を借用> しています。 |
75 | 75 | 他の言語も Perl から文法を借用しています; 特に正規表現拡張をです。 |
76 | 76 | 従って、他の言語でプログラミングしていたなら、Perl にも見たことがあるような |
77 | 77 | ものがあるでしょう。 |
78 | 78 | それらはしばしば同じように動作しますが、違う点についての情報は |
79 | 79 | L<perltrap> を参照してください。 |
80 | 80 | |
81 | 81 | =head2 Declarations |
82 | 82 | X<declaration> X<undef> X<undefined> X<uninitialized> |
83 | 83 | |
84 | 84 | (宣言) |
85 | 85 | |
86 | 86 | =begin original |
87 | 87 | |
88 | 88 | The only things you need to declare in Perl are report formats and |
89 | 89 | subroutines (and sometimes not even subroutines). A scalar variable holds |
90 | 90 | the undefined value (C<undef>) until it has been assigned a defined |
91 | 91 | value, which is anything other than C<undef>. When used as a number, |
92 | 92 | C<undef> is treated as C<0>; when used as a string, it is treated as |
93 | 93 | the empty string, C<"">; and when used as a reference that isn't being |
94 | 94 | assigned to, it is treated as an error. If you enable warnings, |
95 | 95 | you'll be notified of an uninitialized value whenever you treat |
96 | 96 | C<undef> as a string or a number. Well, usually. Boolean contexts, |
97 | 97 | such as: |
98 | 98 | |
99 | 99 | =end original |
100 | 100 | |
101 | 101 | Perl で宣言が必要なものはレポートフォーマットとサブルーチンだけです |
102 | 102 | (サブルーチンすら宣言が不要な場合もあります)。 |
103 | 103 | スカラ変数は、C<undef> 以外の定義された値を代入されるまでは未定義値 |
104 | 104 | (C<undef>)となります。 |
105 | 105 | 数値として使われる場合、C<undef> は C<0> として扱われます; |
106 | 106 | 文字列として使われる場合、これは空文字列 C<""> として扱われます; |
107 | 107 | リファレンスとして使われる場合、これは何も代入されていないので、エラーとして |
108 | 108 | 扱われます。 |
109 | 109 | 警告を有効にしているなら、C<undef> を文字列や数値として扱おうとすると |
110 | 110 | 未初期化値を指摘されます。 |
111 | 111 | ええ、普通は。 |
112 | 112 | 次のような真偽値コンテキストなら: |
113 | 113 | |
114 | if ($ | |
114 | if ($a) {} | |
115 | 115 | |
116 | 116 | =begin original |
117 | 117 | |
118 | 118 | are exempt from warnings (because they care about truth rather than |
119 | 119 | definedness). Operators such as C<++>, C<-->, C<+=>, |
120 | 120 | C<-=>, and C<.=>, that operate on undefined variables such as: |
121 | 121 | |
122 | 122 | =end original |
123 | 123 | |
124 | 124 | (定義済みかどうかではなく、真かどうかを考慮するので)警告から免れます。 |
125 | 125 | 未定義の変数を操作する、C<++>, C<-->, C<+=>, C<-=>, C<.=> のような |
126 | 126 | 演算子でも: |
127 | 127 | |
128 | undef $ | |
128 | undef $a; | |
129 | $ | |
129 | $a++; | |
130 | 130 | |
131 | 131 | =begin original |
132 | 132 | |
133 | 133 | are also always exempt from such warnings. |
134 | 134 | |
135 | 135 | =end original |
136 | 136 | |
137 | 137 | とすることでもそのような警告から免れます。 |
138 | 138 | |
139 | 139 | =begin original |
140 | 140 | |
141 | 141 | A declaration can be put anywhere a statement can, but has no effect on |
142 | 142 | the execution of the primary sequence of statements: declarations all |
143 | 143 | take effect at compile time. All declarations are typically put at |
144 | 144 | the beginning or the end of the script. However, if you're using |
145 | 145 | lexically-scoped private variables created with C<my()>, |
146 | 146 | C<state()>, or C<our()>, you'll have to make sure |
147 | 147 | your format or subroutine definition is within the same block scope |
148 | 148 | as the my if you expect to be able to access those private variables. |
149 | 149 | |
150 | 150 | =end original |
151 | 151 | |
152 | 152 | 宣言は、文が置けるところであればどこにでも置くことができますが、 |
153 | 153 | 基本的な文の並びは実行時には何の効果も持ちません: 宣言はコンパイル時に |
154 | 154 | すべての効果が表れます。 |
155 | 155 | 典型的にはすべての宣言は、スクリプトの先頭か終端に置かれます。 |
156 | 156 | しかしながら、局所変数を C<my()>,C<state()>, or C<our()> を使って作成して |
157 | 157 | レキシカルなスコープを使っているのであれば、フォーマットやサブルーチンの |
158 | 158 | 定義を、同じブロックのスコープの中でその局所変数にアクセスすることが |
159 | 159 | 可能であるようにしておく必要があるでしょう。 |
160 | 160 | |
161 | 161 | =begin original |
162 | 162 | |
163 | 163 | Declaring a subroutine allows a subroutine name to be used as if it were a |
164 | 164 | list operator from that point forward in the program. You can declare a |
165 | 165 | subroutine without defining it by saying C<sub name>, thus: |
166 | 166 | X<subroutine, declaration> |
167 | 167 | |
168 | 168 | =end original |
169 | 169 | |
170 | 170 | サブルーチンの宣言は、プログラムの後のほうにあるサブルーチン名を |
171 | 171 | リスト演算子のように使うことを許します。 |
172 | 172 | 定義されていないサブルーチンの宣言を、C<sub name> と記述することで |
173 | 173 | 宣言できるので、以下のようにできます: |
174 | 174 | X<subroutine, declaration> |
175 | 175 | |
176 | 176 | sub myname; |
177 | $me = myname $0 | |
177 | $me = myname $0 or die "can't get myname"; | |
178 | 178 | |
179 | 179 | =begin original |
180 | 180 | |
181 | 181 | A bare declaration like that declares the function to be a list operator, |
182 | 182 | not a unary operator, so you have to be careful to use parentheses (or |
183 | 183 | C<or> instead of C<||>.) The C<||> operator binds too tightly to use after |
184 | 184 | list operators; it becomes part of the last element. You can always use |
185 | 185 | parentheses around the list operators arguments to turn the list operator |
186 | 186 | back into something that behaves more like a function call. Alternatively, |
187 | 187 | you can use the prototype C<($)> to turn the subroutine into a unary |
188 | 188 | operator: |
189 | 189 | |
190 | 190 | =end original |
191 | 191 | |
192 | 192 | 関数の宣言のような裸の宣言はリスト演算子のように働くのであり、 |
193 | 193 | 単項演算子としてではありません; ですから、かっこ (または C<||> の代わりに |
194 | 194 | C<or>) を使うことには注意してください。 |
195 | 195 | C<||> 演算子はリスト演算子の後ろに使うには束縛が強すぎます; 最後の要素の |
196 | 196 | 一部になります。 |
197 | 197 | リスト演算子の周りをかっこで囲むことでいつでもリスト演算子を |
198 | 198 | 関数呼び出しのように振る舞わせるようにできます。 |
199 | 199 | あるいは、サブルーチンを単項演算子に変えるためにプロトタイプ |
200 | 200 | C<($)> も使えます: |
201 | 201 | |
202 | 202 | sub myname ($); |
203 | 203 | $me = myname $0 || die "can't get myname"; |
204 | 204 | |
205 | 205 | =begin original |
206 | 206 | |
207 | 207 | That now parses as you'd expect, but you still ought to get in the habit of |
208 | 208 | using parentheses in that situation. For more on prototypes, see |
209 | 209 | L<perlsub>. |
210 | 210 | |
211 | 211 | =end original |
212 | 212 | |
213 | 213 | これは予想した通りにパースしますが、それでもこのような場合にはかっこを使う |
214 | 214 | 習慣を付けるべきです。 |
215 | 215 | プロトタイプに関してさらなる情報は、L<perlsub> を参照してください。 |
216 | 216 | |
217 | 217 | =begin original |
218 | 218 | |
219 | 219 | Subroutines declarations can also be loaded up with the C<require> statement |
220 | 220 | or both loaded and imported into your namespace with a C<use> statement. |
221 | 221 | See L<perlmod> for details on this. |
222 | 222 | |
223 | 223 | =end original |
224 | 224 | |
225 | 225 | サブルーチンの宣言は C<require> 文を使って詰め込むこともできますし、 |
226 | 226 | C<use> 文を使って自分の名前空間にロードしたりインポートしたりすることが |
227 | 227 | できます。 |
228 | 228 | これに関する詳細は L<perlmod> を参照してください。 |
229 | 229 | |
230 | 230 | =begin original |
231 | 231 | |
232 | 232 | A statement sequence may contain declarations of lexically-scoped |
233 | 233 | variables, but apart from declaring a variable name, the declaration acts |
234 | 234 | like an ordinary statement, and is elaborated within the sequence of |
235 | 235 | statements as if it were an ordinary statement. That means it actually |
236 | 236 | has both compile-time and run-time effects. |
237 | 237 | |
238 | 238 | =end original |
239 | 239 | |
240 | 240 | 文の並びはレキシカルスコープを持った変数の宣言を含むことができますが、 |
241 | 241 | 変数名の宣言とは切り離され、その宣言は通常の文のように振る舞い、 |
242 | 242 | それが通常の文であるかのように文の並びに組みこまれます。 |
243 | 243 | これは、そういった宣言がコンパイル時の効果と実行時の効果の両方を |
244 | 244 | 持っているということです。 |
245 | 245 | |
246 | 246 | =head2 Comments |
247 | 247 | X<comment> X<#> |
248 | 248 | |
249 | 249 | (コメント) |
250 | 250 | |
251 | 251 | =begin original |
252 | 252 | |
253 | 253 | Text from a C<"#"> character until the end of the line is a comment, |
254 | 254 | and is ignored. Exceptions include C<"#"> inside a string or regular |
255 | 255 | expression. |
256 | 256 | |
257 | 257 | =end original |
258 | 258 | |
259 | 259 | コメントは C<"#"> 文字から、行末まで続き、その部分は無視されます。 |
260 | 260 | 例外は、文字列や正規表現の中にある C<"#"> です。 |
261 | 261 | |
262 | 262 | =head2 Simple Statements |
263 | 263 | X<statement> X<semicolon> X<expression> X<;> |
264 | 264 | |
265 | 265 | (単純文) |
266 | 266 | |
267 | 267 | =begin original |
268 | 268 | |
269 | 269 | The only kind of simple statement is an expression evaluated for its |
270 | 270 | side-effects. Every simple statement must be terminated with a |
271 | 271 | semicolon, unless it is the final statement in a block, in which case |
272 | 272 | the semicolon is optional. But put the semicolon in anyway if the |
273 | 273 | block takes up more than one line, because you may eventually add |
274 | 274 | another line. Note that there are operators like C<eval {}>, C<sub {}>, and |
275 | 275 | C<do {}> that I<look> like compound statements, but aren't--they're just |
276 | 276 | TERMs in an expression--and thus need an explicit termination when used |
277 | 277 | as the last item in a statement. |
278 | 278 | |
279 | 279 | =end original |
280 | 280 | |
281 | 281 | 単純文となる唯一の種類は、その副作用のために評価される式です。 |
282 | 282 | すべての単純文は、それがセミコロンを省略することのできるブロックの |
283 | 283 | 最後にない限りは文を終端するためのセミコロンがなければなりません。 |
284 | 284 | ブロックが二行以上に渡る場合には、とにかくセミコロンを付けてください; |
285 | 285 | なぜなら、別の行を追加する可能性があるからです。 |
286 | 286 | C<eval {}>, C<sub {}>, C<do {}> のように、一見複合文のように I<見える> けれども |
287 | 287 | そうではない--これらは単なる式における TERM です--ものがあって、 |
288 | 288 | そういったものを文の最後のアイテムとして使った場合には明示的に終端する |
289 | 289 | 必要があるのだということに注意してください。 |
290 | 290 | |
291 | =head2 Truth and Falsehood | |
292 | X<truth> X<falsehood> X<true> X<false> X<!> X<not> X<negation> X<0> | |
293 | ||
294 | (真偽値) | |
295 | ||
296 | =begin original | |
297 | ||
298 | The number 0, the strings C<'0'> and C<"">, the empty list C<()>, and | |
299 | C<undef> are all false in a boolean context. All other values are true. | |
300 | Negation of a true value by C<!> or C<not> returns a special false value. | |
301 | When evaluated as a string it is treated as C<"">, but as a number, it | |
302 | is treated as 0. Most Perl operators | |
303 | that return true or false behave this way. | |
304 | ||
305 | =end original | |
306 | ||
307 | 数値 0, 文字列 C<'0'> と C<"">, 空リスト C<()>, C<undef> は全て真偽値 | |
308 | コンテキストでは偽となります。 | |
309 | その他の全ての値は真です。 | |
310 | 真の値を C<!> や C<not> で否定すると、特殊な偽の値を返します。 | |
311 | これを文字列として評価すると C<""> として扱われますが、数値として評価すると | |
312 | 0 として扱われます。 | |
313 | 真または偽を返す、ほとんど Perl の演算子はこのように振る舞います。 | |
314 | ||
291 | 315 | =head2 Statement Modifiers |
292 | 316 | X<statement modifier> X<modifier> X<if> X<unless> X<while> |
293 | 317 | X<until> X<when> X<foreach> X<for> |
294 | 318 | |
295 | 319 | (文修飾子) |
296 | 320 | |
297 | 321 | =begin original |
298 | 322 | |
299 | 323 | Any simple statement may optionally be followed by a I<SINGLE> modifier, |
300 | 324 | just before the terminating semicolon (or block ending). The possible |
301 | 325 | modifiers are: |
302 | 326 | |
303 | 327 | =end original |
304 | 328 | |
305 | 329 | 任意の単純文には、B<一つ> の修飾子を終端のセミコロンの直前(もしくは |
306 | 330 | ブロックの終端の直前)に付けることができます。 |
307 | 331 | 使うことのできる修飾子は以下の通りです。 |
308 | 332 | |
309 | 333 | if EXPR |
310 | 334 | unless EXPR |
311 | 335 | while EXPR |
312 | 336 | until EXPR |
313 | 337 | for LIST |
314 | 338 | foreach LIST |
315 | 339 | when EXPR |
316 | 340 | |
317 | 341 | =begin original |
318 | 342 | |
319 | 343 | The C<EXPR> following the modifier is referred to as the "condition". |
320 | 344 | Its truth or falsehood determines how the modifier will behave. |
321 | 345 | |
322 | 346 | =end original |
323 | 347 | |
324 | 348 | 修飾子に引き続く C<EXPR> は「条件」として参照されます。 |
325 | 349 | その真偽値が修飾子の振る舞いを決定します。 |
326 | 350 | |
327 | 351 | =begin original |
328 | 352 | |
329 | 353 | C<if> executes the statement once I<if> and only if the condition is |
330 | 354 | true. C<unless> is the opposite, it executes the statement I<unless> |
331 | the condition is true (that is, if the condition is false). | |
355 | the condition is true (that is, if the condition is false). | |
332 | L<perldata/Scalar values> for definitions of true and false. | |
333 | 356 | |
334 | 357 | =end original |
335 | 358 | |
336 | 359 | C<if> は I<もし> 条件が真の場合にのみ文を実行します。 |
337 | 360 | C<unless> は逆で、条件が真 I<でない限り> (つまり、条件が偽なら) 文を |
338 | 361 | 実行します。 |
339 | 真と偽の定義については L<perldata/Scalar values> を参照してください。 | |
340 | 362 | |
341 | 363 | print "Basset hounds got long ears" if length $ear >= 10; |
342 | 364 | go_outside() and play() unless $is_raining; |
343 | 365 | |
344 | 366 | =begin original |
345 | 367 | |
346 | 368 | The C<for(each)> modifier is an iterator: it executes the statement once |
347 | 369 | for each item in the LIST (with C<$_> aliased to each item in turn). |
348 | There is no syntax to specify a C-style for loop or a lexically scoped | |
349 | iteration variable in this form. | |
350 | 370 | |
351 | 371 | =end original |
352 | 372 | |
353 | 373 | C<for(each)> 修飾子は反復子です: |
354 | 374 | LIST の値それぞれ毎に文を実行します(実行中は C<$_> がそれぞれの値の |
355 | 375 | エイリアスとなります)。 |
356 | この形式で C 風のループやレキシカルスコープの繰り返し変数を | |
357 | 指定する文法はありません。 | |
358 | 376 | |
359 | 377 | print "Hello $_!\n" for qw(world Dolly nurse); |
360 | 378 | |
361 | 379 | =begin original |
362 | 380 | |
363 | 381 | C<while> repeats the statement I<while> the condition is true. |
364 | Postfix C<while> has the same magic treatment of some kinds of condition | |
365 | that prefix C<while> has. | |
366 | 382 | C<until> does the opposite, it repeats the statement I<until> the |
367 | 383 | condition is true (or while the condition is false): |
368 | 384 | |
369 | 385 | =end original |
370 | 386 | |
371 | 387 | C<while> は条件が真 I<の間> 文を繰り返します。 |
372 | 後置 C<while> は、ある種の条件において、前置 C<while> と同じ | |
373 | マジカルな処理をします。 | |
374 | 388 | C<until> は逆で、条件が真 I<になるまで> (つまり条件が偽の間) 文を |
375 | 389 | 繰り返します: |
376 | 390 | |
377 | 391 | # Both of these count from 0 to 10. |
378 | 392 | print $i++ while $i <= 10; |
379 | 393 | print $j++ until $j > 10; |
380 | 394 | |
381 | 395 | =begin original |
382 | 396 | |
383 | 397 | The C<while> and C<until> modifiers have the usual "C<while> loop" |
384 | 398 | semantics (conditional evaluated first), except when applied to a |
385 | 399 | C<do>-BLOCK (or to the Perl4 C<do>-SUBROUTINE statement), in |
386 | 400 | which case the block executes once before the conditional is |
387 | 401 | evaluated. |
388 | 402 | |
389 | 403 | =end original |
390 | 404 | |
391 | 405 | 修飾子 C<while> と C<until> は、一般的な "C<while> loop" の意味を |
392 | 406 | 持っています(条件が最初に評価される)が、C<do>-ブロック(もしくは Perl4 の |
393 | 407 | C<do>-サブルーチン文)に適用されるときは例外で、 |
394 | 408 | このときは条件が評価されるよりも前に、一度ブロックが実行されます。 |
395 | 409 | |
396 | 410 | =begin original |
397 | 411 | |
398 | 412 | This is so that you can write loops like: |
399 | 413 | |
400 | 414 | =end original |
401 | 415 | |
402 | 416 | このため、次のようなループを記述することができます: |
403 | 417 | |
404 | 418 | do { |
405 | ||
419 | $line = <STDIN>; | |
406 | ||
420 | ... | |
407 | 421 | } until !defined($line) || $line eq ".\n" |
408 | 422 | |
409 | 423 | =begin original |
410 | 424 | |
411 | 425 | See L<perlfunc/do>. Note also that the loop control statements described |
412 | 426 | later will I<NOT> work in this construct, because modifiers don't take |
413 | 427 | loop labels. Sorry. You can always put another block inside of it |
414 | (for C<next> | |
428 | (for C<next>) or around it (for C<last>) to do that sort of thing. | |
429 | For C<next>, just double the braces: | |
415 | 430 | X<next> X<last> X<redo> |
416 | 431 | |
417 | 432 | =end original |
418 | 433 | |
419 | 434 | L<perlfunc/do> を参照してください。 |
420 | 435 | 後述するループの制御文は、修飾子がループラベルを取らないために |
421 | 436 | この構造文では I<動作しない> ということにも注意してください。 |
422 | 437 | 申し訳ない。 |
423 | 438 | こういった場合に対処するのに別のブロックを内側に入れたり(C<next> の場合)、 |
424 | 別のブロックで囲む(C<last> | |
439 | 別のブロックで囲む(C<last> の場合)という方法が常に使えます。 | |
440 | C<next> では単に中かっこを二重にします: | |
425 | 441 | X<next> X<last> X<redo> |
426 | 442 | |
427 | =begin original | |
428 | ||
429 | For C<next> or C<redo>, just double the braces: | |
430 | ||
431 | =end original | |
432 | ||
433 | C<next> や C<redo> では単に中かっこを二重にします: | |
434 | ||
435 | 443 | do {{ |
436 | ||
444 | next if $x == $y; | |
437 | ||
445 | # do something here | |
438 | 446 | }} until $x++ > $z; |
439 | 447 | |
440 | 448 | =begin original |
441 | 449 | |
442 | For C<last>, you have to be more elaborate | |
450 | For C<last>, you have to be more elaborate: | |
443 | 451 | X<last> |
444 | 452 | |
445 | 453 | =end original |
446 | 454 | |
447 | C<last> の場合は、もっと念入りにする必要があり | |
455 | C<last> の場合は、もっと念入りにする必要があります: | |
448 | 456 | |
449 | { | |
457 | LOOP: { | |
450 | ||
458 | do { | |
451 | ||
459 | last if $x = $y**2; | |
452 | ||
460 | # do something here | |
453 | ||
461 | } while $x++ <= $z; | |
454 | 462 | } |
455 | 463 | |
456 | 464 | =begin original |
457 | 465 | |
458 | If you need both C<next> and C<last>, you have to do both and also use a | |
459 | loop label: | |
460 | ||
461 | =end original | |
462 | ||
463 | C<next> と C<last> の両方が必要な場合、両方を使ってさらにループラベルを | |
464 | 使う必要があります: | |
465 | ||
466 | LOOP: { | |
467 | do {{ | |
468 | next if $x == $y; | |
469 | last LOOP if $x == $y**2; | |
470 | # do something here | |
471 | }} until $x++ > $z; | |
472 | } | |
473 | ||
474 | =begin original | |
475 | ||
476 | 466 | B<NOTE:> The behaviour of a C<my>, C<state>, or |
477 | 467 | C<our> modified with a statement modifier conditional |
478 | 468 | or loop construct (for example, C<my $x if ...>) is |
479 | 469 | B<undefined>. The value of the C<my> variable may be C<undef>, any |
480 | 470 | previously assigned value, or possibly anything else. Don't rely on |
481 | 471 | it. Future versions of perl might do something different from the |
482 | 472 | version of perl you try it out on. Here be dragons. |
483 | 473 | X<my> |
484 | 474 | |
485 | 475 | =end original |
486 | 476 | |
487 | 477 | B<注意:> (C<my $x if ...> のような) 条件構造やループ構造で修飾された |
488 | 478 | C<my> C<state>,C<our> 文の振る舞いは B<未定義> です。 |
489 | 479 | C<my> 変数の値は C<undef> かも知れませんし、以前に代入された値かも |
490 | 480 | 知れませんし、その他の如何なる値の可能性もあります。 |
491 | 481 | この値に依存してはいけません。 |
492 | 482 | perl の将来のバージョンでは現在のバージョンとは何か違うかも知れません。 |
493 | 483 | ここには厄介なものがいます。 |
494 | 484 | X<my> |
495 | 485 | |
496 | 486 | =begin original |
497 | 487 | |
498 | 488 | The C<when> modifier is an experimental feature that first appeared in Perl |
499 | 489 | 5.14. To use it, you should include a C<use v5.14> declaration. |
500 | 490 | (Technically, it requires only the C<switch> feature, but that aspect of it |
501 | 491 | was not available before 5.14.) Operative only from within a C<foreach> |
502 | 492 | loop or a C<given> block, it executes the statement only if the smartmatch |
503 | 493 | C<< $_ ~~ I<EXPR> >> is true. If the statement executes, it is followed by |
504 | 494 | a C<next> from inside a C<foreach> and C<break> from inside a C<given>. |
505 | 495 | |
506 | 496 | =end original |
507 | 497 | |
508 | 498 | C<when> 修飾子は Perl 5.14 で最初に現れた実験的機能です。 |
509 | 499 | 使うには、C<use v5.14> 宣言を含めます。 |
510 | 500 | (技術的には、C<switch> 機能だけが必要ですが、この観点では 5.14 より前では |
511 | 501 | 利用できません。) |
512 | 502 | C<foreach> ループか C<given> ブロックの内側でのみ動作可能で、 |
513 | 503 | スマートマッチング C<< $_ ~~ I<EXPR> >> が真の場合にのみ実行されます。 |
514 | 504 | 文が実行されると、C<foreach> の内側からは C<next> に、C<given> の |
515 | 505 | 内側からは C<break> に引き続きます。 |
516 | 506 | |
517 | 507 | =begin original |
518 | 508 | |
519 | 509 | Under the current implementation, the C<foreach> loop can be |
520 | 510 | anywhere within the C<when> modifier's dynamic scope, but must be |
521 | within the C<given> block's lexical scope. This restrict | |
511 | within the C<given> block's lexical scope. This restricted may | |
522 | be relaxed in a future release. See L< | |
512 | be relaxed in a future release. See L<"Switch Statements"> below. | |
523 | 513 | |
524 | 514 | =end original |
525 | 515 | |
526 | 516 | 現在の実装では、C<foreach> ループは C<when> 修飾子の動的スコープの内側の |
527 | 517 | どこでも使えますが、C<given> ブロックのレキシカルスコープの内側で |
528 | 518 | なければなりません。 |
529 | 519 | この制限は将来のリリースで緩和されるかもしれません。 |
530 | 後述する L< | |
520 | 後述する L<"Switch Statements"> を参照してください。 | |
531 | 521 | |
532 | 522 | =head2 Compound Statements |
533 | 523 | X<statement, compound> X<block> X<bracket, curly> X<curly bracket> X<brace> |
534 | 524 | X<{> X<}> X<if> X<unless> X<given> X<while> X<until> X<foreach> X<for> X<continue> |
535 | 525 | |
536 | 526 | (複合文) |
537 | 527 | |
538 | 528 | =begin original |
539 | 529 | |
540 | 530 | In Perl, a sequence of statements that defines a scope is called a block. |
541 | 531 | Sometimes a block is delimited by the file containing it (in the case |
542 | 532 | of a required file, or the program as a whole), and sometimes a block |
543 | 533 | is delimited by the extent of a string (in the case of an eval). |
544 | 534 | |
545 | 535 | =end original |
546 | 536 | |
547 | 537 | Perl では、スコープを定義するような文の並びをブロックと呼びます。 |
548 | 538 | ブロックはそれを含むファイルによって範囲が定められることがあります |
549 | 539 | (ファイルが require されたときか、プログラム全体としての場合)し、 |
550 | 540 | 文字列の展開によって範囲が定められる(eval の場合)こともあります。 |
551 | 541 | |
552 | 542 | =begin original |
553 | 543 | |
554 | But generally, a block is delimited by curly brackets, also known as | |
544 | But generally, a block is delimited by curly brackets, also known as braces. | |
555 | ||
545 | We will call this syntactic construct a BLOCK. | |
556 | braces are also the syntax for hash reference constructor expressions | |
557 | (see L<perlref>), you may occasionally need to disambiguate by placing a | |
558 | C<;> immediately after an opening brace so that Perl realises the brace | |
559 | is the start of a block. You will more frequently need to disambiguate | |
560 | the other way, by placing a C<+> immediately before an opening brace to | |
561 | force it to be interpreted as a hash reference constructor expression. | |
562 | It is considered good style to use these disambiguating mechanisms | |
563 | liberally, not only when Perl would otherwise guess incorrectly. | |
564 | 546 | |
565 | 547 | =end original |
566 | 548 | |
567 | 549 | しかし一般的には、ブロックは中かっこによって範囲が定められます。 |
568 | 550 | この構文的な構造をブロックと呼びます。 |
569 | 中かっこによる囲みはハッシュリファレンス初期化表現 (L<perlref> 参照) の | |
570 | 文法でもあるので、中かっこがブロックの開始であることを Perl が分かるように、 | |
571 | 時々開き中かっこの直後に C<;> を置くことで曖昧さをなくす | |
572 | 必要があるかもしれません。 | |
573 | より頻繁には、ハッシュリファレンス初期化表現として解釈されることを | |
574 | 強制するために、開き中かっこの直前に C<+> を置くことで逆方向に | |
575 | 曖昧さをなくす必要があるかも知れません。 | |
576 | これらのあいまいさをなくす機構は、Perl が間違って推測するときだけではなく、 | |
577 | より安全に使うのがよいスタイルと考えられています。 | |
578 | 551 | |
579 | 552 | =begin original |
580 | 553 | |
581 | 554 | The following compound statements may be used to control flow: |
582 | 555 | |
583 | 556 | =end original |
584 | 557 | |
585 | 558 | 以下に挙げる複合文を制御フローとして使うことができます: |
586 | 559 | |
587 | 560 | if (EXPR) BLOCK |
588 | 561 | if (EXPR) BLOCK else BLOCK |
589 | 562 | if (EXPR) BLOCK elsif (EXPR) BLOCK ... |
590 | 563 | if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK |
591 | 564 | |
592 | 565 | unless (EXPR) BLOCK |
593 | 566 | unless (EXPR) BLOCK else BLOCK |
594 | 567 | unless (EXPR) BLOCK elsif (EXPR) BLOCK ... |
595 | 568 | unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK |
596 | 569 | |
597 | 570 | given (EXPR) BLOCK |
598 | 571 | |
599 | 572 | LABEL while (EXPR) BLOCK |
600 | 573 | LABEL while (EXPR) BLOCK continue BLOCK |
601 | 574 | |
602 | 575 | LABEL until (EXPR) BLOCK |
603 | 576 | LABEL until (EXPR) BLOCK continue BLOCK |
604 | 577 | |
605 | 578 | LABEL for (EXPR; EXPR; EXPR) BLOCK |
606 | 579 | LABEL for VAR (LIST) BLOCK |
607 | 580 | LABEL for VAR (LIST) BLOCK continue BLOCK |
608 | 581 | |
609 | 582 | LABEL foreach (EXPR; EXPR; EXPR) BLOCK |
610 | 583 | LABEL foreach VAR (LIST) BLOCK |
611 | 584 | LABEL foreach VAR (LIST) BLOCK continue BLOCK |
612 | 585 | |
613 | 586 | LABEL BLOCK |
614 | 587 | LABEL BLOCK continue BLOCK |
615 | 588 | |
616 | 589 | PHASE BLOCK |
617 | 590 | |
618 | 591 | =begin original |
619 | 592 | |
620 | ||
593 | The experimental C<given> statement is I<not automatically enabled>; see | |
621 | a list of lexicals within parentheses: | |
622 | ||
623 | =end original | |
624 | ||
625 | Perl 5.36 から、かっこの中にレキシカル変数のリストを指定することで、 | |
626 | 一度に複数の値を反復できます: | |
627 | ||
628 | LABEL for my (VAR, VAR) (LIST) BLOCK | |
629 | LABEL for my (VAR, VAR) (LIST) BLOCK continue BLOCK | |
630 | LABEL foreach my (VAR, VAR) (LIST) BLOCK | |
631 | LABEL foreach my (VAR, VAR) (LIST) BLOCK continue BLOCK | |
632 | ||
633 | =begin original | |
634 | ||
635 | If enabled by the C<try> feature, the following may also be used | |
636 | ||
637 | =end original | |
638 | ||
639 | 実験的な C<try> 機能によって有効の場合、次のものも使えます: | |
640 | ||
641 | try BLOCK catch (VAR) BLOCK | |
642 | try BLOCK catch (VAR) BLOCK finally BLOCK | |
643 | ||
644 | =begin original | |
645 | ||
646 | The experimental C<given> statement is I<not automatically enabled>; see | |
647 | 594 | L</"Switch Statements"> below for how to do so, and the attendant caveats. |
648 | 595 | |
649 | 596 | =end original |
650 | 597 | |
651 | 実験的な C<given> 文は I<自動的に有効に | |
598 | 実験的な C<given> 文は I<自動的には有効になりません>; そうするための | |
652 | ||
599 | 方法と、付随する問題点については後述する L</"Switch Statements"> を | |
600 | 参照してください。 | |
653 | 601 | |
654 | 602 | =begin original |
655 | 603 | |
656 | 604 | Unlike in C and Pascal, in Perl these are all defined in terms of BLOCKs, |
657 | 605 | not statements. This means that the curly brackets are I<required>--no |
658 | 606 | dangling statements allowed. If you want to write conditionals without |
659 | 607 | curly brackets, there are several other ways to do it. The following |
660 | 608 | all do the same thing: |
661 | 609 | |
662 | 610 | =end original |
663 | 611 | |
664 | 612 | C や Pascal とは異なり、Perl ではブロックを取るように |
665 | 613 | 定義されていて文を取るのではありません。 |
666 | 614 | つまり、中かっこは I<必要なもの> です -- 曖昧な文が許されません。 |
667 | 615 | 中かっこなしの条件文を使いたいのであれば、いくつかのやり方があります。 |
668 | 616 | 以下の全ては同じことです: |
669 | 617 | |
670 | 618 | if (!open(FOO)) { die "Can't open $FOO: $!" } |
671 | 619 | die "Can't open $FOO: $!" unless open(FOO); |
672 | 620 | open(FOO) || die "Can't open $FOO: $!"; |
673 | 621 | open(FOO) ? () : die "Can't open $FOO: $!"; |
674 | ||
622 | # a bit exotic, that last one | |
675 | 623 | |
676 | 624 | =begin original |
677 | 625 | |
678 | 626 | The C<if> statement is straightforward. Because BLOCKs are always |
679 | 627 | bounded by curly brackets, there is never any ambiguity about which |
680 | 628 | C<if> an C<else> goes with. If you use C<unless> in place of C<if>, |
681 | 629 | the sense of the test is reversed. Like C<if>, C<unless> can be followed |
682 | 630 | by C<else>. C<unless> can even be followed by one or more C<elsif> |
683 | 631 | statements, though you may want to think twice before using that particular |
684 | 632 | language construct, as everyone reading your code will have to think at least |
685 | 633 | twice before they can understand what's going on. |
686 | 634 | |
687 | 635 | =end original |
688 | 636 | |
689 | 637 | C<if> 文は明解です。 |
690 | 638 | ブロックは常に中かっこで区切られるので、C<if> と C<else> の対応が |
691 | 639 | 曖昧になるようなことは決してありません。 |
692 | 640 | C<unless> を C<if> の代わりに使うと、検査を反転します。 |
693 | 641 | C<if> と同様、C<unless> は C<else> に引き続くことができます。 |
694 | 642 | C<unless> は一つまたはそれ以上の C<elsif> に引き続くことすらできますが、 |
695 | 643 | この特定の言語構文を使う前に二倍考えたいでしょう; あなたのコードを読む |
696 | 644 | 誰もが何が行われているのかを理解する前に少なくとも二倍考える必要が |
697 | 645 | あるからです。 |
698 | 646 | |
699 | 647 | =begin original |
700 | 648 | |
701 | 649 | The C<while> statement executes the block as long as the expression is |
702 | true. | |
650 | L<true|/"Truth and Falsehood">. | |
703 | 651 | The C<until> statement executes the block as long as the expression is |
704 | 652 | false. |
705 | 653 | The LABEL is optional, and if present, consists of an identifier followed |
706 | 654 | by a colon. The LABEL identifies the loop for the loop control |
707 | 655 | statements C<next>, C<last>, and C<redo>. |
708 | 656 | If the LABEL is omitted, the loop control statement |
709 | 657 | refers to the innermost enclosing loop. This may include dynamically |
710 | ||
658 | looking back your call-stack at run time to find the LABEL. Such | |
711 | 659 | desperate behavior triggers a warning if you use the C<use warnings> |
712 | 660 | pragma or the B<-w> flag. |
713 | 661 | |
714 | 662 | =end original |
715 | 663 | |
716 | C<while> 文は、式が真である間、ブロックを | |
664 | C<while> 文は、式が L<真|/"Truth and Falsehood"> である間、ブロックを | |
665 | 実行します。 | |
717 | 666 | C<until> 文は、式が偽である間、ブロックを実行します。 |
718 | 667 | LABEL は省略可能ですが、ある場合には、コロンを伴った識別子になります。 |
719 | 668 | LABEL は C<next>、C<last>、C<redo> といったループ制御文のループを規定します。 |
720 | 669 | LABEL が省略された場合、ループ制御文はそれを含むループの中で最も内側の |
721 | 670 | ループを参照します。 |
722 | これは、実行時に LABEL を検出するための呼び出しスタック | |
671 | これは、実行時に LABEL を検出するための呼び出しスタックの動的な後戻り検索を | |
723 | 672 | 含むことができます。 |
724 | 673 | そのような推奨されない振る舞いは、C<use warnings> プラグマや B<-w> フラグを |
725 | 674 | 使った場合には警告を引き起こします。 |
726 | 675 | |
727 | 676 | =begin original |
728 | 677 | |
729 | If the condition expression of a C<while> statement is based | |
730 | on any of a group of iterative expression types then it gets | |
731 | some magic treatment. The affected iterative expression types | |
732 | are L<C<readline>|perlfunc/readline EXPR>, the L<C<< <FILEHANDLE> | |
733 | >>|perlop/"I/O Operators"> input operator, L<C<readdir>|perlfunc/readdir | |
734 | DIRHANDLE>, L<C<glob>|perlfunc/glob EXPR>, the L<C<< <PATTERN> | |
735 | >>|perlop/"I/O Operators"> globbing operator, and L<C<each>|perlfunc/each | |
736 | HASH>. If the condition expression is one of these expression types, then | |
737 | the value yielded by the iterative operator will be implicitly assigned | |
738 | to C<$_>. If the condition expression is one of these expression types | |
739 | or an explicit assignment of one of them to a scalar, then the condition | |
740 | actually tests for definedness of the expression's value, not for its | |
741 | regular truth value. | |
742 | ||
743 | =end original | |
744 | ||
745 | C<while> 文の条件式が反復式型のグループの一つの場合、 | |
746 | マジカルな扱いを受けます。 | |
747 | 影響を受ける反復式型は、 | |
748 | L<C<readline>|perlfunc/readline EXPR>、L<C<< <FILEHANDLE> | |
749 | >>|perlop/"I/O Operators"> 入力反復子、L<C<readdir>|perlfunc/readdir | |
750 | DIRHANDLE>、L<C<glob>|perlfunc/glob EXPR>、L<C<< <PATTERN> | |
751 | >>|perlop/"I/O Operators"> グロブ演算子、L<C<each>|perlfunc/each | |
752 | HASH> です。 | |
753 | 条件式がこれらの式の型の一つの場合、反復演算子によって | |
754 | 取り出された値は暗黙に C<$_> に代入されます。 | |
755 | 条件式がこれらの式の型の一つか、これらの一つからスカラへの明示的な代入の | |
756 | 場合、条件は実際には通常の真の値かどうかではなく、式の値が | |
757 | 定義値かどうかをテストします。 | |
758 | ||
759 | =begin original | |
760 | ||
761 | 678 | If there is a C<continue> BLOCK, it is always executed just before the |
762 | 679 | conditional is about to be evaluated again. Thus it can be used to |
763 | 680 | increment a loop variable, even when the loop has been continued via |
764 | 681 | the C<next> statement. |
765 | 682 | |
766 | 683 | =end original |
767 | 684 | |
768 | 685 | C<continue> ブロックが存在する場合、 |
769 | 686 | 常に条件が再評価される直前に実行されます。 |
770 | 687 | したがって、このブロックをループ変数のインクリメントのために |
771 | 688 | 使うことができます; |
772 | 689 | これは、ループがC<next>文を通して継続されるときでも実行されます。 |
773 | 690 | |
774 | 691 | =begin original |
775 | 692 | |
776 | When a block is preced | |
693 | When a block is preceding by a compilation phase keyword such as C<BEGIN>, | |
777 | 694 | C<END>, C<INIT>, C<CHECK>, or C<UNITCHECK>, then the block will run only |
778 | 695 | during the corresponding phase of execution. See L<perlmod> for more details. |
779 | 696 | |
780 | 697 | =end original |
781 | 698 | |
782 | 699 | ブロックの前に C<BEGIN>, C<END>, C<INIT>, C<CHECK>, C<UNITCHECK> のような |
783 | 700 | コンパイルフェーズキーワードが前置されると、ブロックは対応する実行フェーズの |
784 | 701 | 間にだけ実行されます。 |
785 | 702 | さらなる詳細については L<perlmod> を参照してください。 |
786 | 703 | |
787 | 704 | =begin original |
788 | 705 | |
789 | 706 | Extension modules can also hook into the Perl parser to define new |
790 | 707 | kinds of compound statements. These are introduced by a keyword which |
791 | 708 | the extension recognizes, and the syntax following the keyword is |
792 | 709 | defined entirely by the extension. If you are an implementor, see |
793 | 710 | L<perlapi/PL_keyword_plugin> for the mechanism. If you are using such |
794 | 711 | a module, see the module's documentation for details of the syntax that |
795 | 712 | it defines. |
796 | 713 | |
797 | 714 | =end original |
798 | 715 | |
799 | 716 | エクステンションモジュールは新しい種類の複合文を定義するために |
800 | 717 | Perl パーサをフックできます。 |
801 | 718 | これらはエクステンションが認識するキーワードで導入され、キーワードに |
802 | 719 | 引き続く文法は完全にエクステンションで定義されます。 |
803 | 720 | もしあなたが実装車なら、仕組みについては L<perlapi/PL_keyword_plugin> を |
804 | 721 | 参照してください。 |
805 | 722 | あなたがそのようなモジュールを使うなら、定義されている文法の詳細については |
806 | 723 | そのモジュールの文書を参照してください。 |
807 | 724 | |
808 | 725 | =head2 Loop Control |
809 | 726 | X<loop control> X<loop, control> X<next> X<last> X<redo> X<continue> |
810 | 727 | |
811 | 728 | (ループ制御) |
812 | 729 | |
813 | 730 | =begin original |
814 | 731 | |
815 | 732 | The C<next> command starts the next iteration of the loop: |
816 | 733 | |
817 | 734 | =end original |
818 | 735 | |
819 | 736 | C<next> コマンドはループの次の繰り返しを開始します: |
820 | 737 | |
821 | 738 | LINE: while (<STDIN>) { |
822 | ||
739 | next LINE if /^#/; # discard comments | |
823 | ||
740 | ... | |
824 | 741 | } |
825 | 742 | |
826 | 743 | =begin original |
827 | 744 | |
828 | 745 | The C<last> command immediately exits the loop in question. The |
829 | 746 | C<continue> block, if any, is not executed: |
830 | 747 | |
831 | 748 | =end original |
832 | 749 | |
833 | 750 | C<last> コマンドはループから即座に脱出します。 |
834 | 751 | C<continue> ブロックがあっても、それは実行されません: |
835 | 752 | |
836 | 753 | LINE: while (<STDIN>) { |
837 | ||
754 | last LINE if /^$/; # exit when done with header | |
838 | ||
755 | ... | |
839 | 756 | } |
840 | 757 | |
841 | 758 | =begin original |
842 | 759 | |
843 | 760 | The C<redo> command restarts the loop block without evaluating the |
844 | 761 | conditional again. The C<continue> block, if any, is I<not> executed. |
845 | 762 | This command is normally used by programs that want to lie to themselves |
846 | 763 | about what was just input. |
847 | 764 | |
848 | 765 | =end original |
849 | 766 | |
850 | 767 | C<redo> コマンドは、条件の再評価をすることなしにループブロックの |
851 | 768 | 再実行を行います。 |
852 | 769 | C<continue> ブロックがあっても、それは I<実行されません>。 |
853 | 770 | このコマンドは通常、プログラムに対する入力に関してプログラム自身を |
854 | 771 | だましたいといったときに使われます。 |
855 | 772 | |
856 | 773 | =begin original |
857 | 774 | |
858 | 775 | For example, when processing a file like F</etc/termcap>. |
859 | 776 | If your input lines might end in backslashes to indicate continuation, you |
860 | 777 | want to skip ahead and get the next record. |
861 | 778 | |
862 | 779 | =end original |
863 | 780 | |
864 | 781 | たとえば、F</etc/termcap> のようなファイルを処理することを |
865 | 782 | 考えてみましょう。 |
866 | 783 | もし入力された行の行末が継続を示すバックスラッシュであった場合、先へ進んで |
867 | 784 | 次のレコードを取り出したいと思うでしょう。 |
868 | 785 | |
869 | 786 | while (<>) { |
870 | ||
787 | chomp; | |
871 | ||
788 | if (s/\\$//) { | |
872 | ||
789 | $_ .= <>; | |
873 | ||
790 | redo unless eof(); | |
874 | ||
791 | } | |
875 | ||
792 | # now process $_ | |
876 | 793 | } |
877 | 794 | |
878 | 795 | =begin original |
879 | 796 | |
880 | 797 | which is Perl shorthand for the more explicitly written version: |
881 | 798 | |
882 | 799 | =end original |
883 | 800 | |
884 | 801 | これは Perl の省略記法で、もっとはっきりと書くと以下のようになります: |
885 | 802 | |
886 | 803 | LINE: while (defined($line = <ARGV>)) { |
887 | ||
804 | chomp($line); | |
888 | ||
805 | if ($line =~ s/\\$//) { | |
889 | ||
806 | $line .= <ARGV>; | |
890 | ||
807 | redo LINE unless eof(); # not eof(ARGV)! | |
891 | ||
808 | } | |
892 | ||
809 | # now process $line | |
893 | 810 | } |
894 | 811 | |
895 | 812 | =begin original |
896 | 813 | |
897 | 814 | Note that if there were a C<continue> block on the above code, it would |
898 | 815 | get executed only on lines discarded by the regex (since redo skips the |
899 | 816 | continue block). A continue block is often used to reset line counters |
900 | 817 | or C<m?pat?> one-time matches: |
901 | 818 | |
902 | 819 | =end original |
903 | 820 | |
904 | 821 | 上記の例で C<continue> ブロックがあったとしたら、それは |
905 | 822 | (redo は continue ブロックをスキップするので) 正規表現によって |
906 | 823 | 捨てられた行だけが実行されるということに注意してください。 |
907 | 824 | continue ブロックは行カウンターをリセットするとか、 |
908 | 825 | 一度だけマッチする C<m?pat?> をリセットするのに使われます。 |
909 | 826 | |
910 | 827 | # inspired by :1,$g/fred/s//WILMA/ |
911 | 828 | while (<>) { |
912 | ||
829 | m?(fred)? && s//WILMA $1 WILMA/; | |
913 | ||
830 | m?(barney)? && s//BETTY $1 BETTY/; | |
914 | ||
831 | m?(homer)? && s//MARGE $1 MARGE/; | |
915 | 832 | } continue { |
916 | ||
833 | print "$ARGV $.: $_"; | |
917 | ||
834 | close ARGV if eof; # reset $. | |
918 | ||
835 | reset if eof; # reset ?pat? | |
919 | 836 | } |
920 | 837 | |
921 | 838 | =begin original |
922 | 839 | |
923 | 840 | If the word C<while> is replaced by the word C<until>, the sense of the |
924 | 841 | test is reversed, but the conditional is still tested before the first |
925 | 842 | iteration. |
926 | 843 | |
927 | 844 | =end original |
928 | 845 | |
929 | 846 | C<while> を C<until> で置き換えた場合検査の意味は逆転しますが、 |
930 | 847 | 繰り返しが実行されるより前に条件が検査されることは変わりありません。 |
931 | 848 | |
932 | 849 | =begin original |
933 | 850 | |
934 | 851 | Loop control statements don't work in an C<if> or C<unless>, since |
935 | 852 | they aren't loops. You can double the braces to make them such, though. |
936 | 853 | |
937 | 854 | =end original |
938 | 855 | |
939 | 856 | ループ制御文は C<if> や C<unless> 中では動作しません; |
940 | 857 | なぜならそこはループではないからです。 |
941 | 858 | しかし中かっこを二重にしてこれに対処することはできます。 |
942 | 859 | |
943 | 860 | if (/pattern/) {{ |
944 | ||
861 | last if /fred/; | |
945 | ||
862 | next if /barney/; # same effect as "last", | |
946 | ||
863 | # but doesn't document as well | |
947 | ||
864 | # do something here | |
948 | 865 | }} |
949 | 866 | |
950 | 867 | =begin original |
951 | 868 | |
952 | 869 | This is caused by the fact that a block by itself acts as a loop that |
953 | executes once, see L< | |
870 | executes once, see L<"Basic BLOCKs">. | |
954 | 871 | |
955 | 872 | =end original |
956 | 873 | |
957 | 874 | これは、ブロック自身は一度だけ実行されるループとして動作するからです; |
958 | L< | |
875 | L<"Basic BLOCKs"> を参照してください。 | |
959 | 876 | |
960 | 877 | =begin original |
961 | 878 | |
962 | 879 | The form C<while/if BLOCK BLOCK>, available in Perl 4, is no longer |
963 | available. Replace any occurrence of C<if BLOCK> by C<if (do BLOCK)>. | |
880 | available. Replace any occurrence of C<if BLOCK> by C<if (do BLOCK)>. | |
964 | 881 | |
965 | 882 | =end original |
966 | 883 | |
967 | 884 | Perl 4 では使うことのできた C<while/if BLOCK BLOCK> という形式は、 |
968 | 885 | もはや使うことができません。 |
969 | 886 | C<if BLOCK> の部分を C<if (do BLOCK)> で置き換えてください。 |
970 | 887 | |
971 | 888 | =head2 For Loops |
972 | 889 | X<for> X<foreach> |
973 | 890 | |
974 | 891 | (for ループ) |
975 | 892 | |
976 | 893 | =begin original |
977 | 894 | |
978 | 895 | Perl's C-style C<for> loop works like the corresponding C<while> loop; |
979 | 896 | that means that this: |
980 | 897 | |
981 | 898 | =end original |
982 | 899 | |
983 | 900 | Perl の C 形式の C<for> ループは、対応する C<while> ループと同様に |
984 | 901 | 動作します; つまり、以下のものは: |
985 | 902 | |
986 | 903 | for ($i = 1; $i < 10; $i++) { |
987 | ||
904 | ... | |
988 | 905 | } |
989 | 906 | |
990 | 907 | =begin original |
991 | 908 | |
992 | 909 | is the same as this: |
993 | 910 | |
994 | 911 | =end original |
995 | 912 | |
996 | 913 | 以下のものと同じです: |
997 | 914 | |
998 | 915 | $i = 1; |
999 | 916 | while ($i < 10) { |
1000 | ||
917 | ... | |
1001 | 918 | } continue { |
1002 | ||
919 | $i++; | |
1003 | 920 | } |
1004 | 921 | |
1005 | 922 | =begin original |
1006 | 923 | |
1007 | 924 | There is one minor difference: if variables are declared with C<my> |
1008 | 925 | in the initialization section of the C<for>, the lexical scope of |
1009 | 926 | those variables is exactly the C<for> loop (the body of the loop |
1010 | and the control sections). | |
927 | and the control sections). | |
1011 | 928 | X<my> |
1012 | 929 | |
1013 | 930 | =end original |
1014 | 931 | |
1015 | 932 | 小さな違いが一つあります: C<for> の初期化部で C<my> を使って変数が |
1016 | 933 | 宣言された場合、この変数のレキシカルスコープは C<for> ループ |
1017 | 934 | (ループ本体と制御部) と完全に同じです。 |
1018 | 図示すると: | |
1019 | 935 | X<my> |
1020 | 936 | |
1021 | my $i = 'samba'; | |
1022 | for (my $i = 1; $i <= 4; $i++) { | |
1023 | print "$i\n"; | |
1024 | } | |
1025 | print "$i\n"; | |
1026 | ||
1027 | 937 | =begin original |
1028 | 938 | |
1029 | when executed, gives: | |
1030 | ||
1031 | =end original | |
1032 | ||
1033 | 実行すると、次のものになります: | |
1034 | ||
1035 | 1 | |
1036 | 2 | |
1037 | 3 | |
1038 | 4 | |
1039 | samba | |
1040 | ||
1041 | =begin original | |
1042 | ||
1043 | 939 | As a special case, if the test in the C<for> loop (or the corresponding |
1044 | 940 | C<while> loop) is empty, it is treated as true. That is, both |
1045 | 941 | |
1046 | 942 | =end original |
1047 | 943 | |
1048 | 944 | 特別な場合として、もし C<for> ループ (または対応する C<while> ループ) の |
1049 | 945 | テストが空なら、真として扱われます。 |
1050 | 946 | つまり、次のもの |
1051 | 947 | |
1052 | 948 | for (;;) { |
1053 | ||
949 | ... | |
1054 | 950 | } |
1055 | 951 | |
1056 | 952 | =begin original |
1057 | 953 | |
1058 | 954 | and |
1059 | 955 | |
1060 | 956 | =end original |
1061 | 957 | |
1062 | 958 | および |
1063 | 959 | |
1064 | 960 | while () { |
1065 | ||
961 | ... | |
1066 | 962 | } |
1067 | 963 | |
1068 | 964 | =begin original |
1069 | 965 | |
1070 | 966 | are treated as infinite loops. |
1071 | 967 | |
1072 | 968 | =end original |
1073 | 969 | |
1074 | 970 | は無限ループとして扱われます。 |
1075 | 971 | |
1076 | 972 | =begin original |
1077 | 973 | |
1078 | 974 | Besides the normal array index looping, C<for> can lend itself |
1079 | 975 | to many other interesting applications. Here's one that avoids the |
1080 | 976 | problem you get into if you explicitly test for end-of-file on |
1081 | 977 | an interactive file descriptor causing your program to appear to |
1082 | 978 | hang. |
1083 | 979 | X<eof> X<end-of-file> X<end of file> |
1084 | 980 | |
1085 | 981 | =end original |
1086 | 982 | |
1087 | 983 | 通常の、配列に対する添え字付けのループのほかにも、C<for> は他の |
1088 | 984 | 多くの興味深いアプリケーションのために借用することができます。 |
1089 | 985 | 以下の例は、対話的なファイル記述子の終端を明示的に検査してしまうと |
1090 | 986 | プログラムをハングアップしたように見えてしまう問題を回避するものです。 |
1091 | 987 | X<eof> X<end-of-file> X<end of file> |
1092 | 988 | |
1093 | 989 | $on_a_tty = -t STDIN && -t STDOUT; |
1094 | 990 | sub prompt { print "yes? " if $on_a_tty } |
1095 | 991 | for ( prompt(); <STDIN>; prompt() ) { |
1096 | ||
992 | # do something | |
1097 | 993 | } |
1098 | 994 | |
1099 | 995 | =begin original |
1100 | 996 | |
1101 | ||
997 | Using C<readline> (or the operator form, C<< <EXPR> >>) as the | |
1102 | ||
998 | conditional of a C<for> loop is shorthand for the following. This | |
999 | behaviour is the same as a C<while> loop conditional. | |
1000 | X<readline> X<< <> >> | |
1103 | 1001 | |
1104 | 1002 | =end original |
1105 | 1003 | |
1106 | C<for> ループの条件 | |
1004 | C<for> ループの条件として C<readline> (または演算子形式の C<< <EXPR> >>) を | |
1107 | ||
1005 | 使う場合、以下のように省略形が使えます。 | |
1006 | この振る舞いは C<while> ループ条件と同じです。 | |
1007 | X<readline> X<< <> >> | |
1108 | 1008 | |
1009 | for ( prompt(); defined( $_ = <STDIN> ); prompt() ) { | |
1010 | # do something | |
1011 | } | |
1012 | ||
1109 | 1013 | =head2 Foreach Loops |
1110 | 1014 | X<for> X<foreach> |
1111 | 1015 | |
1112 | 1016 | (foreach ループ) |
1113 | 1017 | |
1114 | 1018 | =begin original |
1115 | 1019 | |
1116 | The C<foreach> loop iterates over a normal list value and sets the | |
1020 | The C<foreach> loop iterates over a normal list value and sets the | |
1117 | 1021 | variable VAR to be each element of the list in turn. If the variable |
1118 | 1022 | is preceded with the keyword C<my>, then it is lexically scoped, and |
1119 | 1023 | is therefore visible only within the loop. Otherwise, the variable is |
1120 | 1024 | implicitly local to the loop and regains its former value upon exiting |
1121 | 1025 | the loop. If the variable was previously declared with C<my>, it uses |
1122 | 1026 | that variable instead of the global one, but it's still localized to |
1123 | the loop. This implicit localization occurs I<only> | |
1027 | the loop. This implicit localization occurs I<only> in a C<foreach> | |
1124 | loop | |
1028 | loop. | |
1125 | 1029 | X<my> X<local> |
1126 | 1030 | |
1127 | 1031 | =end original |
1128 | 1032 | |
1129 | C<foreach> ループは 通常のリスト値に対しての繰り返しを行い、 | |
1033 | C<foreach> ループは 通常のリスト値に対しての繰り返しを行い、変数 VAR に | |
1130 | 1034 | リストの要素を繰り返し一回に一つずつセットします。 |
1131 | 1035 | 変数の前に C<my> というキーワードが置かれていた場合、その変数は |
1132 | 1036 | レキシカルスコープを持ち、したがってそれはループの中でのみ可視となります。 |
1133 | 1037 | このキーワードがなければ、変数はループに対してローカルとなり、ループを |
1134 | 1038 | 抜けた後で以前の値が再度取得されます。 |
1135 | 1039 | 変数が事前に C<my> を使って宣言されていたならば、グローバルなものの |
1136 | 1040 | 代わりにその変数を使いますが、それもループにローカルなものとなります。 |
1137 | この暗黙のローカル化は | |
1041 | この暗黙のローカル化は C<foreach> の中で I<のみ> 起きます。 | |
1138 | 1042 | X<my> X<local> |
1139 | 1043 | |
1140 | 1044 | =begin original |
1141 | 1045 | |
1142 | 1046 | The C<foreach> keyword is actually a synonym for the C<for> keyword, so |
1143 | 1047 | you can use either. If VAR is omitted, C<$_> is set to each value. |
1144 | 1048 | X<$_> |
1145 | 1049 | |
1146 | 1050 | =end original |
1147 | 1051 | |
1148 | 1052 | |
1149 | 1053 | C<foreach> は実際には C<for> の同義語なので、どちらでも使えます。 |
1150 | 1054 | VAR が省略された場合には、C<$_> に値が設定されます。 |
1151 | 1055 | X<$_> |
1152 | 1056 | |
1153 | 1057 | =begin original |
1154 | 1058 | |
1155 | 1059 | If any element of LIST is an lvalue, you can modify it by modifying |
1156 | 1060 | VAR inside the loop. Conversely, if any element of LIST is NOT an |
1157 | 1061 | lvalue, any attempt to modify that element will fail. In other words, |
1158 | 1062 | the C<foreach> loop index variable is an implicit alias for each item |
1159 | 1063 | in the list that you're looping over. |
1160 | 1064 | X<alias> |
1161 | 1065 | |
1162 | 1066 | =end original |
1163 | 1067 | |
1164 | 1068 | LIST の要素が左辺値であった場合、ループの中で VAR を変更することにより、 |
1165 | 1069 | 対応する値を変更することができます。 |
1166 | 1070 | 逆に、LIST の要素が左辺値でない場合は、この要素を修正しようとしても |
1167 | 1071 | 失敗します。 |
1168 | 1072 | 言い換えると、C<foreach> ループの帰納変数がループの対象となっている |
1169 | 1073 | リスト中の個々のアイテムに対するエイリアスになっているからです。 |
1170 | 1074 | X<alias> |
1171 | 1075 | |
1172 | 1076 | =begin original |
1173 | 1077 | |
1174 | 1078 | If any part of LIST is an array, C<foreach> will get very confused if |
1175 | 1079 | you add or remove elements within the loop body, for example with |
1176 | C<splice>. So don't do that. | |
1080 | C<splice>. So don't do that. | |
1177 | 1081 | X<splice> |
1178 | 1082 | |
1179 | 1083 | =end original |
1180 | 1084 | |
1181 | 1085 | LIST のいずれかの部分が配列であった場合に、たとえば C<splice> を使って |
1182 | 1086 | ループの本体でその要素を削除したりあるいは追加したりすると |
1183 | 1087 | C<foreach> は非常に混乱してしまいます。 |
1184 | 1088 | ですからそういうことをしてはいけません。 |
1185 | 1089 | X<splice> |
1186 | 1090 | |
1187 | 1091 | =begin original |
1188 | 1092 | |
1189 | 1093 | C<foreach> probably won't do what you expect if VAR is a tied or other |
1190 | special variable. Don't do that either. | |
1094 | special variable. Don't do that either. | |
1191 | 1095 | |
1192 | 1096 | =end original |
1193 | 1097 | |
1194 | 1098 | VAR が tie されていたりあるいは他の特殊変数であった場合には |
1195 | 1099 | C<foreach> はあなたのもくろみどおりには動かないでしょう。 |
1196 | 1100 | こういうこともしてはいけません。 |
1197 | 1101 | |
1198 | 1102 | =begin original |
1199 | 1103 | |
1200 | As of Perl 5.22, there is an experimental variant of this loop that accepts | |
1201 | a variable preceded by a backslash for VAR, in which case the items in the | |
1202 | LIST must be references. The backslashed variable will become an alias | |
1203 | to each referenced item in the LIST, which must be of the correct type. | |
1204 | The variable needn't be a scalar in this case, and the backslash may be | |
1205 | followed by C<my>. To use this form, you must enable the C<refaliasing> | |
1206 | feature via C<use feature>. (See L<feature>. See also L<perlref/Assigning | |
1207 | to References>.) | |
1208 | ||
1209 | =end original | |
1210 | ||
1211 | Perl 5.22 から、VAR に逆スラッシュを前置する変数を受け付けるという | |
1212 | このループの実験的なバリエーションがあります; この場合 LIST のアイテムは | |
1213 | リファレンスでなければなりません。 | |
1214 | 逆スラッシュつき変数は LIST の中のアイテムへのリファレンスとなるエイリアスに | |
1215 | なり; 正しい方でなければなりません。 | |
1216 | この場合変数はスカラである必要はなく、逆スラッシュには C<my> が | |
1217 | 引き続くことができます。 | |
1218 | この形式を使うには、C<use feature> で C<refaliasing> 機能を | |
1219 | 有効にしなければなりません。 | |
1220 | (L<feature> を参照してください。 | |
1221 | L<perlref/Assigning to References> も参照してください。) | |
1222 | ||
1223 | =begin original | |
1224 | ||
1225 | As of Perl 5.36, you can iterate over multiple values at a time. | |
1226 | You can only iterate with lexical scalars as the iterator variables - unlike | |
1227 | list assignment, it's not possible to use C<undef> to signify a value that | |
1228 | isn't wanted. This is a limitation of the current implementation, and might | |
1229 | be changed in the future. | |
1230 | ||
1231 | =end original | |
1232 | ||
1233 | Perl 5.36 から、一度に複数の値を反復できます。 | |
1234 | 反復変数としてレキシカルスカラのみ反復できます - リスト代入と異なり、 | |
1235 | 不要な値を示すために C<undef> を使うことは出来ません。 | |
1236 | これは現在の実装の制限で、将来変更されるかもしれません。 | |
1237 | ||
1238 | =begin original | |
1239 | ||
1240 | If the size of the LIST is not an exact multiple of the number of iterator | |
1241 | variables, then on the last iteration the "excess" iterator variables are | |
1242 | aliases to C<undef>, as if the LIST had C<, undef> appended as many times as | |
1243 | needed for its length to become an exact multiple. This happens whether | |
1244 | LIST is a literal LIST or an array - ie arrays are not extended if their | |
1245 | size is not a multiple of the iteration size, consistent with iterating an | |
1246 | array one-at-a-time. As these padding elements are not lvalues, attempting | |
1247 | to modify them will fail, consistent with the behaviour when iterating a | |
1248 | list with literal C<undef>s. If this is not the behaviour you desire, then | |
1249 | before the loop starts either explicitly extend your array to be an exact | |
1250 | multiple, or explicitly throw an exception. | |
1251 | ||
1252 | =end original | |
1253 | ||
1254 | LIST の大きさが反復変数の数の整数倍でない場合、 | |
1255 | 最後の反復では「超過した」反復変数は C<undef> への別名になります; | |
1256 | LIST の長さが整数倍になるのに必要なだけ C<, undef> が | |
1257 | 追加されているかのようになります。 | |
1258 | これは LIST がリテラルな LIST でも配列でも起こります - つまり、配列が | |
1259 | 反復の長さの整数倍でない場合も拡張されません; 一度に一つだけ | |
1260 | 配列を反復する場合と一貫しています。 | |
1261 | これらの追加された要素は左辺値ではないので、これを変更しようとすると | |
1262 | 失敗します; リテラルな C<undef> のリストを反復したときの振る舞いと | |
1263 | 一貫しています。 | |
1264 | これが望んでいる振る舞いでないなら、ループを開始する前に整数倍になるように | |
1265 | 明示的に拡張するか、明示的に例外を投げてください。 | |
1266 | ||
1267 | =begin original | |
1268 | ||
1269 | 1104 | Examples: |
1270 | 1105 | |
1271 | 1106 | =end original |
1272 | 1107 | |
1273 | 1108 | 例: |
1274 | 1109 | |
1275 | 1110 | for (@ary) { s/foo/bar/ } |
1276 | 1111 | |
1277 | 1112 | for my $elem (@elements) { |
1278 | ||
1113 | $elem *= 2; | |
1279 | 1114 | } |
1280 | 1115 | |
1281 | 1116 | for $count (reverse(1..10), "BOOM") { |
1282 | ||
1117 | print $count, "\n"; | |
1283 | ||
1118 | sleep(1); | |
1284 | 1119 | } |
1285 | 1120 | |
1286 | 1121 | for (1..15) { print "Merry Christmas\n"; } |
1287 | 1122 | |
1288 | 1123 | foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) { |
1289 | ||
1124 | print "Item: $item\n"; | |
1290 | 1125 | } |
1291 | 1126 | |
1292 | use feature "refaliasing"; | |
1293 | no warnings "experimental::refaliasing"; | |
1294 | foreach \my %hash (@array_of_hash_references) { | |
1295 | # do something with each %hash | |
1296 | } | |
1297 | ||
1298 | foreach my ($foo, $bar, $baz) (@list) { | |
1299 | # do something three-at-a-time | |
1300 | } | |
1301 | ||
1302 | foreach my ($key, $value) (%hash) { | |
1303 | # iterate over the hash | |
1304 | # The hash is immediately copied to a flat list before the loop | |
1305 | # starts. The list contains copies of keys but aliases of values. | |
1306 | # This is the same behaviour as for $var (%hash) {...} | |
1307 | } | |
1308 | ||
1309 | 1127 | =begin original |
1310 | 1128 | |
1311 | 1129 | Here's how a C programmer might code up a particular algorithm in Perl: |
1312 | 1130 | |
1313 | 1131 | =end original |
1314 | 1132 | |
1315 | 1133 | 以下の例は、C プログラマーが Perl でとあるアルゴリズムを記述するときに |
1316 | 1134 | 使うであろうやり方です: |
1317 | 1135 | |
1318 | 1136 | for (my $i = 0; $i < @ary1; $i++) { |
1319 | ||
1137 | for (my $j = 0; $j < @ary2; $j++) { | |
1320 | ||
1138 | if ($ary1[$i] > $ary2[$j]) { | |
1321 | ||
1139 | last; # can't go to outer :-( | |
1322 | ||
1140 | } | |
1323 | ||
1141 | $ary1[$i] += $ary2[$j]; | |
1324 | ||
1142 | } | |
1325 | ||
1143 | # this is where that last takes me | |
1326 | 1144 | } |
1327 | 1145 | |
1328 | 1146 | =begin original |
1329 | 1147 | |
1330 | 1148 | Whereas here's how a Perl programmer more comfortable with the idiom might |
1331 | 1149 | do it: |
1332 | 1150 | |
1333 | 1151 | =end original |
1334 | 1152 | |
1335 | 1153 | それに対して、次の例は Perl プログラマーが同じことをよりゆったりとして |
1336 | 1154 | 行うやり方です: |
1337 | 1155 | |
1338 | 1156 | OUTER: for my $wid (@ary1) { |
1339 | 1157 | INNER: for my $jet (@ary2) { |
1340 | ||
1158 | next OUTER if $wid > $jet; | |
1341 | ||
1159 | $wid += $jet; | |
1342 | ||
1160 | } | |
1343 | ||
1161 | } | |
1344 | 1162 | |
1345 | 1163 | =begin original |
1346 | 1164 | |
1347 | 1165 | See how much easier this is? It's cleaner, safer, and faster. It's |
1348 | 1166 | cleaner because it's less noisy. It's safer because if code gets added |
1349 | 1167 | between the inner and outer loops later on, the new code won't be |
1350 | 1168 | accidentally executed. The C<next> explicitly iterates the other loop |
1351 | 1169 | rather than merely terminating the inner one. And it's faster because |
1352 | 1170 | Perl executes a C<foreach> statement more rapidly than it would the |
1353 | equivalent C | |
1171 | equivalent C<for> loop. | |
1354 | 1172 | |
1355 | 1173 | =end original |
1356 | 1174 | |
1357 | どのくらいこれが簡単になったように見えますか? | |
1175 | どのくらいこれが簡単になったように見えますか? これは明確で、安全で、 | |
1358 | ||
1176 | 高速です。 | |
1359 | 1177 | これは余計なものが少ないので明確なのです。 |
1360 | 1178 | これは後で内側のループと外側のループとの間にコードを付加えた場合でも、 |
1361 | 1179 | それを間違って実行することがないので安全なのです。 |
1362 | 1180 | C<next> は内側のループを終了するのではなく、外側のループの繰り返しを |
1363 | 1181 | 行います。 |
1364 | そしてこれは、Perl は C<foreach> 文をそれと等価な C | |
1182 | そしてこれは、Perl は C<foreach> 文をそれと等価な C<for> ループよりも | |
1365 | 1183 | すばやく実行するので高速なのです。 |
1366 | 1184 | |
1367 | 1185 | =begin original |
1368 | 1186 | |
1369 | 1187 | Perceptive Perl hackers may have noticed that a C<for> loop has a return |
1370 | 1188 | value, and that this value can be captured by wrapping the loop in a C<do> |
1371 | 1189 | block. The reward for this discovery is this cautionary advice: The |
1372 | 1190 | return value of a C<for> loop is unspecified and may change without notice. |
1373 | 1191 | Do not rely on it. |
1374 | 1192 | |
1375 | 1193 | =end original |
1376 | 1194 | |
1377 | 1195 | 洞察力のある Perl ハッカーは、C<for> ループに返り値があり、この値は |
1378 | 1196 | ループを C<do> ブロックで包むことによって捕捉できることに |
1379 | 1197 | 気付くかもしれません。 |
1380 | 1198 | この発見に対する報奨は、この警告的なアドバイスです: C<for> ループの返り値は |
1381 | 1199 | 未定義で、予告なしに変更されることがあります。 |
1382 | 1200 | これに依存してはいけません。 |
1383 | 1201 | |
1384 | =head2 Try Catch Exception Handling | |
1385 | X<try> X<catch> X<finally> | |
1386 | ||
1387 | =begin original | |
1388 | ||
1389 | The C<try>/C<catch> syntax provides control flow relating to exception | |
1390 | handling. The C<try> keyword introduces a block which will be executed when it | |
1391 | is encountered, and the C<catch> block provides code to handle any exception | |
1392 | that may be thrown by the first. | |
1393 | ||
1394 | =end original | |
1395 | ||
1396 | C<try>/C<catch> 構文は、例外処理に関連する制御フローを提供します。 | |
1397 | C<try> キーワードは、それが検出されたときに実行されるブロックを導入し、 | |
1398 | C<catch> ブロックは、最初のものによって投げられる可能性のある例外を | |
1399 | 処理するコードを提供します。 | |
1400 | ||
1401 | =begin original | |
1402 | ||
1403 | This syntax must first be enabled with C<use feature 'try'>. | |
1404 | ||
1405 | =end original | |
1406 | ||
1407 | この文法は、まず C<use feature 'try'> で有効にしなければなりません。 | |
1408 | ||
1409 | use feature 'try'; | |
1410 | ||
1411 | try { | |
1412 | my $x = call_a_function(); | |
1413 | $x < 100 or die "Too big"; | |
1414 | send_output($x); | |
1415 | } | |
1416 | catch ($e) { | |
1417 | warn "Unable to output a value; $e"; | |
1418 | } | |
1419 | print "Finished\n"; | |
1420 | ||
1421 | =begin original | |
1422 | ||
1423 | Here, the body of the C<catch> block (i.e. the C<warn> statement) will be | |
1424 | executed if the initial block invokes the conditional C<die>, or if either of | |
1425 | the functions it invokes throws an uncaught exception. The C<catch> block can | |
1426 | inspect the C<$e> lexical variable in this case to see what the exception was. | |
1427 | If no exception was thrown then the C<catch> block does not happen. In either | |
1428 | case, execution will then continue from the following statement - in this | |
1429 | example the C<print>. | |
1430 | ||
1431 | =end original | |
1432 | ||
1433 | ここで、C<catch> ブロックの本体 (つまり、C<warn> 文) は、最初の | |
1434 | ブロックが条件付き C<die> を呼び出した場合、または呼び出した関数のいずれかが | |
1435 | 捕捉されない例外を投げた場合に実行されます。 | |
1436 | C<catch> ブロックは、この場合の C<$e> レキシカル変数を検査して、例外が何かを | |
1437 | 調べることができます。 | |
1438 | 例外が投げられなかった場合、C<catch> ブロックは発生しません。 | |
1439 | いずれの場合も、次の文 (この例では C<print>) から実行が続行されます。 | |
1440 | ||
1441 | =begin original | |
1442 | ||
1443 | The C<catch> keyword must be immediately followed by a variable declaration in | |
1444 | parentheses, which introduces a new variable visible to the body of the | |
1445 | subsequent block. Inside the block this variable will contain the exception | |
1446 | value that was thrown by the code in the C<try> block. It is not necessary | |
1447 | to use the C<my> keyword to declare this variable; this is implied (similar | |
1448 | as it is for subroutine signatures). | |
1449 | ||
1450 | =end original | |
1451 | ||
1452 | C<catch> キーワードの直後には、かっこで囲まれた変数宣言が | |
1453 | 続かなければなりません; | |
1454 | これにより、後続のブロックの本文から見える新しい変数が導入されます。 | |
1455 | ブロック内では、この変数には、C<try> ブロック内のコードによって投げられた | |
1456 | 例外値が含まれます。 | |
1457 | この変数を宣言するために C<my> キーワードを使用する必要はありません; | |
1458 | これは(サブルーチンシグネチャの場合と同様)暗黙的です。 | |
1459 | ||
1460 | =begin original | |
1461 | ||
1462 | Both the C<try> and the C<catch> blocks are permitted to contain control-flow | |
1463 | expressions, such as C<return>, C<goto>, or C<next>/C<last>/C<redo>. In all | |
1464 | cases they behave as expected without warnings. In particular, a C<return> | |
1465 | expression inside the C<try> block will make its entire containing function | |
1466 | return - this is in contrast to its behaviour inside an C<eval> block, where | |
1467 | it would only make that block return. | |
1468 | ||
1469 | =end original | |
1470 | ||
1471 | C<try> ブロックと C<catch> ブロックの両方に、C<return>, C<goto>, | |
1472 | C<next>/C<last>/C<redo> などの制御フロー式を含めることができます。 | |
1473 | いずれの場合も、警告なしに想定通りに動作します。 | |
1474 | 特に、C<try> ブロック内の C<return> 式は、それを含む関数全体から返ります - | |
1475 | これは、そのブロックからのみ返る C<eval> ブロック内での動作とは対照的です。 | |
1476 | ||
1477 | =begin original | |
1478 | ||
1479 | Like other control-flow syntax, C<try> and C<catch> will yield the last | |
1480 | evaluated value when placed as the final statement in a function or a C<do> | |
1481 | block. This permits the syntax to be used to create a value. In this case | |
1482 | remember not to use the C<return> expression, or that will cause the | |
1483 | containing function to return. | |
1484 | ||
1485 | =end original | |
1486 | ||
1487 | 他の制御フロー構文と同様に、C<try> と C<catch> は、関数または C<do> | |
1488 | ブロックの最後のステートメントとして配置された場合に、最後に評価された値を | |
1489 | 生成します。 | |
1490 | これにより、この構文を使用して値を作成できます。 | |
1491 | この場合、C<return> 式を使用しないでください; | |
1492 | 使用すると、含まれる関数から戻ることになります。 | |
1493 | ||
1494 | my $value = do { | |
1495 | try { | |
1496 | get_thing(@args); | |
1497 | } | |
1498 | catch ($e) { | |
1499 | warn "Unable to get thing - $e"; | |
1500 | $DEFAULT_THING; | |
1501 | } | |
1502 | }; | |
1503 | ||
1504 | =begin original | |
1505 | ||
1506 | As with other control-flow syntax, C<try> blocks are not visible to | |
1507 | C<caller()> (just as for example, C<while> or C<foreach> loops are not). | |
1508 | Successive levels of the C<caller> result can see subroutine calls and | |
1509 | C<eval> blocks, because those affect the way that C<return> would work. Since | |
1510 | C<try> blocks do not intercept C<return>, they are not of interest to | |
1511 | C<caller>. | |
1512 | ||
1513 | =end original | |
1514 | ||
1515 | 他の制御フロー構文と同様に、C<try> ブロックは C<caller()> には見えません | |
1516 | (例えば、C<while> または C<foreach> ループが見えないのと同様です)。 | |
1517 | C<caller> の結果の連続するレベルは、サブルーチン呼び出しと C<eval> ブロックを | |
1518 | 見ることができます; なぜなら、それらは C<return> の動作方法に | |
1519 | 影響するからです。 | |
1520 | C<try> ブロックは C<return> に割り込まないので、それらは | |
1521 | C<caller> には関係ありません。 | |
1522 | ||
1523 | =begin original | |
1524 | ||
1525 | The C<try> and C<catch> blocks may optionally be followed by a third block | |
1526 | introduced by the C<finally> keyword. This third block is executed after the | |
1527 | rest of the construct has finished. | |
1528 | ||
1529 | =end original | |
1530 | ||
1531 | C<try> と C<catch> のブロックは、オプションとして、C<finally> キーワードで | |
1532 | 導入される第 3 のブロックを引き続かせることができます。 | |
1533 | この第 3 のブロックは構文の残りの部分が完了した後に実行されます。 | |
1534 | ||
1535 | try { | |
1536 | call_a_function(); | |
1537 | } | |
1538 | catch ($e) { | |
1539 | warn "Unable to call; $e"; | |
1540 | } | |
1541 | finally { | |
1542 | print "Finished\n"; | |
1543 | } | |
1544 | ||
1545 | =begin original | |
1546 | ||
1547 | The C<finally> block is equivalent to using a C<defer> block and will be | |
1548 | invoked in the same situations; whether the C<try> block completes | |
1549 | successfully, throws an exception, or transfers control elsewhere by using | |
1550 | C<return>, a loop control, or C<goto>. | |
1551 | ||
1552 | =end original | |
1553 | ||
1554 | C<finally> ブロックは C<defer> ブロックを使うのと等価で、 | |
1555 | 同じ状況で起動されます; C<try> ブロックが正常に完了するか、 | |
1556 | 例外が投げられるか、C<return>、ループ制御、C<goto> を使うことによって | |
1557 | 別の場所に制御が移るかしたときです。 | |
1558 | ||
1559 | =begin original | |
1560 | ||
1561 | Unlike the C<try> and C<catch> blocks, a C<finally> block is not permitted to | |
1562 | C<return>, C<goto> or use any loop controls. The final expression value is | |
1563 | ignored, and does not affect the return value of the containing function even | |
1564 | if it is placed last in the function. | |
1565 | ||
1566 | =end original | |
1567 | ||
1568 | C<try> と C<catch> ブロックとは異なり、C<finally> ブロックは | |
1569 | C<return>, C<goto> あるいはループ制御は許されません。 | |
1570 | 最後の式の値は無視され、たとえこれが関数の最後に置かれていても、 | |
1571 | これを含んでいる関数の返り値には影響を与えません。 | |
1572 | ||
1573 | =begin original | |
1574 | ||
1575 | Use of this C<finally> block syntax is currently experimental and will emit a | |
1576 | warning in the C<experimental::try> category. | |
1577 | ||
1578 | =end original | |
1579 | ||
1580 | この C<finally> ブロック構文の使用は現在実験的であり、 | |
1581 | C<experimental::try> カテゴリの警告を出力します。 | |
1582 | ||
1583 | 1202 | =head2 Basic BLOCKs |
1584 | 1203 | X<block> |
1585 | 1204 | |
1586 | (基本ブロック | |
1205 | (基本ブロック) | |
1587 | 1206 | |
1588 | 1207 | =begin original |
1589 | 1208 | |
1590 | 1209 | A BLOCK by itself (labeled or not) is semantically equivalent to a |
1591 | 1210 | loop that executes once. Thus you can use any of the loop control |
1592 | 1211 | statements in it to leave or restart the block. (Note that this is |
1593 | 1212 | I<NOT> true in C<eval{}>, C<sub{}>, or contrary to popular belief |
1594 | 1213 | C<do{}> blocks, which do I<NOT> count as loops.) The C<continue> |
1595 | 1214 | block is optional. |
1596 | 1215 | |
1597 | 1216 | =end original |
1598 | 1217 | |
1599 | 1218 | ブロックそれ自身は(ラベルが付いていようがついてなかろうが)一度だけ |
1600 | 1219 | 実行されるループと、文法的には等価なものです。 |
1601 | 1220 | このため、ブロックから脱出するためやブロックの再スタートのために |
1602 | 1221 | 任意のループ制御文を使うことができます。 |
1603 | 1222 | (これは C<eval{}>、C<sub{}>、 |
1604 | 1223 | さらに一般的な認識とは異なり I<ループではない> C<do{}> ブロックに対しては |
1605 | 1224 | I<真ではない> ということに注意してください。) |
1606 | 1225 | C<continue> ブロックは省略することができます。 |
1607 | 1226 | |
1608 | 1227 | =begin original |
1609 | 1228 | |
1610 | 1229 | The BLOCK construct can be used to emulate case structures. |
1611 | 1230 | |
1612 | 1231 | =end original |
1613 | 1232 | |
1614 | 1233 | BLOCK 構造は case 構造を行うのにも使えます。 |
1615 | 1234 | |
1616 | 1235 | SWITCH: { |
1617 | ||
1236 | if (/^abc/) { $abc = 1; last SWITCH; } | |
1618 | ||
1237 | if (/^def/) { $def = 1; last SWITCH; } | |
1619 | ||
1238 | if (/^xyz/) { $xyz = 1; last SWITCH; } | |
1620 | ||
1239 | $nothing = 1; | |
1621 | 1240 | } |
1622 | 1241 | |
1623 | 1242 | =begin original |
1624 | 1243 | |
1625 | 1244 | You'll also find that C<foreach> loop used to create a topicalizer |
1626 | 1245 | and a switch: |
1627 | 1246 | |
1628 | 1247 | =end original |
1629 | 1248 | |
1630 | 1249 | 主題化器とスイッチを作るために使われた C<foreach> ループも見るかもしれません: |
1631 | 1250 | |
1632 | 1251 | SWITCH: |
1633 | 1252 | for ($var) { |
1634 | ||
1253 | if (/^abc/) { $abc = 1; last SWITCH; } | |
1635 | ||
1254 | if (/^def/) { $def = 1; last SWITCH; } | |
1636 | ||
1255 | if (/^xyz/) { $xyz = 1; last SWITCH; } | |
1637 | ||
1256 | $nothing = 1; | |
1638 | 1257 | } |
1639 | 1258 | |
1640 | 1259 | =begin original |
1641 | 1260 | |
1642 | 1261 | Such constructs are quite frequently used, both because older versions of |
1643 | 1262 | Perl had no official C<switch> statement, and also because the new version |
1644 | 1263 | described immediately below remains experimental and can sometimes be confusing. |
1645 | 1264 | |
1646 | 1265 | =end original |
1647 | 1266 | |
1648 | 1267 | 古いバージョンの Perl には公式の C<switch> 文がなく、直後に記述する |
1649 | 1268 | 新しいバージョンはまだ実験的で時々混乱させることがあるので、 |
1650 | 1269 | このような構文はとてもよく使われています。 |
1651 | 1270 | |
1652 | =head2 defer blocks | |
1653 | X<defer> | |
1654 | ||
1655 | (defer ブロック) | |
1656 | ||
1657 | =begin original | |
1658 | ||
1659 | A block prefixed by the C<defer> modifier provides a section of code which | |
1660 | runs at a later time during scope exit. | |
1661 | ||
1662 | =end original | |
1663 | ||
1664 | C<defer> 修飾子を前置したブロックは、後でスコープが終了している間に | |
1665 | 実行されるコードを提供します。 | |
1666 | ||
1667 | =begin original | |
1668 | ||
1669 | A C<defer> block can appear at any point where a regular block or other | |
1670 | statement is permitted. If the flow of execution reaches this statement, the | |
1671 | body of the block is stored for later, but not invoked immediately. When the | |
1672 | flow of control leaves the containing block for any reason, this stored block | |
1673 | is executed on the way past. It provides a means of deferring execution until | |
1674 | a later time. This acts similarly to syntax provided by some other languages, | |
1675 | often using keywords named C<try / finally>. | |
1676 | ||
1677 | =end original | |
1678 | ||
1679 | C<defer> ブロックは、通常のブロックは他の文が許される場所なら | |
1680 | どこにでも置くことができます。 | |
1681 | 実行フローがその文に届くと、ブロックの本体は後のために保管されますが、 | |
1682 | 直ちに起動はしません。 | |
1683 | フロー制御がなんらかの理由で含まれているブロックから離れたとき、 | |
1684 | この保管されていたブロックが通過する途中で実行されます。 | |
1685 | これは、後に実行を遅延させる意味を提供します。 | |
1686 | これは他の言語で、しばしば C<try / finally> という名前のキーワードを | |
1687 | 使って提供されている文法と似た動作をします。 | |
1688 | ||
1689 | =begin original | |
1690 | ||
1691 | This syntax is available since Perl 5.36 if enabled by the C<defer> named feature, | |
1692 | and is currently experimental. If experimental warnings are enabled it will emit a | |
1693 | warning when used. | |
1694 | ||
1695 | =end original | |
1696 | ||
1697 | この文法は Perl 5.36 から、C<defer> という名前付き機能が有効の場合に | |
1698 | 利用可能で、これは現在の所実験的です。 | |
1699 | 実験的警告が有効の場合、使われたときに警告が発生します。 | |
1700 | ||
1701 | use feature 'defer'; | |
1702 | ||
1703 | { | |
1704 | say "This happens first"; | |
1705 | defer { say "This happens last"; } | |
1706 | ||
1707 | say "And this happens inbetween"; | |
1708 | } | |
1709 | ||
1710 | =begin original | |
1711 | ||
1712 | If multiple C<defer> blocks are contained in a single scope, they are | |
1713 | executed in LIFO order; the last one reached is the first one executed. | |
1714 | ||
1715 | =end original | |
1716 | ||
1717 | 一つのスコープに複数の C<defer> ブロックがある場合、LIFO 順で実行されます; | |
1718 | 最後に現れたものが最初に実行されます。 | |
1719 | ||
1720 | =begin original | |
1721 | ||
1722 | The code stored by the C<defer> block will be invoked when control leaves | |
1723 | its containing block due to regular fallthrough, explicit C<return>, | |
1724 | exceptions thrown by C<die> or propagated by functions called by it, C<goto>, | |
1725 | or any of the loop control statements C<next>, C<last> or C<redo>. | |
1726 | ||
1727 | =end original | |
1728 | ||
1729 | C<defer> ブロックに保管されているコードは、通常の通過、明示的な C<return>、 | |
1730 | C<die> あるいは呼び出した関数から伝搬した例外、 | |
1731 | C<goto>、またはループ制御文 C<next>, C<last>, C<redo> のいずれかによって、 | |
1732 | 制御がブロックから離れたときに実行されます。 | |
1733 | ||
1734 | =begin original | |
1735 | ||
1736 | If the flow of control does not reach the C<defer> statement itself then its | |
1737 | body is not stored for later execution. (This is in direct contrast to the | |
1738 | code provided by an C<END> phaser block, which is always enqueued by the | |
1739 | compiler, regardless of whether execution ever reached the line it was given | |
1740 | on.) | |
1741 | ||
1742 | =end original | |
1743 | ||
1744 | フロー制御が C<defer> 文自体に届かない場合、その本体は後の実行のために | |
1745 | 保管されません。 | |
1746 | (これは、実行がその行に届くかどうかに関わらず常にコンパイラによって | |
1747 | キューに入れられる C<END> フェーズブロックとの直接の違いです。) | |
1748 | ||
1749 | use feature 'defer'; | |
1750 | ||
1751 | { | |
1752 | defer { say "This will run"; } | |
1753 | return; | |
1754 | defer { say "This will not"; } | |
1755 | } | |
1756 | ||
1757 | =begin original | |
1758 | ||
1759 | Exceptions thrown by code inside a C<defer> block will propagate to the | |
1760 | caller in the same way as any other exception thrown by normal code. | |
1761 | ||
1762 | =end original | |
1763 | ||
1764 | C<defer> ブロックの内側で投げられた例外は、通常のコードで | |
1765 | 投げられたその他の例外と同様に、呼び出し元に伝搬します。 | |
1766 | ||
1767 | =begin original | |
1768 | ||
1769 | If the C<defer> block is being executed due to a thrown exception and throws | |
1770 | another one it is not specified what happens, beyond that the caller will | |
1771 | definitely receive an exception. | |
1772 | ||
1773 | =end original | |
1774 | ||
1775 | C<defer> ブロックが例外が投げられたために実行され、 | |
1776 | もう一つの例外が投げられた場合、、呼び出し元が例外を受け取るということ | |
1777 | 以上に何が起こるかは未規定です。 | |
1778 | ||
1779 | =begin original | |
1780 | ||
1781 | Besides throwing an exception, a C<defer> block is not permitted to | |
1782 | otherwise alter the control flow of its surrounding code. In particular, it | |
1783 | may not cause its containing function to C<return>, nor may it C<goto> a | |
1784 | label, or control a containing loop using C<next>, C<last> or C<redo>. These | |
1785 | constructions are however, permitted entirely within the body of the | |
1786 | C<defer>. | |
1787 | ||
1788 | =end original | |
1789 | ||
1790 | 例外を投げること以外、C<defer> ブロックがその周りのコードの制御フローを | |
1791 | 変えることは許されていません。 | |
1792 | 特に、含んでいる関数に C<return> を引き起こしたり、ラベルに C<goto> したり、 | |
1793 | C<next>, C<last>, C<redo> を使って含まれているループを制御してはいけません。 | |
1794 | しかし、これらの構文は、完全に C<defer> の内部だけの場合は許されます。 | |
1795 | ||
1796 | use feature 'defer'; | |
1797 | ||
1798 | { | |
1799 | defer { | |
1800 | foreach ( 1 .. 5 ) { | |
1801 | last if $_ == 3; # this is permitted | |
1802 | } | |
1803 | } | |
1804 | } | |
1805 | ||
1806 | { | |
1807 | foreach ( 6 .. 10 ) { | |
1808 | defer { | |
1809 | last if $_ == 8; # this is not | |
1810 | } | |
1811 | } | |
1812 | } | |
1813 | ||
1814 | 1271 | =head2 Switch Statements |
1815 | 1272 | |
1816 | 1273 | (switch 文) |
1817 | 1274 | |
1818 | 1275 | X<switch> X<case> X<given> X<when> X<default> |
1819 | 1276 | |
1820 | 1277 | =begin original |
1821 | 1278 | |
1822 | 1279 | Starting from Perl 5.10.1 (well, 5.10.0, but it didn't work |
1823 | 1280 | right), you can say |
1824 | 1281 | |
1825 | 1282 | =end original |
1826 | 1283 | |
1827 | 1284 | Perl 5.10.1 から(えっと、5.10.0 からですが、正しく動いていませんでした)、 |
1828 | 1285 | 以下のように書くと: |
1829 | 1286 | |
1830 | 1287 | use feature "switch"; |
1831 | 1288 | |
1832 | 1289 | =begin original |
1833 | 1290 | |
1834 | 1291 | to enable an experimental switch feature. This is loosely based on an |
1835 | old version of a | |
1292 | old version of a Perl 6 proposal, but it no longer resembles the Perl 6 | |
1836 | construct. You also get the switch feature whenever you declare that your | |
1293 | construct. You also get the switch feature whenever you declare that your | |
1837 | code prefers to run under a version of Perl | |
1294 | code prefers to run under a version of Perl that is 5.10 or later. For | |
1838 | 1295 | example: |
1839 | 1296 | |
1840 | 1297 | =end original |
1841 | 1298 | |
1842 | 1299 | 実験的な switch 機能を有効にします。 |
1843 | これはおおまかに | |
1300 | これはおおまかに Perl 6 提案の古い版を基にしていますが、もはや | |
1844 | ||
1301 | Perl 6 の構文と共通点はありません。 | |
1845 | コードが 5.10 | |
1302 | コードが 5.10 以降の Perl バージョンで実行されるように宣言したときもいつでも | |
1846 | ||
1303 | switch 機能を得られます。 | |
1847 | 1304 | 例えば: |
1848 | 1305 | |
1849 | 1306 | use v5.14; |
1850 | 1307 | |
1851 | 1308 | =begin original |
1852 | 1309 | |
1853 | 1310 | Under the "switch" feature, Perl gains the experimental keywords |
1854 | 1311 | C<given>, C<when>, C<default>, C<continue>, and C<break>. |
1855 | 1312 | Starting from Perl 5.16, one can prefix the switch |
1856 | 1313 | keywords with C<CORE::> to access the feature without a C<use feature> |
1857 | 1314 | statement. The keywords C<given> and |
1858 | 1315 | C<when> are analogous to C<switch> and |
1859 | C<case> in other languages | |
1316 | C<case> in other languages, so the code in the previous section could be | |
1860 | ||
1317 | rewritten as | |
1861 | 1318 | |
1862 | 1319 | =end original |
1863 | 1320 | |
1864 | 1321 | "switch" 機能の基では、Perl は実験的なキーワード C<given>, C<when>, |
1865 | 1322 | C<default>, C<continue>, C<break> を得ます。 |
1866 | 1323 | Perl 5.16 から、C<use feature> 文なしで機能にアクセスするために、 |
1867 | 1324 | switch キーワードに C<CORE::> を前置できます。 |
1868 | キーワード C<given> と C<when> は | |
1325 | キーワード C<given> と C<when> は他の言語での C<switch> および C<case> と | |
1869 | -- C<continue> は違いますが -- | |
1870 | 他の言語での C<switch> および C<case> と | |
1871 | 1326 | 同様のものなので、前の節のコードは以下のように書き直せます: |
1872 | 1327 | |
1873 | 1328 | use v5.10.1; |
1874 | 1329 | for ($var) { |
1875 | ||
1330 | when (/^abc/) { $abc = 1 } | |
1876 | ||
1331 | when (/^def/) { $def = 1 } | |
1877 | ||
1332 | when (/^xyz/) { $xyz = 1 } | |
1878 | ||
1333 | default { $nothing = 1 } | |
1879 | 1334 | } |
1880 | 1335 | |
1881 | 1336 | =begin original |
1882 | 1337 | |
1883 | 1338 | The C<foreach> is the non-experimental way to set a topicalizer. |
1884 | 1339 | If you wish to use the highly experimental C<given>, that could be |
1885 | 1340 | written like this: |
1886 | 1341 | |
1887 | 1342 | =end original |
1888 | 1343 | |
1889 | 1344 | C<foreach> は主題化器を設定する実験的でない方法です。 |
1890 | 1345 | とても実験的な C<given> を使いたいなら、以下のように書けます: |
1891 | 1346 | |
1892 | 1347 | use v5.10.1; |
1893 | 1348 | given ($var) { |
1894 | ||
1349 | when (/^abc/) { $abc = 1 } | |
1895 | ||
1350 | when (/^def/) { $def = 1 } | |
1896 | ||
1351 | when (/^xyz/) { $xyz = 1 } | |
1897 | ||
1352 | default { $nothing = 1 } | |
1898 | 1353 | } |
1899 | 1354 | |
1900 | 1355 | =begin original |
1901 | 1356 | |
1902 | 1357 | As of 5.14, that can also be written this way: |
1903 | 1358 | |
1904 | 1359 | =end original |
1905 | 1360 | |
1906 | 1361 | 5.14 現在、これは以下のようにも書けます: |
1907 | 1362 | |
1908 | 1363 | use v5.14; |
1909 | 1364 | for ($var) { |
1910 | ||
1365 | $abc = 1 when /^abc/; | |
1911 | ||
1366 | $def = 1 when /^def/; | |
1912 | ||
1367 | $xyz = 1 when /^xyz/; | |
1913 | ||
1368 | default { $nothing = 1 } | |
1914 | 1369 | } |
1915 | 1370 | |
1916 | 1371 | =begin original |
1917 | 1372 | |
1918 | 1373 | Or if you don't care to play it safe, like this: |
1919 | 1374 | |
1920 | 1375 | =end original |
1921 | 1376 | |
1922 | 1377 | あるいは、安全にすることを気にしないなら、以下のようにします: |
1923 | 1378 | |
1924 | 1379 | use v5.14; |
1925 | 1380 | given ($var) { |
1926 | ||
1381 | $abc = 1 when /^abc/; | |
1927 | ||
1382 | $def = 1 when /^def/; | |
1928 | ||
1383 | $xyz = 1 when /^xyz/; | |
1929 | ||
1384 | default { $nothing = 1 } | |
1930 | 1385 | } |
1931 | 1386 | |
1932 | 1387 | =begin original |
1933 | 1388 | |
1934 | 1389 | The arguments to C<given> and C<when> are in scalar context, |
1935 | 1390 | and C<given> assigns the C<$_> variable its topic value. |
1936 | 1391 | |
1937 | 1392 | =end original |
1938 | 1393 | |
1939 | 1394 | C<given> と C<when> への引数はスカラコンテキストで、 |
1940 | 1395 | C<given> は C<$_> 変数に注目している値を代入します。 |
1941 | 1396 | |
1942 | 1397 | =begin original |
1943 | 1398 | |
1944 | 1399 | Exactly what the I<EXPR> argument to C<when> does is hard to describe |
1945 | 1400 | precisely, but in general, it tries to guess what you want done. Sometimes |
1946 | 1401 | it is interpreted as C<< $_ ~~ I<EXPR> >>, and sometimes it is not. It |
1947 | 1402 | also behaves differently when lexically enclosed by a C<given> block than |
1948 | 1403 | it does when dynamically enclosed by a C<foreach> loop. The rules are far |
1949 | 1404 | too difficult to understand to be described here. See L</"Experimental Details |
1950 | 1405 | on given and when"> later on. |
1951 | 1406 | |
1952 | 1407 | =end original |
1953 | 1408 | |
1954 | 1409 | C<when> への I<EXPR> 引数が正確に何をするかを正確に記述するのは |
1955 | 1410 | 難しいですが、一般的には、あなたのしたいことを推測しようとします。 |
1956 | 1411 | これは C<< $_ ~~ I<EXPR> >> として解釈される場合もあり、そうでない場合も |
1957 | 1412 | あります。 |
1958 | 1413 | これはまた、C<given> ブロックでレキシカルに囲まれた場合は、C<foreach> |
1959 | 1414 | ループによって動的に囲まれた場合とは異なった振る舞いをします。 |
1960 | 1415 | 規則はここで記述されたよりも遥かに理解しにくいものです。 |
1961 | 1416 | 後述する L</"Experimental Details on given and when"> を参照してください。 |
1962 | 1417 | |
1963 | 1418 | =begin original |
1964 | 1419 | |
1965 | 1420 | Due to an unfortunate bug in how C<given> was implemented between Perl 5.10 |
1966 | 1421 | and 5.16, under those implementations the version of C<$_> governed by |
1967 | 1422 | C<given> is merely a lexically scoped copy of the original, not a |
1968 | 1423 | dynamically scoped alias to the original, as it would be if it were a |
1969 | C<foreach> or under both the original and the current | |
1424 | C<foreach> or under both the original and the current Perl 6 language | |
1970 | specification. This bug was fixed in Perl | |
1425 | specification. This bug was fixed in Perl | |
1971 | ||
1426 | 5.18. If you really want a lexical C<$_>, | |
1427 | specify that explicitly, but note that C<my $_> | |
1428 | is now deprecated and will warn unless warnings | |
1429 | have been disabled: | |
1972 | 1430 | |
1973 | 1431 | =end original |
1974 | 1432 | |
1975 | 1433 | Perl 5.10 と 5.14 の間での C<given> の実装方法による不幸なバグにより、 |
1976 | 1434 | 現在の実装では、C<given> によって管理される C<$_> は、 |
1977 | C<foreach> の場合やオリジナルと現在両方の | |
1435 | C<foreach> の場合やオリジナルと現在両方の Perl 6 言語仕様のように | |
1978 | 1436 | 元のものの動的スコープな別名ではなく、単なるレキシカルスコープのコピーです。 |
1979 | このバグは Perl 5.18 で修正されました | |
1437 | このバグは Perl 5.18 で修正されました。 | |
1980 | ||
1438 | 本当にレキシカルな C<$_> がほしいなら、明示的に指定しますが、C<my $_> は | |
1439 | 非推奨になって、警告を無効にしない限り警告されるようになったことに | |
1440 | 注意してください: | |
1981 | 1441 | |
1442 | given(my $_ = EXPR) { ... } | |
1443 | ||
1982 | 1444 | =begin original |
1983 | 1445 | |
1984 | 1446 | If your code still needs to run on older versions, |
1985 | 1447 | stick to C<foreach> for your topicalizer and |
1986 | 1448 | you will be less unhappy. |
1987 | 1449 | |
1988 | 1450 | =end original |
1989 | 1451 | |
1990 | 1452 | あなたのコードがまだ古いバージョンでも動作する必要があるなら、 |
1991 | 1453 | 主題化器には C<foreach> を使うことで不幸を減らせます。 |
1992 | 1454 | |
1993 | 1455 | =head2 Goto |
1994 | 1456 | X<goto> |
1995 | 1457 | |
1996 | 1458 | (goto 文) |
1997 | 1459 | |
1998 | 1460 | =begin original |
1999 | 1461 | |
2000 | 1462 | Although not for the faint of heart, Perl does support a C<goto> |
2001 | 1463 | statement. There are three forms: C<goto>-LABEL, C<goto>-EXPR, and |
2002 | 1464 | C<goto>-&NAME. A loop's LABEL is not actually a valid target for |
2003 | 1465 | a C<goto>; it's just the name of the loop. |
2004 | 1466 | |
2005 | 1467 | =end original |
2006 | 1468 | |
2007 | 1469 | 気弱な人のためでないにも関らず、Perl は C<goto> 文をサポートしています。 |
2008 | 1470 | C<goto>-LABEL、C<goto>-EXPR、C<goto>-&NAME の三つの形式があります。 |
2009 | 1471 | ループのラベルは実際には C<goto> の正当なターゲットではなく、 |
2010 | 1472 | ループの名前にすぎません。 |
2011 | 1473 | |
2012 | 1474 | =begin original |
2013 | 1475 | |
2014 | 1476 | The C<goto>-LABEL form finds the statement labeled with LABEL and resumes |
2015 | 1477 | execution there. It may not be used to go into any construct that |
2016 | 1478 | requires initialization, such as a subroutine or a C<foreach> loop. It |
2017 | 1479 | also can't be used to go into a construct that is optimized away. It |
2018 | 1480 | can be used to go almost anywhere else within the dynamic scope, |
2019 | 1481 | including out of subroutines, but it's usually better to use some other |
2020 | 1482 | construct such as C<last> or C<die>. The author of Perl has never felt the |
2021 | 1483 | need to use this form of C<goto> (in Perl, that is--C is another matter). |
2022 | 1484 | |
2023 | 1485 | =end original |
2024 | 1486 | |
2025 | 1487 | C<goto>-LABEL 形式は LABEL でラベル付けされた文を見つけだし、そこから |
2026 | 1488 | 実行を再開します。 |
2027 | 1489 | これはサブルーチンであるとか C<foreach> ループのような |
2028 | 1490 | 初期化を必要とするような構造へ飛び込むために使うことはできません。 |
2029 | 1491 | また、最適化されて無くなってしまうような構造へ飛び込むこともできません。 |
2030 | 1492 | 動的スコープの中以外のほとんどの場所へは、サブルーチンの外も含めて |
2031 | 1493 | 移動することができます; しかし、通常は C<last> や C<die> のような |
2032 | 1494 | 別のやり方を使ったほうが良いでしょう。 |
2033 | 1495 | Perl の作者は、未だかつてこの形式の C<goto> を使うことが |
2034 | 1496 | 必要だと感じたことはありません(Perl の場合です--C の場合はまた別の話です)。 |
2035 | 1497 | |
2036 | 1498 | =begin original |
2037 | 1499 | |
2038 | 1500 | The C<goto>-EXPR form expects a label name, whose scope will be resolved |
2039 | 1501 | dynamically. This allows for computed C<goto>s per FORTRAN, but isn't |
2040 | 1502 | necessarily recommended if you're optimizing for maintainability: |
2041 | 1503 | |
2042 | 1504 | =end original |
2043 | 1505 | |
2044 | 1506 | C<goto>-EXPR 形式は動的に解決されるスコープを持っているラベル名を |
2045 | 1507 | 期待しています。 |
2046 | 1508 | これによって FORTRAN の計算型 C<goto> が実現できますが、 |
2047 | 1509 | これは保守性に重きを置くのであれば使うことは止めた方が良いでしょう。 |
2048 | 1510 | |
2049 | 1511 | goto(("FOO", "BAR", "GLARCH")[$i]); |
2050 | 1512 | |
2051 | 1513 | =begin original |
2052 | 1514 | |
2053 | 1515 | The C<goto>-&NAME form is highly magical, and substitutes a call to the |
2054 | 1516 | named subroutine for the currently running subroutine. This is used by |
2055 | 1517 | C<AUTOLOAD()> subroutines that wish to load another subroutine and then |
2056 | 1518 | pretend that the other subroutine had been called in the first place |
2057 | 1519 | (except that any modifications to C<@_> in the current subroutine are |
2058 | 1520 | propagated to the other subroutine.) After the C<goto>, not even C<caller()> |
2059 | 1521 | will be able to tell that this routine was called first. |
2060 | 1522 | |
2061 | 1523 | =end original |
2062 | 1524 | |
2063 | 1525 | C<goto>-&NAME は高度にマジカルで、名前付きサブルーチンの呼び出しを |
2064 | 1526 | カレントで実行されているサブルーチンに置き換えます。 |
2065 | 1527 | これは別のサブルーチンをロードして、最初の場所で呼び出された |
2066 | 1528 | 別のサブルーチンを要求することをしようとする |
2067 | 1529 | C<AUTOLOAD()> サブルーチンで使われていてます |
2068 | 1530 | (カレントのサブルーチンにおける C<@_> に対するもの以外の変更は、 |
2069 | 1531 | 別のサブルーチンへ伝播します)。 |
2070 | 1532 | C<goto> の後で、C<caller()> でなくてもこのサブルーチンが |
2071 | 1533 | 最初に呼ばれたのだということを伝えることすらできるでしょう。 |
2072 | 1534 | |
2073 | 1535 | =begin original |
2074 | 1536 | |
2075 | 1537 | In almost all cases like this, it's usually a far, far better idea to use the |
2076 | 1538 | structured control flow mechanisms of C<next>, C<last>, or C<redo> instead of |
2077 | 1539 | resorting to a C<goto>. For certain applications, the catch and throw pair of |
2078 | 1540 | C<eval{}> and die() for exception processing can also be a prudent approach. |
2079 | 1541 | |
2080 | 1542 | =end original |
2081 | 1543 | |
2082 | 1544 | このようなケースのほとんどすべての場合、C<goto> に頼るのではなくて |
2083 | 1545 | C<next>、C<last>、C<redo> といった制御フロー機構を使うことが、 |
2084 | 1546 | ずっとずっと良いアイデアでしょう。 |
2085 | 1547 | 一部のアプリケーションに対しては、C<eval{}> と die() を |
2086 | 1548 | catch と throw のペアとして例外処理を行うための賢明なアプローチとして |
2087 | 1549 | 使うことができるでしょう。 |
2088 | 1550 | |
2089 | 1551 | =head2 The Ellipsis Statement |
2090 | 1552 | X<...> |
2091 | 1553 | X<... statement> |
2092 | 1554 | X<ellipsis operator> |
2093 | 1555 | X<elliptical statement> |
2094 | 1556 | X<unimplemented statement> |
2095 | 1557 | X<unimplemented operator> |
2096 | 1558 | X<yada-yada> |
2097 | 1559 | X<yada-yada operator> |
2098 | 1560 | X<... operator> |
2099 | 1561 | X<whatever operator> |
2100 | 1562 | X<triple-dot operator> |
2101 | 1563 | |
2102 | 1564 | (省略文) |
2103 | 1565 | |
2104 | 1566 | =begin original |
2105 | 1567 | |
2106 | 1568 | Beginning in Perl 5.12, Perl accepts an ellipsis, "C<...>", as a |
2107 | placeholder for code that you haven't implemented yet. | |
1569 | placeholder for code that you haven't implemented yet. This form of | |
1570 | ellipsis, the unimplemented statement, should not be confused with the | |
1571 | binary flip-flop C<...> operator. One is a statement and the other an | |
1572 | operator. (Perl doesn't usually confuse them because usually Perl can tell | |
1573 | whether it wants an operator or a statement, but see below for exceptions.) | |
1574 | ||
1575 | =end original | |
1576 | ||
1577 | Perl 5.12 から、Perl はまだ実装していないコードのプレースホルダとして | |
1578 | 省略 "C<...>" を受け付けるようになりました。 | |
1579 | この形式の省略、未実装文は、二項フリップフロップ C<...> 演算子と | |
1580 | 混乱しないでください。 | |
1581 | 片方は文で、もう片方は演算子です。 | |
1582 | (Perl は普通混乱しません; なぜなら普通は Perl は演算子を求めているか | |
1583 | 文を求めているかを伝えるからです; しかし以下の例外を参照してください。) | |
1584 | ||
1585 | =begin original | |
1586 | ||
2108 | 1587 | When Perl 5.12 or later encounters an ellipsis statement, it parses this |
2109 | 1588 | without error, but if and when you should actually try to execute it, Perl |
2110 | 1589 | throws an exception with the text C<Unimplemented>: |
2111 | 1590 | |
2112 | 1591 | =end original |
2113 | 1592 | |
2114 | Perl 5.12 から、Perl はまだ実装していないコードのプレースホルダとして | |
2115 | 省略 "C<...>" を受け付けるようになりました。 | |
2116 | 1593 | Perl 5.12 以降で省略文に遭遇すると、エラーなくパースしますが、実際に |
2117 | 1594 | 実行しようとすると、C<Unimplemented> というテキストと共に例外を投げます: |
2118 | 1595 | |
2119 | 1596 | use v5.12; |
2120 | 1597 | sub unimplemented { ... } |
2121 | 1598 | eval { unimplemented() }; |
2122 | 1599 | if ($@ =~ /^Unimplemented at /) { |
2123 | ||
1600 | say "I found an ellipsis!"; | |
2124 | 1601 | } |
2125 | 1602 | |
2126 | 1603 | =begin original |
2127 | 1604 | |
2128 | You can only use the elliptical statement to stand in for a | |
1605 | You can only use the elliptical statement to stand in for a | |
2129 | statement. | |
1606 | complete statement. These examples of how the ellipsis works: | |
2130 | as with other kinds of semicolon-terminated statement, the semicolon | |
2131 | may be omitted if "C<...>" appears immediately before a closing brace. | |
2132 | These examples show how the ellipsis works: | |
2133 | 1607 | |
2134 | 1608 | =end original |
2135 | 1609 | |
2136 | 1610 | 完全な文を使える場所でのみ省略文を使えます。 |
2137 | 文法的には、"C<...;>" は完全な文ですが、その他のセミコロン終端する | |
2138 | 文と同様、"C<...>" が閉じ中かっこの直前に現れる場合は、 | |
2139 | セミコロンは省略できます。 | |
2140 | 1611 | 次の例は省略がどのように動作するかの例です: |
2141 | 1612 | |
2142 | 1613 | use v5.12; |
2143 | 1614 | { ... } |
2144 | 1615 | sub foo { ... } |
2145 | 1616 | ...; |
2146 | 1617 | eval { ... }; |
2147 | 1618 | sub somemeth { |
2148 | ||
1619 | my $self = shift; | |
2149 | ||
1620 | ...; | |
2150 | 1621 | } |
2151 | 1622 | $x = do { |
2152 | ||
1623 | my $n; | |
2153 | ||
1624 | ...; | |
2154 | ||
1625 | say "Hurrah!"; | |
2155 | ||
1626 | $n; | |
2156 | 1627 | }; |
2157 | 1628 | |
2158 | 1629 | =begin original |
2159 | 1630 | |
2160 | 1631 | The elliptical statement cannot stand in for an expression that |
2161 | is part of a larger statement. | |
1632 | is part of a larger statement, since the C<...> is also the three-dot | |
1633 | version of the flip-flop operator (see L<perlop/"Range Operators">). | |
1634 | ||
1635 | =end original | |
1636 | ||
1637 | C<...> はフリップフロップ演算子(L<Range Operators> 参照)の 3 点版でも | |
1638 | あるので、省略文はより大きな文の一部の式としては使えません。 | |
1639 | ||
1640 | =begin original | |
1641 | ||
2162 | 1642 | These examples of attempts to use an ellipsis are syntax errors: |
2163 | 1643 | |
2164 | 1644 | =end original |
2165 | 1645 | |
2166 | 省略文はより大きな文の一部の式としては使えません。 | |
2167 | 1646 | 省略を使おうとする以下の例は文法エラーになります: |
2168 | 1647 | |
2169 | 1648 | use v5.12; |
2170 | 1649 | |
2171 | 1650 | print ...; |
2172 | 1651 | open(my $fh, ">", "/dev/passwd") or ...; |
2173 | 1652 | if ($condition && ... ) { say "Howdy" }; |
2174 | ... if $x > $y; | |
2175 | say "Cromulent" if ...; | |
2176 | $flub = 5 + ...; | |
2177 | 1653 | |
2178 | 1654 | =begin original |
2179 | 1655 | |
2180 | 1656 | There are some cases where Perl can't immediately tell the difference |
2181 | 1657 | between an expression and a statement. For instance, the syntax for a |
2182 | 1658 | block and an anonymous hash reference constructor look the same unless |
2183 | 1659 | there's something in the braces to give Perl a hint. The ellipsis is a |
2184 | syntax error if Perl doesn't guess that the C<{ ... }> is a block. | |
1660 | syntax error if Perl doesn't guess that the C<{ ... }> is a block. In that | |
2185 | ||
1661 | case, it doesn't think the C<...> is an ellipsis because it's expecting an | |
2186 | ||
1662 | expression instead of a statement: | |
2187 | 1663 | |
2188 | 1664 | =end original |
2189 | 1665 | |
2190 | 1666 | 式と文との違いをすぐに説明できない場合があります。 |
2191 | 1667 | 例えば、ブロックと無名ハッシュリファレンスのコンストラクタは、 |
2192 | 1668 | Perl にヒントを与える中かっこがなければ同じに見えます。 |
2193 | 1669 | 省略文は Perl が C<{ ... }> をブロックと判断できなかった場合は |
2194 | 1670 | 文法エラーとなります。 |
2195 | ||
1671 | この場合、文ではなく式と推測するので、C<...> は省略とは判断されません: | |
2196 | コンストラクタでないことを示すために、省略の前に C<;> を使えます。 | |
2197 | 1672 | |
2198 | 1673 | =begin original |
2199 | 1674 | |
1675 | @transformed = map { ... } @input; # syntax error | |
1676 | ||
1677 | =end original | |
1678 | ||
1679 | @transformed = map { ... } @input; # 文法エラー | |
1680 | ||
1681 | =begin original | |
1682 | ||
1683 | You can use a C<;> inside your block to denote that the C<{ ... }> is a | |
1684 | block and not a hash reference constructor. Now the ellipsis works: | |
1685 | ||
1686 | =end original | |
1687 | ||
1688 | C<{ ... }> がブロックであって、ハッシュリファレンスのコンストラクタでは | |
1689 | ないことを示すためにブロックの中で C<;> を使えます。 | |
1690 | これで省略は動作します: | |
1691 | ||
1692 | =begin original | |
1693 | ||
1694 | @transformed = map {; ... } @input; # ; disambiguates | |
1695 | ||
1696 | =end original | |
1697 | ||
1698 | @transformed = map {; ... } @input; # ; 曖昧でない | |
1699 | ||
1700 | =begin original | |
1701 | ||
1702 | @transformed = map { ...; } @input; # ; disambiguates | |
1703 | ||
1704 | =end original | |
1705 | ||
1706 | @transformed = map { ...; } @input; # ; 曖昧でない | |
1707 | ||
1708 | =begin original | |
1709 | ||
2200 | 1710 | Note: Some folks colloquially refer to this bit of punctuation as a |
2201 | 1711 | "yada-yada" or "triple-dot", but its true name |
2202 | is actually an ellipsis. | |
1712 | is actually an ellipsis. Perl does not yet | |
1713 | accept the Unicode version, U+2026 HORIZONTAL ELLIPSIS, as an alias for | |
1714 | C<...>, but someday it may. | |
2203 | 1715 | |
2204 | 1716 | =end original |
2205 | 1717 | |
2206 | 1718 | 注意: 一部の人間はこの句読点を口語的に「ヤダヤダ」や「3 点」として |
2207 | 1719 | 参照しますが、真の名前は実際には省略です。 |
1720 | Perl はまだ Unicode 版の U+2026 HORIZONTAL ELLIPSIS を C<...> の別名として | |
1721 | 認識しませんが、いつかそうなるかもしれません。 | |
2208 | 1722 | |
2209 | 1723 | =head2 PODs: Embedded Documentation |
2210 | 1724 | X<POD> X<documentation> |
2211 | 1725 | |
2212 | 1726 | (POD: 組み込みドキュメント) |
2213 | 1727 | |
2214 | 1728 | =begin original |
2215 | 1729 | |
2216 | 1730 | Perl has a mechanism for intermixing documentation with source code. |
2217 | 1731 | While it's expecting the beginning of a new statement, if the compiler |
2218 | 1732 | encounters a line that begins with an equal sign and a word, like this |
2219 | 1733 | |
2220 | 1734 | =end original |
2221 | 1735 | |
2222 | 1736 | Perl は、ソースコードとドキュメントとを混ぜ書きするための仕掛けを |
2223 | 1737 | 持っています。 |
2224 | 1738 | 新しい文の始まりが期待されているときに、コンパイラは |
2225 | 1739 | 以下の例のような = 記号で始まっている語を見つけると: |
2226 | 1740 | |
2227 | 1741 | =head1 Here There Be Pods! |
2228 | 1742 | |
2229 | 1743 | =begin original |
2230 | 1744 | |
2231 | 1745 | Then that text and all remaining text up through and including a line |
2232 | 1746 | beginning with C<=cut> will be ignored. The format of the intervening |
2233 | 1747 | text is described in L<perlpod>. |
2234 | 1748 | |
2235 | 1749 | =end original |
2236 | 1750 | |
2237 | 1751 | そのテキストと、C<=cut> で始まる行までの内容を無視します。 |
2238 | 1752 | 間に入るテキストの書式は L<perlpod> で説明されています。 |
2239 | 1753 | |
2240 | 1754 | =begin original |
2241 | 1755 | |
2242 | 1756 | This allows you to intermix your source code |
2243 | 1757 | and your documentation text freely, as in |
2244 | 1758 | |
2245 | 1759 | =end original |
2246 | 1760 | |
2247 | 1761 | これによって、ソースコードとドキュメントとを以下に示す例のように |
2248 | 1762 | 自由に混ぜることができるようになります。 |
2249 | 1763 | |
2250 | 1764 | =item snazzle($) |
2251 | 1765 | |
2252 | 1766 | The snazzle() function will behave in the most spectacular |
2253 | 1767 | form that you can possibly imagine, not even excepting |
2254 | 1768 | cybernetic pyrotechnics. |
2255 | 1769 | |
2256 | 1770 | =cut back to the compiler, nuff of this pod stuff! |
2257 | 1771 | |
2258 | 1772 | sub snazzle($) { |
2259 | ||
1773 | my $thingie = shift; | |
2260 | ||
1774 | ......... | |
2261 | 1775 | } |
2262 | 1776 | |
2263 | 1777 | =begin original |
2264 | 1778 | |
2265 | 1779 | Note that pod translators should look at only paragraphs beginning |
2266 | 1780 | with a pod directive (it makes parsing easier), whereas the compiler |
2267 | 1781 | actually knows to look for pod escapes even in the middle of a |
2268 | 1782 | paragraph. This means that the following secret stuff will be |
2269 | 1783 | ignored by both the compiler and the translators. |
2270 | 1784 | |
2271 | 1785 | =end original |
2272 | 1786 | |
2273 | 1787 | コンパイラはパラグラフの途中に pod エスケープがあったとしてもそれを |
2274 | 1788 | 認識できるのに、pod トランスレータは pod 指示子で始まっている |
2275 | 1789 | パラグラフのみに注目すべき(これは構文解析を簡単にするためです)で |
2276 | 1790 | あるということに注意して下さい。 |
2277 | 1791 | つまり、以下の例にある "secret stuff" はコンパイラからも、 |
2278 | 1792 | トランスレータからも無視されるということです。 |
2279 | 1793 | |
2280 | $ | |
1794 | $a=3; | |
2281 | 1795 | =secret stuff |
2282 | 1796 | warn "Neither POD nor CODE!?" |
2283 | 1797 | =cut back |
2284 | print "got $ | |
1798 | print "got $a\n"; | |
2285 | 1799 | |
2286 | 1800 | =begin original |
2287 | 1801 | |
2288 | 1802 | You probably shouldn't rely upon the C<warn()> being podded out forever. |
2289 | 1803 | Not all pod translators are well-behaved in this regard, and perhaps |
2290 | 1804 | the compiler will become pickier. |
2291 | 1805 | |
2292 | 1806 | =end original |
2293 | 1807 | |
2294 | 1808 | この例の C<warn()> のようなものが、将来に渡って無視されるということに |
2295 | 1809 | 依存すべきではありません。 |
2296 | 1810 | すべての pod トランスレータがそのように振る舞うわけではありませんし、 |
2297 | 1811 | コンパイラは将来これを無視しないようになるかもしれません。 |
2298 | 1812 | |
2299 | 1813 | =begin original |
2300 | 1814 | |
2301 | 1815 | One may also use pod directives to quickly comment out a section |
2302 | 1816 | of code. |
2303 | 1817 | |
2304 | 1818 | =end original |
2305 | 1819 | |
2306 | 1820 | pod 指示子を、コードの一部を手っ取り早くコメントアウトするために |
2307 | 1821 | 使うこともできます。 |
2308 | 1822 | |
2309 | 1823 | =head2 Plain Old Comments (Not!) |
2310 | 1824 | X<comment> X<line> X<#> X<preprocessor> X<eval> |
2311 | 1825 | |
2312 | 1826 | =begin original |
2313 | 1827 | |
2314 | 1828 | Perl can process line directives, much like the C preprocessor. Using |
2315 | 1829 | this, one can control Perl's idea of filenames and line numbers in |
2316 | 1830 | error or warning messages (especially for strings that are processed |
2317 | 1831 | with C<eval()>). The syntax for this mechanism is almost the same as for |
2318 | 1832 | most C preprocessors: it matches the regular expression |
2319 | 1833 | |
2320 | 1834 | =end original |
2321 | 1835 | |
2322 | 1836 | C のプリプロセッサと同じように、Perl は行指示子を処理できます。 |
2323 | 1837 | これを使うことによって、エラーメッセージや警告メッセージにある |
2324 | 1838 | ファイル名や行番号を制御することができます |
2325 | 1839 | (特に、C<eval()> で処理される文字列のために)。 |
2326 | 1840 | この仕組みの構文はほとんどの C のプリプロセッサとほとんど同じで、正規表現: |
2327 | 1841 | |
2328 | 1842 | # example: '# line 42 "new_filename.plx"' |
2329 | 1843 | /^\# \s* |
2330 | 1844 | line \s+ (\d+) \s* |
2331 | 1845 | (?:\s("?)([^"]+)\g2)? \s* |
2332 | 1846 | $/x |
2333 | 1847 | |
2334 | 1848 | =begin original |
2335 | 1849 | |
2336 | 1850 | with C<$1> being the line number for the next line, and C<$3> being |
2337 | 1851 | the optional filename (specified with or without quotes). Note that |
2338 | 1852 | no whitespace may precede the C<< # >>, unlike modern C preprocessors. |
2339 | 1853 | |
2340 | 1854 | =end original |
2341 | 1855 | |
2342 | 1856 | にマッチしたものの C<$1> が次の行の行番号となり、省略することもできる |
2343 | 1857 | C<$3> は(クォートありかなしで指定された)ファイル名となります。 |
2344 | 1858 | 最近の C プリプロセッサとは違って、C<< # >> の前に空白を置けないことに |
2345 | 1859 | 注意してください。 |
2346 | 1860 | |
2347 | 1861 | =begin original |
2348 | 1862 | |
2349 | 1863 | There is a fairly obvious gotcha included with the line directive: |
2350 | 1864 | Debuggers and profilers will only show the last source line to appear |
2351 | 1865 | at a particular line number in a given file. Care should be taken not |
2352 | 1866 | to cause line number collisions in code you'd like to debug later. |
2353 | 1867 | |
2354 | 1868 | =end original |
2355 | 1869 | |
2356 | 1870 | 行指示子にはかなり明らかな技があります: デバッガとプロファイラは、 |
2357 | 1871 | 与えられたファイルの特定の行番号に対して現れた最新のソース行のみを |
2358 | 1872 | 表示します。 |
2359 | 1873 | あとでデバッグしたいコードでは行番号の衝突が起きないように注意するべきです。 |
2360 | 1874 | |
2361 | 1875 | =begin original |
2362 | 1876 | |
2363 | 1877 | Here are some examples that you should be able to type into your command |
2364 | 1878 | shell: |
2365 | 1879 | |
2366 | 1880 | =end original |
2367 | 1881 | |
2368 | 1882 | コマンドシェルでタイプすることのできる例をいくつか挙げます: |
2369 | 1883 | |
2370 | 1884 | % perl |
2371 | 1885 | # line 200 "bzzzt" |
2372 | 1886 | # the '#' on the previous line must be the first char on line |
2373 | 1887 | die 'foo'; |
2374 | 1888 | __END__ |
2375 | 1889 | foo at bzzzt line 201. |
2376 | 1890 | |
2377 | 1891 | % perl |
2378 | 1892 | # line 200 "bzzzt" |
2379 | 1893 | eval qq[\n#line 2001 ""\ndie 'foo']; print $@; |
2380 | 1894 | __END__ |
2381 | 1895 | foo at - line 2001. |
2382 | 1896 | |
2383 | 1897 | % perl |
2384 | 1898 | eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@; |
2385 | 1899 | __END__ |
2386 | 1900 | foo at foo bar line 200. |
2387 | 1901 | |
2388 | 1902 | % perl |
2389 | 1903 | # line 345 "goop" |
2390 | 1904 | eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'"; |
2391 | 1905 | print $@; |
2392 | 1906 | __END__ |
2393 | 1907 | foo at goop line 345. |
2394 | 1908 | |
2395 | 1909 | =head2 Experimental Details on given and when |
2396 | 1910 | |
2397 | 1911 | (given と when の実験的な詳細) |
2398 | 1912 | |
2399 | 1913 | =begin original |
2400 | 1914 | |
2401 | 1915 | As previously mentioned, the "switch" feature is considered highly |
2402 | experimental | |
1916 | experimental; it is subject to change with little notice. In particular, | |
2403 | it is subject to change with little notice. In particular, | |
2404 | 1917 | C<when> has tricky behaviours that are expected to change to become less |
2405 | 1918 | tricky in the future. Do not rely upon its current (mis)implementation. |
2406 | 1919 | Before Perl 5.18, C<given> also had tricky behaviours that you should still |
2407 | 1920 | beware of if your code must run on older versions of Perl. |
2408 | 1921 | |
2409 | 1922 | =end original |
2410 | 1923 | |
2411 | 既に述べたように、"switch" 機能は非常に実験的であると考えられています | |
1924 | 既に述べたように、"switch" 機能は非常に実験的であると考えられています; | |
2412 | (また perl 5.42.0 で削除されることが予定されています); | |
2413 | 1925 | ほとんど知らせることなく変更されることがあります。 |
2414 | 1926 | 特に C<when> はトリッキーな振る舞いがあり、将来よりトリッキーで |
2415 | 1927 | なくなるように変更される予定です。 |
2416 | 1928 | 現在の(誤)実装に依存しないでください。 |
2417 | 1929 | Perl 5.18 より前では、C<given> には、コードが古いバージョンの Perl でも |
2418 | 1930 | 動かなければならない場合に気をつけるべきトリッキーな振る舞いもあります: |
2419 | 1931 | |
2420 | 1932 | =begin original |
2421 | 1933 | |
2422 | 1934 | Here is a longer example of C<given>: |
2423 | 1935 | |
2424 | 1936 | =end original |
2425 | 1937 | |
2426 | 1938 | これはより長い C<given> の例です: |
2427 | 1939 | |
2428 | 1940 | use feature ":5.10"; |
2429 | 1941 | given ($foo) { |
2430 | ||
1942 | when (undef) { | |
2431 | ||
1943 | say '$foo is undefined'; | |
2432 | ||
1944 | } | |
2433 | ||
1945 | when ("foo") { | |
2434 | ||
1946 | say '$foo is the string "foo"'; | |
2435 | ||
1947 | } | |
2436 | ||
1948 | when ([1,3,5,7,9]) { | |
2437 | ||
1949 | say '$foo is an odd digit'; | |
2438 | ||
1950 | continue; # Fall through | |
2439 | ||
1951 | } | |
2440 | ||
1952 | when ($_ < 100) { | |
2441 | ||
1953 | say '$foo is numerically less than 100'; | |
2442 | ||
1954 | } | |
2443 | ||
1955 | when (\&complicated_check) { | |
2444 | ||
1956 | say 'a complicated check for $foo is true'; | |
2445 | ||
1957 | } | |
2446 | ||
1958 | default { | |
2447 | ||
1959 | die q(I don't know what to do with $foo); | |
2448 | ||
1960 | } | |
2449 | 1961 | } |
2450 | 1962 | |
2451 | 1963 | =begin original |
2452 | 1964 | |
2453 | 1965 | Before Perl 5.18, C<given(EXPR)> assigned the value of I<EXPR> to |
2454 | 1966 | merely a lexically scoped I<B<copy>> (!) of C<$_>, not a dynamically |
2455 | 1967 | scoped alias the way C<foreach> does. That made it similar to |
2456 | 1968 | |
2457 | 1969 | =end original |
2458 | 1970 | |
2459 | 1971 | Perl 5.18 より前では、C<given(EXPR)> は I<EXPR> の値を単にレキシカルスコープを |
2460 | 1972 | 持つ C<$_> の I<B<コピー>> (!) に代入します; C<foreach> がするような動的 |
2461 | 1973 | スコープを持つ別名ではありません。 |
2462 | 1974 | これは以下と似ています: |
2463 | 1975 | |
2464 | ||
1976 | do { my $_ = EXPR; ... } | |
2465 | 1977 | |
2466 | 1978 | =begin original |
2467 | 1979 | |
2468 | 1980 | except that the block was automatically broken out of by a successful |
2469 | 1981 | C<when> or an explicit C<break>. Because it was only a copy, and because |
2470 | 1982 | it was only lexically scoped, not dynamically scoped, you could not do the |
2471 | 1983 | things with it that you are used to in a C<foreach> loop. In particular, |
2472 | 1984 | it did not work for arbitrary function calls if those functions might try |
2473 | 1985 | to access $_. Best stick to C<foreach> for that. |
2474 | 1986 | |
2475 | 1987 | =end original |
2476 | 1988 | |
2477 | 1989 | しかし、ブロックは C<when> が成功するか、明示的な C<break> によって |
2478 | 1990 | 自動的に破壊されるところが違いました。 |
2479 | 1991 | これはただのコピーで、動的スコープではなくレキシカルなスコープしか |
2480 | 1992 | 持たないので、C<foreach> ループの中でできるようなことはできませんでした。 |
2481 | 1993 | 特に、$_ にアクセスしようとするかもしれない関数の場合、任意の関数呼び出しに |
2482 | 1994 | 対しては動作していませんでした。 |
2483 | 1995 | そのためには C<foreach> にこだわるのが最良です。 |
2484 | 1996 | |
2485 | 1997 | =begin original |
2486 | 1998 | |
2487 | 1999 | Most of the power comes from the implicit smartmatching that can |
2488 | 2000 | sometimes apply. Most of the time, C<when(EXPR)> is treated as an |
2489 | 2001 | implicit smartmatch of C<$_>, that is, C<$_ ~~ EXPR>. (See |
2490 | 2002 | L<perlop/"Smartmatch Operator"> for more information on smartmatching.) |
2491 | 2003 | But when I<EXPR> is one of the 10 exceptional cases (or things like them) |
2492 | 2004 | listed below, it is used directly as a boolean. |
2493 | 2005 | |
2494 | 2006 | =end original |
2495 | 2007 | |
2496 | 2008 | 強力さのほとんどは時々適用される暗黙のスマートマッチングによるものです。 |
2497 | 2009 | ほとんどの場合、C<when(EXPR)> は暗黙の C<$_> のスマートマッチング、 |
2498 | 2010 | つまり C<$_ ~~ EXPR> として扱われます。 |
2499 | 2011 | (スマートマッチングに関するさらなる情報については |
2500 | 2012 | L<perlop/"Smartmatch Operator"> を参照してください。) |
2501 | 2013 | しかし I<EXPR> が後述する 10 の例外の場合(および似たような場合) の |
2502 | 2014 | 一つの場合、直接真偽値として使われます。 |
2503 | 2015 | |
2504 | 2016 | =over 4 |
2505 | 2017 | |
2506 | 2018 | =item Z<>1. |
2507 | 2019 | |
2508 | 2020 | =begin original |
2509 | 2021 | |
2510 | 2022 | A user-defined subroutine call or a method invocation. |
2511 | 2023 | |
2512 | 2024 | =end original |
2513 | 2025 | |
2514 | 2026 | ユーザー定義サブルーチンかメソッド呼び出し。 |
2515 | 2027 | |
2516 | 2028 | =item Z<>2. |
2517 | 2029 | |
2518 | 2030 | =begin original |
2519 | 2031 | |
2520 | 2032 | A regular expression match in the form of C</REGEX/>, C<$foo =~ /REGEX/>, |
2521 | 2033 | or C<$foo =~ EXPR>. Also, a negated regular expression match in |
2522 | 2034 | the form C<!/REGEX/>, C<$foo !~ /REGEX/>, or C<$foo !~ EXPR>. |
2523 | 2035 | |
2524 | 2036 | =end original |
2525 | 2037 | |
2526 | 2038 | C</REGEX/>, C<$foo =~ /REGEX/>, C<$foo =~ EXPR> 形式の 正規表現マッチング。 |
2527 | 2039 | また、C<!/REGEX/>, C<$foo !~ /REGEX/>, C<$foo !~ EXPR> 形式の |
2528 | 2040 | 正規表現マッチングの否定。 |
2529 | 2041 | |
2530 | 2042 | =item Z<>3. |
2531 | 2043 | |
2532 | 2044 | =begin original |
2533 | 2045 | |
2534 | 2046 | A smart match that uses an explicit C<~~> operator, such as C<EXPR ~~ EXPR>. |
2535 | 2047 | |
2536 | 2048 | =end original |
2537 | 2049 | |
2538 | 2050 | C<EXPR ~~ EXPR> のように、明示的に C<~~> 演算子を使うスマートマッチング。 |
2539 | 2051 | |
2540 | =begin original | |
2541 | ||
2542 | B<NOTE:> You will often have to use C<$c ~~ $_> because the default case | |
2543 | uses C<$_ ~~ $c> , which is frequently the opposite of what you want. | |
2544 | ||
2545 | =end original | |
2546 | ||
2547 | B<注意:> しばしば C<$c ~~ $_> を使う必要があることに注意してください; | |
2548 | なぜならデフォルトの場合は C<$_ ~~ $c> を使い、これはしばしばしたいことの | |
2549 | 逆だからです。 | |
2550 | ||
2551 | 2052 | =item Z<>4. |
2552 | 2053 | |
2553 | 2054 | =begin original |
2554 | 2055 | |
2555 | 2056 | A boolean comparison operator such as C<$_ E<lt> 10> or C<$x eq "abc">. The |
2556 | 2057 | relational operators that this applies to are the six numeric comparisons |
2557 | 2058 | (C<< < >>, C<< > >>, C<< <= >>, C<< >= >>, C<< == >>, and C<< != >>), and |
2558 | 2059 | the six string comparisons (C<lt>, C<gt>, C<le>, C<ge>, C<eq>, and C<ne>). |
2559 | 2060 | |
2560 | 2061 | =end original |
2561 | 2062 | |
2562 | 2063 | C<$_ E<lt> 10> や C<$x eq "abc"> のような真偽値比較。 |
2563 | 2064 | これを適用する関係演算子は、六つの数値比較 |
2564 | 2065 | (C<< < >>, C<< > >>, C<< <= >>, C<< >= >>, C<< == >>, C<< != >>) および |
2565 | 2066 | 六つの文字列比較 (C<lt>, C<gt>, C<le>, C<ge>, C<eq>, C<ne>) です。 |
2566 | 2067 | |
2068 | =begin original | |
2069 | ||
2070 | B<NOTE:> You will often have to use C<$c ~~ $_> because | |
2071 | the default case uses C<$_ ~~ $c> , which is frequently | |
2072 | the opposite of what you want. | |
2073 | ||
2074 | =end original | |
2075 | ||
2076 | B<注意:> しばしば C<$c ~~ $_> を使う必要があることに注意してください; | |
2077 | なぜならデフォルトの場合は C<$_ ~~ $c> を使い、これはしばしばしたいことの | |
2078 | 逆だからです。 | |
2079 | ||
2567 | 2080 | =item Z<>5. |
2568 | 2081 | |
2569 | 2082 | =begin original |
2570 | 2083 | |
2571 | 2084 | At least the three builtin functions C<defined(...)>, C<exists(...)>, and |
2572 | 2085 | C<eof(...)>. We might someday add more of these later if we think of them. |
2573 | 2086 | |
2574 | 2087 | =end original |
2575 | 2088 | |
2576 | 2089 | 少なくとも三つの組み込み関数 C<defined(...)>, C<exists(...)>, C<eof(...)>。 |
2577 | 2090 | これらについて考えるとき、いつかもっと追加するかもしれません。 |
2578 | 2091 | |
2579 | 2092 | =item Z<>6. |
2580 | 2093 | |
2581 | 2094 | =begin original |
2582 | 2095 | |
2583 | 2096 | A negated expression, whether C<!(EXPR)> or C<not(EXPR)>, or a logical |
2584 | 2097 | exclusive-or, C<(EXPR1) xor (EXPR2)>. The bitwise versions (C<~> and C<^>) |
2585 | 2098 | are not included. |
2586 | 2099 | |
2587 | 2100 | =end original |
2588 | 2101 | |
2589 | 2102 | 否定表現 C<!(EXPR)> または C<not(EXPR)>、 排他的論理和 |
2590 | 2103 | C<(EXPR1) xor (EXPR2)>。 |
2591 | 2104 | ビット版 (C<~> と C<^>) は含まれません。 |
2592 | 2105 | |
2593 | 2106 | =item Z<>7. |
2594 | 2107 | |
2595 | 2108 | =begin original |
2596 | 2109 | |
2597 | 2110 | A filetest operator, with exactly 4 exceptions: C<-s>, C<-M>, C<-A>, and |
2598 | 2111 | C<-C>, as these return numerical values, not boolean ones. The C<-z> |
2599 | 2112 | filetest operator is not included in the exception list. |
2600 | 2113 | |
2601 | 2114 | =end original |
2602 | 2115 | |
2603 | 2116 | 真偽値ではなく数値を返す四つの例外: C<-s>, C<-M>, C<-A>, C<-C> を除く |
2604 | 2117 | ファイルテスト演算子。 |
2605 | 2118 | C<-z> ファイルテスト演算子は例外の一覧には含まれません。 |
2606 | 2119 | |
2607 | 2120 | =item Z<>8. |
2608 | 2121 | |
2609 | 2122 | =begin original |
2610 | 2123 | |
2611 | 2124 | The C<..> and C<...> flip-flop operators. Note that the C<...> flip-flop |
2612 | 2125 | operator is completely different from the C<...> elliptical statement |
2613 | 2126 | just described. |
2614 | 2127 | |
2615 | 2128 | =end original |
2616 | 2129 | |
2617 | 2130 | フリップフロップ演算子 C<..> と C<...>。 |
2618 | 2131 | C<...> フリップフロップ演算子は先に記述した C<...> 省略文とは完全に |
2619 | 2132 | 異なることに注意してください。 |
2620 | 2133 | |
2621 | 2134 | =back |
2622 | 2135 | |
2623 | 2136 | =begin original |
2624 | 2137 | |
2625 | 2138 | In those 8 cases above, the value of EXPR is used directly as a boolean, so |
2626 | 2139 | no smartmatching is done. You may think of C<when> as a smartsmartmatch. |
2627 | 2140 | |
2628 | 2141 | =end original |
2629 | 2142 | |
2630 | 2143 | 上述の八つの場合、EXPR の値は直接真偽値として使われるため、 |
2631 | 2144 | スマートマッチングは行われません。 |
2632 | 2145 | スマートマッチングとして C<when> を考えるかもしれません。 |
2633 | 2146 | |
2634 | 2147 | =begin original |
2635 | 2148 | |
2636 | 2149 | Furthermore, Perl inspects the operands of logical operators to |
2637 | 2150 | decide whether to use smartmatching for each one by applying the |
2638 | 2151 | above test to the operands: |
2639 | 2152 | |
2640 | 2153 | =end original |
2641 | 2154 | |
2642 | 2155 | 更に、Perl はオペランドに上述のテストを適用することで、それぞれに |
2643 | 2156 | スマートマッチングを使うかどうかを決定するために論理演算子の |
2644 | 2157 | オペランドを調べます: |
2645 | 2158 | |
2646 | 2159 | =over 4 |
2647 | 2160 | |
2648 | 2161 | =item Z<>9. |
2649 | 2162 | |
2650 | 2163 | =begin original |
2651 | 2164 | |
2652 | 2165 | If EXPR is C<EXPR1 && EXPR2> or C<EXPR1 and EXPR2>, the test is applied |
2653 | 2166 | I<recursively> to both EXPR1 and EXPR2. |
2654 | 2167 | Only if I<both> operands also pass the |
2655 | 2168 | test, I<recursively>, will the expression be treated as boolean. Otherwise, |
2656 | 2169 | smartmatching is used. |
2657 | 2170 | |
2658 | 2171 | =end original |
2659 | 2172 | |
2660 | 2173 | EXPR が C<EXPR1 && EXPR2> または C<EXPR1 and EXPR2> の場合、テストは |
2661 | 2174 | EXPR1 と EXPR2 の両方に I<再帰的> に適用されます。 |
2662 | 2175 | I<両方の> オペランドがテストに I<再帰的に> 成功した場合にのみ、この式は |
2663 | 2176 | 真偽値として扱われます。 |
2664 | 2177 | さもなければ、スマートマッチングが使われます。 |
2665 | 2178 | |
2666 | 2179 | =item Z<>10. |
2667 | 2180 | |
2668 | 2181 | =begin original |
2669 | 2182 | |
2670 | 2183 | If EXPR is C<EXPR1 || EXPR2>, C<EXPR1 // EXPR2>, or C<EXPR1 or EXPR2>, the |
2671 | 2184 | test is applied I<recursively> to EXPR1 only (which might itself be a |
2672 | 2185 | higher-precedence AND operator, for example, and thus subject to the |
2673 | 2186 | previous rule), not to EXPR2. If EXPR1 is to use smartmatching, then EXPR2 |
2674 | 2187 | also does so, no matter what EXPR2 contains. But if EXPR2 does not get to |
2675 | 2188 | use smartmatching, then the second argument will not be either. This is |
2676 | 2189 | quite different from the C<&&> case just described, so be careful. |
2677 | 2190 | |
2678 | 2191 | =end original |
2679 | 2192 | |
2680 | 2193 | EXPR が C<EXPR1 || EXPR2>, C<EXPR1 // EXPR2>, or C<EXPR1 or EXPR2> の場合、 |
2681 | 2194 | テストは EXPR1 (例えば、より高い優先順位である AND 演算子; 従って |
2682 | 2195 | 前述の規則に従う)のみに対して I<再帰的に> 適用されます; EXPR2 には |
2683 | 2196 | 適用されません。 |
2684 | 2197 | EXPR1 がスマートマッチングを使うなら、EXPR2 も、その内容に関わらず |
2685 | 2198 | そうします。 |
2686 | 2199 | しかし EXPR2 がスマートマッチングを使わないなら、二番目の引数はどちらでも |
2687 | 2200 | ありません。 |
2688 | 2201 | これは既に記述した C<&&> の場合とはかなり異なりますので注意してください。 |
2689 | 2202 | |
2690 | 2203 | =back |
2691 | 2204 | |
2692 | 2205 | =begin original |
2693 | 2206 | |
2694 | 2207 | These rules are complicated, but the goal is for them to do what you want |
2695 | 2208 | (even if you don't quite understand why they are doing it). For example: |
2696 | 2209 | |
2697 | 2210 | =end original |
2698 | 2211 | |
2699 | 2212 | これらの規則は複雑ですが、この目標は (たとえあなたがなぜそうしているかを |
2700 | 2213 | 完全に理解していなくても) あなたが実行したい通りに実行することです。 |
2701 | 2214 | 例えば: |
2702 | 2215 | |
2703 | 2216 | when (/^\d+$/ && $_ < 75) { ... } |
2704 | 2217 | |
2705 | 2218 | =begin original |
2706 | 2219 | |
2707 | 2220 | will be treated as a boolean match because the rules say both |
2708 | 2221 | a regex match and an explicit test on C<$_> will be treated |
2709 | 2222 | as boolean. |
2710 | 2223 | |
2711 | 2224 | =end original |
2712 | 2225 | |
2713 | 2226 | これは真偽値マッチングとして扱われます; 規則では正規表現マッチングと |
2714 | 2227 | C<$_> への明示的なテストはどちらも真偽値として扱われるからです。 |
2715 | 2228 | |
2716 | 2229 | =begin original |
2717 | 2230 | |
2718 | 2231 | Also: |
2719 | 2232 | |
2720 | 2233 | =end original |
2721 | 2234 | |
2722 | 2235 | また: |
2723 | 2236 | |
2724 | 2237 | when ([qw(foo bar)] && /baz/) { ... } |
2725 | 2238 | |
2726 | 2239 | =begin original |
2727 | 2240 | |
2728 | 2241 | will use smartmatching because only I<one> of the operands is a boolean: |
2729 | 2242 | the other uses smartmatching, and that wins. |
2730 | 2243 | |
2731 | 2244 | =end original |
2732 | 2245 | |
2733 | 2246 | これはスマートマッチングを使います; オペランドの I<一つ> だけが |
2734 | 2247 | 真偽値だからです: もう片方はスマートマッチングを使うので、こちらが |
2735 | 2248 | 優先されます。 |
2736 | 2249 | |
2737 | 2250 | =begin original |
2738 | 2251 | |
2739 | 2252 | Further: |
2740 | 2253 | |
2741 | 2254 | =end original |
2742 | 2255 | |
2743 | 2256 | さらに: |
2744 | 2257 | |
2745 | 2258 | when ([qw(foo bar)] || /^baz/) { ... } |
2746 | 2259 | |
2747 | 2260 | =begin original |
2748 | 2261 | |
2749 | 2262 | will use smart matching (only the first operand is considered), whereas |
2750 | 2263 | |
2751 | 2264 | =end original |
2752 | 2265 | |
2753 | 2266 | これはスマートマッチングを使います(最初のオペランドのみが考慮されます); 一方 |
2754 | 2267 | |
2755 | 2268 | when (/^baz/ || [qw(foo bar)]) { ... } |
2756 | 2269 | |
2757 | 2270 | =begin original |
2758 | 2271 | |
2759 | 2272 | will test only the regex, which causes both operands to be |
2760 | 2273 | treated as boolean. Watch out for this one, then, because an |
2761 | 2274 | arrayref is always a true value, which makes it effectively |
2762 | 2275 | redundant. Not a good idea. |
2763 | 2276 | |
2764 | 2277 | =end original |
2765 | 2278 | |
2766 | 2279 | これは正規表現のみがテストされ、両方のオペランドは真偽値として |
2767 | 2280 | 扱われることになります。 |
2768 | 2281 | この場合、配列リファレンスは常に真の値なので、効率的に冗長になることに |
2769 | 2282 | 注目してください。 |
2770 | 2283 | 良い考えではありません。 |
2771 | 2284 | |
2772 | 2285 | =begin original |
2773 | 2286 | |
2774 | 2287 | Tautologous boolean operators are still going to be optimized |
2775 | 2288 | away. Don't be tempted to write |
2776 | 2289 | |
2777 | 2290 | =end original |
2778 | 2291 | |
2779 | 2292 | 恒久的な真偽値演算子は最適化されて除去されます。 |
2780 | 2293 | 以下のように書こうとしないでください |
2781 | 2294 | |
2782 | 2295 | when ("foo" or "bar") { ... } |
2783 | 2296 | |
2784 | 2297 | =begin original |
2785 | 2298 | |
2786 | 2299 | This will optimize down to C<"foo">, so C<"bar"> will never be considered (even |
2787 | 2300 | though the rules say to use a smartmatch |
2788 | 2301 | on C<"foo">). For an alternation like |
2789 | 2302 | this, an array ref will work, because this will instigate smartmatching: |
2790 | 2303 | |
2791 | 2304 | =end original |
2792 | 2305 | |
2793 | 2306 | これは C<"foo"> に最適化されるので、C<"bar"> は (たとえ規則では C<"foo"> に |
2794 | 2307 | スマートマッチングを使うとなっていたとしても) 考慮されることはありません。 |
2795 | 2308 | このような代替としては、配列リファレンスは動作します; これは |
2796 | 2309 | スマートマッチングを使わせるからです: |
2797 | 2310 | |
2798 | 2311 | when ([qw(foo bar)] { ... } |
2799 | 2312 | |
2800 | 2313 | =begin original |
2801 | 2314 | |
2802 | 2315 | This is somewhat equivalent to the C-style switch statement's fallthrough |
2803 | 2316 | functionality (not to be confused with I<Perl's> fallthrough |
2804 | 2317 | functionality--see below), wherein the same block is used for several |
2805 | 2318 | C<case> statements. |
2806 | 2319 | |
2807 | 2320 | =end original |
2808 | 2321 | |
2809 | 2322 | これはある意味 C スタイルの switch 文の次の条件への移動(fallthrough)機能と |
2810 | 2323 | 等価です(I<Perl の> 次の条件への移動機能と混同しないでください-- |
2811 | 2324 | 後述します); 複数の C<case> 文に同じブロックが使われます。 |
2812 | 2325 | |
2813 | 2326 | =begin original |
2814 | 2327 | |
2815 | 2328 | Another useful shortcut is that, if you use a literal array or hash as the |
2816 | 2329 | argument to C<given>, it is turned into a reference. So C<given(@foo)> is |
2817 | 2330 | the same as C<given(\@foo)>, for example. |
2818 | 2331 | |
2819 | 2332 | =end original |
2820 | 2333 | |
2821 | 2334 | その他の便利な省略記法としては、C<given> の引数としてリテラルな配列や |
2822 | 2335 | ハッシュを書くと、これはリファレンスに変化します。 |
2823 | 2336 | それで、例えば C<given(@foo)> は C<given(\@foo)> と同じです。 |
2824 | 2337 | |
2825 | 2338 | =begin original |
2826 | 2339 | |
2827 | 2340 | C<default> behaves exactly like C<when(1 == 1)>, which is |
2828 | 2341 | to say that it always matches. |
2829 | 2342 | |
2830 | 2343 | =end original |
2831 | 2344 | |
2832 | 2345 | C<default> は正確に C<when(1 == 1)> のように振る舞い、常に |
2833 | 2346 | マッチングします。 |
2834 | 2347 | |
2835 | 2348 | =head3 Breaking out |
2836 | 2349 | |
2837 | 2350 | (脱出) |
2838 | 2351 | |
2839 | 2352 | =begin original |
2840 | 2353 | |
2841 | 2354 | You can use the C<break> keyword to break out of the enclosing |
2842 | 2355 | C<given> block. Every C<when> block is implicitly ended with |
2843 | 2356 | a C<break>. |
2844 | 2357 | |
2845 | 2358 | =end original |
2846 | 2359 | |
2847 | 2360 | 囲まれている C<given> ブロックから脱出するために、C<break> キーワードが |
2848 | 2361 | 使えます。 |
2849 | 2362 | 全ての C<when> ブロックの末尾には暗黙に C<break> があります。 |
2850 | 2363 | |
2851 | 2364 | =head3 Fall-through |
2852 | 2365 | |
2853 | 2366 | (次の条件への移動(Fall-through)) |
2854 | 2367 | |
2855 | 2368 | =begin original |
2856 | 2369 | |
2857 | 2370 | You can use the C<continue> keyword to fall through from one |
2858 | case to the next | |
2371 | case to the next: | |
2859 | 2372 | |
2860 | 2373 | =end original |
2861 | 2374 | |
2862 | ||
2375 | 一つの条件から次へ移動するためには、C<continue> キーワードが使えます: | |
2863 | C<continue> を使えます: | |
2864 | 2376 | |
2865 | 2377 | given($foo) { |
2866 | ||
2378 | when (/x/) { say '$foo contains an x'; continue } | |
2867 | ||
2379 | when (/y/) { say '$foo contains a y' } | |
2868 | ||
2380 | default { say '$foo does not contain a y' } | |
2869 | 2381 | } |
2870 | 2382 | |
2871 | 2383 | =head3 Return value |
2872 | 2384 | |
2873 | 2385 | (返り値) |
2874 | 2386 | |
2875 | 2387 | =begin original |
2876 | 2388 | |
2877 | 2389 | When a C<given> statement is also a valid expression (for example, |
2878 | 2390 | when it's the last statement of a block), it evaluates to: |
2879 | 2391 | |
2880 | 2392 | =end original |
2881 | 2393 | |
2882 | 2394 | C<given> が有効な式でもある(例えばブロックの最後の文である)場合、 |
2883 | 2395 | 以下のように評価されます: |
2884 | 2396 | |
2885 | 2397 | =over 4 |
2886 | 2398 | |
2887 | 2399 | =item * |
2888 | 2400 | |
2889 | 2401 | =begin original |
2890 | 2402 | |
2891 | 2403 | An empty list as soon as an explicit C<break> is encountered. |
2892 | 2404 | |
2893 | 2405 | =end original |
2894 | 2406 | |
2895 | 2407 | 明示的な C<break> に遭遇した直後なら空リスト。 |
2896 | 2408 | |
2897 | 2409 | =item * |
2898 | 2410 | |
2899 | 2411 | =begin original |
2900 | 2412 | |
2901 | 2413 | The value of the last evaluated expression of the successful |
2902 | 2414 | C<when>/C<default> clause, if there happens to be one. |
2903 | 2415 | |
2904 | 2416 | =end original |
2905 | 2417 | |
2906 | 2418 | もし成功していれば、成功した C<when>/C<default> 節で最後に評価された式の値。 |
2907 | 2419 | |
2908 | 2420 | =item * |
2909 | 2421 | |
2910 | 2422 | =begin original |
2911 | 2423 | |
2912 | 2424 | The value of the last evaluated expression of the C<given> block if no |
2913 | 2425 | condition is true. |
2914 | 2426 | |
2915 | 2427 | =end original |
2916 | 2428 | |
2917 | 2429 | どの条件も真でなければ C<given> ブロックで最後に評価された式の値。 |
2918 | 2430 | |
2919 | 2431 | =back |
2920 | 2432 | |
2921 | 2433 | =begin original |
2922 | 2434 | |
2923 | 2435 | In both last cases, the last expression is evaluated in the context that |
2924 | 2436 | was applied to the C<given> block. |
2925 | 2437 | |
2926 | 2438 | =end original |
2927 | 2439 | |
2928 | 2440 | 最後の二つの場合、最後の式は適用された C<given> ブロックに適用された |
2929 | 2441 | コンテキストで評価されます。 |
2930 | 2442 | |
2931 | 2443 | =begin original |
2932 | 2444 | |
2933 | 2445 | Note that, unlike C<if> and C<unless>, failed C<when> statements always |
2934 | 2446 | evaluate to an empty list. |
2935 | 2447 | |
2936 | 2448 | =end original |
2937 | 2449 | |
2938 | 2450 | C<if> や C<unless> と異なり、失敗した C<when> 文は常に空リストに |
2939 | 2451 | 評価されます。 |
2940 | 2452 | |
2941 | 2453 | my $price = do { |
2942 | ||
2454 | given ($item) { | |
2943 | ||
2455 | when (["pear", "apple"]) { 1 } | |
2944 | ||
2456 | break when "vote"; # My vote cannot be bought | |
2945 | ||
2457 | 1e10 when /Mona Lisa/; | |
2946 | ||
2458 | "unknown"; | |
2947 | ||
2459 | } | |
2948 | 2460 | }; |
2949 | 2461 | |
2950 | 2462 | =begin original |
2951 | 2463 | |
2952 | 2464 | Currently, C<given> blocks can't always |
2953 | 2465 | be used as proper expressions. This |
2954 | 2466 | may be addressed in a future version of Perl. |
2955 | 2467 | |
2956 | 2468 | =end original |
2957 | 2469 | |
2958 | 2470 | 現在のところ、C<given> ブロックは常に適切な式として使うことはできません。 |
2959 | 2471 | これは将来のバージョンの Perl に対処されるでしょう。 |
2960 | 2472 | |
2961 | 2473 | =head3 Switching in a loop |
2962 | 2474 | |
2963 | 2475 | (ループ内の switch) |
2964 | 2476 | |
2965 | 2477 | =begin original |
2966 | 2478 | |
2967 | 2479 | Instead of using C<given()>, you can use a C<foreach()> loop. |
2968 | 2480 | For example, here's one way to count how many times a particular |
2969 | 2481 | string occurs in an array: |
2970 | 2482 | |
2971 | 2483 | =end original |
2972 | 2484 | |
2973 | 2485 | C<given()> を使う代わりに、C<foreach()> ループを使えます。 |
2974 | 2486 | たとえば、以下は配列内に特定の文字列が何回現れるかを数えるための |
2975 | 2487 | ひとつの方法です: |
2976 | 2488 | |
2977 | 2489 | use v5.10.1; |
2978 | 2490 | my $count = 0; |
2979 | 2491 | for (@array) { |
2980 | ||
2492 | when ("foo") { ++$count } | |
2981 | 2493 | } |
2982 | 2494 | print "\@array contains $count copies of 'foo'\n"; |
2983 | 2495 | |
2984 | 2496 | =begin original |
2985 | 2497 | |
2986 | 2498 | Or in a more recent version: |
2987 | 2499 | |
2988 | 2500 | =end original |
2989 | 2501 | |
2990 | 2502 | あるいはより最近のバージョンでは: |
2991 | 2503 | |
2992 | 2504 | use v5.14; |
2993 | 2505 | my $count = 0; |
2994 | 2506 | for (@array) { |
2995 | ||
2507 | ++$count when "foo"; | |
2996 | 2508 | } |
2997 | 2509 | print "\@array contains $count copies of 'foo'\n"; |
2998 | 2510 | |
2999 | 2511 | =begin original |
3000 | 2512 | |
3001 | 2513 | At the end of all C<when> blocks, there is an implicit C<next>. |
3002 | 2514 | You can override that with an explicit C<last> if you're |
3003 | 2515 | interested in only the first match alone. |
3004 | 2516 | |
3005 | 2517 | =end original |
3006 | 2518 | |
3007 | 2519 | C<when> ブロックの末尾に、暗黙の C<next> があります。 |
3008 | 2520 | もし最初のマッチングだけに興味があるなら、明示的な C<last> でこれを |
3009 | 2521 | 上書きできます。 |
3010 | 2522 | |
3011 | 2523 | =begin original |
3012 | 2524 | |
3013 | 2525 | This doesn't work if you explicitly specify a loop variable, as |
3014 | 2526 | in C<for $item (@array)>. You have to use the default variable C<$_>. |
3015 | 2527 | |
3016 | 2528 | =end original |
3017 | 2529 | |
3018 | 2530 | これは、C<for $item (@array)> のように明示的にループ変数を指定した場合は |
3019 | 2531 | 動作しません。 |
3020 | 2532 | デフォルト変数 C<$_> を使う必要があります。 |
3021 | 2533 | |
3022 | =head3 Differences from | |
2534 | =head3 Differences from Perl 6 | |
3023 | 2535 | |
3024 | ( | |
2536 | (Perl 6 からの違い) | |
3025 | 2537 | |
3026 | 2538 | =begin original |
3027 | 2539 | |
3028 | 2540 | The Perl 5 smartmatch and C<given>/C<when> constructs are not compatible |
3029 | with their | |
2541 | with their Perl 6 analogues. The most visible difference and least | |
3030 | 2542 | important difference is that, in Perl 5, parentheses are required around |
3031 | 2543 | the argument to C<given()> and C<when()> (except when this last one is used |
3032 | as a statement modifier). Parentheses in | |
2544 | as a statement modifier). Parentheses in Perl 6 are always optional in a | |
3033 | 2545 | control construct such as C<if()>, C<while()>, or C<when()>; they can't be |
3034 | 2546 | made optional in Perl 5 without a great deal of potential confusion, |
3035 | 2547 | because Perl 5 would parse the expression |
3036 | 2548 | |
3037 | 2549 | =end original |
3038 | 2550 | |
3039 | Perl 5 のスマートマッチングと C<given>/C<when> 構文は | |
2551 | Perl 5 のスマートマッチングと C<given>/C<when> 構文は Perl 6 のものと | |
3040 | 2552 | 互換性はありません。 |
3041 | 2553 | もっとも目に見えて、もっとも重要でない違いは、Perl 5 では、C<given()> と |
3042 | 2554 | C<when()> の引数は (後者を文修飾子として使う場合を除いて)かっこでくくる |
3043 | 2555 | 必要があります。 |
3044 | ||
2556 | Perl 6 では、C<if()>, C<while()>, C<when()> のような制御構造での | |
3045 | 2557 | かっこは常に省略可能です; |
3046 | 2558 | Perl 5 では、潜在的な大混乱と引き換えにしなければこれを省略できません; |
3047 | 2559 | なぜなら Perl 5 は以下のような表現において: |
3048 | 2560 | |
3049 | 2561 | given $foo { |
3050 | ||
2562 | ... | |
3051 | 2563 | } |
3052 | 2564 | |
3053 | 2565 | =begin original |
3054 | 2566 | |
3055 | 2567 | as though the argument to C<given> were an element of the hash |
3056 | 2568 | C<%foo>, interpreting the braces as hash-element syntax. |
3057 | 2569 | |
3058 | 2570 | =end original |
3059 | 2571 | |
3060 | 2572 | C<given> の引数はハッシュ C<%foo> の要素であるかのようにパースして、 |
3061 | 2573 | 中かっこをハッシュ要素文法として解釈するからです。 |
3062 | 2574 | |
3063 | 2575 | =begin original |
3064 | 2576 | |
3065 | However, ther | |
2577 | However, their are many, many other differences. For example, | |
3066 | 2578 | this works in Perl 5: |
3067 | 2579 | |
3068 | 2580 | =end original |
3069 | 2581 | |
3070 | 2582 | しかし、ここにはとても多くのその他の違いがあります。 |
3071 | 2583 | 例えば、これは Perl 5 で動作します: |
3072 | 2584 | |
3073 | 2585 | use v5.12; |
3074 | 2586 | my @primary = ("red", "blue", "green"); |
3075 | 2587 | |
3076 | 2588 | if (@primary ~~ "red") { |
3077 | 2589 | say "primary smartmatches red"; |
3078 | 2590 | } |
3079 | 2591 | |
3080 | 2592 | if ("red" ~~ @primary) { |
3081 | 2593 | say "red smartmatches primary"; |
3082 | 2594 | } |
3083 | 2595 | |
3084 | 2596 | say "that's all, folks!"; |
3085 | 2597 | |
3086 | 2598 | =begin original |
3087 | 2599 | |
3088 | But it doesn't work at all in | |
2600 | But it doesn't work at all in Perl 6. Instead, you should | |
3089 | 2601 | use the (parallelizable) C<any> operator: |
3090 | 2602 | |
3091 | 2603 | =end original |
3092 | 2604 | |
3093 | しかしこれは | |
2605 | しかしこれは Perl 6 では全く動作しません。 | |
3094 | 2606 | 代わりに、(並列化可能な) C<any> 演算子を使います: |
3095 | 2607 | |
3096 | 2608 | if any(@primary) eq "red" { |
3097 | 2609 | say "primary smartmatches red"; |
3098 | 2610 | } |
3099 | 2611 | |
3100 | 2612 | if "red" eq any(@primary) { |
3101 | 2613 | say "red smartmatches primary"; |
3102 | 2614 | } |
3103 | 2615 | |
3104 | 2616 | =begin original |
3105 | 2617 | |
3106 | 2618 | The table of smartmatches in L<perlop/"Smartmatch Operator"> is not |
3107 | identical to that proposed by the | |
2619 | identical to that proposed by the Perl 6 specification, mainly due to | |
3108 | differences between | |
2620 | differences between Perl 6's and Perl 5's data models, but also because | |
3109 | the | |
2621 | the Perl 6 spec has changed since Perl 5 rushed into early adoption. | |
3110 | 2622 | |
3111 | 2623 | =end original |
3112 | 2624 | |
3113 | L<perlop/"Smartmatch Operator"> のスマートマッチングの表は | |
2625 | L<perlop/"Smartmatch Operator"> のスマートマッチングの表は Perl 6 仕様で | |
3114 | 提案されれているものと同一ではありません; 主に | |
2626 | 提案されれているものと同一ではありません; 主に Perl 6 と Perl 5 の | |
3115 | データモデルの違いによりますが、 | |
2627 | データモデルの違いによりますが、Perl 6 の仕様は Perl 5 が早期に採用した | |
3116 | 2628 | 後に変更されたからです。 |
3117 | 2629 | |
3118 | 2630 | =begin original |
3119 | 2631 | |
3120 | In | |
2632 | In Perl 6, C<when()> will always do an implicit smartmatch with its | |
3121 | 2633 | argument, while in Perl 5 it is convenient (albeit potentially confusing) to |
3122 | 2634 | suppress this implicit smartmatch in various rather loosely-defined |
3123 | 2635 | situations, as roughly outlined above. (The difference is largely because |
3124 | 2636 | Perl 5 does not have, even internally, a boolean type.) |
3125 | 2637 | |
3126 | 2638 | =end original |
3127 | 2639 | |
3128 | ||
2640 | Perl 6 では、C<when()> は常にその引数に対する暗黙のスマートマッチングを | |
3129 | 2641 | 行いますが、Perl 5 では既に大まかに示した通り、ゆるく定義された状況によっては |
3130 | 2642 | 暗黙のスマートマッチングを抑制したほうが便利(たとえ混乱させるかも |
3131 | 2643 | 知れないにしても)です。 |
3132 | 2644 | (主な違いは、Perl 5 は内部的にさえ真偽値型を持たないことによります。) |
3133 | 2645 | |
3134 | 2646 | =cut |
3135 | 2647 | |
3136 | 2648 | =begin meta |
3137 | 2649 | |
3138 | 2650 | Translate: KIMURA Koichi (5.005_03) |
3139 | 2651 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.8.8-) |
3140 | Status: | |
2652 | Status: completed | |
3141 | 2653 | |
3142 | 2654 | =end meta |