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