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