perlop >
5.10.1
との差分
perlop 5.10.1 と 5.14.1 の差分
1 | 1 | |
2 | 2 | =encoding euc-jp |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | X<operator> |
6 | 6 | |
7 | 7 | =begin original |
8 | 8 | |
9 | 9 | perlop - Perl operators and precedence |
10 | 10 | |
11 | 11 | =end original |
12 | 12 | |
13 | 13 | perlop - Perl の演算子と優先順位 |
14 | 14 | |
15 | 15 | =head1 DESCRIPTION |
16 | 16 | |
17 | 17 | =head2 Operator Precedence and Associativity |
18 | 18 | X<operator, precedence> X<precedence> X<associativity> |
19 | 19 | |
20 | 20 | (演算子の優先順位と結合性) |
21 | 21 | |
22 | 22 | =begin original |
23 | 23 | |
24 | 24 | Operator precedence and associativity work in Perl more or less like |
25 | 25 | they do in mathematics. |
26 | 26 | |
27 | 27 | =end original |
28 | 28 | |
29 | 29 | Perl での演算子の優先順位と結合性は多かれ少なかれ数学のものと似ています。 |
30 | 30 | |
31 | 31 | =begin original |
32 | 32 | |
33 | 33 | I<Operator precedence> means some operators are evaluated before |
34 | 34 | others. For example, in C<2 + 4 * 5>, the multiplication has higher |
35 | 35 | precedence so C<4 * 5> is evaluated first yielding C<2 + 20 == |
36 | 36 | 22> and not C<6 * 5 == 30>. |
37 | 37 | |
38 | 38 | =end original |
39 | 39 | |
40 | 40 | I<演算子の優先順位> とは、他の演算子より先に評価される演算子が |
41 | 41 | あるということです。 |
42 | 42 | 例えば、C<2 + 4 * 5> の場合、乗算が高い優先順位を持っているので、 |
43 | 43 | C<4 * 5> が先に評価され、結果は C<6 * 5 == 30> ではなく、 |
44 | 44 | C<2 + 20 == 22> となります。 |
45 | 45 | |
46 | 46 | =begin original |
47 | 47 | |
48 | 48 | I<Operator associativity> defines what happens if a sequence of the |
49 | 49 | same operators is used one after another: whether the evaluator will |
50 | 50 | evaluate the left operations first or the right. For example, in C<8 |
51 | 51 | - 4 - 2>, subtraction is left associative so Perl evaluates the |
52 | 52 | expression left to right. C<8 - 4> is evaluated first making the |
53 | 53 | expression C<4 - 2 == 2> and not C<8 - 2 == 6>. |
54 | 54 | |
55 | 55 | =end original |
56 | 56 | |
57 | 57 | I<演算子の結合性> は、同じ演算子が連続して現れた場合に何が起こるかを |
58 | 58 | 定義します: 評価器が左側を先に評価するか右側を先に評価するかです。 |
59 | 59 | 例えば、C<8 - 4 - 2> の場合、減算は左結合なので、Perl は式を左から |
60 | 60 | 右に評価します。 |
61 | 61 | C<8 - 4> が先に評価されるので、C<8 - 2 == 6> ではなく |
62 | 62 | C<4 - 2 == 2> となります。 |
63 | 63 | |
64 | 64 | =begin original |
65 | 65 | |
66 | 66 | Perl operators have the following associativity and precedence, |
67 | 67 | listed from highest precedence to lowest. Operators borrowed from |
68 | 68 | C keep the same precedence relationship with each other, even where |
69 | 69 | C's precedence is slightly screwy. (This makes learning Perl easier |
70 | 70 | for C folks.) With very few exceptions, these all operate on scalar |
71 | 71 | values only, not array values. |
72 | 72 | |
73 | 73 | =end original |
74 | 74 | |
75 | 75 | Perl の演算子には、以下のような結合性と優先順位 (高い優先順位から |
76 | 76 | 低いものへ並べている) があります。 |
77 | 77 | C から持ってきた演算子の優先順位は、C での優先順位が多少おかしくても、 |
78 | 78 | そのままにしてあります。 |
79 | 79 | (これによって、C を使っている方が Perl に移りやすくなっています。) |
80 | 80 | ごく僅かな例外を別として、全ての演算子はスカラ値のみを持ち、 |
81 | 81 | 配列値を持ちません。 |
82 | 82 | |
83 | 83 | =begin original |
84 | 84 | |
85 | 85 | left terms and list operators (leftward) |
86 | 86 | left -> |
87 | 87 | nonassoc ++ -- |
88 | 88 | right ** |
89 | 89 | right ! ~ \ and unary + and - |
90 | 90 | left =~ !~ |
91 | 91 | left * / % x |
92 | 92 | left + - . |
93 | 93 | left << >> |
94 | 94 | nonassoc named unary operators |
95 | 95 | nonassoc < > <= >= lt gt le ge |
96 | 96 | nonassoc == != <=> eq ne cmp ~~ |
97 | 97 | left & |
98 | 98 | left | ^ |
99 | 99 | left && |
100 | 100 | left || // |
101 | 101 | nonassoc .. ... |
102 | 102 | right ?: |
103 | 103 | right = += -= *= etc. |
104 | 104 | left , => |
105 | 105 | nonassoc list operators (rightward) |
106 | 106 | right not |
107 | 107 | left and |
108 | 108 | left or xor |
109 | 109 | |
110 | 110 | =end original |
111 | 111 | |
112 | 112 | 左結合 項 リスト演算子 (左方向に対して) |
113 | 113 | 左結合 -> |
114 | 114 | 非結合 ++ -- |
115 | 115 | 右結合 ** |
116 | 116 | 右結合 ! ~ \ 単項の+ 単項の- |
117 | 117 | 左結合 =~ !~ |
118 | 118 | 左結合 * / % x |
119 | 119 | 左結合 + - . |
120 | 120 | 左結合 << >> |
121 | 121 | 非結合 名前付き単項演算子 |
122 | 122 | 非結合 < > <= >= lt gt le ge |
123 | 123 | 非結合 == != <=> eq ne cmp ~~ |
124 | 124 | 左結合 & |
125 | 125 | 左結合 | ^ |
126 | 126 | 左結合 && |
127 | 127 | 左結合 || // |
128 | 128 | 非結合 .. ... |
129 | 129 | 右結合 ?: |
130 | 130 | 右結合 = += -= *= などの代入演算子 |
131 | 131 | 左結合 , => |
132 | 132 | 非結合 リスト演算子 (右方向に対して) |
133 | 133 | 右結合 not |
134 | 134 | 左結合 and |
135 | 135 | 左結合 or xor |
136 | 136 | |
137 | 137 | =begin original |
138 | 138 | |
139 | 139 | In the following sections, these operators are covered in precedence order. |
140 | 140 | |
141 | 141 | =end original |
142 | 142 | |
143 | 143 | 以下の章では、演算子は優先順位の順に記述されています。 |
144 | 144 | |
145 | 145 | =begin original |
146 | 146 | |
147 | 147 | Many operators can be overloaded for objects. See L<overload>. |
148 | 148 | |
149 | 149 | =end original |
150 | 150 | |
151 | 151 | 多くの演算子はオブジェクトでオーバーロードできます。 |
152 | 152 | L<overload> を参照して下さい。 |
153 | 153 | |
154 | 154 | =head2 Terms and List Operators (Leftward) |
155 | 155 | X<list operator> X<operator, list> X<term> |
156 | 156 | |
157 | 157 | (項とリスト演算子 (左方向)) |
158 | 158 | |
159 | 159 | =begin original |
160 | 160 | |
161 | 161 | A TERM has the highest precedence in Perl. They include variables, |
162 | 162 | quote and quote-like operators, any expression in parentheses, |
163 | 163 | and any function whose arguments are parenthesized. Actually, there |
164 | 164 | aren't really functions in this sense, just list operators and unary |
165 | 165 | operators behaving as functions because you put parentheses around |
166 | 166 | the arguments. These are all documented in L<perlfunc>. |
167 | 167 | |
168 | 168 | =end original |
169 | 169 | |
170 | 170 | 「項」は Perl でもっとも優先順位が高いものです。 |
171 | 171 | これには、変数、クォートとクォート的な演算子、括弧で括った任意の式、 |
172 | 172 | 引数を括弧で括った任意の関数が含まれます。 |
173 | 173 | 実際には、この意味では本当の関数はなく、リスト演算子と関数のように働く |
174 | 174 | 単項演算子が、引数を括弧で括るためそのように見えます。 |
175 | 175 | これらはすべて L<perlfunc> に記述しています。 |
176 | 176 | |
177 | 177 | =begin original |
178 | 178 | |
179 | 179 | If any list operator (print(), etc.) or any unary operator (chdir(), etc.) |
180 | 180 | is followed by a left parenthesis as the next token, the operator and |
181 | 181 | arguments within parentheses are taken to be of highest precedence, |
182 | 182 | just like a normal function call. |
183 | 183 | |
184 | 184 | =end original |
185 | 185 | |
186 | ||
186 | リスト演算子 (print() など) や単項演算子 (chdir() など) は、 | |
187 | ||
187 | すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、 | |
188 | 通常の関数呼び出しのように | |
188 | 通常の関数呼び出しのようにもっとも高い優先順位として扱われます。 | |
189 | 189 | |
190 | 190 | =begin original |
191 | 191 | |
192 | 192 | In the absence of parentheses, the precedence of list operators such as |
193 | 193 | C<print>, C<sort>, or C<chmod> is either very high or very low depending on |
194 | 194 | whether you are looking at the left side or the right side of the operator. |
195 | 195 | For example, in |
196 | 196 | |
197 | 197 | =end original |
198 | 198 | |
199 | 199 | 括弧が無い場合には、C<print>、C<sort>、C<chmod> のようなリスト演算子の |
200 | 200 | 優先順位は、演算子の左側をからすると非常に高く、右側からすると |
201 | 201 | 非常に低く見えます。たとえば、 |
202 | 202 | |
203 | 203 | @ary = (1, 3, sort 4, 2); |
204 | 204 | print @ary; # prints 1324 |
205 | 205 | |
206 | 206 | =begin original |
207 | 207 | |
208 | 208 | the commas on the right of the sort are evaluated before the sort, |
209 | 209 | but the commas on the left are evaluated after. In other words, |
210 | 210 | list operators tend to gobble up all arguments that follow, and |
211 | 211 | then act like a simple TERM with regard to the preceding expression. |
212 | 212 | Be careful with parentheses: |
213 | 213 | |
214 | 214 | =end original |
215 | 215 | |
216 | 216 | では、sort の右のコンマは sort よりも前に評価されます (右側から見ると |
217 | 217 | sort の優先順位が低い) が、左側のコンマは sort のあとに評価されます |
218 | 218 | (左側から見ると sort の方が優先順位が高くなっている)。 |
219 | 219 | 言い方を変えると、リスト演算子は自分の後にある引数をすべて使って処理を行ない、 |
220 | 220 | その結果を自分の前の式に対する「項」であるかのように見せるということです。 |
221 | 221 | ただし、括弧には気を付けないといけません: |
222 | 222 | |
223 | 223 | =begin original |
224 | 224 | |
225 | 225 | # These evaluate exit before doing the print: |
226 | 226 | print($foo, exit); # Obviously not what you want. |
227 | 227 | print $foo, exit; # Nor is this. |
228 | 228 | |
229 | 229 | =end original |
230 | 230 | |
231 | 231 | # 以下は print を行なう前に exit を評価します: |
232 | 232 | print($foo, exit); # 明らかにやりたいことではないでしょう。 |
233 | 233 | print $foo, exit; # これでもない。 |
234 | 234 | |
235 | 235 | =begin original |
236 | 236 | |
237 | 237 | # These do the print before evaluating exit: |
238 | 238 | (print $foo), exit; # This is what you want. |
239 | 239 | print($foo), exit; # Or this. |
240 | 240 | print ($foo), exit; # Or even this. |
241 | 241 | |
242 | 242 | =end original |
243 | 243 | |
244 | 244 | # 以下は exit を評価する前に print を行ないます: |
245 | 245 | (print $foo), exit; # これがしたかった。 |
246 | 246 | print($foo), exit; # これでもいい。 |
247 | 247 | print ($foo), exit; # これも OK。 |
248 | 248 | |
249 | 249 | =begin original |
250 | 250 | |
251 | 251 | Also note that |
252 | 252 | |
253 | 253 | =end original |
254 | 254 | |
255 | 255 | また、 |
256 | 256 | |
257 | 257 | print ($foo & 255) + 1, "\n"; |
258 | 258 | |
259 | 259 | =begin original |
260 | 260 | |
261 | 261 | probably doesn't do what you expect at first glance. The parentheses |
262 | 262 | enclose the argument list for C<print> which is evaluated (printing |
263 | 263 | the result of C<$foo & 255>). Then one is added to the return value |
264 | 264 | of C<print> (usually 1). The result is something like this: |
265 | 265 | |
266 | 266 | =end original |
267 | 267 | |
268 | 268 | の動作を一目見ただけで判断するのは、難しいでしょう。 |
269 | 269 | かっこは C<print> のために評価される引数リストを囲っています |
270 | 270 | (C<$foo & 255> の結果が表示されます)。 |
271 | 271 | それから C<print> の返り値 (通常は 1) に 1 が加えられます。 |
272 | 272 | 結果は以下のようになります: |
273 | 273 | |
274 | =begin original | |
275 | ||
274 | 276 | 1 + 1, "\n"; # Obviously not what you meant. |
275 | 277 | |
278 | =end original | |
279 | ||
280 | 1 + 1, "\n"; # 明らかにやりたいことではないでしょう。 | |
281 | ||
276 | 282 | =begin original |
277 | 283 | |
278 | 284 | To do what you meant properly, you must write: |
279 | 285 | |
280 | 286 | =end original |
281 | 287 | |
282 | 288 | 意味したいことを適切に行うには、以下のように書く必要があります: |
283 | 289 | |
284 | 290 | print(($foo & 255) + 1, "\n"); |
285 | 291 | |
286 | 292 | =begin original |
287 | 293 | |
288 | 294 | See L<Named Unary Operators> for more discussion of this. |
289 | 295 | |
290 | 296 | =end original |
291 | 297 | |
292 | 298 | 詳しくは、L<Named Unary Operators> を参照してください。 |
293 | 299 | |
294 | 300 | =begin original |
295 | 301 | |
296 | 302 | Also parsed as terms are the C<do {}> and C<eval {}> constructs, as |
297 | 303 | well as subroutine and method calls, and the anonymous |
298 | 304 | constructors C<[]> and C<{}>. |
299 | 305 | |
300 | 306 | =end original |
301 | 307 | |
302 | 308 | この他に「項」として解析されるものには、C<do {}> や C<eval {}> の |
303 | 309 | 構成、サブルーティンやメソッドの呼び出し、無名のコンストラクタ |
304 | 310 | C<[]> と C<{}> があります。 |
305 | 311 | |
306 | 312 | =begin original |
307 | 313 | |
308 | 314 | See also L<Quote and Quote-like Operators> toward the end of this section, |
309 | 315 | as well as L</"I/O Operators">. |
310 | 316 | |
311 | 317 | =end original |
312 | 318 | |
313 | 319 | 後の方のL<Quote and Quote-like Operators>や |
314 | 320 | L</"I/O Operators">も参照してください。 |
315 | 321 | |
316 | 322 | =head2 The Arrow Operator |
317 | 323 | X<arrow> X<dereference> X<< -> >> |
318 | 324 | |
319 | 325 | (矢印演算子) |
320 | 326 | |
321 | 327 | =begin original |
322 | 328 | |
323 | 329 | "C<< -> >>" is an infix dereference operator, just as it is in C |
324 | 330 | and C++. If the right side is either a C<[...]>, C<{...}>, or a |
325 | 331 | C<(...)> subscript, then the left side must be either a hard or |
326 | 332 | symbolic reference to an array, a hash, or a subroutine respectively. |
327 | 333 | (Or technically speaking, a location capable of holding a hard |
328 | 334 | reference, if it's an array or hash reference being used for |
329 | 335 | assignment.) See L<perlreftut> and L<perlref>. |
330 | 336 | |
331 | 337 | =end original |
332 | 338 | |
333 | 339 | C や C++ と同じように "C<< -> >>" は中置の被参照演算子です。 |
334 | 340 | 右側が C<[...]>, C<{...}>, |
335 | 341 | C<(...)> のいずれかの形の添字であれば、左側は配列、ハッシュ、 |
336 | サブルーチンへのハードリファレンスかシンボリックリファレンス | |
342 | サブルーチンへのハードリファレンスかシンボリックリファレンスでなければなりません。 | |
337 | 技術的には、配列またはハードリファレンスが代入可能であれば | |
343 | (あるいは技術的には、配列またはハードリファレンスが代入可能であれば | |
338 | ハードリファレンスを保持できる場所) | |
344 | ハードリファレンスを保持できる場所です。) | |
339 | ||
345 | L<perlreftut> と L<perlref> を参照してください。 | |
340 | 346 | |
341 | 347 | =begin original |
342 | 348 | |
343 | 349 | Otherwise, the right side is a method name or a simple scalar |
344 | 350 | variable containing either the method name or a subroutine reference, |
345 | 351 | and the left side must be either an object (a blessed reference) |
346 | 352 | or a class name (that is, a package name). See L<perlobj>. |
347 | 353 | |
348 | 354 | =end original |
349 | 355 | |
350 | 356 | そうでなければ、右側はメソッド名かサブルーチンのリファレンスを持った |
351 | 357 | 単純スカラ変数で、左側はオブジェクト (bless されたリファレンス) か |
352 | 358 | クラス名でなければなりません。 |
353 | 359 | L<perlobj> を参照してください。 |
354 | 360 | |
355 | 361 | =head2 Auto-increment and Auto-decrement |
356 | 362 | X<increment> X<auto-increment> X<++> X<decrement> X<auto-decrement> X<--> |
357 | 363 | |
358 | 364 | (インクリメントとデクリメント) |
359 | 365 | |
360 | 366 | =begin original |
361 | 367 | |
362 | 368 | "++" and "--" work as in C. That is, if placed before a variable, |
363 | 369 | they increment or decrement the variable by one before returning the |
364 | 370 | value, and if placed after, increment or decrement after returning the |
365 | 371 | value. |
366 | 372 | |
367 | 373 | =end original |
368 | 374 | |
369 | 375 | "++" と "--" は、C の場合と同じように動作します。 |
370 | 376 | つまり、変数の前に置かれれば、値を返す前に変数を 1 インクリメントまたは |
371 | 377 | デクリメントし、後に置かれれば、値を返した後で変数を |
372 | 378 | インクリメントまたはデクリメントします。 |
373 | 379 | |
374 | 380 | $i = 0; $j = 0; |
375 | 381 | print $i++; # prints 0 |
376 | 382 | print ++$j; # prints 1 |
377 | 383 | |
378 | 384 | =begin original |
379 | 385 | |
380 | 386 | Note that just as in C, Perl doesn't define B<when> the variable is |
381 | 387 | incremented or decremented. You just know it will be done sometime |
382 | 388 | before or after the value is returned. This also means that modifying |
383 | a variable twice in the same statement will lead to undefined behavio | |
389 | a variable twice in the same statement will lead to undefined behavior. | |
384 | 390 | Avoid statements like: |
385 | 391 | |
386 | 392 | =end original |
387 | 393 | |
388 | 394 | C と同様、Perl は B<いつ> 変数がインクリメントまたはデクリメントされるかは |
389 | 395 | 定義されません。 |
390 | 396 | 値が返される前か後のどこかで行われる、ということだけがわかります。 |
391 | 397 | これは、同じ文である変数を 2 回修正すると、振る舞いが未定義になることを |
392 | 398 | 意味します。 |
393 | 399 | 以下のような文は避けてください: |
394 | 400 | |
395 | 401 | $i = $i ++; |
396 | 402 | print ++ $i + $i ++; |
397 | 403 | |
398 | 404 | =begin original |
399 | 405 | |
400 | 406 | Perl will not guarantee what the result of the above statements is. |
401 | 407 | |
402 | 408 | =end original |
403 | 409 | |
404 | 410 | Perl は上記の文の結果について保障しません。 |
405 | 411 | |
406 | 412 | =begin original |
407 | 413 | |
408 | 414 | The auto-increment operator has a little extra builtin magic to it. If |
409 | 415 | you increment a variable that is numeric, or that has ever been used in |
410 | 416 | a numeric context, you get a normal increment. If, however, the |
411 | 417 | variable has been used in only string contexts since it was set, and |
412 | 418 | has a value that is not the empty string and matches the pattern |
413 | 419 | C</^[a-zA-Z]*[0-9]*\z/>, the increment is done as a string, preserving each |
414 | 420 | character within its range, with carry: |
415 | 421 | |
416 | 422 | =end original |
417 | 423 | |
418 | 424 | インクリメント演算子には、ちょっと風変わりな機能が組み込まれています。 |
419 | 425 | 数値が入った変数や、数値の文脈で使われてきた変数を |
420 | 426 | インクリメントする場合には、通常のインクリメントとして動作します。 |
421 | 427 | しかし、その変数が設定されてからずっと文字列の文脈で |
422 | 428 | しか使われていなくて、空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする |
423 | 429 | 値を持っているときには、個々の文字の範囲を保ちながら桁あげを行なって、 |
424 | 430 | 文字列としてインクリメントが行なわれます (マジカルインクリメントと呼ばれます): |
425 | 431 | |
426 | print ++($foo = | |
432 | print ++($foo = "99"); # prints "100" | |
427 | print ++($foo = | |
433 | print ++($foo = "a0"); # prints "a1" | |
428 | print ++($foo = | |
434 | print ++($foo = "Az"); # prints "Ba" | |
429 | print ++($foo = | |
435 | print ++($foo = "zz"); # prints "aaa" | |
430 | 436 | |
431 | 437 | =begin original |
432 | 438 | |
433 | 439 | C<undef> is always treated as numeric, and in particular is changed |
434 | 440 | to C<0> before incrementing (so that a post-increment of an undef value |
435 | 441 | will return C<0> rather than C<undef>). |
436 | 442 | |
437 | 443 | =end original |
438 | 444 | |
439 | 445 | C<undef> は常に数値として扱われ、特にインクリメントされる前には C<0> に |
440 | 446 | 変換されます(従って、undef のポストインクリメント値は C<undef> ではなく |
441 | 447 | C<0> になります)。 |
442 | 448 | |
443 | 449 | =begin original |
444 | 450 | |
445 | 451 | The auto-decrement operator is not magical. |
446 | 452 | |
447 | 453 | =end original |
448 | 454 | |
449 | 455 | デクリメント演算子には、マジカルなものはありません。 |
450 | 456 | |
451 | 457 | =head2 Exponentiation |
452 | 458 | X<**> X<exponentiation> X<power> |
453 | 459 | |
454 | 460 | (指数演算子) |
455 | 461 | |
456 | 462 | =begin original |
457 | 463 | |
458 | 464 | Binary "**" is the exponentiation operator. It binds even more |
459 | 465 | tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is |
460 | 466 | implemented using C's pow(3) function, which actually works on doubles |
461 | 467 | internally.) |
462 | 468 | |
463 | 469 | =end original |
464 | 470 | |
465 | 471 | 二項演算子の "**" は指数演算子です。 |
466 | 472 | この演算子は、単項のマイナスよりも結合が強い演算子で、 |
467 | 473 | -2**4 は (-2)**4 ではなく、-(2**4) と解釈されます。 |
468 | 474 | (これは C の pow(3) を使って実装されていますので、 |
469 | 475 | 内部的には double で動作します。) |
470 | 476 | |
471 | 477 | =head2 Symbolic Unary Operators |
472 | 478 | X<unary operator> X<operator, unary> |
473 | 479 | |
474 | 480 | (単項演算子) |
475 | 481 | |
476 | 482 | =begin original |
477 | 483 | |
478 | 484 | Unary "!" performs logical negation, i.e., "not". See also C<not> for a lower |
479 | 485 | precedence version of this. |
480 | 486 | X<!> |
481 | 487 | |
482 | 488 | =end original |
483 | 489 | |
484 | 単項演算子の "!" は論理否定を行ないます。 | |
490 | 単項演算子の "!" は論理否定を行ないます; つまり「not」ということです。 | |
485 | つまり 「not」 ということです。 | |
486 | 491 | この演算子の優先順位を低くしたものとして、C<not> が用意されています。 |
487 | 492 | X<!> |
488 | 493 | |
489 | 494 | =begin original |
490 | 495 | |
491 | Unary "-" performs arithmetic negation if the operand is numeric | |
496 | Unary "-" performs arithmetic negation if the operand is numeric, | |
492 | ||
497 | including any string that looks like a number. If the operand is | |
493 | ||
498 | an identifier, a string consisting of a minus sign concatenated | |
494 | ||
499 | with the identifier is returned. Otherwise, if the string starts | |
495 | is r | |
500 | with a plus or minus, a string starting with the opposite sign is | |
501 | returned. One effect of these rules is that -bareword is equivalent | |
496 | 502 | to the string "-bareword". If, however, the string begins with a |
497 | 503 | non-alphabetic character (excluding "+" or "-"), Perl will attempt to convert |
498 | 504 | the string to a numeric and the arithmetic negation is performed. If the |
499 | 505 | string cannot be cleanly converted to a numeric, Perl will give the warning |
500 | 506 | B<Argument "the string" isn't numeric in negation (-) at ...>. |
501 | 507 | X<-> X<negation, arithmetic> |
502 | 508 | |
503 | 509 | =end original |
504 | 510 | |
505 | 単項演算子の "-" は被演算子が数値であれば、 | |
511 | 単項演算子の "-" は被演算子が数値または数値に見える文字列であれば、 | |
512 | 算術否定を行ないます。 | |
506 | 513 | 被演算子が識別子ならば、マイナス記号にその識別子をつなげた |
507 | 514 | 文字列が返されます。 |
508 | 515 | これ以外で被演算子の最初の文字がプラスかマイナスのときには、 |
509 | 516 | その記号を逆のものに置き換えた文字列を返します。 |
510 | 517 | この規則の結果、-bareword が文字列 "-bareword" に等価となります。 |
511 | 518 | しかし、文字列が英字以外("+" と "-" を除く)で始まっていると、 |
512 | 519 | Perl は文字列を数値に変換しようとし、それから算術否定が実行されます。 |
513 | 520 | もし文字列が明確に数値に変換できない場合、Perl は |
514 | 521 | B<Argument "the string" isn't numeric in negation (-) at ...> という |
515 | 522 | 警告を出します。 |
516 | 523 | X<-> X<negation, arithmetic> |
517 | 524 | |
518 | 525 | =begin original |
519 | 526 | |
520 | 527 | Unary "~" performs bitwise negation, i.e., 1's complement. For |
521 | 528 | example, C<0666 & ~027> is 0640. (See also L<Integer Arithmetic> and |
522 | 529 | L<Bitwise String Operators>.) Note that the width of the result is |
523 | 530 | platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64 |
524 | 531 | bits wide on a 64-bit platform, so if you are expecting a certain bit |
525 | width, remember to use the & operator to mask off the excess bits. | |
532 | width, remember to use the "&" operator to mask off the excess bits. | |
526 | 533 | X<~> X<negation, binary> |
527 | 534 | |
528 | 535 | =end original |
529 | 536 | |
530 | 単項演算子の "~" はビットごとの否定を行ないます。 | |
537 | 単項演算子の "~" はビットごとの否定を行ないます; つまり、1 の補数を返します。 | |
531 | つまり、1 の補数を返します。 | |
532 | 538 | 例えば、C<0666 & ~027> は 0640 です。 |
533 | 539 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) |
534 | 結果の幅はプラットホーム依存であることに注意してください | |
540 | 結果の幅はプラットホーム依存であることに注意してください: ~0 は | |
535 | ||
541 | 32 ビットプラットホームでは 32 ビット幅ですが、64 ビットプラットホームでは | |
536 | 64 | |
542 | 64 ビット幅なので、特定のビット幅を仮定する場合は、 | |
537 | ||
543 | 余分なビットをマスクするために "&" 演算子を使うことを忘れないでください。 | |
538 | 余分なビットをマスクするために & 演算子を使うことを忘れないでください。 | |
539 | 544 | X<~> X<negation, binary> |
540 | 545 | |
541 | 546 | =begin original |
542 | 547 | |
548 | When complementing strings, if all characters have ordinal values under | |
549 | 256, then their complements will, also. But if they do not, all | |
550 | characters will be in either 32- or 64-bit complements, depending on your | |
551 | architecture. So for example, C<~"\x{3B1}"> is C<"\x{FFFF_FC4E}"> on | |
552 | 32-bit machines and C<"\x{FFFF_FFFF_FFFF_FC4E}"> on 64-bit machines. | |
553 | ||
554 | =end original | |
555 | ||
556 | 文字列の補数を計算するとき、全ての文字の値が 256 未満の場合は、その値の | |
557 | 補数が計算されます。 | |
558 | そうでない場合は、全ての文字はアーキテクチャに依存しいて 32 ビットまたは | |
559 | 64 ビットで補数が計算されます。 | |
560 | 従って例えば、C<~"\x{3B1}"> は 32 ビットマシンでは C<"\x{FFFF_FC4E}"> に、 | |
561 | 64 ビットマシンでは C<"\x{FFFF_FFFF_FFFF_FC4E}"> になります。 | |
562 | ||
563 | =begin original | |
564 | ||
543 | 565 | Unary "+" has no effect whatsoever, even on strings. It is useful |
544 | 566 | syntactically for separating a function name from a parenthesized expression |
545 | 567 | that would otherwise be interpreted as the complete list of function |
546 | 568 | arguments. (See examples above under L<Terms and List Operators (Leftward)>.) |
547 | 569 | X<+> |
548 | 570 | |
549 | 571 | =end original |
550 | 572 | |
551 | 573 | 単項演算子の "+" は、たとえ文字列に対して用いられた場合にも、何もしません。 |
552 | 574 | 関数名に続けて括弧付きの式を書く場合に、関数の引数リストと |
553 | 575 | 解釈されないようにするために用いることができます。 |
554 | 576 | (下記 L<Terms and List Operators (Leftward)> の例を参照してください。) |
555 | 577 | X<+> |
556 | 578 | |
557 | 579 | =begin original |
558 | 580 | |
559 | 581 | Unary "\" creates a reference to whatever follows it. See L<perlreftut> |
560 | 582 | and L<perlref>. Do not confuse this behavior with the behavior of |
561 | 583 | backslash within a string, although both forms do convey the notion |
562 | 584 | of protecting the next thing from interpolation. |
563 | 585 | X<\> X<reference> X<backslash> |
564 | 586 | |
565 | 587 | =end original |
566 | 588 | |
567 | 589 | 単項演算子の "\" はその後に続くものへのリファレンスを生成します。 |
568 | 590 | L<perlreftut> と L<perlref> を参照してください。 |
569 | 591 | この用法も文字列中のバックスラッシュも、後に続くものが展開されるのを |
570 | 592 | 防ぐことになりますが、動作を混同しないでください。 |
571 | 593 | X<\> X<reference> X<backslash> |
572 | 594 | |
573 | 595 | =head2 Binding Operators |
574 | 596 | X<binding> X<operator, binding> X<=~> X<!~> |
575 | 597 | |
576 | 598 | (拘束演算子) |
577 | 599 | |
578 | 600 | =begin original |
579 | 601 | |
580 | 602 | Binary "=~" binds a scalar expression to a pattern match. Certain operations |
581 | 603 | search or modify the string $_ by default. This operator makes that kind |
582 | 604 | of operation work on some other string. The right argument is a search |
583 | 605 | pattern, substitution, or transliteration. The left argument is what is |
584 | 606 | supposed to be searched, substituted, or transliterated instead of the default |
585 | 607 | $_. When used in scalar context, the return value generally indicates the |
586 | success of the operation. | |
608 | success of the operation. The exceptions are substitution (s///) | |
587 | ||
609 | and transliteration (y///) with the C</r> (non-destructive) option, | |
588 | ||
610 | which cause the B<r>eturn value to be the result of the substitution. | |
611 | Behavior in list context depends on the particular operator. | |
612 | See L</"Regexp Quote-Like Operators"> for details and L<perlretut> for | |
613 | examples using these operators. | |
589 | 614 | |
590 | 615 | =end original |
591 | 616 | |
592 | 617 | 二項演算子の "=~" は、スカラ式をパターンマッチに拘束します。 |
593 | 618 | デフォルトで $_ の文字列を検索したり、変更したりする演算があります。 |
594 | 619 | この演算子は、そのような演算を他の文字列に対して行なわせるようにするものです。 |
595 | 620 | 右引数は、検索パターン、置換、文字変換のいずれかです。 |
596 | 621 | 左引数は、デフォルトの $_ の代わりに検索、置換、文字変換の対象となるものです。 |
597 | 622 | スカラコンテキストで使うと、返り値は一般的に演算の結果が成功したか否かです。 |
623 | 例外は、C</r> (非破壊) オプション付きの置換 (s///) と文字変換 | |
624 | (y///) です; この場合は変換した結果を返します。 | |
598 | 625 | リストコンテキストでの振る舞いは演算子に依存します。 |
599 | 626 | 詳しくは L</"Regexp Quote-Like Operators"> を、これらの演算子を使った |
600 | 627 | 例については L<perlretut> を参照して下さい。 |
601 | 628 | |
602 | 629 | =begin original |
603 | 630 | |
604 | 631 | If the right argument is an expression rather than a search pattern, |
605 | 632 | substitution, or transliteration, it is interpreted as a search pattern at run |
606 | 633 | time. Note that this means that its contents will be interpolated twice, so |
607 | 634 | |
608 | 635 | =end original |
609 | 636 | |
610 | 637 | 右引数が検索パターン、置換、文字変換ではなく、式であれば、 |
611 | 638 | それは実行時に決まる検索パターンと解釈されます。 |
612 | 639 | これは、内容が 2 回展開されることを意味することに注意してください; |
613 | 640 | つまり: |
614 | 641 | |
615 | 642 | '\\' =~ q'\\'; |
616 | 643 | |
617 | 644 | =begin original |
618 | 645 | |
619 | 646 | is not ok, as the regex engine will end up trying to compile the |
620 | 647 | pattern C<\>, which it will consider a syntax error. |
621 | 648 | |
622 | 649 | =end original |
623 | 650 | |
624 | 651 | は正しくありません; 正規表現エンジンは最終的にパターン C<\> を |
625 | 652 | コンパイルしようとして、これは文法エラーと考えるからです。 |
626 | 653 | |
627 | 654 | =begin original |
628 | 655 | |
629 | 656 | Binary "!~" is just like "=~" except the return value is negated in |
630 | 657 | the logical sense. |
631 | 658 | |
632 | 659 | =end original |
633 | 660 | |
634 | 661 | 二項演算子の "!~" は、返される値が論理否定されることを除いて |
635 | 662 | "=~" と同じです。 |
636 | 663 | |
664 | =begin original | |
665 | ||
666 | Binary "!~" with a non-destructive substitution (s///r) or transliteration | |
667 | (y///r) is a syntax error. | |
668 | ||
669 | =end original | |
670 | ||
671 | 二項演算子の "!~" を非破壊置換 (s///r) や変換 (y///r) で使うと | |
672 | 文法エラーとなります。 | |
673 | ||
637 | 674 | =head2 Multiplicative Operators |
638 | 675 | X<operator, multiplicative> |
639 | 676 | |
640 | 677 | (乗法演算子) |
641 | 678 | |
642 | 679 | =begin original |
643 | 680 | |
644 | 681 | Binary "*" multiplies two numbers. |
645 | 682 | X<*> |
646 | 683 | |
647 | 684 | =end original |
648 | 685 | |
649 | 686 | 二項演算子の "*" は 2 つの数値の積を返します。 |
650 | 687 | X<*> |
651 | 688 | |
652 | 689 | =begin original |
653 | 690 | |
654 | 691 | Binary "/" divides two numbers. |
655 | 692 | X</> X<slash> |
656 | 693 | |
657 | 694 | =end original |
658 | 695 | |
659 | 696 | 二項演算子の "/" は 2 つの数値の商を返します。 |
660 | 697 | X</> X<slash> |
661 | 698 | |
662 | 699 | =begin original |
663 | 700 | |
664 | 701 | Binary "%" is the modulo operator, which computes the division |
665 | 702 | remainder of its first argument with respect to its second argument. |
666 | 703 | Given integer |
667 | 704 | operands C<$a> and C<$b>: If C<$b> is positive, then C<$a % $b> is |
668 | 705 | C<$a> minus the largest multiple of C<$b> less than or equal to |
669 | 706 | C<$a>. If C<$b> is negative, then C<$a % $b> is C<$a> minus the |
670 | 707 | smallest multiple of C<$b> that is not less than C<$a> (i.e. the |
671 | 708 | result will be less than or equal to zero). If the operands |
672 | 709 | C<$a> and C<$b> are floating point values and the absolute value of |
673 | 710 | C<$b> (that is C<abs($b)>) is less than C<(UV_MAX + 1)>, only |
674 | 711 | the integer portion of C<$a> and C<$b> will be used in the operation |
675 | 712 | (Note: here C<UV_MAX> means the maximum of the unsigned integer type). |
676 | 713 | If the absolute value of the right operand (C<abs($b)>) is greater than |
677 | 714 | or equal to C<(UV_MAX + 1)>, "%" computes the floating-point remainder |
678 | 715 | C<$r> in the equation C<($r = $a - $i*$b)> where C<$i> is a certain |
679 | 716 | integer that makes C<$r> have the same sign as the right operand |
680 | 717 | C<$b> (B<not> as the left operand C<$a> like C function C<fmod()>) |
681 | 718 | and the absolute value less than that of C<$b>. |
682 | 719 | Note that when C<use integer> is in scope, "%" gives you direct access |
683 | 720 | to the modulo operator as implemented by your C compiler. This |
684 | 721 | operator is not as well defined for negative operands, but it will |
685 | 722 | execute faster. |
686 | 723 | X<%> X<remainder> X<modulo> X<mod> |
687 | 724 | |
688 | 725 | =end original |
689 | 726 | |
690 | 727 | 二項演算子の "%" は剰余演算子で、一つ目の引数を二つ目の引数で割ったときの |
691 | 728 | 余りを返します。 |
692 | 729 | C<$a> と C<$b> の二つの整数の被演算子を取ったとすると: |
693 | 730 | C<$b> が正の場合、C<$a % $b> は、C<$a> から C<$a> 以下の最大の |
694 | 731 | C<$b> の倍数を引いた値です。 |
695 | 732 | C<$b> が負の場合、C<$a % $b> は、C<$a> から C<$a> を下回らない |
696 | 最小の C<$b> の倍数を引いた値です | |
733 | 最小の C<$b> の倍数を引いた値です(従って結果はゼロ以下になります)。 | |
697 | 734 | オペランド C<$a> と C<$b> が浮動小数点数で、C<$b> の絶対値 |
698 | 735 | (つまり C<abs($b)>) が C<(UV_MAX + 1)> より小さい場合、 |
699 | 736 | C<$a> と C<$b> の整数部のみが操作で使われます |
700 | 737 | (注意: ここで C<UV_MAX> は符号なし整数の最大値を意味します)。 |
701 | 738 | 右オペランドの絶対値 (C<abs($b)>) が C<(UV_MAX + 1)> 以上の場合、 |
702 | 739 | "%" は、C<($r = $a - $i*$b)> となる浮動小数点剰余 C<$r> を計算します; |
703 | 740 | ここで C<$i> は、C<$r> が右オペランド C<$b> と同じ符号 (C の |
704 | 741 | 関数 C<fmod()> のように左オペランド C<$a> B<ではありません>) で、 |
705 | 742 | 絶対値が C<$b> より小さいものになるような、ある整数です。 |
706 | 743 | C<use integer> がスコープ内にある場合、 |
707 | 744 | "%" は C コンパイラで実装された剰余演算子を使います。 |
708 | 745 | この演算子は被演算子が負の場合の挙動が不確実ですが、 |
709 | 746 | より高速です。 |
710 | 747 | X<%> X<remainder> X<modulo> X<mod> |
711 | 748 | |
712 | 749 | =begin original |
713 | 750 | |
714 | 751 | Binary "x" is the repetition operator. In scalar context or if the left |
715 | 752 | operand is not enclosed in parentheses, it returns a string consisting |
716 | 753 | of the left operand repeated the number of times specified by the right |
717 | 754 | operand. In list context, if the left operand is enclosed in |
718 | 755 | parentheses or is a list formed by C<qw/STRING/>, it repeats the list. |
719 | 756 | If the right operand is zero or negative, it returns an empty string |
720 | 757 | or an empty list, depending on the context. |
721 | 758 | X<x> |
722 | 759 | |
723 | 760 | =end original |
724 | 761 | |
725 | 762 | 二項演算子の "x" は繰り返し演算子です。 |
726 | 763 | スカラコンテキストまたは左辺値が括弧で括られていない場合は、 |
727 | 764 | 左被演算子を右被演算子に示す数だけ繰り返したもので構成される |
728 | 765 | 文字列を返します。 |
729 | 766 | リストコンテキストでは、左被演算子が括弧で括られているか、C<qw/STRING> の |
730 | 767 | 形のリストの場合、リストを繰り返します。 |
731 | 768 | 右オペランドが 0 か負数の場合、コンテキストによって、空文字列か空リストを |
732 | 769 | 返します。 |
733 | 770 | X<x> |
734 | 771 | |
735 | 772 | print '-' x 80; # print row of dashes |
736 | 773 | |
737 | 774 | print "\t" x ($tab/8), ' ' x ($tab%8); # tab over |
738 | 775 | |
739 | 776 | @ones = (1) x 80; # a list of 80 1's |
740 | 777 | @ones = (5) x @ones; # set all elements to 5 |
741 | 778 | |
742 | 779 | |
743 | 780 | =head2 Additive Operators |
744 | 781 | X<operator, additive> |
745 | 782 | |
746 | 783 | (加法演算子) |
747 | 784 | |
748 | 785 | =begin original |
749 | 786 | |
750 | 787 | Binary "+" returns the sum of two numbers. |
751 | 788 | X<+> |
752 | 789 | |
753 | 790 | =end original |
754 | 791 | |
755 | 792 | 二項演算子の "+" は 2 つの数値の和を返します。 |
756 | 793 | X<+> |
757 | 794 | |
758 | 795 | =begin original |
759 | 796 | |
760 | 797 | Binary "-" returns the difference of two numbers. |
761 | 798 | X<-> |
762 | 799 | |
763 | 800 | =end original |
764 | 801 | |
765 | 802 | 二項演算子の "-" は 2 つの数値の差を返します。 |
766 | 803 | X<-> |
767 | 804 | |
768 | 805 | =begin original |
769 | 806 | |
770 | 807 | Binary "." concatenates two strings. |
771 | 808 | X<string, concatenation> X<concatenation> |
772 | 809 | X<cat> X<concat> X<concatenate> X<.> |
773 | 810 | |
774 | 811 | =end original |
775 | 812 | |
776 | 813 | 二項演算子の "." は 2 つの文字列を連結します。 |
777 | 814 | X<string, concatenation> X<concatenation> |
778 | 815 | X<cat> X<concat> X<concatenate> X<.> |
779 | 816 | |
780 | 817 | =head2 Shift Operators |
781 | 818 | X<shift operator> X<operator, shift> X<<< << >>> |
782 | 819 | X<<< >> >>> X<right shift> X<left shift> X<bitwise shift> |
783 | 820 | X<shl> X<shr> X<shift, right> X<shift, left> |
784 | 821 | |
785 | 822 | (シフト演算子) |
786 | 823 | |
787 | 824 | =begin original |
788 | 825 | |
789 | 826 | Binary "<<" returns the value of its left argument shifted left by the |
790 | 827 | number of bits specified by the right argument. Arguments should be |
791 | 828 | integers. (See also L<Integer Arithmetic>.) |
792 | 829 | |
793 | 830 | =end original |
794 | 831 | |
795 | 832 | 二項演算子の "<<" は左引数の値を、右引数で示すビット数だけ、 |
796 | 833 | 左にシフトした値を返します。 |
797 | 834 | 引数は整数でなければなりません。 |
798 | 835 | (L<Integer Arithmetic> も参照して下さい。) |
799 | 836 | |
800 | 837 | =begin original |
801 | 838 | |
802 | 839 | Binary ">>" returns the value of its left argument shifted right by |
803 | 840 | the number of bits specified by the right argument. Arguments should |
804 | 841 | be integers. (See also L<Integer Arithmetic>.) |
805 | 842 | |
806 | 843 | =end original |
807 | 844 | |
808 | 845 | 二項演算子の ">>" は左引数の値を、右引数で示すビット数だけ、 |
809 | 846 | 右にシフトした値を返します。 |
810 | 847 | 引数は整数でなければなりません。 |
811 | 848 | (L<Integer Arithmetic> も参照して下さい。) |
812 | 849 | |
813 | 850 | =begin original |
814 | 851 | |
815 | 852 | Note that both "<<" and ">>" in Perl are implemented directly using |
816 | 853 | "<<" and ">>" in C. If C<use integer> (see L<Integer Arithmetic>) is |
817 | 854 | in force then signed C integers are used, else unsigned C integers are |
818 | 855 | used. Either way, the implementation isn't going to generate results |
819 | 856 | larger than the size of the integer type Perl was built with (32 bits |
820 | 857 | or 64 bits). |
821 | 858 | |
822 | 859 | =end original |
823 | 860 | |
824 | 861 | Perl での "<<" と ">>" は C での "<<" と ">>" を直接利用して |
825 | 862 | 実装されていることに注意してください。 |
826 | 863 | C<use integer> (L<Integer Arithmetic> を参照してください)が有効な場合、 |
827 | 864 | C の符号付き整数が使われ、そうでない場合は C の符号なし整数が使われます。 |
828 | 865 | どちらの場合も、この実装は Perl がビルドされた整数型のサイズ(32 ビットか |
829 | 866 | 64 ビット)よりも大きい結果を生成することはありません。 |
830 | 867 | |
831 | 868 | =begin original |
832 | 869 | |
833 | 870 | The result of overflowing the range of the integers is undefined |
834 | 871 | because it is undefined also in C. In other words, using 32-bit |
835 | 872 | integers, C<< 1 << 32 >> is undefined. Shifting by a negative number |
836 | 873 | of bits is also undefined. |
837 | 874 | |
838 | 875 | =end original |
839 | 876 | |
840 | 877 | 整数の範囲をオーバーフローした場合の結果は、C でも未定義なので、未定義です。 |
841 | 878 | 言い換えると、32 ビット整数を使っているとき、C<< 1 << 32 >> は未定義です。 |
842 | 879 | シフトするビット数として負の数を指定した場合も未定義です。 |
843 | 880 | |
844 | 881 | =head2 Named Unary Operators |
845 | 882 | X<operator, named unary> |
846 | 883 | |
847 | 884 | (名前付き単項演算子) |
848 | 885 | |
849 | 886 | =begin original |
850 | 887 | |
851 | 888 | The various named unary operators are treated as functions with one |
852 | 889 | argument, with optional parentheses. |
853 | 890 | |
854 | 891 | =end original |
855 | 892 | |
856 | 893 | さまざまな名前付き単項演算子が、引数を 1 つ持ち、括弧が省略可能な、 |
857 | 894 | 関数として扱われます。 |
858 | 895 | |
859 | 896 | =begin original |
860 | 897 | |
861 | 898 | If any list operator (print(), etc.) or any unary operator (chdir(), etc.) |
862 | 899 | is followed by a left parenthesis as the next token, the operator and |
863 | 900 | arguments within parentheses are taken to be of highest precedence, |
864 | 901 | just like a normal function call. For example, |
865 | 902 | because named unary operators are higher precedence than ||: |
866 | 903 | |
867 | 904 | =end original |
868 | 905 | |
869 | 906 | リスト演算子 (print() など) や単項演算子 (chdir() など) は、 |
870 | 907 | すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、 |
871 | 908 | 通常の関数呼び出しのようにもっとも高い優先順位として扱われます。 |
872 | 909 | たとえば、名前つき単項演算子は || より優先順位が高いので、 |
873 | 910 | 以下のようになります: |
874 | 911 | |
875 | 912 | chdir $foo || die; # (chdir $foo) || die |
876 | 913 | chdir($foo) || die; # (chdir $foo) || die |
877 | 914 | chdir ($foo) || die; # (chdir $foo) || die |
878 | 915 | chdir +($foo) || die; # (chdir $foo) || die |
879 | 916 | |
880 | 917 | =begin original |
881 | 918 | |
882 | 919 | but, because * is higher precedence than named operators: |
883 | 920 | |
884 | 921 | =end original |
885 | 922 | |
886 | 923 | しかし * は名前つき演算子より優先順位が高いので、以下のようになります: |
887 | 924 | |
888 | 925 | chdir $foo * 20; # chdir ($foo * 20) |
889 | 926 | chdir($foo) * 20; # (chdir $foo) * 20 |
890 | 927 | chdir ($foo) * 20; # (chdir $foo) * 20 |
891 | 928 | chdir +($foo) * 20; # chdir ($foo * 20) |
892 | 929 | |
893 | 930 | rand 10 * 20; # rand (10 * 20) |
894 | 931 | rand(10) * 20; # (rand 10) * 20 |
895 | 932 | rand (10) * 20; # (rand 10) * 20 |
896 | 933 | rand +(10) * 20; # rand (10 * 20) |
897 | 934 | |
898 | 935 | =begin original |
899 | 936 | |
900 | 937 | Regarding precedence, the filetest operators, like C<-f>, C<-M>, etc. are |
901 | 938 | treated like named unary operators, but they don't follow this functional |
902 | 939 | parenthesis rule. That means, for example, that C<-f($file).".bak"> is |
903 | 940 | equivalent to C<-f "$file.bak">. |
904 | 941 | X<-X> X<filetest> X<operator, filetest> |
905 | 942 | |
906 | 943 | =end original |
907 | 944 | |
908 | 945 | 優先順位に関して、C<-f> や C<-M> のようなファイルテスト演算子は、名前付き |
909 | 946 | 単項演算子として扱われますが、この関数のかっこルールは適用されません。 |
910 | 947 | これは、例えば C<-f($file).".bak"> は C<-f "$file.bak"> と等価であることを |
911 | 948 | 意味します。 |
912 | 949 | X<-X> X<filetest> X<operator, filetest> |
913 | 950 | |
914 | 951 | =begin original |
915 | 952 | |
916 | 953 | See also L<"Terms and List Operators (Leftward)">. |
917 | 954 | |
918 | 955 | =end original |
919 | 956 | |
920 | 957 | L<"Terms and List Operators (Leftward)"> も参照して下さい。 |
921 | 958 | |
922 | 959 | =head2 Relational Operators |
923 | 960 | X<relational operator> X<operator, relational> |
924 | 961 | |
925 | 962 | (比較演算子) |
926 | 963 | |
927 | 964 | =begin original |
928 | 965 | |
929 | 966 | Binary "<" returns true if the left argument is numerically less than |
930 | 967 | the right argument. |
931 | 968 | X<< < >> |
932 | 969 | |
933 | 970 | =end original |
934 | 971 | |
935 | 972 | 二項演算子の "<" は左引数が数値的に右引数よりも小さければ、 |
936 | 973 | 真を返します。 |
974 | X<< < >> | |
937 | 975 | |
938 | 976 | =begin original |
939 | 977 | |
940 | 978 | Binary ">" returns true if the left argument is numerically greater |
941 | 979 | than the right argument. |
942 | 980 | X<< > >> |
943 | 981 | |
944 | 982 | =end original |
945 | 983 | |
946 | 984 | 二項演算子の ">" は左引数が数値的に右引数よりも大きければ、 |
947 | 985 | 真を返します。 |
986 | X<< > >> | |
948 | 987 | |
949 | 988 | =begin original |
950 | 989 | |
951 | 990 | Binary "<=" returns true if the left argument is numerically less than |
952 | 991 | or equal to the right argument. |
953 | 992 | X<< <= >> |
954 | 993 | |
955 | 994 | =end original |
956 | 995 | |
957 | 996 | 二項演算子の "<=" は左引数が数値的に右引数よりも小さいか等しければ、 |
958 | 997 | 真を返します。 |
998 | X<< <= >> | |
959 | 999 | |
960 | 1000 | =begin original |
961 | 1001 | |
962 | 1002 | Binary ">=" returns true if the left argument is numerically greater |
963 | 1003 | than or equal to the right argument. |
964 | 1004 | X<< >= >> |
965 | 1005 | |
966 | 1006 | =end original |
967 | 1007 | |
968 | 1008 | 二項演算子の ">=" は左引数が数値的に右引数よりも大きいか等しければ、 |
969 | 1009 | 真を返します。 |
1010 | X<< >= >> | |
970 | 1011 | |
971 | 1012 | =begin original |
972 | 1013 | |
973 | 1014 | Binary "lt" returns true if the left argument is stringwise less than |
974 | 1015 | the right argument. |
975 | 1016 | X<< lt >> |
976 | 1017 | |
977 | 1018 | =end original |
978 | 1019 | |
979 | 1020 | 二項演算子の "lt" は左引数が文字列的に右引数よりも小さければ、 |
980 | 1021 | 真を返します。 |
1022 | X<< lt >> | |
981 | 1023 | |
982 | 1024 | =begin original |
983 | 1025 | |
984 | 1026 | Binary "gt" returns true if the left argument is stringwise greater |
985 | 1027 | than the right argument. |
986 | 1028 | X<< gt >> |
987 | 1029 | |
988 | 1030 | =end original |
989 | 1031 | |
990 | 1032 | 二項演算子の "gt" は左引数が文字列的に右引数よりも大きければ、 |
991 | 1033 | 真を返します。 |
1034 | X<< gt >> | |
992 | 1035 | |
993 | 1036 | =begin original |
994 | 1037 | |
995 | 1038 | Binary "le" returns true if the left argument is stringwise less than |
996 | 1039 | or equal to the right argument. |
997 | 1040 | X<< le >> |
998 | 1041 | |
999 | 1042 | =end original |
1000 | 1043 | |
1001 | 1044 | 二項演算子の "le" は左引数が文字列的に右引数よりも小さいか等しければ、 |
1002 | 1045 | 真を返します。 |
1046 | X<< le >> | |
1003 | 1047 | |
1004 | 1048 | =begin original |
1005 | 1049 | |
1006 | 1050 | Binary "ge" returns true if the left argument is stringwise greater |
1007 | 1051 | than or equal to the right argument. |
1008 | 1052 | X<< ge >> |
1009 | 1053 | |
1010 | 1054 | =end original |
1011 | 1055 | |
1012 | 1056 | 二項演算子の "ge" は左引数が文字列的に右引数よりも大きいか等しければ、 |
1013 | 1057 | 真を返します。 |
1058 | X<< ge >> | |
1014 | 1059 | |
1015 | 1060 | =head2 Equality Operators |
1016 | 1061 | X<equality> X<equal> X<equals> X<operator, equality> |
1017 | 1062 | |
1018 | 1063 | (等価演算子) |
1019 | 1064 | |
1020 | 1065 | =begin original |
1021 | 1066 | |
1022 | 1067 | Binary "==" returns true if the left argument is numerically equal to |
1023 | 1068 | the right argument. |
1024 | 1069 | X<==> |
1025 | 1070 | |
1026 | 1071 | =end original |
1027 | 1072 | |
1028 | 1073 | 二項演算子の "==" は左引数が数値的に右引数と等しければ、 |
1029 | 1074 | 真を返します。 |
1075 | X<==> | |
1030 | 1076 | |
1031 | 1077 | =begin original |
1032 | 1078 | |
1033 | 1079 | Binary "!=" returns true if the left argument is numerically not equal |
1034 | 1080 | to the right argument. |
1035 | 1081 | X<!=> |
1036 | 1082 | |
1037 | 1083 | =end original |
1038 | 1084 | |
1039 | 1085 | 二項演算子の "!=" は左引数が数値的に右引数と等しくなければ、 |
1040 | 1086 | 真を返します。 |
1087 | X<!=> | |
1041 | 1088 | |
1042 | 1089 | =begin original |
1043 | 1090 | |
1044 | 1091 | Binary "<=>" returns -1, 0, or 1 depending on whether the left |
1045 | 1092 | argument is numerically less than, equal to, or greater than the right |
1046 | 1093 | argument. If your platform supports NaNs (not-a-numbers) as numeric |
1047 | 1094 | values, using them with "<=>" returns undef. NaN is not "<", "==", ">", |
1048 | 1095 | "<=" or ">=" anything (even NaN), so those 5 return false. NaN != NaN |
1049 | 1096 | returns true, as does NaN != anything else. If your platform doesn't |
1050 | 1097 | support NaNs then NaN is just a string with numeric value 0. |
1051 | 1098 | X<< <=> >> X<spaceship> |
1052 | 1099 | |
1053 | 1100 | =end original |
1054 | 1101 | |
1055 | 1102 | 二項演算子の "<=>" は左引数が数値的に右引数より小さいか、等しいか、 |
1056 | 1103 | 大きいかに従って、-1, 0, 1 を返します。 |
1057 | 1104 | 数値として NaN (非数) に対応しているプラットフォームでは、 |
1058 | 1105 | NaN に対して "<=>" を使うと undef を返します。 |
1059 | 1106 | NaN はどの値に対しても(NaN に対してでさえも) "<", "==", ">", |
1060 | 1107 | "<=", ">=" のいずれも成立しないので、これらは全て偽となります。 |
1061 | 1108 | NaN != NaN は真を返しますが、その他のどの値に対しても != は偽を返します。 |
1062 | 1109 | NaN に対応していないプラットフォームでは、NaN は単に数としての値 0 を持つ |
1063 | 1110 | 文字列です。 |
1111 | X<< <=> >> X<spaceship> | |
1064 | 1112 | |
1065 | 1113 | perl -le '$a = "NaN"; print "No NaN support here" if $a == $a' |
1066 | 1114 | perl -le '$a = "NaN"; print "NaN support here" if $a != $a' |
1067 | 1115 | |
1068 | 1116 | =begin original |
1069 | 1117 | |
1070 | 1118 | Binary "eq" returns true if the left argument is stringwise equal to |
1071 | 1119 | the right argument. |
1072 | 1120 | X<eq> |
1073 | 1121 | |
1074 | 1122 | =end original |
1075 | 1123 | |
1076 | 1124 | 二項演算子の "eq" は左引数が文字列的に右引数と等しければ、 |
1077 | 1125 | 真を返します。 |
1126 | X<eq> | |
1078 | 1127 | |
1079 | 1128 | =begin original |
1080 | 1129 | |
1081 | 1130 | Binary "ne" returns true if the left argument is stringwise not equal |
1082 | 1131 | to the right argument. |
1083 | 1132 | X<ne> |
1084 | 1133 | |
1085 | 1134 | =end original |
1086 | 1135 | |
1087 | 1136 | 二項演算子の "ne" は左引数が文字列的に右引数と等しくなければ、 |
1088 | 1137 | 真を返します。 |
1138 | X<ne> | |
1089 | 1139 | |
1090 | 1140 | =begin original |
1091 | 1141 | |
1092 | 1142 | Binary "cmp" returns -1, 0, or 1 depending on whether the left |
1093 | 1143 | argument is stringwise less than, equal to, or greater than the right |
1094 | 1144 | argument. |
1095 | 1145 | X<cmp> |
1096 | 1146 | |
1097 | 1147 | =end original |
1098 | 1148 | |
1099 | 1149 | 二項演算子の "cmp" は左引数が文字列的に右引数より小さいか、 |
1100 | 1150 | 等しいか、大きいかに従って、-1, 0, 1 を返します。 |
1151 | X<cmp> | |
1101 | 1152 | |
1102 | 1153 | =begin original |
1103 | 1154 | |
1104 | 1155 | Binary "~~" does a smart match between its arguments. Smart matching |
1105 | 1156 | is described in L<perlsyn/"Smart matching in detail">. |
1106 | 1157 | X<~~> |
1107 | 1158 | |
1108 | 1159 | =end original |
1109 | 1160 | |
1110 | 1161 | 二項演算子の "~~" はスマートマッチングとして働きます。 |
1111 | 1162 | スマートマッチングについては L<perlsyn/"Smart matching in detail"> で |
1112 | 1163 | 述べられています。 |
1113 | 1164 | X<~~> |
1114 | 1165 | |
1115 | 1166 | =begin original |
1116 | 1167 | |
1117 | 1168 | "lt", "le", "ge", "gt" and "cmp" use the collation (sort) order specified |
1118 | 1169 | by the current locale if C<use locale> is in effect. See L<perllocale>. |
1119 | 1170 | |
1120 | 1171 | =end original |
1121 | 1172 | |
1122 | 1173 | "lt", "le", "ge", "gt", "cmp" は C<use locale> が有効な場合は |
1123 | 1174 | 現在のロケールで指定された辞書(ソート)順が使われます。 |
1124 | 1175 | L<perllocale> を参照して下さい。 |
1125 | 1176 | |
1126 | 1177 | =head2 Bitwise And |
1127 | 1178 | X<operator, bitwise, and> X<bitwise and> X<&> |
1128 | 1179 | |
1129 | 1180 | (ビットごとの AND) |
1130 | 1181 | |
1131 | 1182 | =begin original |
1132 | 1183 | |
1133 | 1184 | Binary "&" returns its operands ANDed together bit by bit. |
1134 | 1185 | (See also L<Integer Arithmetic> and L<Bitwise String Operators>.) |
1135 | 1186 | |
1136 | 1187 | =end original |
1137 | 1188 | |
1138 | 1189 | 二項演算子の "&" は、両オペランドのビットごとに論理積をとって、 |
1139 | 1190 | その結果を返します。 |
1140 | 1191 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) |
1141 | 1192 | |
1142 | 1193 | =begin original |
1143 | 1194 | |
1144 | 1195 | Note that "&" has lower priority than relational operators, so for example |
1145 | 1196 | the brackets are essential in a test like |
1146 | 1197 | |
1147 | 1198 | =end original |
1148 | 1199 | |
1149 | 1200 | "&" は関係演算子より優先順位が低いので、例えば以下のようなテストでは、 |
1150 | 1201 | かっこが重要です: |
1151 | 1202 | |
1152 | 1203 | print "Even\n" if ($x & 1) == 0; |
1153 | 1204 | |
1154 | 1205 | =head2 Bitwise Or and Exclusive Or |
1155 | 1206 | X<operator, bitwise, or> X<bitwise or> X<|> X<operator, bitwise, xor> |
1156 | 1207 | X<bitwise xor> X<^> |
1157 | 1208 | |
1158 | 1209 | (ビットごとの OR と XOR) |
1159 | 1210 | |
1160 | 1211 | =begin original |
1161 | 1212 | |
1162 | 1213 | Binary "|" returns its operands ORed together bit by bit. |
1163 | 1214 | (See also L<Integer Arithmetic> and L<Bitwise String Operators>.) |
1164 | 1215 | |
1165 | 1216 | =end original |
1166 | 1217 | |
1167 | 1218 | 二項演算子の "|" は、両オペランドのビットごとに論理和をとって、 |
1168 | 1219 | その結果を返します。 |
1169 | 1220 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) |
1170 | 1221 | |
1171 | 1222 | =begin original |
1172 | 1223 | |
1173 | 1224 | Binary "^" returns its operands XORed together bit by bit. |
1174 | 1225 | (See also L<Integer Arithmetic> and L<Bitwise String Operators>.) |
1175 | 1226 | |
1176 | 1227 | =end original |
1177 | 1228 | |
1178 | 1229 | 二項演算子の "^" は、両オペランドのビットごとに排他論理和をとって、 |
1179 | 1230 | その結果を返します。 |
1180 | 1231 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) |
1181 | 1232 | |
1182 | 1233 | =begin original |
1183 | 1234 | |
1184 | 1235 | Note that "|" and "^" have lower priority than relational operators, so |
1185 | 1236 | for example the brackets are essential in a test like |
1186 | 1237 | |
1187 | 1238 | =end original |
1188 | 1239 | |
1189 | 1240 | "|" と "^" は関係演算子より優先順位が低いので、例えば以下のような |
1190 | 1241 | テストでは、かっこが重要です: |
1191 | 1242 | |
1192 | 1243 | print "false\n" if (8 | 2) != 10; |
1193 | 1244 | |
1194 | 1245 | =head2 C-style Logical And |
1195 | 1246 | X<&&> X<logical and> X<operator, logical, and> |
1196 | 1247 | |
1197 | 1248 | (C スタイルの論理積) |
1198 | 1249 | |
1199 | 1250 | =begin original |
1200 | 1251 | |
1201 | 1252 | Binary "&&" performs a short-circuit logical AND operation. That is, |
1202 | 1253 | if the left operand is false, the right operand is not even evaluated. |
1203 | 1254 | Scalar or list context propagates down to the right operand if it |
1204 | 1255 | is evaluated. |
1205 | 1256 | |
1206 | 1257 | =end original |
1207 | 1258 | |
1208 | 1259 | 二項演算子の "&&" は、短絡の論理積演算を行ないます。 |
1209 | 1260 | つまり、左被演算子が偽であれば、右被演算子は評価さえ |
1210 | 1261 | 行なわれないということです。 |
1211 | 1262 | 評価される場合には、スカラーかリストかというコンテキストは、 |
1212 | 1263 | 右被演算子にも及びます。 |
1213 | 1264 | |
1214 | 1265 | =head2 C-style Logical Or |
1215 | 1266 | X<||> X<operator, logical, or> |
1216 | 1267 | |
1217 | 1268 | (C スタイルの論理和) |
1218 | 1269 | |
1219 | 1270 | =begin original |
1220 | 1271 | |
1221 | 1272 | Binary "||" performs a short-circuit logical OR operation. That is, |
1222 | 1273 | if the left operand is true, the right operand is not even evaluated. |
1223 | 1274 | Scalar or list context propagates down to the right operand if it |
1224 | 1275 | is evaluated. |
1225 | 1276 | |
1226 | 1277 | =end original |
1227 | 1278 | |
1228 | 1279 | 二項演算子の "||" は、短絡の論理和演算を行ないます。 |
1229 | 1280 | つまり、左被演算子が真であれば、右被演算子は評価さえ |
1230 | 1281 | 行なわれないということです。 |
1231 | 1282 | 評価される場合には、スカラーかリストかというコンテキストは、 |
1232 | 1283 | 右被演算子にも及びます。 |
1233 | 1284 | |
1234 | 1285 | =head2 C-style Logical Defined-Or |
1235 | 1286 | X<//> X<operator, logical, defined-or> |
1236 | 1287 | |
1237 | 1288 | (C スタイルの定義性和) |
1238 | 1289 | |
1239 | 1290 | =begin original |
1240 | 1291 | |
1241 | 1292 | Although it has no direct equivalent in C, Perl's C<//> operator is related |
1242 | 1293 | to its C-style or. In fact, it's exactly the same as C<||>, except that it |
1243 | 1294 | tests the left hand side's definedness instead of its truth. Thus, C<$a // $b> |
1244 | 1295 | is similar to C<defined($a) || $b> (except that it returns the value of C<$a> |
1245 | rather than the value of C<defined($a)>) and is e | |
1296 | rather than the value of C<defined($a)>) and yields the same result as | |
1246 | C<defined($a) ? $a : $b> | |
1297 | C<defined($a) ? $a : $b> (except that the ternary-operator form can be | |
1247 | ||
1298 | used as a lvalue, while C<$a // $b> cannot). This is very useful for | |
1248 | ||
1299 | providing default values for variables. If you actually want to test if | |
1300 | at least one of C<$a> and C<$b> is defined, use C<defined($a // $b)>. | |
1249 | 1301 | |
1250 | 1302 | =end original |
1251 | 1303 | |
1252 | 1304 | C では直接等価なものはありませんが、Perl の C<//> 演算子は C スタイル |
1253 | 1305 | 論理和に関連しています。 |
1254 | 1306 | 実際、左辺の真偽ではなく定義されているかを判定することを除けば |
1255 | 1307 | C<||> と同じです。 |
1256 | 1308 | したがって C<$a // $b> は C<defined($a) || $b> と似ていて (ただし、 |
1257 | 1309 | C<defined($a)> ではなく C<$a> の値を返します)、C<defined($a) ? $a : $b> と |
1258 | 完全に等価です | |
1310 | 完全に等価です (例外は 3 項演算子形式は左辺値として使えますが、 | |
1311 | C<$a // $b> は使えません)。 | |
1259 | 1312 | これは、変数に対するデフォルト値を設定するのにとても有用です。 |
1260 | 1313 | 実際に、C<$a> と C<$b> の少なくとも片方が定義されているかを判定したいなら、 |
1261 | 1314 | C<defined($a // $b)> を使ってください。 |
1262 | 1315 | |
1263 | 1316 | =begin original |
1264 | 1317 | |
1265 | 1318 | The C<||>, C<//> and C<&&> operators return the last value evaluated |
1266 | 1319 | (unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably |
1267 | 1320 | portable way to find out the home directory might be: |
1268 | 1321 | |
1269 | 1322 | =end original |
1270 | 1323 | |
1271 | 1324 | C<||>, C<//>, C<&&> 演算子は、(C のように 単に 0 や 1 を返すのではなく) |
1272 | 1325 | 最後に評価された値を返します。 |
1273 | 1326 | これにより、かなり一般的に使えるホームディレクトリを探す方法は: |
1274 | 1327 | |
1275 | $home = $ENV{ | |
1328 | $home = $ENV{HOME} | |
1276 | | |
1329 | // $ENV{LOGDIR} | |
1330 | // (getpwuid($<))[7] | |
1331 | // die "You're homeless!\n"; | |
1277 | 1332 | |
1278 | 1333 | =begin original |
1279 | 1334 | |
1280 | 1335 | In particular, this means that you shouldn't use this |
1281 | 1336 | for selecting between two aggregates for assignment: |
1282 | 1337 | |
1283 | 1338 | =end original |
1284 | 1339 | |
1285 | 1340 | 特に、これは代入のために二つの集合を選択するためには |
1286 | 1341 | 使うべきではないことを意味します。 |
1287 | 1342 | |
1343 | =begin original | |
1344 | ||
1288 | 1345 | @a = @b || @c; # this is wrong |
1289 | 1346 | @a = scalar(@b) || @c; # really meant this |
1290 | 1347 | @a = @b ? @b : @c; # this works fine, though |
1291 | 1348 | |
1349 | =end original | |
1350 | ||
1351 | @a = @b || @c; # これは間違い | |
1352 | @a = scalar(@b) || @c; # 本当にしたいこと | |
1353 | @a = @b ? @b : @c; # しかしこれも動作する | |
1354 | ||
1292 | 1355 | =begin original |
1293 | 1356 | |
1294 | 1357 | As more readable alternatives to C<&&> and C<||> when used for |
1295 | 1358 | control flow, Perl provides the C<and> and C<or> operators (see below). |
1296 | 1359 | The short-circuit behavior is identical. The precedence of "and" |
1297 | 1360 | and "or" is much lower, however, so that you can safely use them after a |
1298 | 1361 | list operator without the need for parentheses: |
1299 | 1362 | |
1300 | 1363 | =end original |
1301 | 1364 | |
1302 | 1365 | Perl では、フロー制御に使う場合の多少読みやすい C<&&> と C<||> の |
1303 | 1366 | 同義語として、C<and> 演算子と C<or> 演算子が用意されています (下記参照)。 |
1304 | 1367 | 短絡の動作は全く同じです。 |
1305 | 1368 | しかし、"and" と "or" の優先順位はかなり低くしてあるので、引数に括弧を |
1306 | 1369 | 使っていないリスト演算子のあとに続けて使う場合にも、 |
1307 | 1370 | 安心して使うことができます: |
1308 | 1371 | |
1309 | 1372 | unlink "alpha", "beta", "gamma" |
1310 | 1373 | or gripe(), next LINE; |
1311 | 1374 | |
1312 | 1375 | =begin original |
1313 | 1376 | |
1314 | 1377 | With the C-style operators that would have been written like this: |
1315 | 1378 | |
1316 | 1379 | =end original |
1317 | 1380 | |
1318 | 1381 | C スタイルの演算子では以下のように書く必要があります。 |
1319 | 1382 | |
1320 | 1383 | unlink("alpha", "beta", "gamma") |
1321 | 1384 | || (gripe(), next LINE); |
1322 | 1385 | |
1323 | 1386 | =begin original |
1324 | 1387 | |
1325 | 1388 | Using "or" for assignment is unlikely to do what you want; see below. |
1326 | 1389 | |
1327 | 1390 | =end original |
1328 | 1391 | |
1329 | 代入で "or" を使うと、したいことと違うことになります。 | |
1392 | 代入で "or" を使うと、したいことと違うことになります; 以下を参照して下さい。 | |
1330 | 以下を参照して下さい。 | |
1331 | 1393 | |
1332 | 1394 | =head2 Range Operators |
1333 | 1395 | X<operator, range> X<range> X<..> X<...> |
1334 | 1396 | |
1335 | 1397 | (範囲演算子) |
1336 | 1398 | |
1337 | 1399 | =begin original |
1338 | 1400 | |
1339 | 1401 | Binary ".." is the range operator, which is really two different |
1340 | 1402 | operators depending on the context. In list context, it returns a |
1341 | 1403 | list of values counting (up by ones) from the left value to the right |
1342 | 1404 | value. If the left value is greater than the right value then it |
1343 | 1405 | returns the empty list. The range operator is useful for writing |
1344 | 1406 | C<foreach (1..10)> loops and for doing slice operations on arrays. In |
1345 | 1407 | the current implementation, no temporary array is created when the |
1346 | 1408 | range operator is used as the expression in C<foreach> loops, but older |
1347 | 1409 | versions of Perl might burn a lot of memory when you write something |
1348 | 1410 | like this: |
1349 | 1411 | |
1350 | 1412 | =end original |
1351 | 1413 | |
1352 | 1414 | 二項演算子の ".." は範囲演算子で、使われるコンテキストによって |
1353 | 1415 | 異なる動作をする 2 つの演算子を合わせたものです。 |
1354 | 1416 | リストコンテキストでは、左の値から右の値まで (1 づつ昇順で) 数えあげた値から |
1355 | 1417 | なるリストを返します。 |
1356 | 1418 | 左側の値が右側の値より大きい場合は、空リストを返します。 |
1357 | 1419 | 範囲演算子は、C<foreach (1..10)> のようなループを書くときや、 |
1358 | 1420 | 配列のスライス演算を行なうときに便利です。 |
1359 | 1421 | 現状の実装では、C<foreach> ループの式の中で範囲演算子を使っても |
1360 | 1422 | 一時配列は作りませんが、古い Perl は以下のようなことを書くと、 |
1361 | 1423 | 大量のメモリを消費することになります: |
1362 | 1424 | |
1363 | 1425 | for (1 .. 1_000_000) { |
1364 | 1426 | # code |
1365 | 1427 | } |
1366 | 1428 | |
1367 | 1429 | =begin original |
1368 | 1430 | |
1369 | The range operator also works on strings, using the magical | |
1431 | The range operator also works on strings, using the magical | |
1370 | see below. | |
1432 | auto-increment, see below. | |
1371 | 1433 | |
1372 | 1434 | =end original |
1373 | 1435 | |
1374 | 範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します | |
1436 | 範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します; | |
1375 | 1437 | 以下を参照してください。 |
1376 | 1438 | |
1377 | 1439 | =begin original |
1378 | 1440 | |
1379 | 1441 | In scalar context, ".." returns a boolean value. The operator is |
1380 | bistable, like a flip-flop, and emulates the line-range (comma) | |
1442 | bistable, like a flip-flop, and emulates the line-range (comma) | |
1381 | of B<sed>, B<awk>, and various editors. | |
1443 | operator of B<sed>, B<awk>, and various editors. Each ".." operator | |
1382 | own boolean state | |
1444 | maintains its own boolean state, even across calls to a subroutine | |
1445 | that contains it. It is false as long as its left operand is false. | |
1383 | 1446 | Once the left operand is true, the range operator stays true until the |
1384 | 1447 | right operand is true, I<AFTER> which the range operator becomes false |
1385 | again. It doesn't become false till the next time the range operator | |
1448 | again. It doesn't become false till the next time the range operator | |
1386 | evaluated. It can test the right operand and become false on th | |
1449 | is evaluated. It can test the right operand and become false on the | |
1387 | evaluation it became true (as in B<awk>), but it still returns | |
1450 | same evaluation it became true (as in B<awk>), but it still returns | |
1388 | If you don't want it to test the right operand til | |
1451 | true once. If you don't want it to test the right operand until the | |
1389 | evaluation, as in B<sed>, just use three dots ("...") instead of | |
1452 | next evaluation, as in B<sed>, just use three dots ("...") instead of | |
1390 | 1453 | two. In all other regards, "..." behaves just like ".." does. |
1391 | 1454 | |
1392 | 1455 | =end original |
1393 | 1456 | |
1394 | 1457 | スカラコンテキストで使われたときには、".." は真偽値を返します。 |
1395 | 1458 | この演算子は、フリップフロップのように 2 値安定で、 |
1396 | 1459 | B<sed> や B<awk> や多くのエディタでの行範囲 (コンマ) 演算子を |
1397 | 1460 | エミュレートするものとなります。 |
1398 | 各々の ".." 演算子 | |
1461 | 各々の ".." 演算子は、例えそれを含むサブルーチンの呼び出しを | |
1462 | またいでも、それぞれに独立して自分の真偽状態を管理します。 | |
1399 | 1463 | はじめは、左被演算子が偽である間、演算全体も偽となっています。 |
1400 | ||
1464 | いったん左被演算子が真になると、範囲演算子は、右被演算子が真である間、 | |
1401 | 真を返すようになります。 | |
1465 | 真を返すようになります; 範囲演算子が再び偽になった I<後> です。 | |
1402 | ||
1466 | 次に範囲演算子が評価されるまでは、偽とはなりません。 | |
1403 | (次に範囲演算子が評価されるまでは、偽とはなりません。 | |
1404 | 1467 | (B<awk> でのように) 真となった、その評価の中で右被演算子をテストし、 |
1405 | 1468 | 偽とすることができますが、1 度は真を返すことになります。 |
1406 | 1469 | B<sed> でのように、次に評価されるまで右被演算子をテストしたくなければ、 |
1407 | 1470 | 2 個のドットの代わりに 3 つのドット ("...") を使ってください。 |
1408 | 1471 | その他の点では、"..." は ".." と同様に振舞います. |
1409 | 1472 | |
1410 | 1473 | =begin original |
1411 | 1474 | |
1412 | 1475 | The right operand is not evaluated while the operator is in the |
1413 | 1476 | "false" state, and the left operand is not evaluated while the |
1414 | 1477 | operator is in the "true" state. The precedence is a little lower |
1415 | 1478 | than || and &&. The value returned is either the empty string for |
1416 | false, or a sequence number (beginning with 1) for true. The | |
1479 | false, or a sequence number (beginning with 1) for true. The sequence | |
1417 | ||
1480 | number is reset for each range encountered. The final sequence number | |
1418 | ||
1481 | in a range has the string "E0" appended to it, which doesn't affect | |
1419 | ||
1482 | its numeric value, but gives you something to search for if you want | |
1420 | ||
1483 | to exclude the endpoint. You can exclude the beginning point by | |
1421 | ||
1484 | waiting for the sequence number to be greater than 1. | |
1422 | than 1. | |
1423 | 1485 | |
1424 | 1486 | =end original |
1425 | 1487 | |
1426 | 1488 | 右被演算子は、演算子の状態が「偽」である間は評価されることがなく、 |
1427 | 1489 | 左被演算子は、演算子の状態が「真」である間は評価されることがありません。 |
1428 | 1490 | 優先順位は、|| と && の少し下です。 |
1429 | 1491 | 偽としては空文字列が返され、 |
1430 | 1492 | 真としては (1 から始まる) 順に並んだ数値が返されます。 |
1431 | 1493 | この通し番号は、新たに範囲が始まるごとにリセットされます。 |
1432 | 範囲の最後の数字には、文字列 "E0" が末尾につけられます | |
1494 | 範囲の最後の数字には、文字列 "E0" が末尾につけられます; これは、数値としては | |
1433 | ||
1495 | 何の影響もありませんが、範囲の終わりで何か特別なことをしたい場合に、 | |
1434 | ||
1496 | 目印として使うことができます。 | |
1435 | 1497 | 範囲の始まりで何かしたい場合には、通し番号が 1 よりも大きくなるのを |
1436 | 1498 | 待っていればよいでしょう。 |
1437 | 1499 | |
1438 | 1500 | =begin original |
1439 | 1501 | |
1440 | 1502 | If either operand of scalar ".." is a constant expression, |
1441 | 1503 | that operand is considered true if it is equal (C<==>) to the current |
1442 | 1504 | input line number (the C<$.> variable). |
1443 | 1505 | |
1444 | 1506 | =end original |
1445 | 1507 | |
1446 | 1508 | スカラの ".." の被演算子が定数表現であるときは、その被演算子は暗黙に、 |
1447 | 1509 | 現在の入力行番号(変数 C<$.>) と等しい(C<==>) 場合に真となります。 |
1448 | 1510 | |
1449 | 1511 | =begin original |
1450 | 1512 | |
1451 | 1513 | To be pedantic, the comparison is actually C<int(EXPR) == int(EXPR)>, |
1452 | 1514 | but that is only an issue if you use a floating point expression; when |
1453 | 1515 | implicitly using C<$.> as described in the previous paragraph, the |
1454 | 1516 | comparison is C<int(EXPR) == int($.)> which is only an issue when C<$.> |
1455 | 1517 | is set to a floating point value and you are not reading from a file. |
1456 | 1518 | Furthermore, C<"span" .. "spat"> or C<2.18 .. 3.14> will not do what |
1457 | 1519 | you want in scalar context because each of the operands are evaluated |
1458 | 1520 | using their integer representation. |
1459 | 1521 | |
1460 | 1522 | =end original |
1461 | 1523 | |
1462 | 1524 | とても細かい話をすると、比較は実際には C<int(EXPR) == int(EXPR)> ですが、 |
1463 | 1525 | これは浮動小数点数を使うときにだけ問題になります; 前の段落で記述したように |
1464 | 1526 | 明示的に C<$.> を使ったとき、比較は C<int(EXPR) == int($.)> となり、 |
1465 | 1527 | C<$.> に浮動小数点数がセットされ、ファイルから読み込みを行わない場合にのみ |
1466 | 1528 | 問題になります。 |
1467 | 1529 | さらに、C<"span" .. "spat"> や C<2.18 .. 3.14> は、それぞれのオペランドが |
1468 | 1530 | 整数表現を使って評価されるため、スカラコンテキストでは望みどおりの結果に |
1469 | 1531 | なりません。 |
1470 | 1532 | |
1471 | 1533 | =begin original |
1472 | 1534 | |
1473 | 1535 | Examples: |
1474 | 1536 | |
1475 | 1537 | =end original |
1476 | 1538 | |
1477 | 1539 | 例: |
1478 | 1540 | |
1479 | 1541 | =begin original |
1480 | 1542 | |
1481 | 1543 | As a scalar operator: |
1482 | 1544 | |
1483 | 1545 | =end original |
1484 | 1546 | |
1485 | 1547 | スカラ演算子として: |
1486 | 1548 | |
1487 | 1549 | if (101 .. 200) { print; } # print 2nd hundred lines, short for |
1488 | # | |
1550 | # if ($. == 101 .. $. == 200) { print; } | |
1489 | 1551 | |
1490 | 1552 | next LINE if (1 .. /^$/); # skip header lines, short for |
1491 | 1553 | # next LINE if ($. == 1 .. /^$/); |
1492 | 1554 | # (typically in a loop labeled LINE) |
1493 | 1555 | |
1494 | 1556 | s/^/> / if (/^$/ .. eof()); # quote body |
1495 | 1557 | |
1496 | 1558 | # parse mail messages |
1497 | 1559 | while (<>) { |
1498 | 1560 | $in_header = 1 .. /^$/; |
1499 | 1561 | $in_body = /^$/ .. eof; |
1500 | 1562 | if ($in_header) { |
1501 | 1563 | # do something |
1502 | 1564 | } else { # in body |
1503 | 1565 | # do something else |
1504 | 1566 | } |
1505 | 1567 | } continue { |
1506 | 1568 | close ARGV if eof; # reset $. each file |
1507 | 1569 | } |
1508 | 1570 | |
1509 | 1571 | =begin original |
1510 | 1572 | |
1511 | 1573 | Here's a simple example to illustrate the difference between |
1512 | 1574 | the two range operators: |
1513 | 1575 | |
1514 | 1576 | =end original |
1515 | 1577 | |
1516 | 1578 | 以下は二つの範囲演算子の違いを示す単純な例です: |
1517 | 1579 | |
1518 | 1580 | @lines = (" - Foo", |
1519 | 1581 | "01 - Bar", |
1520 | 1582 | "1 - Baz", |
1521 | 1583 | " - Quux"); |
1522 | 1584 | |
1523 | 1585 | foreach (@lines) { |
1524 | 1586 | if (/0/ .. /1/) { |
1525 | 1587 | print "$_\n"; |
1526 | 1588 | } |
1527 | 1589 | } |
1528 | 1590 | |
1529 | 1591 | =begin original |
1530 | 1592 | |
1531 | 1593 | This program will print only the line containing "Bar". If |
1532 | 1594 | the range operator is changed to C<...>, it will also print the |
1533 | 1595 | "Baz" line. |
1534 | 1596 | |
1535 | 1597 | =end original |
1536 | 1598 | |
1537 | 1599 | このプログラムは "Bar" を含む行だけを表示します。 |
1538 | 1600 | 範囲演算子を C<...> に変更すると、"Baz" の行も表示します。 |
1539 | 1601 | |
1540 | 1602 | =begin original |
1541 | 1603 | |
1542 | 1604 | And now some examples as a list operator: |
1543 | 1605 | |
1544 | 1606 | =end original |
1545 | 1607 | |
1546 | 1608 | これはリスト演算子の例です: |
1547 | 1609 | |
1610 | =begin original | |
1611 | ||
1548 | 1612 | for (101 .. 200) { print; } # print $_ 100 times |
1549 | 1613 | @foo = @foo[0 .. $#foo]; # an expensive no-op |
1550 | 1614 | @foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items |
1551 | 1615 | |
1616 | =end original | |
1617 | ||
1618 | for (101 .. 200) { print; } # $_ を 100 回出力 | |
1619 | @foo = @foo[0 .. $#foo]; # 高価な no-op | |
1620 | @foo = @foo[$#foo-4 .. $#foo]; # 末尾の 5 アイテムをスライス | |
1621 | ||
1552 | 1622 | =begin original |
1553 | 1623 | |
1554 | 1624 | The range operator (in list context) makes use of the magical |
1555 | 1625 | auto-increment algorithm if the operands are strings. You |
1556 | 1626 | can say |
1557 | 1627 | |
1558 | 1628 | =end original |
1559 | 1629 | |
1560 | 1630 | (リストコンテキストでの) 範囲演算子は、被演算子が文字列であるときには、 |
1561 | 1631 | マジカルインクリメントの機能を使います。 |
1562 | 1632 | 以下のように書くと: |
1563 | 1633 | |
1564 | @alphabet = ( | |
1634 | @alphabet = ("A" .. "Z"); | |
1565 | 1635 | |
1566 | 1636 | =begin original |
1567 | 1637 | |
1568 | 1638 | to get all normal letters of the English alphabet, or |
1569 | 1639 | |
1570 | 1640 | =end original |
1571 | 1641 | |
1572 | 1642 | 英語の大文字すべてを得られますし: |
1573 | 1643 | |
1574 | $hexdigit = (0 .. 9, | |
1644 | $hexdigit = (0 .. 9, "a" .. "f")[$num & 15]; | |
1575 | 1645 | |
1576 | 1646 | =begin original |
1577 | 1647 | |
1578 | 1648 | to get a hexadecimal digit, or |
1579 | 1649 | |
1580 | 1650 | =end original |
1581 | 1651 | |
1582 | 1652 | と書けば、16 進の数字が得られますし、 |
1583 | 1653 | |
1584 | @z2 = ( | |
1654 | @z2 = ("01" .. "31"); print $z2[$mday]; | |
1585 | 1655 | |
1586 | 1656 | =begin original |
1587 | 1657 | |
1588 | 1658 | to get dates with leading zeros. |
1589 | 1659 | |
1590 | 1660 | =end original |
1591 | 1661 | |
1592 | 1662 | とすれば、0 付きの日付が得られます。 |
1593 | 1663 | |
1594 | 1664 | =begin original |
1595 | 1665 | |
1596 | 1666 | If the final value specified is not in the sequence that the magical |
1597 | 1667 | increment would produce, the sequence goes until the next value would |
1598 | 1668 | be longer than the final value specified. |
1599 | 1669 | |
1600 | 1670 | =end original |
1601 | 1671 | |
1602 | 1672 | マジカルインクリメントによって得られる値の中に指定した最終値に |
1603 | 1673 | ちょうど一致するものが見つからないような場合には、 |
1604 | 1674 | マジカルインクリメントによって得られる次の値の文字列長が、 |
1605 | 1675 | 最終値として指定した値のものより長くなるまでインクリメントが続けられます。 |
1606 | 1676 | |
1607 | 1677 | =begin original |
1608 | 1678 | |
1609 | 1679 | If the initial value specified isn't part of a magical increment |
1610 | sequence (that is, a non-empty string matching | |
1680 | sequence (that is, a non-empty string matching C</^[a-zA-Z]*[0-9]*\z/>), | |
1611 | 1681 | only the initial value will be returned. So the following will only |
1612 | 1682 | return an alpha: |
1613 | 1683 | |
1614 | 1684 | =end original |
1615 | 1685 | |
1616 | 1686 | 指定された初期値がマジカルインクリメント処理の一部でない場合 |
1617 | (つまり、 | |
1687 | (つまり、C</^[a-zA-Z]*[0-9]*\z/> にマッチングする、空でない文字列の場合)、 | |
1618 | 1688 | 初期値のみが返されます。 |
1619 | 1689 | 従って、以下はαのみを返します: |
1620 | 1690 | |
1621 | use charnames | |
1691 | use charnames "greek"; | |
1622 | 1692 | my @greek_small = ("\N{alpha}" .. "\N{omega}"); |
1623 | 1693 | |
1624 | 1694 | =begin original |
1625 | 1695 | |
1626 | To get lower | |
1696 | To get the 25 traditional lowercase Greek letters, including both sigmas, | |
1697 | you could use this instead: | |
1627 | 1698 | |
1628 | 1699 | =end original |
1629 | 1700 | |
1630 | 小文字のギリシャ文字を得るためには、 | |
1701 | 両方のシグマを含む、25 文字の伝統的な小文字のギリシャ文字を得るためには、 | |
1702 | 代わりに以下のようにしてください: | |
1631 | 1703 | |
1632 | | |
1704 | use charnames "greek"; | |
1705 | my @greek_small = map { chr } | |
1706 | ord "\N{alpha}" .. ord "\N{omega}"; | |
1633 | 1707 | |
1634 | 1708 | =begin original |
1635 | 1709 | |
1710 | However, because there are I<many> other lowercase Greek characters than | |
1711 | just those, to match lowercase Greek characters in a regular expression, | |
1712 | you would use the pattern C</(?:(?=\p{Greek})\p{Lower})+/>. | |
1713 | ||
1714 | =end original | |
1715 | ||
1716 | しかし、小文字のギリシャ文字はここに書いたものよりも I<たくさん> あるので、 | |
1717 | 正規表現で小文字のギリシャ文字にマッチングさせるためには、 | |
1718 | C</(?:(?=\p{Greek})\p{Lower})+/> というパターンを使います。 | |
1719 | ||
1720 | =begin original | |
1721 | ||
1636 | 1722 | Because each operand is evaluated in integer form, C<2.18 .. 3.14> will |
1637 | 1723 | return two elements in list context. |
1638 | 1724 | |
1639 | 1725 | =end original |
1640 | 1726 | |
1641 | 1727 | それぞれのオペランドは整数の形で評価されるので、C<2.18 .. 3.14> は |
1642 | 1728 | リストコンテキストでは二つの要素を返します。 |
1643 | 1729 | |
1730 | =begin original | |
1731 | ||
1644 | 1732 | @list = (2.18 .. 3.14); # same as @list = (2 .. 3); |
1645 | 1733 | |
1734 | =end original | |
1735 | ||
1736 | @list = (2.18 .. 3.14); # @list = (2 .. 3); と同じ | |
1737 | ||
1646 | 1738 | =head2 Conditional Operator |
1647 | 1739 | X<operator, conditional> X<operator, ternary> X<ternary> X<?:> |
1648 | 1740 | |
1649 | 1741 | (条件演算子) |
1650 | 1742 | |
1651 | 1743 | =begin original |
1652 | 1744 | |
1653 | 1745 | Ternary "?:" is the conditional operator, just as in C. It works much |
1654 | 1746 | like an if-then-else. If the argument before the ? is true, the |
1655 | 1747 | argument before the : is returned, otherwise the argument after the : |
1656 | 1748 | is returned. For example: |
1657 | 1749 | |
1658 | 1750 | =end original |
1659 | 1751 | |
1660 | 1752 | 三項演算子の "?:" は、C の場合と同じ条件演算子です。 |
1661 | 1753 | これは、if-then-else のように働きます。 |
1662 | 1754 | "?" の前の引数が真であれば ":" の前の引数が返されますが、 |
1663 | 1755 | 真でなければ、":" の後の引数が返されます。 |
1664 | 1756 | 例えば: |
1665 | 1757 | |
1666 | 1758 | printf "I have %d dog%s.\n", $n, |
1667 | ($n == 1) ? | |
1759 | ($n == 1) ? "" : "s"; | |
1668 | 1760 | |
1669 | 1761 | =begin original |
1670 | 1762 | |
1671 | 1763 | Scalar or list context propagates downward into the 2nd |
1672 | 1764 | or 3rd argument, whichever is selected. |
1673 | 1765 | |
1674 | 1766 | =end original |
1675 | 1767 | |
1676 | 1768 | スカラコンテキストかリストコンテキストかという状況は、 |
1677 | 1769 | 選択された 2 番目もしくは 3 番目の引数にまで伝わります。 |
1678 | 1770 | |
1771 | =begin original | |
1772 | ||
1679 | 1773 | $a = $ok ? $b : $c; # get a scalar |
1680 | 1774 | @a = $ok ? @b : @c; # get an array |
1681 | 1775 | $a = $ok ? @b : @c; # oops, that's just a count! |
1682 | 1776 | |
1777 | =end original | |
1778 | ||
1779 | $a = $ok ? $b : $c; # スカラを取る | |
1780 | @a = $ok ? @b : @c; # 配列を取る | |
1781 | $a = $ok ? @b : @c; # うわ、これは単なる数です! | |
1782 | ||
1683 | 1783 | =begin original |
1684 | 1784 | |
1685 | 1785 | The operator may be assigned to if both the 2nd and 3rd arguments are |
1686 | 1786 | legal lvalues (meaning that you can assign to them): |
1687 | 1787 | |
1688 | 1788 | =end original |
1689 | 1789 | |
1690 | 1790 | 2 番目と 3 番目の引数双方が左辺値 (代入可能ということ)であれば、 |
1691 | 1791 | この演算子に代入を行なうこともできます: |
1692 | 1792 | |
1693 | 1793 | ($a_or_b ? $a : $b) = $c; |
1694 | 1794 | |
1695 | 1795 | =begin original |
1696 | 1796 | |
1697 | 1797 | Because this operator produces an assignable result, using assignments |
1698 | 1798 | without parentheses will get you in trouble. For example, this: |
1699 | 1799 | |
1700 | 1800 | =end original |
1701 | 1801 | |
1702 | 1802 | この演算子は代入可能な結果を生み出すので、 |
1703 | 1803 | 括弧なしで代入を行うとおかしくなるかもしれません。 |
1704 | 1804 | 例えばこれは: |
1705 | 1805 | |
1706 | 1806 | $a % 2 ? $a += 10 : $a += 2 |
1707 | 1807 | |
1708 | 1808 | =begin original |
1709 | 1809 | |
1710 | 1810 | Really means this: |
1711 | 1811 | |
1712 | 1812 | =end original |
1713 | 1813 | |
1714 | 1814 | 以下を意味し: |
1715 | 1815 | |
1716 | 1816 | (($a % 2) ? ($a += 10) : $a) += 2 |
1717 | 1817 | |
1718 | 1818 | =begin original |
1719 | 1819 | |
1720 | 1820 | Rather than this: |
1721 | 1821 | |
1722 | 1822 | =end original |
1723 | 1823 | |
1724 | 1824 | 以下のようにはなりません: |
1725 | 1825 | |
1726 | 1826 | ($a % 2) ? ($a += 10) : ($a += 2) |
1727 | 1827 | |
1728 | 1828 | =begin original |
1729 | 1829 | |
1730 | 1830 | That should probably be written more simply as: |
1731 | 1831 | |
1732 | 1832 | =end original |
1733 | 1833 | |
1734 | 1834 | 恐らく以下のようにもっと単純に書くべきでしょう: |
1735 | 1835 | |
1736 | 1836 | $a += ($a % 2) ? 10 : 2; |
1737 | 1837 | |
1738 | 1838 | =head2 Assignment Operators |
1739 | 1839 | X<assignment> X<operator, assignment> X<=> X<**=> X<+=> X<*=> X<&=> |
1740 | 1840 | X<<< <<= >>> X<&&=> X<-=> X</=> X<|=> X<<< >>= >>> X<||=> X<//=> X<.=> |
1741 | 1841 | X<%=> X<^=> X<x=> |
1742 | 1842 | |
1743 | 1843 | (代入演算子) |
1744 | 1844 | |
1745 | 1845 | =begin original |
1746 | 1846 | |
1747 | 1847 | "=" is the ordinary assignment operator. |
1748 | 1848 | |
1749 | 1849 | =end original |
1750 | 1850 | |
1751 | 1851 | "=" は通常の代入演算子です。 |
1752 | 1852 | |
1753 | 1853 | =begin original |
1754 | 1854 | |
1755 | 1855 | Assignment operators work as in C. That is, |
1756 | 1856 | |
1757 | 1857 | =end original |
1758 | 1858 | |
1759 | 1859 | 代入演算子は C の場合と同様の働きをします。 |
1760 | 1860 | つまり、 |
1761 | 1861 | |
1762 | 1862 | $a += 2; |
1763 | 1863 | |
1764 | 1864 | =begin original |
1765 | 1865 | |
1766 | 1866 | is equivalent to |
1767 | 1867 | |
1768 | 1868 | =end original |
1769 | 1869 | |
1770 | 1870 | は以下と等価です: |
1771 | 1871 | |
1772 | 1872 | $a = $a + 2; |
1773 | 1873 | |
1774 | 1874 | =begin original |
1775 | 1875 | |
1776 | 1876 | although without duplicating any side effects that dereferencing the lvalue |
1777 | 1877 | might trigger, such as from tie(). Other assignment operators work similarly. |
1778 | 1878 | The following are recognized: |
1779 | 1879 | |
1780 | 1880 | =end original |
1781 | 1881 | |
1782 | 1882 | しかし、tie() のようなもので起こる左辺値の被参照による |
1783 | 1883 | 副作用が 2 回起こることはありません。 |
1784 | 1884 | 他の代入演算も同様に働きます。 |
1785 | 1885 | 以下のものが認識されます: |
1786 | 1886 | |
1787 | 1887 | **= += *= &= <<= &&= |
1788 | 1888 | -= /= |= >>= ||= |
1789 | 1889 | .= %= ^= //= |
1790 | 1890 | x= |
1791 | 1891 | |
1792 | 1892 | =begin original |
1793 | 1893 | |
1794 | 1894 | Although these are grouped by family, they all have the precedence |
1795 | 1895 | of assignment. |
1796 | 1896 | |
1797 | 1897 | =end original |
1798 | 1898 | |
1799 | 1899 | グループ分けしてありますが、これらはいずれも代入演算子として |
1800 | 1900 | 同じ優先順位となっています。 |
1801 | 1901 | |
1802 | 1902 | =begin original |
1803 | 1903 | |
1804 | 1904 | Unlike in C, the scalar assignment operator produces a valid lvalue. |
1805 | 1905 | Modifying an assignment is equivalent to doing the assignment and |
1806 | 1906 | then modifying the variable that was assigned to. This is useful |
1807 | 1907 | for modifying a copy of something, like this: |
1808 | 1908 | |
1809 | 1909 | =end original |
1810 | 1910 | |
1811 | 1911 | C と違って、スカラ代入演算子は有効な左辺値を作り出します。 |
1812 | 1912 | 代入を修正することは、代入を行なってから、その代入された変数を修正するのと |
1813 | 1913 | 同じことになります。 |
1814 | 1914 | これは、以下のように何かのコピーを変更したいときに便利です: |
1815 | 1915 | |
1816 | ($tmp = $global) =~ tr [ | |
1916 | ($tmp = $global) =~ tr [0-9] [a-j]; | |
1817 | 1917 | |
1818 | 1918 | =begin original |
1819 | 1919 | |
1820 | 1920 | Likewise, |
1821 | 1921 | |
1822 | 1922 | =end original |
1823 | 1923 | |
1824 | 1924 | 同様に、 |
1825 | 1925 | |
1826 | 1926 | ($a += 2) *= 3; |
1827 | 1927 | |
1828 | 1928 | =begin original |
1829 | 1929 | |
1830 | 1930 | is equivalent to |
1831 | 1931 | |
1832 | 1932 | =end original |
1833 | 1933 | |
1834 | 1934 | は以下と等価です: |
1835 | 1935 | |
1836 | 1936 | $a += 2; |
1837 | 1937 | $a *= 3; |
1838 | 1938 | |
1839 | 1939 | =begin original |
1840 | 1940 | |
1841 | 1941 | Similarly, a list assignment in list context produces the list of |
1842 | 1942 | lvalues assigned to, and a list assignment in scalar context returns |
1843 | 1943 | the number of elements produced by the expression on the right hand |
1844 | 1944 | side of the assignment. |
1845 | 1945 | |
1846 | 1946 | =end original |
1847 | 1947 | |
1848 | 1948 | 同様に、リストコンテキストでのリストへの代入は代入可能な左辺値のリストとなり、 |
1849 | 1949 | スカラコンテキストでのリストへの代入は代入の右側の式で作成された |
1850 | 1950 | 要素の数を返します。 |
1851 | 1951 | |
1952 | =head2 The Triple-Dot Operator | |
1953 | X<...> X<... operator> X<yada-yada operator> X<whatever operator> | |
1954 | X<triple-dot operator> | |
1955 | ||
1956 | (3 点演算子) | |
1957 | ||
1958 | =begin original | |
1959 | ||
1960 | The triple-dot operator, C<...>, sometimes called the "whatever operator", the | |
1961 | "yada-yada operator", or the "I<et cetera>" operator, is a placeholder for | |
1962 | code. Perl parses it without error, but when you try to execute a whatever, | |
1963 | it throws an exception with the text C<Unimplemented>: | |
1964 | ||
1965 | =end original | |
1966 | ||
1967 | 3 点演算子 C<...>、(「何でも演算子」、「ヤダヤダ演算子」、「I<エトセトラ>」 | |
1968 | 演算子と呼ばれることもあります)はコードのためのプレースホルダです。 | |
1969 | Perl はこれをエラーを出すことなくパースしますが、これを | |
1970 | 実行しようとすると、C<Unimplemented> の文章と共に例外を発生させます: | |
1971 | ||
1972 | sub unimplemented { ... } | |
1973 | ||
1974 | eval { unimplemented() }; | |
1975 | if ($@ eq "Unimplemented" ) { | |
1976 | say "Oh look, an exception--whatever."; | |
1977 | } | |
1978 | ||
1979 | =begin original | |
1980 | ||
1981 | You can only use the triple-dot operator to stand in for a complete statement. | |
1982 | These examples of the triple-dot work: | |
1983 | ||
1984 | =end original | |
1985 | ||
1986 | 3 点演算子は完全な文の代わりにのみ使えます。 | |
1987 | 以下の例の 3 点演算子は動作します: | |
1988 | ||
1989 | { ... } | |
1990 | ||
1991 | sub foo { ... } | |
1992 | ||
1993 | ...; | |
1994 | ||
1995 | eval { ... }; | |
1996 | ||
1997 | sub foo { | |
1998 | my ($self) = shift; | |
1999 | ...; | |
2000 | } | |
2001 | ||
2002 | do { | |
2003 | my $variable; | |
2004 | ...; | |
2005 | say "Hurrah!"; | |
2006 | } while $cheering; | |
2007 | ||
2008 | =begin original | |
2009 | ||
2010 | The yada-yada--or whatever--cannot stand in for an expression that is | |
2011 | part of a larger statement since the C<...> is also the three-dot version | |
2012 | of the binary range operator (see L<Range Operators>). These examples of | |
2013 | the whatever operator are still syntax errors: | |
2014 | ||
2015 | =end original | |
2016 | ||
2017 | C<...> は 2 項範囲演算子(L<Range Operators> を参照してください)の 3 点版でも | |
2018 | あるので、ヤダヤダ(あるいは何でも)はより大きな文の一部の式としては使えません。 | |
2019 | 以下の例の何でも演算子は文法エラーになります: | |
2020 | ||
2021 | print ...; | |
2022 | ||
2023 | open(PASSWD, ">", "/dev/passwd") or ...; | |
2024 | ||
2025 | if ($condition && ...) { say "Hello" } | |
2026 | ||
2027 | =begin original | |
2028 | ||
2029 | There are some cases where Perl can't immediately tell the difference | |
2030 | between an expression and a statement. For instance, the syntax for a | |
2031 | block and an anonymous hash reference constructor look the same unless | |
2032 | there's something in the braces that give Perl a hint. The whatever | |
2033 | is a syntax error if Perl doesn't guess that the C<{ ... }> is a | |
2034 | block. In that case, it doesn't think the C<...> is the whatever | |
2035 | because it's expecting an expression instead of a statement: | |
2036 | ||
2037 | =end original | |
2038 | ||
2039 | 式と文との違いをすぐに説明できない場合があります。 | |
2040 | 例えば、ブロックと無名ハッシュリファレンスのコンストラクタは、 | |
2041 | Perl にヒントを与える中かっこがなければ同じに見えます。 | |
2042 | 何でも演算子は Perl が C<{ ... }> をブロックと判断できなかった場合は | |
2043 | 文法エラーとなります。 | |
2044 | この場合、文ではなく式と推測するので、C<...> は何でも演算子とは | |
2045 | 判断されません: | |
2046 | ||
2047 | =begin original | |
2048 | ||
2049 | my @transformed = map { ... } @input; # syntax error | |
2050 | ||
2051 | =end original | |
2052 | ||
2053 | my @transformed = map { ... } @input; # 文法エラー | |
2054 | ||
2055 | =begin original | |
2056 | ||
2057 | You can use a C<;> inside your block to denote that the C<{ ... }> is | |
2058 | a block and not a hash reference constructor. Now the whatever works: | |
2059 | ||
2060 | =end original | |
2061 | ||
2062 | C<{ ... }> がブロックであって、ハッシュリファレンスのコンストラクタでは | |
2063 | ないことを示すためにブロックの中で C<;> を使えます。 | |
2064 | これで何でも演算子は動作します: | |
2065 | ||
2066 | =begin original | |
2067 | ||
2068 | my @transformed = map {; ... } @input; # ; disambiguates | |
2069 | ||
2070 | =end original | |
2071 | ||
2072 | my @transformed = map {; ... } @input; # ; 曖昧でない | |
2073 | ||
2074 | =begin original | |
2075 | ||
2076 | my @transformed = map { ...; } @input; # ; disambiguates | |
2077 | ||
2078 | =end original | |
2079 | ||
2080 | my @transformed = map { ...; } @input; # ; 曖昧でない | |
2081 | ||
1852 | 2082 | =head2 Comma Operator |
1853 | 2083 | X<comma> X<operator, comma> X<,> |
1854 | 2084 | |
1855 | 2085 | (コンマ演算子) |
1856 | 2086 | |
1857 | 2087 | =begin original |
1858 | 2088 | |
1859 | 2089 | Binary "," is the comma operator. In scalar context it evaluates |
1860 | 2090 | its left argument, throws that value away, then evaluates its right |
1861 | 2091 | argument and returns that value. This is just like C's comma operator. |
1862 | 2092 | |
1863 | 2093 | =end original |
1864 | 2094 | |
1865 | 2095 | 二項演算子の "," はコンマ演算子です。 |
1866 | 2096 | スカラコンテキストではその左引数を評価し、その値を捨てて、 |
1867 | 2097 | それから右引数を評価し、その値を返します。 |
1868 | 2098 | これはちょうど、C のコンマ演算子と同じです。 |
1869 | 2099 | |
1870 | 2100 | =begin original |
1871 | 2101 | |
1872 | 2102 | In list context, it's just the list argument separator, and inserts |
1873 | 2103 | both its arguments into the list. These arguments are also evaluated |
1874 | 2104 | from left to right. |
1875 | 2105 | |
1876 | 2106 | =end original |
1877 | 2107 | |
1878 | 2108 | リストコンテキストでは、これは単にリスト引数の区切り文字で、 |
1879 | 2109 | 双方の引数をそのリストに挿入する働きがあります。 |
1880 | 2110 | これらの引数も左から右に評価されます。 |
1881 | 2111 | |
1882 | 2112 | =begin original |
1883 | 2113 | |
1884 | 2114 | The C<< => >> operator is a synonym for the comma except that it causes |
1885 | 2115 | its left operand to be interpreted as a string if it begins with a letter |
1886 | 2116 | or underscore and is composed only of letters, digits and underscores. |
1887 | 2117 | This includes operands that might otherwise be interpreted as operators, |
1888 | 2118 | constants, single number v-strings or function calls. If in doubt about |
1889 | this behavio | |
2119 | this behavior, the left operand can be quoted explicitly. | |
1890 | 2120 | |
1891 | 2121 | =end original |
1892 | 2122 | |
1893 | 2123 | C<< => >> 演算子はコンマ演算子の同義語ですが、 |
1894 | 2124 | もし左オペランドが文字か下線で始まっていて、かつ文字、数字、下線でのみ |
1895 | 2125 | 構成されている場合、これを文字列として扱うという効果もあります。 |
1896 | 2126 | これには他の場所では演算子、定数、v-文字列、関数呼び出しとして扱われる |
1897 | 2127 | オペランドを含みます。 |
1898 | 2128 | この振る舞いについて迷うことがあるなら、左オペランドを明示的に |
1899 | 2129 | クォートすることも出来ます。 |
1900 | 2130 | |
1901 | 2131 | =begin original |
1902 | 2132 | |
1903 | 2133 | Otherwise, the C<< => >> operator behaves exactly as the comma operator |
1904 | 2134 | or list argument separator, according to context. |
1905 | 2135 | |
1906 | 2136 | =end original |
1907 | 2137 | |
1908 | 2138 | さもなければ、C<< => >> 演算子はコンテキストによって、 |
1909 | 2139 | カンマ演算子かリスト引数の区切り文字と全く同様に振る舞います。 |
1910 | 2140 | |
1911 | 2141 | =begin original |
1912 | 2142 | |
1913 | 2143 | For example: |
1914 | 2144 | |
1915 | 2145 | =end original |
1916 | 2146 | |
1917 | 2147 | 例えば: |
1918 | 2148 | |
1919 | 2149 | use constant FOO => "something"; |
1920 | 2150 | |
1921 | 2151 | my %h = ( FOO => 23 ); |
1922 | 2152 | |
1923 | 2153 | =begin original |
1924 | 2154 | |
1925 | 2155 | is equivalent to: |
1926 | 2156 | |
1927 | 2157 | =end original |
1928 | 2158 | |
1929 | 2159 | は、以下と等価です: |
1930 | 2160 | |
1931 | 2161 | my %h = ("FOO", 23); |
1932 | 2162 | |
1933 | 2163 | =begin original |
1934 | 2164 | |
1935 | 2165 | It is I<NOT>: |
1936 | 2166 | |
1937 | 2167 | =end original |
1938 | 2168 | |
1939 | 2169 | これは I<違います>: |
1940 | 2170 | |
1941 | 2171 | my %h = ("something", 23); |
1942 | 2172 | |
1943 | 2173 | =begin original |
1944 | 2174 | |
1945 | 2175 | The C<< => >> operator is helpful in documenting the correspondence |
1946 | 2176 | between keys and values in hashes, and other paired elements in lists. |
1947 | 2177 | |
1948 | 2178 | =end original |
1949 | 2179 | |
1950 | 2180 | C<< => >> 演算子は、ハッシュのキーと値や、その他のリスト中の組となる |
1951 | 2181 | 要素の関係を表現するのに便利です。 |
1952 | 2182 | |
1953 | 2183 | %hash = ( $key => $value ); |
1954 | 2184 | login( $username => $password ); |
1955 | 2185 | |
1956 | 2186 | =head2 List Operators (Rightward) |
1957 | 2187 | X<operator, list, rightward> X<list operator> |
1958 | 2188 | |
1959 | 2189 | (リスト演算子 (右方向)) |
1960 | 2190 | |
1961 | 2191 | =begin original |
1962 | 2192 | |
1963 | On the right side of a list operator, | |
2193 | On the right side of a list operator, the comma has very low precedence, | |
1964 | 2194 | such that it controls all comma-separated expressions found there. |
1965 | 2195 | The only operators with lower precedence are the logical operators |
1966 | 2196 | "and", "or", and "not", which may be used to evaluate calls to list |
1967 | 2197 | operators without the need for extra parentheses: |
1968 | 2198 | |
1969 | 2199 | =end original |
1970 | 2200 | |
1971 | リスト演算子の右側のものにとって、 | |
2201 | リスト演算子の右側のものにとって、カンマはとても低い優先順位になります; | |
1972 | 2202 | これによってコンマで区切った式をリスト演算子の引数として |
1973 | 2203 | 置くことができます。 |
1974 | 2204 | これよりも優先順位が低いものは、論理演算子の "and", "or", "not" のみで、 |
1975 | 2205 | 余分な括弧を付けないリスト演算子の呼び出しを評価するために使えます: |
1976 | 2206 | |
1977 | open HANDLE, "fil | |
2207 | open HANDLE, "< $file" | |
1978 | or die "Can't open: $!\n"; | |
2208 | or die "Can't open $file: $!\n"; | |
1979 | 2209 | |
1980 | 2210 | =begin original |
1981 | 2211 | |
1982 | 2212 | See also discussion of list operators in L<Terms and List Operators (Leftward)>. |
1983 | 2213 | |
1984 | 2214 | =end original |
1985 | 2215 | |
1986 | 2216 | L<Terms and List Operators (Leftward)> のリスト演算子の議論も参照して下さい。 |
1987 | 2217 | |
1988 | 2218 | =head2 Logical Not |
1989 | 2219 | X<operator, logical, not> X<not> |
1990 | 2220 | |
1991 | 2221 | (論理否定) |
1992 | 2222 | |
1993 | 2223 | =begin original |
1994 | 2224 | |
1995 | 2225 | Unary "not" returns the logical negation of the expression to its right. |
1996 | 2226 | It's the equivalent of "!" except for the very low precedence. |
1997 | 2227 | |
1998 | 2228 | =end original |
1999 | 2229 | |
2000 | 2230 | 単項演算子の "not" は右側に来る式の否定を返します。 |
2001 | 2231 | これは、優先順位がずっと低いことを除いては "!" と等価です。 |
2002 | 2232 | |
2003 | 2233 | =head2 Logical And |
2004 | 2234 | X<operator, logical, and> X<and> |
2005 | 2235 | |
2006 | 2236 | (論理積) |
2007 | 2237 | |
2008 | 2238 | =begin original |
2009 | 2239 | |
2010 | 2240 | Binary "and" returns the logical conjunction of the two surrounding |
2011 | expressions. It's equivalent to && except for the very low | |
2241 | expressions. It's equivalent to C<&&> except for the very low | |
2012 | precedence. This means that it short-circuits: | |
2242 | precedence. This means that it short-circuits: the right | |
2013 | 2243 | expression is evaluated only if the left expression is true. |
2014 | 2244 | |
2015 | 2245 | =end original |
2016 | 2246 | |
2017 | 2247 | 二項演算子の "and" は両側の式の論理積を返します。 |
2018 | これは、優先順位がずっと低いことを除けば && と等価です。 | |
2248 | これは、優先順位がずっと低いことを除けば C<&&> と等価です。 | |
2019 | 2249 | つまり、これも短絡演算を行ない、右側の式は左側の式が |
2020 | 2250 | 「真」であった場合にのみ評価されます。 |
2021 | 2251 | |
2022 | 2252 | =head2 Logical or, Defined or, and Exclusive Or |
2023 | 2253 | X<operator, logical, or> X<operator, logical, xor> |
2024 | 2254 | X<operator, logical, defined or> X<operator, logical, exclusive or> |
2025 | 2255 | X<or> X<xor> |
2026 | 2256 | |
2027 | 2257 | (論理和と定義性和と排他論理和) |
2028 | 2258 | |
2029 | 2259 | =begin original |
2030 | 2260 | |
2031 | 2261 | Binary "or" returns the logical disjunction of the two surrounding |
2032 | expressions. It's equivalent to || except for the very low precedence. | |
2262 | expressions. It's equivalent to C<||> except for the very low precedence. | |
2033 | This makes it useful for control flow | |
2263 | This makes it useful for control flow: | |
2034 | 2264 | |
2035 | 2265 | =end original |
2036 | 2266 | |
2037 | 2267 | 二項演算子の "or" は両側の式の論理和を返します。 |
2038 | これは、優先順位がずっと低いことを除いて || と等価です。 | |
2268 | これは、優先順位がずっと低いことを除いて C<||> と等価です。 | |
2039 | 2269 | これはフローを制御するのに有用です: |
2040 | 2270 | |
2041 | 2271 | print FH $data or die "Can't write to FH: $!"; |
2042 | 2272 | |
2043 | 2273 | =begin original |
2044 | 2274 | |
2045 | This means that it short-circuits: | |
2275 | This means that it short-circuits: the right expression is evaluated | |
2046 | only if the left expression is false. Due to its precedence, you | |
2276 | only if the left expression is false. Due to its precedence, you must | |
2047 | ||
2277 | be careful to avoid using it as replacement for the C<||> operator. | |
2278 | It usually works out better for flow control than in assignments: | |
2048 | 2279 | |
2049 | 2280 | =end original |
2050 | 2281 | |
2051 | 2282 | つまり、これも短絡演算を行ない、右側の式は左側の式が |
2052 | 2283 | 「偽」であった場合にのみ評価されます。 |
2053 | 優先度の関係で、これ | |
2284 | 優先度の関係で、これを C<||> 演算子の置き換えに使うのは慎重に | |
2285 | 避けなければなりません。 | |
2286 | これは普通代入よりも、フローの制御でうまく動作します: | |
2054 | 2287 | |
2288 | =begin original | |
2289 | ||
2055 | 2290 | $a = $b or $c; # bug: this is wrong |
2056 | 2291 | ($a = $b) or $c; # really means this |
2057 | 2292 | $a = $b || $c; # better written this way |
2058 | 2293 | |
2294 | =end original | |
2295 | ||
2296 | $a = $b or $c; # バグ: これは間違い | |
2297 | ($a = $b) or $c; # 本当にしたいこと | |
2298 | $a = $b || $c; # こう書いた方がいい | |
2299 | ||
2059 | 2300 | =begin original |
2060 | 2301 | |
2061 | 2302 | However, when it's a list-context assignment and you're trying to use |
2062 | ||
2303 | C<||> for control flow, you probably need "or" so that the assignment | |
2063 | 2304 | takes higher precedence. |
2064 | 2305 | |
2065 | 2306 | =end original |
2066 | 2307 | |
2067 | しかし、代入がリストコンテキストの時に | |
2308 | しかし、代入がリストコンテキストの時に C<||> をフロー制御に使おうとする場合、 | |
2068 | 2309 | 代入により大きな優先順位を持たせるために "or" が必要かもしれません。 |
2069 | 2310 | |
2311 | =begin original | |
2312 | ||
2070 | 2313 | @info = stat($file) || die; # oops, scalar sense of stat! |
2071 | 2314 | @info = stat($file) or die; # better, now @info gets its due |
2072 | 2315 | |
2316 | =end original | |
2317 | ||
2318 | @info = stat($file) || die; # うわ、stat がスカラの意味だ! | |
2319 | @info = stat($file) or die; # よりよい; @info はその目的を果たす | |
2320 | ||
2073 | 2321 | =begin original |
2074 | 2322 | |
2075 | 2323 | Then again, you could always use parentheses. |
2076 | 2324 | |
2077 | 2325 | =end original |
2078 | 2326 | |
2079 | 2327 | もちろん、常に括弧をつけてもよいです。 |
2080 | 2328 | |
2081 | 2329 | =begin original |
2082 | 2330 | |
2083 | 2331 | Binary "xor" returns the exclusive-OR of the two surrounding expressions. |
2084 | It cannot short | |
2332 | It cannot short-circuit (of course). | |
2085 | 2333 | |
2086 | 2334 | =end original |
2087 | 2335 | |
2088 | 2336 | 二項演算子の "xor" は両側の式の排他論理和を返します。 |
2089 | これはもちろん | |
2337 | これは (もちろん) 短絡できません。 | |
2090 | 2338 | |
2091 | 2339 | =head2 C Operators Missing From Perl |
2092 | 2340 | X<operator, missing from perl> X<&> X<*> |
2093 | 2341 | X<typecasting> X<(TYPE)> |
2094 | 2342 | |
2095 | 2343 | (Perl にない C の演算子) |
2096 | 2344 | |
2097 | 2345 | =begin original |
2098 | 2346 | |
2099 | 2347 | Here is what C has that Perl doesn't: |
2100 | 2348 | |
2101 | 2349 | =end original |
2102 | 2350 | |
2103 | 2351 | C にあって Perl に無いものは以下の通りです: |
2104 | 2352 | |
2105 | 2353 | =over 8 |
2106 | 2354 | |
2107 | 2355 | =item unary & |
2108 | 2356 | |
2109 | 2357 | =begin original |
2110 | 2358 | |
2111 | 2359 | Address-of operator. (But see the "\" operator for taking a reference.) |
2112 | 2360 | |
2113 | 2361 | =end original |
2114 | 2362 | |
2115 | 2363 | アドレス演算子。 |
2116 | 2364 | ("\" 演算子がリファレンスのために用いられます。) |
2117 | 2365 | |
2118 | 2366 | =item unary * |
2119 | 2367 | |
2120 | 2368 | =begin original |
2121 | 2369 | |
2122 | 2370 | Dereference-address operator. (Perl's prefix dereferencing |
2123 | 2371 | operators are typed: $, @, %, and &.) |
2124 | 2372 | |
2125 | 2373 | =end original |
2126 | 2374 | |
2127 | 2375 | 被アドレス参照演算子。 |
2128 | 2376 | (Perl の被参照プリフィクス演算子が型づけを行ないます: $, @, %, &。) |
2129 | 2377 | |
2130 | 2378 | =item (TYPE) |
2131 | 2379 | |
2132 | 2380 | =begin original |
2133 | 2381 | |
2134 | 2382 | Type-casting operator. |
2135 | 2383 | |
2136 | 2384 | =end original |
2137 | 2385 | |
2138 | 2386 | 型のキャスト演算子。 |
2139 | 2387 | |
2140 | 2388 | =back |
2141 | 2389 | |
2142 | 2390 | =head2 Quote and Quote-like Operators |
2143 | 2391 | X<operator, quote> X<operator, quote-like> X<q> X<qq> X<qx> X<qw> X<m> |
2144 | 2392 | X<qr> X<s> X<tr> X<'> X<''> X<"> X<""> X<//> X<`> X<``> X<<< << >>> |
2145 | 2393 | X<escape sequence> X<escape> |
2146 | 2394 | |
2147 | 2395 | (クォートとクォート風の演算子) |
2148 | 2396 | |
2149 | 2397 | =begin original |
2150 | 2398 | |
2151 | 2399 | While we usually think of quotes as literal values, in Perl they |
2152 | 2400 | function as operators, providing various kinds of interpolating and |
2153 | 2401 | pattern matching capabilities. Perl provides customary quote characters |
2154 | 2402 | for these behaviors, but also provides a way for you to choose your |
2155 | 2403 | quote character for any of them. In the following table, a C<{}> represents |
2156 | 2404 | any pair of delimiters you choose. |
2157 | 2405 | |
2158 | 2406 | =end original |
2159 | 2407 | |
2160 | 2408 | クォートはリテラル値であると考えるのが普通ですが、Perl において、 |
2161 | 2409 | クォートは演算子として働き、さまざまな展開やパターンマッチの機能を |
2162 | 2410 | 持っています。 |
2163 | 2411 | そのような動作をさせるのに、Perl は慣習的にクォート文字を使っていますが、 |
2164 | 2412 | どの種類のクォートも、自分でクォート文字を選べるようになっています。 |
2165 | 2413 | 以下の表では、{} がその選んだ区切文字のペアを示しています。 |
2166 | 2414 | |
2167 | 2415 | =begin original |
2168 | 2416 | |
2169 | 2417 | Customary Generic Meaning Interpolates |
2170 | 2418 | '' q{} Literal no |
2171 | 2419 | "" qq{} Literal yes |
2172 | 2420 | `` qx{} Command yes* |
2173 | 2421 | qw{} Word list no |
2174 | 2422 | // m{} Pattern match yes* |
2175 | 2423 | qr{} Pattern yes* |
2176 | 2424 | s{}{} Substitution yes* |
2177 | 2425 | tr{}{} Transliteration no (but see below) |
2426 | y{}{} Transliteration no (but see below) | |
2178 | 2427 | <<EOF here-doc yes* |
2179 | 2428 | |
2180 | 2429 | * unless the delimiter is ''. |
2181 | 2430 | |
2182 | 2431 | =end original |
2183 | 2432 | |
2184 | 2433 | 通常記法 汎用記法 意味 展開 |
2185 | 2434 | ================================================= |
2186 | 2435 | '' q{} リテラル 不可 |
2187 | 2436 | "" qq{} リテラル 可 |
2188 | 2437 | `` qx{} コマンド 可 * |
2189 | 2438 | qw{} 単語リスト 不可 |
2190 | 2439 | // m{} パターンマッチ 可 * |
2191 | 2440 | qr{} パターン 可 * |
2192 | 2441 | s{}{} 置換 可 * |
2193 | 2442 | tr{}{} 変換 不可 (但し以下を参照のこと) |
2443 | y{}{} 変換 不可 (但し以下を参照のこと) | |
2194 | 2444 | <<EOF ヒアドキュメント 可 * |
2195 | 2445 | |
2196 | 2446 | * '' がデリミタでない場合のみ |
2197 | 2447 | |
2198 | 2448 | =begin original |
2199 | 2449 | |
2200 | 2450 | Non-bracketing delimiters use the same character fore and aft, but the four |
2201 | sorts of brackets (round, angle, square, curly) | |
2451 | sorts of ASCII brackets (round, angle, square, curly) all nest, which means | |
2202 | 2452 | that |
2203 | 2453 | |
2204 | 2454 | =end original |
2205 | 2455 | |
2206 | 2456 | 選んだ区切文字が括弧の類でない場合には、前後の文字として同一のものを |
2207 | 使いますが、4 つの | |
2457 | 使いますが、4 つの ASCII のかっこ ((), <>, [], {}) の場合にはネストできます; | |
2208 | 2458 | つまり、以下のものは、 |
2209 | 2459 | |
2210 | ||
2460 | q{foo{bar}baz} | |
2211 | 2461 | |
2212 | 2462 | =begin original |
2213 | 2463 | |
2214 | 2464 | is the same as |
2215 | 2465 | |
2216 | 2466 | =end original |
2217 | 2467 | |
2218 | 2468 | 以下と同じです。 |
2219 | 2469 | |
2220 | ||
2470 | 'foo{bar}baz' | |
2221 | 2471 | |
2222 | 2472 | =begin original |
2223 | 2473 | |
2224 | 2474 | Note, however, that this does not always work for quoting Perl code: |
2225 | 2475 | |
2226 | 2476 | =end original |
2227 | 2477 | |
2228 | 2478 | しかし、以下のコードはクォートされた Perl コードでは |
2229 | 2479 | いつも正しく動くわけではないことに注意してください: |
2230 | 2480 | |
2231 | ||
2481 | $s = q{ if($a eq "}") ... }; # WRONG | |
2232 | 2482 | |
2233 | 2483 | =begin original |
2234 | 2484 | |
2235 | is a syntax error. The C<Text::Balanced> module ( | |
2485 | is a syntax error. The C<Text::Balanced> module (standard as of v5.8, | |
2236 | ||
2486 | and from CPAN before then) is able to do this properly. | |
2237 | to do this properly. | |
2238 | 2487 | |
2239 | 2488 | =end original |
2240 | 2489 | |
2241 | 2490 | これは文法エラーとなります。 |
2242 | C<Text::Balanced> モジュール( | |
2491 | C<Text::Balanced> モジュール(Perl 5.8 からは標準配布、それ以前は CPAN に | |
2243 | ||
2492 | あります)はこれを適切に行います。 | |
2244 | 2493 | |
2245 | 2494 | =begin original |
2246 | 2495 | |
2247 | 2496 | There can be whitespace between the operator and the quoting |
2248 | 2497 | characters, except when C<#> is being used as the quoting character. |
2249 | 2498 | C<q#foo#> is parsed as the string C<foo>, while C<q #foo#> is the |
2250 | 2499 | operator C<q> followed by a comment. Its argument will be taken |
2251 | 2500 | from the next line. This allows you to write: |
2252 | 2501 | |
2253 | 2502 | =end original |
2254 | 2503 | |
2255 | 演算子とクォート文字の間に空白を置くことも出来ます | |
2504 | 演算子とクォート文字の間に空白を置くことも出来ます; ただし、C<#> を | |
2256 | ||
2505 | クォート文字として使う場合は例外です。 | |
2257 | 2506 | C<q#foo#> は文字列 C<foo> としてパースされますが、 |
2258 | 2507 | C<q #foo#> は C<q> 演算子の後にコメントがあるとみなされます。 |
2259 | 2508 | この引数は次の行から取られます。つまり、以下のように書けます: |
2260 | 2509 | |
2510 | =begin original | |
2511 | ||
2261 | 2512 | s {foo} # Replace foo |
2262 | 2513 | {bar} # with bar. |
2263 | 2514 | |
2515 | =end original | |
2516 | ||
2517 | s {foo} # foo を | |
2518 | {bar} # bar で置き換える | |
2519 | ||
2264 | 2520 | =begin original |
2265 | 2521 | |
2266 | The following escape sequences are available in constructs that interpolate | |
2522 | The following escape sequences are available in constructs that interpolate, | |
2267 | and in transliterations | |
2523 | and in transliterations: | |
2268 | X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> | |
2524 | X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}> | |
2525 | X<\o{}> | |
2269 | 2526 | |
2270 | 2527 | =end original |
2271 | 2528 | |
2272 | 以下のエスケープシーケンスが展開と文字変換の構文で利用可能です | |
2529 | 以下のエスケープシーケンスが展開と文字変換の構文で利用可能です: | |
2273 | X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> | |
2530 | X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}> | |
2531 | X<\o{}> | |
2274 | 2532 | |
2275 | 2533 | =begin original |
2276 | 2534 | |
2277 | | |
2535 | Sequence Note Description | |
2278 | \ | |
2536 | \t tab (HT, TAB) | |
2279 | \ | |
2537 | \n newline (NL) | |
2280 | \ | |
2538 | \r return (CR) | |
2281 | \ | |
2539 | \f form feed (FF) | |
2282 | \ | |
2540 | \b backspace (BS) | |
2283 | \ | |
2541 | \a alarm (bell) (BEL) | |
2284 | \ | |
2542 | \e escape (ESC) | |
2285 | \x1 | |
2543 | \x{263A} [1,8] hex char (example: SMILEY) | |
2286 | \x | |
2544 | \x1b [2,8] restricted range hex char (example: ESC) | |
2287 | \ | |
2545 | \N{name} [3] named Unicode character or character sequence | |
2288 | \N{ | |
2546 | \N{U+263D} [4,8] Unicode character (example: FIRST QUARTER MOON) | |
2547 | \c[ [5] control char (example: chr(27)) | |
2548 | \o{23072} [6,8] octal char (example: SMILEY) | |
2549 | \033 [7,8] restricted range octal char (example: ESC) | |
2289 | 2550 | |
2290 | 2551 | =end original |
2291 | 2552 | |
2292 | | |
2553 | シーケンス 注意 説明 | |
2293 | \ | |
2554 | \t タブ (HT, TAB) | |
2294 | \ | |
2555 | \n 改行 (NL) | |
2295 | \ | |
2556 | \r 復帰 (CR) | |
2296 | \ | |
2557 | \f 改ページ (FF) | |
2297 | \ | |
2558 | \b バックスペース (BS) | |
2298 | \ | |
2559 | \a アラーム (BEL) | |
2299 | \ | |
2560 | \e エスケープ (ESC) | |
2300 | \x | |
2561 | \x{263a} [1,8] 16 進数で表した文字 (例: SMILEY) | |
2301 | \x | |
2562 | \x1b [2,8] 範囲制限された 16 進数で表した文字 (例: ESC) | |
2302 | \ | |
2563 | \N{name} [3] 名前つき Unicode 文字または文字シーケンス | |
2303 | \N{ | |
2564 | \N{U+263D} [4,8] Unicode 文字 (例: FIRST QUARTER MOON) | |
2565 | \c[ [5] 制御文字 (例: ESC) | |
2566 | \o{23072} [6,8] 8 進数で表した文字 (例: SMILEY) | |
2567 | \033 [7,8] 範囲制限された 8 進数で表した文字 (例: ESC) | |
2304 | 2568 | |
2569 | =over 4 | |
2570 | ||
2571 | =item [1] | |
2572 | ||
2305 | 2573 | =begin original |
2306 | 2574 | |
2307 | The character | |
2575 | The result is the character specified by the hexadecimal number between | |
2308 | ||
2576 | the braces. See L</[8]> below for details on which character. | |
2309 | the 7th bit (0x40). The most interesting range is from '@' to '_' | |
2310 | (0x40 through 0x5F), resulting in a control character from 0x00 | |
2311 | through 0x1F. A '?' maps to the DEL character. On EBCDIC systems only | |
2312 | '@', the letters, '[', '\', ']', '^', '_' and '?' will work, resulting | |
2313 | in 0x00 through 0x1F and 0x7F. | |
2314 | 2577 | |
2315 | 2578 | =end original |
2316 | 2579 | |
2317 | ||
2580 | 結果は中かっこで囲まれた 16 進数で指定された文字です。 | |
2318 | ||
2581 | その文字に関する詳細については以下の L</[8]> を参照してください。 | |
2319 | 最も興味深い範囲は '@' から '_' (0x40 から 0x5F) で、結果として | |
2320 | コントロール文字 0x00 から 0x1F になります。 | |
2321 | '?' は DEL 文字にマッピングされます。 | |
2322 | EBCDIC システムでは '@', 英字, '[', '\', ']', '^', '_', '?' のみが | |
2323 | 動作し、結果として 0x00 から 0x1F と 0x7F になります。 | |
2324 | 2582 | |
2325 | 2583 | =begin original |
2326 | 2584 | |
2327 | ||
2585 | Only hexadecimal digits are valid between the braces. If an invalid | |
2328 | ||
2586 | character is encountered, a warning will be issued and the invalid | |
2587 | character and all subsequent characters (valid or invalid) within the | |
2588 | braces will be discarded. | |
2329 | 2589 | |
2330 | 2590 | =end original |
2331 | 2591 | |
2592 | 中かっこの中には 16 進数字のみが妥当です。 | |
2593 | 不正な文字に遭遇すると、警告が発生し、中かっこの内側の不正な文字と | |
2594 | それ以降の文字(妥当でも不正でも)は捨てられます。 | |
2595 | ||
2596 | =begin original | |
2597 | ||
2598 | If there are no valid digits between the braces, the generated character is | |
2599 | the NULL character (C<\x{00}>). However, an explicit empty brace (C<\x{}>) | |
2600 | will not cause a warning (currently). | |
2601 | ||
2602 | =end original | |
2603 | ||
2604 | 中かっこの中に妥当な文字がなければ、生成される文字は NULL 文字 | |
2605 | (C<\x{00}>) です。 | |
2606 | しかし、明示的な空の中かっこ (C<\x{}>) は(今のところ)警告を出しません。 | |
2607 | ||
2608 | =item [2] | |
2609 | ||
2610 | =begin original | |
2611 | ||
2612 | The result is the character specified by the hexadecimal number in the range | |
2613 | 0x00 to 0xFF. See L</[8]> below for details on which character. | |
2614 | ||
2615 | =end original | |
2616 | ||
2617 | 結果は 0x00 から 0xFF の範囲の 16 進数で指定された文字です。 | |
2618 | その文字に関する詳細については以下の L</[8]> を参照してください。 | |
2619 | ||
2620 | =begin original | |
2621 | ||
2622 | Only hexadecimal digits are valid following C<\x>. When C<\x> is followed | |
2623 | by fewer than two valid digits, any valid digits will be zero-padded. This | |
2624 | means that C<\x7> will be interpreted as C<\x07>, and a lone <\x> will be | |
2625 | interpreted as C<\x00>. Except at the end of a string, having fewer than | |
2626 | two valid digits will result in a warning. Note that although the warning | |
2627 | says the illegal character is ignored, it is only ignored as part of the | |
2628 | escape and will still be used as the subsequent character in the string. | |
2629 | For example: | |
2630 | ||
2631 | =end original | |
2632 | ||
2633 | C<\x> に引き続くのは 16 進数字のみが妥当です。 | |
2634 | C<\x> に引き続く妥当な数字が 2 桁ない場合、妥当な数字はゼロで | |
2635 | パッディングされます。 | |
2636 | これは、C<\x7> は C<\x07> と解釈され、単独の <\x> は C<\x00> と | |
2637 | 解釈されるということです。 | |
2638 | 文字列の末尾を例外として、妥当な数字が 2 桁ない場合は警告が発生します。 | |
2639 | 警告は不正な文字が無視されると言うにも関わらず、エスケープの一部のみが無視され、 | |
2640 | 文字列中の引き続く文字は使われるままであることに注意してください。 | |
2641 | 例えば: | |
2642 | ||
2643 | Original Result Warns? | |
2644 | "\x7" "\x07" no | |
2645 | "\x" "\x00" no | |
2646 | "\x7q" "\x07q" yes | |
2647 | "\xq" "\x00q" yes | |
2648 | ||
2649 | =item [3] | |
2650 | ||
2651 | =begin original | |
2652 | ||
2653 | The result is the Unicode character or character sequence given by I<name>. | |
2654 | See L<charnames>. | |
2655 | ||
2656 | =end original | |
2657 | ||
2658 | 結果は I<name> で指定される Unicode 文字または文字の並びです。 | |
2659 | L<charnames> を参照してください。 | |
2660 | ||
2661 | =item [4] | |
2662 | ||
2663 | =begin original | |
2664 | ||
2665 | C<\N{U+I<hexadecimal number>}> means the Unicode character whose Unicode code | |
2666 | point is I<hexadecimal number>. | |
2667 | ||
2668 | =end original | |
2669 | ||
2670 | C<\N{U+I<hexadecimal number>}> は、Unicode 符号位置が I<hexadecimal number> の | |
2671 | Unicode 文字を意味します。 | |
2672 | ||
2673 | =item [5] | |
2674 | ||
2675 | =begin original | |
2676 | ||
2677 | The character following C<\c> is mapped to some other character as shown in the | |
2678 | table: | |
2679 | ||
2680 | =end original | |
2681 | ||
2682 | C<\c> に引き続く文字は以下の表に示すように他の文字にマッピングされます: | |
2683 | ||
2684 | Sequence Value | |
2685 | \c@ chr(0) | |
2686 | \cA chr(1) | |
2687 | \ca chr(1) | |
2688 | \cB chr(2) | |
2689 | \cb chr(2) | |
2690 | ... | |
2691 | \cZ chr(26) | |
2692 | \cz chr(26) | |
2693 | \c[ chr(27) | |
2694 | \c] chr(29) | |
2695 | \c^ chr(30) | |
2696 | \c? chr(127) | |
2697 | ||
2698 | =begin original | |
2699 | ||
2700 | Also, C<\c\I<X>> yields C< chr(28) . "I<X>"> for any I<X>, but cannot come at the | |
2701 | end of a string, because the backslash would be parsed as escaping the end | |
2702 | quote. | |
2703 | ||
2704 | =end original | |
2705 | ||
2706 | また、C<\c\I<X>> は任意の I<X> について C< chr(28) . "I<X>"> となりますが、 | |
2707 | 文字列の末尾には来ません; 逆スラッシュは末尾のクォートをエスケープするように | |
2708 | パースされるからです。 | |
2709 | ||
2710 | =begin original | |
2711 | ||
2712 | On ASCII platforms, the resulting characters from the list above are the | |
2713 | complete set of ASCII controls. This isn't the case on EBCDIC platforms; see | |
2714 | L<perlebcdic/OPERATOR DIFFERENCES> for the complete list of what these | |
2715 | sequences mean on both ASCII and EBCDIC platforms. | |
2716 | ||
2717 | =end original | |
2718 | ||
2719 | ASCII プラットフォームでは、上述の一覧からの結果の文字は ASCII 制御文字の | |
2720 | 完全な集合です。 | |
2721 | これは EBCDIC プラットフォームには当てはまりません; これらの並びが | |
2722 | ASCII と EBCDIC プラットフォームで何を意味するかの完全な一覧は | |
2723 | L<perlebcdic/OPERATOR DIFFERENCES> を参照してください。 | |
2724 | ||
2725 | =begin original | |
2726 | ||
2727 | Use of any other character following the "c" besides those listed above is | |
2728 | discouraged, and some are deprecated with the intention of removing | |
2729 | those in Perl 5.16. What happens for any of these | |
2730 | other characters currently though, is that the value is derived by inverting | |
2731 | the 7th bit (0x40). | |
2732 | ||
2733 | =end original | |
2734 | ||
2735 | "c" に引き続いて上述した以外の文字を使うことは非推奨であり、Perl 5.16 での | |
2736 | 削除を意図した廃止予定です。 | |
2737 | しかし、現在のところ他の文字を置いたときに起こることは、値が第 7 ビット | |
2738 | (0x40) を反転させたものになります。 | |
2739 | ||
2740 | =begin original | |
2741 | ||
2742 | To get platform independent controls, you can use C<\N{...}>. | |
2743 | ||
2744 | =end original | |
2745 | ||
2746 | プラットフォーム非依存の制御文字を得るには、C<\N{...}> を使ってください。 | |
2747 | ||
2748 | =item [6] | |
2749 | ||
2750 | =begin original | |
2751 | ||
2752 | The result is the character specified by the octal number between the braces. | |
2753 | See L</[8]> below for details on which character. | |
2754 | ||
2755 | =end original | |
2756 | ||
2757 | 結果は中かっこで囲まれた 8 進数で指定された文字です。 | |
2758 | その文字に関する詳細については以下の L</[8]> を参照してください。 | |
2759 | ||
2760 | =begin original | |
2761 | ||
2762 | If a character that isn't an octal digit is encountered, a warning is raised, | |
2763 | and the value is based on the octal digits before it, discarding it and all | |
2764 | following characters up to the closing brace. It is a fatal error if there are | |
2765 | no octal digits at all. | |
2766 | ||
2767 | =end original | |
2768 | ||
2769 | 8 進数でない文字に遭遇すると、警告が発生し、値はそこまでの 8 進数字を | |
2770 | 基として、それ以降閉じ中かっこまでの全ての文字を捨てます。 | |
2771 | 8 進数字がまったくないと致命的エラーになります。 | |
2772 | ||
2773 | =item [7] | |
2774 | ||
2775 | =begin original | |
2776 | ||
2777 | The result is the character specified by the three-digit octal number in the | |
2778 | range 000 to 777 (but best to not use above 077, see next paragraph). See | |
2779 | L</[8]> below for details on which character. | |
2780 | ||
2781 | =end original | |
2782 | ||
2783 | 結果は範囲 000 から 777 までの 3 桁の 8 進数で指定される文字です | |
2784 | (しかし 077 より上は使わないのが最良です; 次の段落を参照してください)。 | |
2785 | その文字に関する詳細については以下の L</[8]> を参照してください。 | |
2786 | ||
2787 | =begin original | |
2788 | ||
2789 | Some contexts allow 2 or even 1 digit, but any usage without exactly | |
2790 | three digits, the first being a zero, may give unintended results. (For | |
2791 | example, see L<perlrebackslash/Octal escapes>.) Starting in Perl 5.14, you may | |
2792 | use C<\o{}> instead, which avoids all these problems. Otherwise, it is best to | |
2793 | use this construct only for ordinals C<\077> and below, remembering to pad to | |
2794 | the left with zeros to make three digits. For larger ordinals, either use | |
2795 | C<\o{}> , or convert to something else, such as to hex and use C<\x{}> | |
2796 | instead. | |
2797 | ||
2798 | =end original | |
2799 | ||
2800 | 一部のコンテキストでは 2 桁や、1 桁ですらゆるされますが、正確に 3 桁かつ | |
2801 | 先頭が 0、以外の使い方は意図していない結果をもたらすかもしれません。 | |
2802 | (例えば、L<perlrebackslash/Octal escapes> を参照してください。) | |
2803 | Perl 5.14 から、代わりに C<\o{}> を使えます; これはこれらすべての問題を | |
2804 | 避けられます。 | |
2805 | さもなければ、この構文を値 C<\077> 以下でのみ使用するのが最良です; | |
2806 | 3 桁にするために左側にゼロをパッディングするのを忘れないでください。 | |
2807 | より大きな値では、C<\o{}> を使うか、代わりに 16 進数にして C<\x{}> を | |
2808 | 使うような、他のものに変換してください。 | |
2809 | ||
2810 | =begin original | |
2811 | ||
2812 | Having fewer than 3 digits may lead to a misleading warning message that says | |
2813 | that what follows is ignored. For example, C<"\128"> in the ASCII character set | |
2814 | is equivalent to the two characters C<"\n8">, but the warning C<Illegal octal | |
2815 | digit '8' ignored> will be thrown. To avoid this warning, make sure to pad | |
2816 | your octal number with C<0>'s: C<"\0128">. | |
2817 | ||
2818 | =end original | |
2819 | ||
2820 | 3 桁より少ない場合は以後が無視されるという間違った警告メッセージを | |
2821 | 引き起こすかもしれません。 | |
2822 | 例えば、ASCII 文字集合での C<"\128"> は 2 文字 C<"\n8"> と等価ですが、 | |
2823 | C<Illegal octal digit '8' ignored> という警告が投げられます。 | |
2824 | この警告を避けるには、8 進数を C<0> でパッディングしてください: C<"\0128">。 | |
2825 | ||
2826 | =item [8] | |
2827 | ||
2828 | =begin original | |
2829 | ||
2830 | Several constructs above specify a character by a number. That number | |
2831 | gives the character's position in the character set encoding (indexed from 0). | |
2832 | This is called synonymously its ordinal, code position, or code point. Perl | |
2833 | works on platforms that have a native encoding currently of either ASCII/Latin1 | |
2834 | or EBCDIC, each of which allow specification of 256 characters. In general, if | |
2835 | the number is 255 (0xFF, 0377) or below, Perl interprets this in the platform's | |
2836 | native encoding. If the number is 256 (0x100, 0400) or above, Perl interprets | |
2837 | it as a Unicode code point and the result is the corresponding Unicode | |
2838 | character. For example C<\x{50}> and C<\o{120}> both are the number 80 in | |
2839 | decimal, which is less than 256, so the number is interpreted in the native | |
2840 | character set encoding. In ASCII the character in the 80th position (indexed | |
2841 | from 0) is the letter "P", and in EBCDIC it is the ampersand symbol "&". | |
2842 | C<\x{100}> and C<\o{400}> are both 256 in decimal, so the number is interpreted | |
2843 | as a Unicode code point no matter what the native encoding is. The name of the | |
2844 | character in the 100th position (indexed by 0) in Unicode is | |
2845 | C<LATIN CAPITAL LETTER A WITH MACRON>. | |
2846 | ||
2847 | =end original | |
2848 | ||
2849 | 上述のいくつかの構造は文字を数値で指定しています。 | |
2850 | この値は文字集合エンコーディングで (0 から始めた) 文字の位置を指定します。 | |
2851 | これは同意語として序数(ordinal)、コード位置(code position)、 | |
2852 | 符号位置(code point)と呼ばれます。 | |
2853 | Perl は現在のところ、それぞれ 256 文字を定義している ASCII/Latin1 または | |
2854 | EBCDIC のどちらかのネイティブエンコーディングを持つプラットフォームで | |
2855 | 動作します。 | |
2856 | 一般的に、数値が 255 (0xFF, 0377) 以下なら、Perl はこれをプラットフォームの | |
2857 | ネイティブエンコーディングと解釈します。 | |
2858 | 数値が 256 (0x100, 0400) 以上なら、Perl はこれを Unicode 符号位置と解釈し、 | |
2859 | 結果は対応する Unicode 文字となります。 | |
2860 | 例えば C<\x{50}> と C<\o{120}> はどちらも 10 進数では 80 で、これは 256 より | |
2861 | 小さいので、数値はネイティブ文字集合エンコーディングとして解釈されます。 | |
2862 | ASCII では (0 から始めて) 80 番目の位置の文字は "P" で、EBCDIC では | |
2863 | アンパサンド記号 "&" です。 | |
2864 | C<\x{100}> と C<\o{400}> はどちらも 10 進数では 256 なので、数値は | |
2865 | ネイティブエンコーディングが何かに関わらず Unicode 符号位置として解釈されます。 | |
2866 | Unicode での (0 から始めて) 100 番目の位置の文字の名前は | |
2867 | C<LATIN CAPITAL LETTER A WITH MACRON> です。 | |
2868 | ||
2869 | =begin original | |
2870 | ||
2871 | There are a couple of exceptions to the above rule. C<\N{U+I<hex number>}> is | |
2872 | always interpreted as a Unicode code point, so that C<\N{U+0050}> is "P" even | |
2873 | on EBCDIC platforms. And if L<C<S<use encoding>>|encoding> is in effect, the | |
2874 | number is considered to be in that encoding, and is translated from that into | |
2875 | the platform's native encoding if there is a corresponding native character; | |
2876 | otherwise to Unicode. | |
2877 | ||
2878 | =end original | |
2879 | ||
2880 | 上述の規則には二つの例外があります。 | |
2881 | C<\N{U+I<hex number>}> は常に Unicode 符号位置として解釈されるので、 | |
2882 | C<\N{U+0050}> は EBCDIC プラットフォームでも "P" です。 | |
2883 | そして L<C<S<use encoding>>|encoding> が有効なら、数値はその | |
2884 | エンコーディングであると考えられ、対応するネイティブな文字があるなら | |
2885 | プラットフォームのネイティブなエンコーディングに変換されます; さもなければ | |
2886 | Unicode です。 | |
2887 | ||
2888 | =back | |
2889 | ||
2890 | =begin original | |
2891 | ||
2892 | B<NOTE>: Unlike C and other languages, Perl has no C<\v> escape sequence for | |
2893 | the vertical tab (VT - ASCII 11), but you may use C<\ck> or C<\x0b>. (C<\v> | |
2894 | does have meaning in regular expression patterns in Perl, see L<perlre>.) | |
2895 | ||
2896 | =end original | |
2897 | ||
2332 | 2898 | B<注意>: C やその他の言語と違って、Perl は垂直タブ (VT - ASCII 11) のための |
2333 | 2899 | \v エスケープシーケンスはありませんが、C<\ck> または C<\x0b> が使えます。 |
2900 | (C<\v> は Perl の正規表現パターンでは意味があります; L<perlre> を | |
2901 | 参照してください。) | |
2334 | 2902 | |
2335 | 2903 | =begin original |
2336 | 2904 | |
2337 | The following escape sequences are available in constructs that interpolate | |
2905 | The following escape sequences are available in constructs that interpolate, | |
2338 | 2906 | but not in transliterations. |
2339 | 2907 | X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> |
2340 | 2908 | |
2341 | 2909 | =end original |
2342 | 2910 | |
2343 | 2911 | 以下のエスケープシーケンスが展開と文字変換の構文で利用可能です。 |
2344 | 2912 | X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> |
2345 | 2913 | |
2346 | 2914 | =begin original |
2347 | 2915 | |
2348 | \l lowercase next char | |
2916 | \l lowercase next character only | |
2349 | \u uppercase next char | |
2917 | \u titlecase (not uppercase!) next character only | |
2350 | \L lowercase till \E | |
2918 | \L lowercase all characters till \E seen | |
2351 | \U uppercase till \E | |
2919 | \U uppercase all characters till \E seen | |
2352 | \E end case modification | |
2353 | 2920 | \Q quote non-word characters till \E |
2921 | \E end either case modification or quoted section | |
2922 | (whichever was last seen) | |
2354 | 2923 | |
2355 | 2924 | =end original |
2356 | 2925 | |
2357 | \l | |
2926 | \l 次の文字だけを小文字にする | |
2358 | \u | |
2927 | \u 次の文字だけをタイトル文字(大文字ではありません!)にする | |
2359 | \L | |
2928 | \L \E まで小文字にする | |
2360 | \U | |
2929 | \U \E まで大文字にする | |
2361 | \E | |
2930 | \Q \E まで非単語文字をクォートする | |
2362 | \ | |
2931 | \E 大文字小文字変換かクォート部分(どちらか最後に現れたもの)を | |
2932 | 終了させる | |
2363 | 2933 | |
2364 | 2934 | =begin original |
2365 | 2935 | |
2936 | C<\L>, C<\U>, and C<\Q> can stack, in which case you need one | |
2937 | C<\E> for each. For example: | |
2938 | ||
2939 | =end original | |
2940 | ||
2941 | C<\L>, C<\U>, C<\Q> はスタックできます; この場合それぞれに対して C<\E> が | |
2942 | 必要です。 | |
2943 | 例えば: | |
2944 | ||
2945 | say "This \Qquoting \ubusiness \Uhere isn't quite\E done yet,\E is it?"; | |
2946 | This quoting\ Business\ HERE\ ISN\'T\ QUITE\ done\ yet\, is it? | |
2947 | ||
2948 | =begin original | |
2949 | ||
2366 | 2950 | If C<use locale> is in effect, the case map used by C<\l>, C<\L>, |
2367 | C<\u> and C<\U> is taken from the current locale. See L<perllocale>. | |
2951 | C<\u>, and C<\U> is taken from the current locale. See L<perllocale>. | |
2368 | If Unicode (for example, C<\N{}> or | |
2952 | If Unicode (for example, C<\N{}> or code points of 0x100 or | |
2369 | beyond) is being used, the case map used by C<\l>, C<\L>, C<\u> and | |
2953 | beyond) is being used, the case map used by C<\l>, C<\L>, C<\u>, and | |
2370 | C<\U> is as defined by Unicode. | |
2954 | C<\U> is as defined by Unicode. That means that case-mapping | |
2371 | se | |
2955 | a single character can sometimes produce several characters. | |
2372 | 2956 | |
2373 | 2957 | =end original |
2374 | 2958 | |
2375 | 2959 | C<use locale> が有効の場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる |
2376 | 2960 | 大文字小文字テーブルは現在のロケールのものが使われます。 |
2377 | 2961 | L<perllocale> を参照して下さい。 |
2378 | (例えば、C<\N{}> や、0x100 以上の | |
2962 | (例えば、C<\N{}> や、0x100 以上の符号位置の) Unicode が | |
2379 | 2963 | 使われている場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる大文字小文字 |
2380 | 2964 | テーブルは Unicode で定義されているものになります。 |
2381 | ||
2965 | これは、単一の文字の大文字小文字マッピングは複数の文字を生成することが | |
2966 | あるということです。 | |
2382 | 2967 | |
2383 | 2968 | =begin original |
2384 | 2969 | |
2385 | 2970 | All systems use the virtual C<"\n"> to represent a line terminator, |
2386 | 2971 | called a "newline". There is no such thing as an unvarying, physical |
2387 | 2972 | newline character. It is only an illusion that the operating system, |
2388 | 2973 | device drivers, C libraries, and Perl all conspire to preserve. Not all |
2389 | 2974 | systems read C<"\r"> as ASCII CR and C<"\n"> as ASCII LF. For example, |
2390 | on | |
2975 | on the ancient Macs (pre-MacOS X) of yesteryear, these used to be reversed, | |
2391 | ||
2976 | and on systems without line terminator, | |
2977 | printing C<"\n"> might emit no actual data. In general, use C<"\n"> when | |
2392 | 2978 | you mean a "newline" for your system, but use the literal ASCII when you |
2393 | 2979 | need an exact character. For example, most networking protocols expect |
2394 | 2980 | and prefer a CR+LF (C<"\015\012"> or C<"\cM\cJ">) for line terminators, |
2395 | 2981 | and although they often accept just C<"\012">, they seldom tolerate just |
2396 | 2982 | C<"\015">. If you get in the habit of using C<"\n"> for networking, |
2397 | 2983 | you may be burned some day. |
2398 | 2984 | X<newline> X<line terminator> X<eol> X<end of line> |
2399 | 2985 | X<\n> X<\r> X<\r\n> |
2400 | 2986 | |
2401 | 2987 | =end original |
2402 | 2988 | |
2403 | 2989 | 全てのシステムでは "newline" と呼ばれる行端末子を表現するために |
2404 | 2990 | 仮想的な C<"\n"> が用いられます。 |
2405 | 2991 | 普遍の、物理的な "newline" 文字と言うものはありません。 |
2406 | 2992 | オペレーティングシステム、デバイスドライバ、C ライブラリ、Perl が全て |
2407 | 2993 | 協力して保存しようとすると言うのは単なる幻想です。 |
2408 | 2994 | 全てのシステムで C<"\r"> を ASCII CR として、また C<"\n"> を |
2409 | 2995 | ASCII LF として読み込むわけではありません。 |
2410 | 例えば Mac ではこれらは保存され | |
2996 | 例えば昔の Mac (MacOS X 以前)ではこれらは保存され、 | |
2997 | 行端末子のないシステムでは、 | |
2411 | 2998 | C<"\n"> を print しても実際のデータは何も出力しません。 |
2412 | 2999 | 一般に、システムで "newline" を意味したいときには C<"\n"> を使いますが、 |
2413 | 3000 | 正確な文字が必要な場合はリテラルな ASCII を使います。 |
2414 | 3001 | 例えば、ほとんどのネットワークプロトコルでは行端末子として |
2415 | 3002 | CR+LF (C<"\015\012"> または C<"\cM\cJ">) を予想し、また好みますが、 |
2416 | 3003 | しばしば C<"\012"> だけでも許容し、さらに時々は C<"\015"> だけでも認めます。 |
2417 | 3004 | もしネットワーク関係で C<"\n"> を使う習慣がついていると、 |
2418 | 3005 | いつか痛い目を見ることになるでしょう。 |
3006 | X<newline> X<line terminator> X<eol> X<end of line> | |
3007 | X<\n> X<\r> X<\r\n> | |
2419 | 3008 | |
2420 | 3009 | =begin original |
2421 | 3010 | |
2422 | 3011 | For constructs that do interpolate, variables beginning with "C<$>" |
2423 | 3012 | or "C<@>" are interpolated. Subscripted variables such as C<$a[3]> or |
2424 | 3013 | C<< $href->{key}[0] >> are also interpolated, as are array and hash slices. |
2425 | 3014 | But method calls such as C<< $obj->meth >> are not. |
2426 | 3015 | |
2427 | 3016 | =end original |
2428 | 3017 | |
2429 | 3018 | 展開が行なわれる構文では、"C<$>" や "C<@>" で始まる変数が展開されます。 |
2430 | ||
3019 | C<$a[3]> や C<< $href->{key}[0] >> のような添え字付き変数もまた | |
2431 | ||
3020 | 配列やハッシュのスライスのように展開されます。 | |
2432 | 3021 | しかし、C<< $obj->meth >> のようなメソッド呼び出しは展開されません。 |
2433 | 3022 | |
2434 | 3023 | =begin original |
2435 | 3024 | |
2436 | 3025 | Interpolating an array or slice interpolates the elements in order, |
2437 | 3026 | separated by the value of C<$">, so is equivalent to interpolating |
2438 | C<join $", @array>. | |
3027 | C<join $", @array>. "Punctuation" arrays such as C<@*> are usually | |
2439 | interpolated if the name is enclosed in braces C<@{*}>, but | |
3028 | interpolated only if the name is enclosed in braces C<@{*}>, but the | |
2440 | arrays C<@_>, C<@+>, and C<@-> are interpolated | |
3029 | arrays C<@_>, C<@+>, and C<@-> are interpolated even without braces. | |
2441 | 3030 | |
2442 | 3031 | =end original |
2443 | 3032 | |
2444 | 3033 | 配列やスライスの展開は、要素を順番に、C<$"> の値で分割して展開されるので、 |
2445 | 3034 | C<join $", @array> の展開と等価です。 |
2446 | C<@*> のような「句読点」配列は名前が C<@{*}> のように中かっこで | |
3035 | C<@*> のような「句読点」配列は普通は名前が C<@{*}> のように中かっこで | |
2447 | 場合にのみ展開されますが、 | |
3036 | 囲われている場合にのみ展開されますが、配列 C<@_>, C<@+>, C<@-> は | |
2448 | 展開されます。 | |
3037 | 中かっこなしでも展開されます。 | |
2449 | 3038 | |
2450 | 3039 | =begin original |
2451 | 3040 | |
2452 | ||
3041 | For double-quoted strings, the quoting from C<\Q> is applied after | |
2453 | ||
3042 | interpolation and escapes are processed. | |
2454 | while escaping will cause the literal string C<\$> to be inserted. | |
2455 | You'll need to write something like C<m/\Quser\E\@\Qhost/>. | |
2456 | 3043 | |
2457 | 3044 | =end original |
2458 | 3045 | |
2459 | C<\Q> | |
3046 | ダブルクォートされた文字列では、C<\Q> からのクォートは文字変換と | |
2460 | エスケープされ | |
3047 | エスケープが処理された後に適用されます。 | |
2461 | ||
3049 | "abc\Qfoo\tbar$s\Exyz" | |
3050 | ||
3051 | =begin original | |
3052 | ||
3053 | is equivalent to | |
3054 | ||
3055 | =end original | |
3056 | ||
3057 | は以下と等価です: | |
3058 | ||
3059 | "abc" . quotemeta("foo\tbar$s") . "xyz" | |
3060 | ||
3061 | =begin original | |
3062 | ||
3063 | For the pattern of regex operators (C<qr//>, C<m//> and C<s///>), | |
3064 | the quoting from C<\Q> is applied after interpolation is processed, | |
3065 | but before escapes are processed. This allows the pattern to match | |
3066 | literally (except for C<$> and C<@>). For example, the following matches: | |
3067 | ||
3068 | =end original | |
3069 | ||
3070 | 正規表現演算子 (C<qr//>, C<m//> and C<s///>) のパターンでは、 | |
3071 | C<\Q> によるクォートは変数展開が行われた後、エスケープが処理される前に | |
3072 | 適用されます。 | |
3073 | これによりパターンを (C<$> and C<@>) 以外はリテラルにマッチングすることが | |
3074 | 出来ます。 | |
3075 | 例えば、以下はマッチングします: | |
3076 | ||
3077 | '\s\t' =~ /\Q\s\t/ | |
3078 | ||
3079 | =begin original | |
3080 | ||
3081 | Because C<$> or C<@> trigger interpolation, you'll need to use something | |
3082 | like C</\Quser\E\@\Qhost/> to match them literally. | |
3083 | ||
3084 | =end original | |
3085 | ||
3086 | C<$> や C<@> は変換を引き起こすので、リテラルにマッチングさせるためには | |
2462 | 3087 | C<m/\Quser\E\@\Qhost/> などという風に書く必要があります。 |
2463 | 3088 | |
2464 | 3089 | =begin original |
2465 | 3090 | |
2466 | 3091 | Patterns are subject to an additional level of interpretation as a |
2467 | 3092 | regular expression. This is done as a second pass, after variables are |
2468 | 3093 | interpolated, so that regular expressions may be incorporated into the |
2469 | 3094 | pattern from the variables. If this is not what you want, use C<\Q> to |
2470 | 3095 | interpolate a variable literally. |
2471 | 3096 | |
2472 | 3097 | =end original |
2473 | 3098 | |
2474 | 3099 | パターンはさらに、正規表現として展開が行なわれます。 |
2475 | 3100 | これは、変数が展開された後の 2 回目のパスで行なわれるので、変数に |
2476 | 3101 | 正規表現を含めておき、パターンの中へ展開することができます。 |
2477 | 3102 | もし、そうしたくないのであれば、C<\Q> を使うと変数の内容を文字通りに |
2478 | 3103 | 展開することができます。 |
2479 | 3104 | |
2480 | 3105 | =begin original |
2481 | 3106 | |
2482 | 3107 | Apart from the behavior described above, Perl does not expand |
2483 | 3108 | multiple levels of interpolation. In particular, contrary to the |
2484 | 3109 | expectations of shell programmers, back-quotes do I<NOT> interpolate |
2485 | 3110 | within double quotes, nor do single quotes impede evaluation of |
2486 | 3111 | variables when used within double quotes. |
2487 | 3112 | |
2488 | 3113 | =end original |
2489 | 3114 | |
2490 | 3115 | 上記の振る舞いを除けば、Perl は 複数の段階を踏んで展開を行ないません。 |
2491 | 3116 | 特に、シェルのプログラマの期待とは裏腹に、 |
2492 | 3117 | バッククォートはダブルクォートの中では展開されませんし、シングルクォートが |
2493 | 3118 | ダブルクォートの中で使われても、変数の展開を妨げることは I<ありません>。 |
2494 | 3119 | |
2495 | 3120 | =head2 Regexp Quote-Like Operators |
2496 | 3121 | X<operator, regexp> |
2497 | 3122 | |
2498 | 3123 | (正規表現のクォート風の演算子) |
2499 | 3124 | |
2500 | 3125 | =begin original |
2501 | 3126 | |
2502 | 3127 | Here are the quote-like operators that apply to pattern |
2503 | 3128 | matching and related activities. |
2504 | 3129 | |
2505 | 3130 | =end original |
2506 | 3131 | |
2507 | 3132 | 以下はパターンマッチングと関連する行動に関するクォート風の演算子です。 |
2508 | 3133 | |
2509 | 3134 | =over 8 |
2510 | 3135 | |
2511 | =item qr/STRING/msixpo | |
3136 | =item qr/STRING/msixpodual | |
2512 | 3137 | X<qr> X</i> X</m> X</o> X</s> X</x> X</p> |
2513 | 3138 | |
2514 | 3139 | =begin original |
2515 | 3140 | |
2516 | 3141 | This operator quotes (and possibly compiles) its I<STRING> as a regular |
2517 | 3142 | expression. I<STRING> is interpolated the same way as I<PATTERN> |
2518 | 3143 | in C<m/PATTERN/>. If "'" is used as the delimiter, no interpolation |
2519 | 3144 | is done. Returns a Perl value which may be used instead of the |
2520 | corresponding C</STRING/msixpo> expression. The returned value is a | |
3145 | corresponding C</STRING/msixpodual> expression. The returned value is a | |
2521 | 3146 | normalized version of the original pattern. It magically differs from |
2522 | 3147 | a string containing the same characters: C<ref(qr/x/)> returns "Regexp", |
2523 | 3148 | even though dereferencing the result returns undef. |
2524 | 3149 | |
2525 | 3150 | =end original |
2526 | 3151 | |
2527 | 3152 | この演算子は I<STRING> を正規表現としてクォートします |
2528 | 3153 | (そして可能ならコンパイルします)。 |
2529 | 3154 | I<STRING> は C<m/PATTERN/> 内の I<PATTERN> と同様に文字変換されます。 |
2530 | 3155 | "'" がデリミタとして使用された場合、文字変換は行われません。 |
2531 | 対応する C</STRING/msixpo> 表現の代わりに使われた Perl の値を返します。 | |
3156 | 対応する C</STRING/msixpodual> 表現の代わりに使われた Perl の値を返します。 | |
2532 | 3157 | 返り値は元のパターンを正規化したものです。 |
2533 | 3158 | これは不思議なことに、同じ文字を含む文字列とは異なります: |
2534 | 3159 | C<ref(qr/x/)> は、結果をデリファレンスすると未定義値を返すにも関わらず、 |
2535 | 3160 | "Regexp" を返します。 |
2536 | 3161 | |
2537 | 3162 | =begin original |
2538 | 3163 | |
2539 | 3164 | For example, |
2540 | 3165 | |
2541 | 3166 | =end original |
2542 | 3167 | |
2543 | 3168 | 例えば: |
2544 | 3169 | |
2545 | 3170 | $rex = qr/my.STRING/is; |
2546 | 3171 | print $rex; # prints (?si-xm:my.STRING) |
2547 | 3172 | s/$rex/foo/; |
2548 | 3173 | |
2549 | 3174 | =begin original |
2550 | 3175 | |
2551 | 3176 | is equivalent to |
2552 | 3177 | |
2553 | 3178 | =end original |
2554 | 3179 | |
2555 | 3180 | は以下と等価です: |
2556 | 3181 | |
2557 | 3182 | s/my.STRING/foo/is; |
2558 | 3183 | |
2559 | 3184 | =begin original |
2560 | 3185 | |
2561 | 3186 | The result may be used as a subpattern in a match: |
2562 | 3187 | |
2563 | 3188 | =end original |
2564 | 3189 | |
2565 | 3190 | 結果はマッチのサブパターンとして使えます: |
2566 | 3191 | |
3192 | =begin original | |
3193 | ||
2567 | 3194 | $re = qr/$pattern/; |
2568 | 3195 | $string =~ /foo${re}bar/; # can be interpolated in other patterns |
2569 | 3196 | $string =~ $re; # or used standalone |
2570 | 3197 | $string =~ /$re/; # or this way |
2571 | 3198 | |
3199 | =end original | |
3200 | ||
3201 | $re = qr/$pattern/; | |
3202 | $string =~ /foo${re}bar/; # 他のパターンに展開できる | |
3203 | $string =~ $re; # または単独で使う | |
3204 | $string =~ /$re/; # またはこのようにする | |
3205 | ||
2572 | 3206 | =begin original |
2573 | 3207 | |
2574 | Since Perl may compile the pattern at the moment of execution of qr() | |
3208 | Since Perl may compile the pattern at the moment of execution of the qr() | |
2575 | 3209 | operator, using qr() may have speed advantages in some situations, |
2576 | 3210 | notably if the result of qr() is used standalone: |
2577 | 3211 | |
2578 | 3212 | =end original |
2579 | 3213 | |
2580 | 3214 | Perl は qr() 演算子を実行する瞬間にパターンをコンパイルするので、 |
2581 | qr() を使うことでいくつかの場面で速度的に有利になります | |
3215 | qr() を使うことでいくつかの場面で速度的に有利になります; | |
2582 | 3216 | 特に qr() の結果が独立して使われる場合に有利になります。 |
2583 | 3217 | |
2584 | 3218 | sub match { |
2585 | 3219 | my $patterns = shift; |
2586 | 3220 | my @compiled = map qr/$_/i, @$patterns; |
2587 | 3221 | grep { |
2588 | 3222 | my $success = 0; |
2589 | 3223 | foreach my $pat (@compiled) { |
2590 | 3224 | $success = 1, last if /$pat/; |
2591 | 3225 | } |
2592 | 3226 | $success; |
2593 | 3227 | } @_; |
2594 | 3228 | } |
2595 | 3229 | |
2596 | 3230 | =begin original |
2597 | 3231 | |
2598 | 3232 | Precompilation of the pattern into an internal representation at |
2599 | 3233 | the moment of qr() avoids a need to recompile the pattern every |
2600 | 3234 | time a match C</$pat/> is attempted. (Perl has many other internal |
2601 | 3235 | optimizations, but none would be triggered in the above example if |
2602 | 3236 | we did not use qr() operator.) |
2603 | 3237 | |
2604 | 3238 | =end original |
2605 | 3239 | |
2606 | 3240 | qr() の時点でパターンを内部表現にプリコンパイルすることにより、C</$pat/> を |
2607 | 試みる毎に毎回パターンを再コンパイルするのを避けることができます | |
3241 | 試みる毎に毎回パターンを再コンパイルするのを避けることができます。 | |
2608 | 3242 | (Perl はその他にも多くの内部最適化を行いますが、上の例で qr() 演算子を |
2609 | 使わなかった場合はどの最適化も行われません | |
3243 | 使わなかった場合はどの最適化も行われません。) | |
2610 | 3244 | |
2611 | 3245 | =begin original |
2612 | 3246 | |
2613 | Options are: | |
3247 | Options (specified by the following modifiers) are: | |
2614 | 3248 | |
2615 | 3249 | =end original |
2616 | 3250 | |
2617 | オプションは以下の通りです: | |
3251 | (以下の修飾子で指定される)オプションは以下の通りです: | |
2618 | 3252 | |
2619 | 3253 | =begin original |
2620 | 3254 | |
2621 | 3255 | m Treat string as multiple lines. |
2622 | 3256 | s Treat string as single line. (Make . match a newline) |
2623 | 3257 | i Do case-insensitive pattern matching. |
2624 | 3258 | x Use extended regular expressions. |
2625 | 3259 | p When matching preserve a copy of the matched string so |
2626 | 3260 | that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be defined. |
2627 | 3261 | o Compile pattern only once. |
3262 | l Use the locale | |
3263 | u Use Unicode rules | |
3264 | a Use ASCII for \d, \s, \w; specifying two a's further restricts | |
3265 | /i matching so that no ASCII character will match a non-ASCII | |
3266 | one | |
3267 | d Use Unicode or native charset, as in 5.12 and earlier | |
2628 | 3268 | |
2629 | 3269 | =end original |
2630 | 3270 | |
2631 | m 文字列を複数行として扱う | |
3271 | m 文字列を複数行として扱う。 | |
2632 | s 文字列を一行として扱う (. が 改行にマッチングするようにする) | |
3272 | s 文字列を一行として扱う。 (. が 改行にマッチングするようにする) | |
2633 | i パターンマッチにおいて大文字小文字を区別しない | |
3273 | i パターンマッチにおいて大文字小文字を区別しない。 | |
2634 | x 拡張正規表現を使う | |
3274 | x 拡張正規表現を使う。 | |
2635 | 3275 | p マッチング時にマッチングした文字列を保存するので、 |
2636 | ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} が定義される | |
3276 | ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} が定義される。 | |
2637 | o 一度だけコンパイルする | |
3277 | o 一度だけコンパイルする。 | |
3278 | l ロケールを使う | |
3279 | u Unicode の規則を使う | |
3280 | a \d, \s, \w に ASCII を使う; a を二つ指定すると、/i で ASCII 文字が | |
3281 | 非 ASCII 文字にマッチングしないようにさらに制限する | |
3282 | d 5.12 以降かどうかで、Unicode かネイティブな文字集合を使う | |
2638 | 3283 | |
2639 | 3284 | =begin original |
2640 | 3285 | |
2641 | 3286 | If a precompiled pattern is embedded in a larger pattern then the effect |
2642 | of | |
3287 | of "msixpluad" will be propagated appropriately. The effect the "o" | |
2643 | 3288 | modifier has is not propagated, being restricted to those patterns |
2644 | 3289 | explicitly using it. |
2645 | 3290 | |
2646 | 3291 | =end original |
2647 | 3292 | |
2648 | 3293 | プリコンパイルされたパターンがより大きいパターンに組み込まれている場合、 |
2649 | ||
3294 | "msixpluad" の効果は適切に伝播します。 | |
2650 | ||
3295 | "o" 修飾子の効果は伝播せず、明示的に使われたパターンに制限されます。 | |
2651 | 3296 | |
2652 | 3297 | =begin original |
2653 | 3298 | |
3299 | The last four modifiers listed above, added in Perl 5.14, | |
3300 | control the character set semantics. | |
3301 | ||
3302 | =end original | |
3303 | ||
3304 | 上述のうち最後の四つの修飾子は Perl 5.14 で追加され、文字集合の意味論を | |
3305 | 制御します。 | |
3306 | ||
3307 | =begin original | |
3308 | ||
2654 | 3309 | See L<perlre> for additional information on valid syntax for STRING, and |
2655 | for a detailed look at the semantics of regular expressions. | |
3310 | for a detailed look at the semantics of regular expressions. In | |
3311 | particular, all the modifiers execpt C</o> are further explained in | |
3312 | L<perlre/Modifiers>. C</o> is described in the next section. | |
2656 | 3313 | |
2657 | 3314 | =end original |
2658 | 3315 | |
2659 | 3316 | STRING として有効な文法に関する追加の情報と、正規表現の意味論に関する |
2660 | 3317 | 詳細については、L<perlre> を参照してください。 |
3318 | 特に、C</o> 以外の全ての修飾子については L<perlre/Modifiers> でさらに | |
3319 | 説明されています。 | |
3320 | C</o> は次の節に記述されています。 | |
2661 | 3321 | |
2662 | =item m/PATTERN/msixpogc | |
3322 | =item m/PATTERN/msixpodualgc | |
2663 | 3323 | X<m> X<operator, match> |
2664 | 3324 | X<regexp, options> X<regexp> X<regex, options> X<regex> |
2665 | 3325 | X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> |
2666 | 3326 | |
2667 | =item /PATTERN/msixpogc | |
3327 | =item /PATTERN/msixpodualgc | |
2668 | 3328 | |
2669 | 3329 | =begin original |
2670 | 3330 | |
2671 | 3331 | Searches a string for a pattern match, and in scalar context returns |
2672 | 3332 | true if it succeeds, false if it fails. If no string is specified |
2673 | 3333 | via the C<=~> or C<!~> operator, the $_ string is searched. (The |
2674 | 3334 | string specified with C<=~> need not be an lvalue--it may be the |
2675 | 3335 | result of an expression evaluation, but remember the C<=~> binds |
2676 | rather tightly.) See also L<perlre>. | |
3336 | rather tightly.) See also L<perlre>. | |
2677 | discussion of additional considerations that apply when C<use locale> | |
2678 | is in effect. | |
2679 | 3337 | |
2680 | 3338 | =end original |
2681 | 3339 | |
2682 | 3340 | パターンマッチで文字列検索を行ない、スカラコンテキストでは成功したときは真、 |
2683 | 3341 | 失敗したときは偽を返します。 |
2684 | 3342 | C<=~> 演算子か C<!~> 演算子で検索対象の文字列を示さなかったときには、 |
2685 | 3343 | C<$_> の文字列が検索対象となります。 |
2686 | (C<=~> で指定される文字列は、左辺値である必要はありません | |
3344 | (C<=~> で指定される文字列は、左辺値である必要はありません-- | |
2687 | 3345 | 式を評価した結果でもかまいませんが、C<=~> の優先順位がいくぶん高いことに |
2688 | 3346 | 注意してください。) |
2689 | 3347 | L<perlre> も参照してください。 |
2690 | C<use locale> が有効の場合の議論については L<perllocale> を参照して下さい。 | |
2691 | 3348 | |
2692 | 3349 | =begin original |
2693 | 3350 | |
2694 | Options are as described in C<qr//>; in addition, the following match | |
3351 | Options are as described in C<qr//> above; in addition, the following match | |
2695 | 3352 | process modifiers are available: |
2696 | 3353 | |
2697 | 3354 | =end original |
2698 | 3355 | |
2699 | オプションは C<qr//> に記述されています; さらに、以下の | |
3356 | オプションは上述した C<qr//> に記述されています; さらに、以下の | |
2700 | 修飾子が利用可能です: | |
3357 | マッチング処理修飾子が利用可能です: | |
2701 | 3358 | |
2702 | 3359 | =begin original |
2703 | 3360 | |
2704 | | |
3361 | g Match globally, i.e., find all occurrences. | |
2705 | | |
3362 | c Do not reset search position on a failed match when /g is in effect. | |
2706 | 3363 | |
2707 | 3364 | =end original |
2708 | 3365 | |
2709 | | |
3366 | g グローバルにマッチング、つまり、すべてを探し出す。 | |
2710 | | |
3367 | c /g が有効なとき、マッチングに失敗しても検索位置をリセットしない。 | |
2711 | 3368 | |
2712 | 3369 | =begin original |
2713 | 3370 | |
2714 | 3371 | If "/" is the delimiter then the initial C<m> is optional. With the C<m> |
2715 | you can use any pair of non- | |
3372 | you can use any pair of non-whitespace (ASCII) characters | |
2716 | 3373 | as delimiters. This is particularly useful for matching path names |
2717 | 3374 | that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is |
2718 | the delimiter, then | |
3375 | the delimiter, then a match-only-once rule applies, | |
3376 | described in C<m?PATTERN?> below. | |
2719 | 3377 | If "'" is the delimiter, no interpolation is performed on the PATTERN. |
3378 | When using a character valid in an identifier, whitespace is required | |
3379 | after the C<m>. | |
2720 | 3380 | |
2721 | 3381 | =end original |
2722 | 3382 | |
2723 | 3383 | 区切文字が "/" のときには、最初の C<m> は付けても付けなくてもかまいません。 |
2724 | C<m> を付けるときには、 | |
3384 | C<m> を付けるときには、(ASCII の)空白でもない、任意の文字のペアを | |
2725 | 3385 | 区切文字として使うことができます。 |
2726 | 3386 | これは特に、"/" を含むパス名にパターンマッチングを行なうときに、 |
2727 | 3387 | LTS (傾斜楊枝症候群) を避けるために便利でしょう。 |
3388 | "?" がデリミタなら、後述する C<m?PATTERN?> にある「一度だけマッチング」 | |
3389 | ルールが適用されます。 | |
2728 | 3390 | "'" がデリミタの場合、PATTERN に対する展開は行われません。 |
3391 | 識別子として有効な文字を使う場合、C<m> の後に空白が必要です。 | |
2729 | 3392 | |
2730 | 3393 | =begin original |
2731 | 3394 | |
2732 | PATTERN may contain variables, which will be interpolated | |
3395 | PATTERN may contain variables, which will be interpolated | |
2733 | ||
3396 | every time the pattern search is evaluated, except | |
2734 | 3397 | for when the delimiter is a single quote. (Note that C<$(>, C<$)>, and |
2735 | 3398 | C<$|> are not interpolated because they look like end-of-string tests.) |
2736 | ||
3399 | Perl will not recompile the pattern unless an interpolated | |
2737 | ||
3400 | variable that it contains changes. You can force Perl to skip the | |
2738 | and | |
3401 | test and never recompile by adding a C</o> (which stands for "once") | |
2739 | t | |
3402 | after the trailing delimiter. | |
2740 | ||
3403 | Once upon a time, Perl would recompile regular expressions | |
2741 | ||
3404 | unnecessarily, and this modifier was useful to tell it not to do so, in the | |
3405 | interests of speed. But now, the only reasons to use C</o> are either: | |
2742 | 3406 | |
2743 | 3407 | =end original |
2744 | 3408 | |
2745 | 3409 | PATTERN には、変数が含まれていてもよく、パターンが評価されるごとに、 |
2746 | 3410 | (デリミタがシングルクォートでない限り)変数は展開され |
2747 | 3411 | (パターンが再コンパイルされ) ます。 |
2748 | 3412 | (変数 C<$(>, C<$)>, C<$|> は文字列の終わりを調べるパターンであると |
2749 | 3413 | 解釈されるので、展開されません。) |
2750 | パターン | |
3414 | Perl は展開された変数の値が変更されない限りパターンを再コンパイルしません。 | |
2751 | ||
3415 | デリミタに引き続いて C</o> ("once" を意味します) を追加することで、 | |
2752 | ||
3416 | テストを飛ばして再コンパイルしないようにすることができます。 | |
2753 | ||
3417 | 昔々、Perl は不必要に正規表現を再コンパイルしていたので、速度に関心が | |
2754 | ||
3418 | ある場合は再コンパイルしないようにするためにこの修飾子は有用でした。 | |
2755 | ||
3419 | しかし今では、C</o> を使う理由は以下のどちらかだけです: | |
2756 | 変更したとしても、Perl がそれに気付くことはありません。 | |
2757 | L<"qr/STRING/msixpo"> も参照して下さい。 | |
2758 | 3420 | |
3421 | =over | |
3422 | ||
3423 | =item 1 | |
3424 | ||
3425 | =begin original | |
3426 | ||
3427 | The variables are thousands of characters long and you know that they | |
3428 | don't change, and you need to wring out the last little bit of speed by | |
3429 | having Perl skip testing for that. (There is a maintenance penalty for | |
3430 | doing this, as mentioning C</o> constitutes a promise that you won't | |
3431 | change the variables in the pattern. If you change them, Perl won't | |
3432 | even notice.) | |
3433 | ||
3434 | =end original | |
3435 | ||
3436 | 変数が数千文字の長さで、これが変更されないことが分かっており、これに対する | |
3437 | テストを飛ばすことであともう少しだけ速度を稼ぐ必要がある。 | |
3438 | (こうすることには保守上のペナルティがあります; なぜなら C</o> と言及することで | |
3439 | パターン内の変数を変更しないことを約束したことになるからです。 | |
3440 | 変更しても、Perl は気づきもしません。) | |
3441 | ||
3442 | =item 2 | |
3443 | ||
3444 | =begin original | |
3445 | ||
3446 | you want the pattern to use the initial values of the variables | |
3447 | regardless of whether they change or not. (But there are saner ways | |
3448 | of accomplishing this than using C</o>.) | |
3449 | ||
3450 | =end original | |
3451 | ||
3452 | 変数が変更されようが変更されまいが、変数の初期値を使ったパターンがほしい。 | |
3453 | (しかしこれを達成するための、C</o> を使うよりもまともな方法があります。) | |
3454 | ||
3455 | =back | |
3456 | ||
2759 | 3457 | =item The empty pattern // |
2760 | 3458 | |
2761 | 3459 | (空パターン //) |
2762 | 3460 | |
2763 | 3461 | =begin original |
2764 | 3462 | |
2765 | 3463 | If the PATTERN evaluates to the empty string, the last |
2766 | 3464 | I<successfully> matched regular expression is used instead. In this |
2767 | case, only the C<g> and C<c> flags on the empty pattern | |
3465 | case, only the C<g> and C<c> flags on the empty pattern are honored; | |
2768 | 3466 | the other flags are taken from the original pattern. If no match has |
2769 | 3467 | previously succeeded, this will (silently) act instead as a genuine |
2770 | 3468 | empty pattern (which will always match). |
2771 | 3469 | |
2772 | 3470 | =end original |
2773 | 3471 | |
2774 | 3472 | PATTERN を評価した結果が空文字列となった場合には、最後にマッチに |
2775 | 3473 | I<成功した> 正規表現が、代わりに使われます。 |
2776 | この場合、空パターンに対して C<g> の C<c> フラグだけが有効です | |
3474 | この場合、空パターンに対して C<g> の C<c> フラグだけが有効です; | |
2777 | 3475 | その他のフラグは元のパターンから取られます。 |
2778 | 3476 | 以前に成功したマッチングがない場合、これは(暗黙に)真の空パターンとして |
2779 | 3477 | 動作します(つまり常にマッチングします)。 |
2780 | 3478 | |
2781 | 3479 | =begin original |
2782 | 3480 | |
2783 | 3481 | Note that it's possible to confuse Perl into thinking C<//> (the empty |
2784 | 3482 | regex) is really C<//> (the defined-or operator). Perl is usually pretty |
2785 | 3483 | good about this, but some pathological cases might trigger this, such as |
2786 | 3484 | C<$a///> (is that C<($a) / (//)> or C<$a // />?) and C<print $fh //> |
2787 | 3485 | (C<print $fh(//> or C<print($fh //>?). In all of these examples, Perl |
2788 | 3486 | will assume you meant defined-or. If you meant the empty regex, just |
2789 | 3487 | use parentheses or spaces to disambiguate, or even prefix the empty |
2790 | 3488 | regex with an C<m> (so C<//> becomes C<m//>). |
2791 | 3489 | |
2792 | 3490 | =end original |
2793 | 3491 | |
2794 | 3492 | Perl が C<//> (空正規表現) と C<//> (定義性和演算子) を混同する |
2795 | 3493 | 可能性があることに注意してください。 |
2796 | 3494 | Perl は普通これをかなりうまく処理しますが、C<$a///> (C<($a) / (//)> |
2797 | 3495 | それとも C<$a // />?) や C<print $fh //> (C<print $fh(//> それとも |
2798 | 3496 | C<print($fh //>?) のような病的な状況ではこれが起こりえます。 |
2799 | 3497 | これらの例の全てでは、Perl は定義性和を意味していると仮定します。 |
2800 | 3498 | もし空正規表現を意味したいなら、あいまいさをなくすために単に |
2801 | 3499 | かっこや空白を使うか、空正規表現に接頭辞 C<m> を付けてください |
2802 | 3500 | (つまり C<//> を C<m//> にします)。 |
2803 | 3501 | |
2804 | 3502 | =item Matching in list context |
2805 | 3503 | |
2806 | 3504 | (リストコンテキストでのマッチング) |
2807 | 3505 | |
2808 | 3506 | =begin original |
2809 | 3507 | |
2810 | 3508 | If the C</g> option is not used, C<m//> in list context returns a |
2811 | 3509 | list consisting of the subexpressions matched by the parentheses in the |
2812 | 3510 | pattern, i.e., (C<$1>, C<$2>, C<$3>...). (Note that here C<$1> etc. are |
2813 | 3511 | also set, and that this differs from Perl 4's behavior.) When there are |
2814 | 3512 | no parentheses in the pattern, the return value is the list C<(1)> for |
2815 | 3513 | success. With or without parentheses, an empty list is returned upon |
2816 | 3514 | failure. |
2817 | 3515 | |
2818 | 3516 | =end original |
2819 | 3517 | |
2820 | 3518 | C</g>オプションが使われなかった場合、リストコンテキストでのC<m//>は |
2821 | 3519 | パターンの中の括弧で括られた部分列にマッチしたもので構成されるリストを |
2822 | 返します。 | |
3520 | 返します; これは、(C<$1>, C<$2>, C<$3>, ...) ということです。 | |
2823 | こ | |
3521 | (この場合、C<$1> なども設定されます; この点で Perl 4 の動作と違っています。) | |
2824 | (この場合、C<$1> なども設定されます。 | |
2825 | この点で Perl 4 の動作と違っています。) | |
2826 | 3522 | パターンに括弧がない場合は、返り値は成功時はリスト C<(1)> です。 |
2827 | 3523 | 括弧のあるなしに関わらず、失敗時は空リストを返します。 |
2828 | 3524 | |
2829 | 3525 | =begin original |
2830 | 3526 | |
2831 | 3527 | Examples: |
2832 | 3528 | |
2833 | 3529 | =end original |
2834 | 3530 | |
2835 | 3531 | 例: |
2836 | 3532 | |
2837 | open(TTY, | |
3533 | open(TTY, "+>/dev/tty") | |
3534 | || die "can't access /dev/tty: $!"; | |
3535 | ||
2838 | 3536 | <TTY> =~ /^y/i && foo(); # do foo if desired |
2839 | 3537 | |
2840 | 3538 | if (/Version: *([0-9.]*)/) { $version = $1; } |
2841 | 3539 | |
2842 | 3540 | next if m#^/usr/spool/uucp#; |
2843 | 3541 | |
2844 | 3542 | # poor man's grep |
2845 | 3543 | $arg = shift; |
2846 | 3544 | while (<>) { |
2847 | print if /$arg/o; # compile only once | |
3545 | print if /$arg/o; # compile only once (no longer needed!) | |
2848 | 3546 | } |
2849 | 3547 | |
2850 | 3548 | if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/)) |
2851 | 3549 | |
2852 | 3550 | =begin original |
2853 | 3551 | |
2854 | 3552 | This last example splits $foo into the first two words and the |
2855 | 3553 | remainder of the line, and assigns those three fields to $F1, $F2, and |
2856 | $Etc. The conditional is true if any variables were assigned | |
3554 | $Etc. The conditional is true if any variables were assigned; that is, | |
2857 | the pattern matched. | |
3555 | if the pattern matched. | |
2858 | 3556 | |
2859 | 3557 | =end original |
2860 | 3558 | |
2861 | 3559 | 最後の例は、$foo を最初の 2 つの単語と行の残りに分解し、 |
2862 | 3560 | $F1 と $F2 と $Etc に代入しています。 |
2863 | 3561 | 変数に代入されれば、すなわちパターンがマッチすれば、 |
2864 | 3562 | if の条件が真となります。 |
2865 | 3563 | |
2866 | 3564 | =begin original |
2867 | 3565 | |
2868 | 3566 | The C</g> modifier specifies global pattern matching--that is, |
2869 | matching as many times as possible within the string. | |
3567 | matching as many times as possible within the string. How it behaves | |
2870 | depends on the context. | |
3568 | depends on the context. In list context, it returns a list of the | |
2871 | 3569 | substrings matched by any capturing parentheses in the regular |
2872 | expression. | |
3570 | expression. If there are no parentheses, it returns a list of all | |
2873 | 3571 | the matched strings, as if there were parentheses around the whole |
2874 | 3572 | pattern. |
2875 | 3573 | |
2876 | 3574 | =end original |
2877 | 3575 | |
2878 | 3576 | C</g> 修飾子は、グローバルなパターンマッチを指定するもので、 |
2879 | 3577 | 文字列の中で可能な限りたくさんマッチを行ないます。 |
2880 | 3578 | この動作は、コンテキストに依存します。 |
2881 | 3579 | リストコンテキストでは、正規表現内の括弧付けされたものにマッチした |
2882 | 3580 | 部分文字列のリストが返されます。 |
2883 | 3581 | 括弧がなければ、パターン全体を括弧で括っていたかのように、 |
2884 | 3582 | すべてのマッチした文字列のリストが返されます。 |
2885 | 3583 | |
2886 | 3584 | =begin original |
2887 | 3585 | |
2888 | 3586 | In scalar context, each execution of C<m//g> finds the next match, |
2889 | 3587 | returning true if it matches, and false if there is no further match. |
2890 | The position after the last match can be read or set using the pos() | |
3588 | The position after the last match can be read or set using the C<pos()> | |
2891 | function; see L<perlfunc/pos>. | |
3589 | function; see L<perlfunc/pos>. A failed match normally resets the | |
2892 | 3590 | search position to the beginning of the string, but you can avoid that |
2893 | by adding the C</c> modifier (e.g. C<m//gc>). | |
3591 | by adding the C</c> modifier (e.g. C<m//gc>). Modifying the target | |
2894 | 3592 | string also resets the search position. |
2895 | 3593 | |
2896 | 3594 | =end original |
2897 | 3595 | |
2898 | スカラコンテキストでは、C<m//g> を実行する毎に次のマッチを探します | |
3596 | スカラコンテキストでは、C<m//g> を実行する毎に次のマッチを探します; | |
2899 | 3597 | マッチした場合は真を返し、もうマッチしなくなったら偽を返します。 |
2900 | 最後のマッチの位置は pos() 関数で読み出しや設定ができます | |
3598 | 最後のマッチの位置は C<pos()> 関数で読み出しや設定ができます; | |
2901 | 3599 | L<perlfunc/pos> を参照して下さい。 |
2902 | 3600 | マッチに失敗すると通常は検索位置を文字列の先頭にリセットしますが、 |
2903 | 3601 | C</c> 修飾子をつける(つまり C<m//gc>)ことでこれを防ぐことができます。 |
2904 | 3602 | ターゲットとなる文字列が変更された場合も検索位置はリセットされます。 |
2905 | 3603 | |
2906 | 3604 | =item \G assertion |
2907 | 3605 | |
2908 | 3606 | (\G アサート) |
2909 | 3607 | |
2910 | 3608 | =begin original |
2911 | 3609 | |
2912 | 3610 | You can intermix C<m//g> matches with C<m/\G.../g>, where C<\G> is a |
2913 | zero-width assertion that matches the exact position where the | |
3611 | zero-width assertion that matches the exact position where the | |
2914 | C<m//g>, if any, left off. | |
3612 | previous C<m//g>, if any, left off. Without the C</g> modifier, the | |
2915 | still anchors at pos() | |
3613 | C<\G> assertion still anchors at C<pos()> as it was at the start of | |
2916 | ||
3614 | the operation (see L<perlfunc/pos>), but the match is of course only | |
2917 | ||
3615 | attempted once. Using C<\G> without C</g> on a target string that has | |
2918 | t | |
3616 | not previously had a C</g> match applied to it is the same as using | |
2919 | ||
3617 | the C<\A> assertion to match the beginning of the string. Note also | |
3618 | that, currently, C<\G> is only properly supported when anchored at the | |
3619 | very beginning of the pattern. | |
2920 | 3620 | |
2921 | 3621 | =end original |
2922 | 3622 | |
2923 | C<m//g> マッチを C<m/\G.../g> と混ぜることもできます | |
3623 | C<m//g> マッチを C<m/\G.../g> と混ぜることもできます; C<\G> は前回の | |
2924 | C< | |
3624 | C<m//g> があればその同じ位置でマッチする | |
2925 | 3625 | ゼロ文字幅のアサートです。 |
2926 | C</g> 修飾子なしの場合、C<\G> アサートは | |
3626 | C</g> 修飾子なしの場合、C<\G> アサートは操作の最初としてC<pos()> に | |
2927 | マッチはもちろん一度だけ試されます。 | |
3627 | 固定しますが、(L<perlfunc/pos> 参照) マッチはもちろん一度だけ試されます。 | |
2928 | 3628 | 以前に C</g> マッチを適用していないターゲット文字列に対して C</g> なしで |
2929 | 3629 | C<\G> を使うと、文字列の先頭にマッチする C<\A> アサートを使うのと |
2930 | 3630 | 同じことになります。 |
2931 | 3631 | C<\G> は現在のところ、パターンのまさに先頭を示す場合にのみ正しく |
2932 | 3632 | 対応することにも注意してください。 |
2933 | 3633 | |
2934 | 3634 | =begin original |
2935 | 3635 | |
2936 | 3636 | Examples: |
2937 | 3637 | |
2938 | 3638 | =end original |
2939 | 3639 | |
2940 | 3640 | 例: |
2941 | 3641 | |
2942 | 3642 | # list context |
2943 | 3643 | ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g); |
2944 | 3644 | |
2945 | 3645 | # scalar context |
2946 | $/ = ""; | |
3646 | local $/ = ""; | |
2947 | while ( | |
3647 | while ($paragraph = <>) { | |
2948 | while ($paragraph =~ / | |
3648 | while ($paragraph =~ /\p{Ll}['")]*[.!?]+['")]*\s/g) { | |
2949 | 3649 | $sentences++; |
2950 | 3650 | } |
2951 | 3651 | } |
2952 | | |
3652 | say $sentences; | |
2953 | 3653 | |
2954 | ||
3654 | =begin original | |
3655 | ||
3656 | Here's another way to check for sentences in a paragraph: | |
3657 | ||
3658 | =end original | |
3659 | ||
3660 | 以下は段落内の文をチェックするためのもう一つの方法です: | |
3661 | ||
3662 | my $sentence_rx = qr{ | |
3663 | (?: (?<= ^ ) | (?<= \s ) ) # after start-of-string or whitespace | |
3664 | \p{Lu} # capital letter | |
3665 | .*? # a bunch of anything | |
3666 | (?<= \S ) # that ends in non-whitespace | |
3667 | (?<! \b [DMS]r ) # but isn't a common abbreviation | |
3668 | (?<! \b Mrs ) | |
3669 | (?<! \b Sra ) | |
3670 | (?<! \b St ) | |
3671 | [.?!] # followed by a sentence ender | |
3672 | (?= $ | \s ) # in front of end-of-string or whitespace | |
3673 | }sx; | |
3674 | local $/ = ""; | |
3675 | while (my $paragraph = <>) { | |
3676 | say "NEW PARAGRAPH"; | |
3677 | my $count = 0; | |
3678 | while ($paragraph =~ /($sentence_rx)/g) { | |
3679 | printf "\tgot sentence %d: <%s>\n", ++$count, $1; | |
3680 | } | |
3681 | } | |
3682 | ||
3683 | =begin original | |
3684 | ||
3685 | Here's how to use C<m//gc> with C<\G>: | |
3686 | ||
3687 | =end original | |
3688 | ||
3689 | 以下は C<m//gc> を C<\G> で使う方法です: | |
3690 | ||
2955 | 3691 | $_ = "ppooqppqq"; |
2956 | 3692 | while ($i++ < 2) { |
2957 | 3693 | print "1: '"; |
2958 | 3694 | print $1 while /(o)/gc; print "', pos=", pos, "\n"; |
2959 | 3695 | print "2: '"; |
2960 | 3696 | print $1 if /\G(q)/gc; print "', pos=", pos, "\n"; |
2961 | 3697 | print "3: '"; |
2962 | 3698 | print $1 while /(p)/gc; print "', pos=", pos, "\n"; |
2963 | 3699 | } |
2964 | 3700 | print "Final: '$1', pos=",pos,"\n" if /\G(.)/; |
2965 | 3701 | |
2966 | 3702 | =begin original |
2967 | 3703 | |
2968 | 3704 | The last example should print: |
2969 | 3705 | |
2970 | 3706 | =end original |
2971 | 3707 | |
2972 | 3708 | 最後のものは以下のものを表示するはずです: |
2973 | 3709 | |
2974 | 3710 | 1: 'oo', pos=4 |
2975 | 3711 | 2: 'q', pos=5 |
2976 | 3712 | 3: 'pp', pos=7 |
2977 | 3713 | 1: '', pos=7 |
2978 | 3714 | 2: 'q', pos=8 |
2979 | 3715 | 3: '', pos=8 |
2980 | 3716 | Final: 'q', pos=8 |
2981 | 3717 | |
2982 | 3718 | =begin original |
2983 | 3719 | |
2984 | 3720 | Notice that the final match matched C<q> instead of C<p>, which a match |
2985 | 3721 | without the C<\G> anchor would have done. Also note that the final match |
2986 | did not update C<pos> | |
3722 | did not update C<pos>. C<pos> is only updated on a C</g> match. If the | |
2987 | final match did indeed match C<p>, it's a good bet that you're running a | |
3723 | final match did indeed match C<p>, it's a good bet that you're running a | |
2988 | old | |
3724 | very old (pre-5.6.0) version of Perl. | |
2989 | 3725 | |
2990 | 3726 | =end original |
2991 | 3727 | |
2992 | 3728 | C<\G> なしでのマッチが行われたため、最後のマッチでは C<p> ではなく |
2993 | 3729 | C<q> がマッチすることに注意してください。 |
2994 | 3730 | また、最後のマッチは C<pos> を更新しないことに注意してください。 |
2995 | 3731 | C<pos> は C</g> マッチでのみ更新されます。 |
2996 | 3732 | もし最後のマッチで C<p> にマッチした場合、かなりの確率で |
2997 | 古い (5.6.0 以前の) Perl で実行しているはずです。 | |
3733 | とても古い (5.6.0 以前の) Perl で実行しているはずです。 | |
2998 | 3734 | |
2999 | 3735 | =begin original |
3000 | 3736 | |
3001 | 3737 | A useful idiom for C<lex>-like scanners is C</\G.../gc>. You can |
3002 | 3738 | combine several regexps like this to process a string part-by-part, |
3003 | 3739 | doing different actions depending on which regexp matched. Each |
3004 | 3740 | regexp tries to match where the previous one leaves off. |
3005 | 3741 | |
3006 | 3742 | =end original |
3007 | 3743 | |
3008 | 3744 | C<lex> 風にスキャンするために便利な指定は C</\G.../gc> です。 |
3009 | 3745 | 文字列を部分ごとに処理するためにいくつかの正規表現をつなげて、どの |
3010 | 3746 | 正規表現にマッチしたかによって異なる処理をすることができます。 |
3011 | 3747 | それぞれの正規表現は前の正規表現が飛ばした部分に対して |
3012 | 3748 | マッチを試みます。 |
3013 | 3749 | |
3014 | 3750 | $_ = <<'EOL'; |
3015 | | |
3751 | $url = URI::URL->new( "http://example.com/" ); die if $url eq "xXx"; | |
3016 | 3752 | EOL |
3017 | LOOP: | |
3018 | { | |
3019 | print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc; | |
3020 | print(" lowercase"), redo LOOP if /\G[a-z]+\b[,.;]?\s*/gc; | |
3021 | print(" UPPERCASE"), redo LOOP if /\G[A-Z]+\b[,.;]?\s*/gc; | |
3022 | print(" Capitalized"), redo LOOP if /\G[A-Z][a-z]+\b[,.;]?\s*/gc; | |
3023 | print(" MiXeD"), redo LOOP if /\G[A-Za-z]+\b[,.;]?\s*/gc; | |
3024 | print(" alphanumeric"), redo LOOP if /\G[A-Za-z0-9]+\b[,.;]?\s*/gc; | |
3025 | print(" line-noise"), redo LOOP if /\G[^A-Za-z0-9]+/gc; | |
3026 | print ". That's all!\n"; | |
3027 | } | |
3028 | 3753 | |
3754 | LOOP: { | |
3755 | print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc; | |
3756 | print(" lowercase"), redo LOOP if /\G\p{Ll}+\b[,.;]?\s*/gc; | |
3757 | print(" UPPERCASE"), redo LOOP if /\G\p{Lu}+\b[,.;]?\s*/gc; | |
3758 | print(" Capitalized"), redo LOOP if /\G\p{Lu}\p{Ll}+\b[,.;]?\s*/gc; | |
3759 | print(" MiXeD"), redo LOOP if /\G\pL+\b[,.;]?\s*/gc; | |
3760 | print(" alphanumeric"), redo LOOP if /\G[\p{Alpha}\pN]+\b[,.;]?\s*/gc; | |
3761 | print(" line-noise"), redo LOOP if /\G\W+/gc; | |
3762 | print ". That's all!\n"; | |
3763 | } | |
3764 | ||
3029 | 3765 | =begin original |
3030 | 3766 | |
3031 | 3767 | Here is the output (split into several lines): |
3032 | 3768 | |
3033 | 3769 | =end original |
3034 | 3770 | |
3035 | 3771 | 出力は以下のようになります(何行かに分割しています): |
3036 | 3772 | |
3037 | line-noise lowercase line-noise | |
3773 | line-noise lowercase line-noise UPPERCASE line-noise UPPERCASE | |
3038 | | |
3774 | line-noise lowercase line-noise lowercase line-noise lowercase | |
3039 | | |
3775 | lowercase line-noise lowercase lowercase line-noise lowercase | |
3040 | MiXeD line-noise. That's all! | |
3776 | lowercase line-noise MiXeD line-noise. That's all! | |
3041 | 3777 | |
3042 | =item ?PATTERN? | |
3778 | =item m?PATTERN?msixpodualgc | |
3043 | X<?> | |
3779 | X<?> X<operator, match-once> | |
3044 | 3780 | |
3781 | =item ?PATTERN?msixpodualgc | |
3782 | ||
3045 | 3783 | =begin original |
3046 | 3784 | |
3047 | This is just like the C</ | |
3785 | This is just like the C<m/PATTERN/> search, except that it matches | |
3048 | once between calls to the reset() operator. This is a useful | |
3786 | only once between calls to the reset() operator. This is a useful | |
3049 | 3787 | optimization when you want to see only the first occurrence of |
3050 | something in each file of a set of files, for instance. Only C<??> | |
3788 | something in each file of a set of files, for instance. Only C<m??> | |
3051 | 3789 | patterns local to the current package are reset. |
3052 | 3790 | |
3053 | 3791 | =end original |
3054 | 3792 | |
3055 | 3793 | これは、reset() 演算子を呼び出すごとに 1 度だけしか |
3056 | マッチしないことを除いては C</pattern/> による検索と全く同じです。 | |
3794 | マッチしないことを除いては C<m/pattern/> による検索と全く同じです。 | |
3057 | 3795 | たとえば、ファイルの集まりの中で個々のファイルについて、あるものを |
3058 | 3796 | 探すとき、最初の 1 つだけの存在がわかれば良いのであれば、この機能を |
3059 | 3797 | 使って最適化をはかることができます。 |
3060 | 現在のパッケージにローカルとなっている C<??> のパターンだけが | |
3798 | 現在のパッケージにローカルとなっている C<m??> のパターンだけが | |
3061 | 3799 | リセットされます。 |
3062 | 3800 | |
3063 | 3801 | while (<>) { |
3064 | if (?^$?) { | |
3802 | if (m?^$?) { | |
3065 | 3803 | # blank line between header and body |
3066 | 3804 | } |
3067 | 3805 | } continue { |
3068 | reset if eof; # clear ?? status for next file | |
3806 | reset if eof; # clear m?? status for next file | |
3069 | 3807 | } |
3070 | 3808 | |
3071 | 3809 | =begin original |
3072 | 3810 | |
3073 | ||
3811 | Another example switched the first "latin1" encoding it finds | |
3074 | ||
3812 | to "utf8" in a pod file: | |
3075 | around the year 2168. | |
3076 | 3813 | |
3077 | 3814 | =end original |
3078 | 3815 | |
3079 | ||
3816 | 次の例は、pod ファイル中の最初の "latin1" エンコーディングを "utf8" に | |
3080 | ||
3817 | 切り替えます: | |
3081 | 3818 | |
3082 | ||
3819 | s//utf8/ if m? ^ =encoding \h+ \K latin1 ?x; | |
3820 | ||
3821 | =begin original | |
3822 | ||
3823 | The match-once behavior is controlled by the match delimiter being | |
3824 | C<?>; with any other delimiter this is the normal C<m//> operator. | |
3825 | ||
3826 | =end original | |
3827 | ||
3828 | 一度だけマッチングという振る舞いはマッチングデリミタが C<?> かどうかで | |
3829 | 制御されます: その他のデリミタの場合はこれは通常の C<m//> 演算子です。 | |
3830 | ||
3831 | =begin original | |
3832 | ||
3833 | For historical reasons, the leading C<m> in C<m?PATTERN?> is optional, | |
3834 | but the resulting C<?PATTERN?> syntax is deprecated, will warn on | |
3835 | usage and might be removed from a future stable release of Perl (without | |
3836 | further notice!). | |
3837 | ||
3838 | =end original | |
3839 | ||
3840 | 歴史的な理由により、C<m?PATTERN?> の先頭の C<m> は省略可能ですが、 | |
3841 | その結果となる C<?PATTERN?> という文法は非推奨であり、使用すると警告が | |
3842 | 出ます; また将来の Perl の安定版リリースでは (さらなる注意なしに!) | |
3843 | 削除されるかもしれません。 | |
3844 | ||
3845 | =item s/PATTERN/REPLACEMENT/msixpodualgcer | |
3083 | 3846 | X<substitute> X<substitution> X<replace> X<regexp, replace> |
3084 | X<regexp, substitute> X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> X</e> | |
3847 | X<regexp, substitute> X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> X</e> X</r> | |
3085 | 3848 | |
3086 | 3849 | =begin original |
3087 | 3850 | |
3088 | 3851 | Searches a string for a pattern, and if found, replaces that pattern |
3089 | 3852 | with the replacement text and returns the number of substitutions |
3090 | 3853 | made. Otherwise it returns false (specifically, the empty string). |
3091 | 3854 | |
3092 | 3855 | =end original |
3093 | 3856 | |
3094 | 3857 | 文字列中でパターンを検索し、もし見つかれば、置換テキストで置き換え、 |
3095 | 3858 | 置換した数を返します。 |
3096 | 3859 | 見つからなければ、偽 (具体的には、空文字列) を返します。 |
3097 | 3860 | |
3098 | 3861 | =begin original |
3099 | 3862 | |
3863 | If the C</r> (non-destructive) option is used then it runs the | |
3864 | substitution on a copy of the string and instead of returning the | |
3865 | number of substitutions, it returns the copy whether or not a | |
3866 | substitution occurred. The original string is never changed when | |
3867 | C</r> is used. The copy will always be a plain string, even if the | |
3868 | input is an object or a tied variable. | |
3869 | ||
3870 | =end original | |
3871 | ||
3872 | C</r> (非破壊) オプションが使われると、文字列のコピーに対して置換が行われ、 | |
3873 | 置換された数ではなく、置換が行われたかどうかにかかわらずこのコピーが返されます。 | |
3874 | C</r> が使われた場合、元の文字列は決して変更されません。 | |
3875 | コピーは、たとえ入力がオブジェクトや tie された変数でも、常に | |
3876 | プレーンな文字列です。 | |
3877 | ||
3878 | =begin original | |
3879 | ||
3100 | 3880 | If no string is specified via the C<=~> or C<!~> operator, the C<$_> |
3101 | variable is searched and modified. | |
3881 | variable is searched and modified. Unless the C</r> option is used, | |
3102 | be scalar variable, an array element, a | |
3882 | the string specified must be a scalar variable, an array element, a | |
3103 | to one of those | |
3883 | hash element, or an assignment to one of those; that is, some sort of | |
3884 | scalar lvalue. | |
3104 | 3885 | |
3105 | 3886 | =end original |
3106 | 3887 | |
3107 | 3888 | C<=~> 演算子や C<!~> 演算子によって文字列が指定されていなければ、 |
3108 | 3889 | 変数 C<$_> が検索され、修正されます。 |
3109 | ||
3890 | C</r> が指定されていない限り、 | |
3110 | ||
3891 | C<=~> で指定される文字列は、スカラ変数、配列要素、ハッシュ要素、 | |
3892 | あるいは、これらへの代入式といったある種の | |
3893 | スカラ左辺値でなければなりません。 | |
3111 | 3894 | |
3112 | 3895 | =begin original |
3113 | 3896 | |
3114 | 3897 | If the delimiter chosen is a single quote, no interpolation is |
3115 | 3898 | done on either the PATTERN or the REPLACEMENT. Otherwise, if the |
3116 | 3899 | PATTERN contains a $ that looks like a variable rather than an |
3117 | 3900 | end-of-string test, the variable will be interpolated into the pattern |
3118 | 3901 | at run-time. If you want the pattern compiled only once the first time |
3119 | 3902 | the variable is interpolated, use the C</o> option. If the pattern |
3120 | 3903 | evaluates to the empty string, the last successfully executed regular |
3121 | 3904 | expression is used instead. See L<perlre> for further explanation on these. |
3122 | See L<perllocale> for discussion of additional considerations that apply | |
3123 | when C<use locale> is in effect. | |
3124 | 3905 | |
3125 | 3906 | =end original |
3126 | 3907 | |
3127 | あとで述べますが、区切り文字はスラッシュとは限りません。 | |
3128 | 3908 | シングルクォートを区切り文字として使った場合には、 |
3129 | 3909 | PATTERN にも REPLACEMENT にも変数の展開を行ないません。 |
3130 | 3910 | それ以外の場合、文字列の最後を表わすものには見えない $ が |
3131 | 3911 | PATTERN に含まれると、実行時に変数がパターン内に展開されます。 |
3132 | 3912 | 最初に変数が展開されるときにだけパターンのコンパイルを行ないたいときには、 |
3133 | 3913 | C</o> オプションを使ってください。 |
3134 | 3914 | パターンの評価結果が空文字列になった場合には、最後に成功した正規表現が |
3135 | 3915 | 代わりに使われます。 |
3136 | 3916 | これについてさらに詳しくは、L<perlre> を参照してください。 |
3137 | C<use locale> が有効の場合の議論については L<perllocale> を参照して下さい。 | |
3138 | 3917 | |
3139 | 3918 | =begin original |
3140 | 3919 | |
3141 | 3920 | Options are as with m// with the addition of the following replacement |
3142 | 3921 | specific options: |
3143 | 3922 | |
3144 | 3923 | =end original |
3145 | 3924 | |
3146 | 3925 | オプションは、m// のものに加えて、以下の置換固有のものがあります: |
3147 | 3926 | |
3148 | 3927 | =begin original |
3149 | 3928 | |
3150 | 3929 | e Evaluate the right side as an expression. |
3151 | ee Evaluate the right side as a string then eval the result | |
3930 | ee Evaluate the right side as a string then eval the result. | |
3931 | r Return substitution and leave the original string untouched. | |
3152 | 3932 | |
3153 | 3933 | =end original |
3154 | 3934 | |
3155 | e 式の右側の評価を行なう | |
3935 | e 式の右側の評価を行なう。 | |
3156 | ee 右側を文字列として評価して、その結果を評価する | |
3936 | ee 右側を文字列として評価して、その結果を評価する。 | |
3937 | r 置換した結果を返し、もとの文字列はそのままにする。 | |
3157 | 3938 | |
3158 | 3939 | =begin original |
3159 | 3940 | |
3160 | Any non- | |
3941 | Any non-whitespace delimiter may replace the slashes. Add space after | |
3161 | ||
3942 | the C<s> when using a character allowed in identifiers. If single quotes | |
3162 | replacement string (the C</e> | |
3943 | are used, no interpretation is done on the replacement string (the C</e> | |
3163 | Perl 4, Perl 5 treats backticks | |
3944 | modifier overrides this, however). Unlike Perl 4, Perl 5 treats backticks | |
3164 | text is not evaluated as a command. | |
3945 | as normal delimiters; the replacement text is not evaluated as a command. | |
3165 | PATTERN is delimited by bracketing quotes, the REPLACEMENT has | |
3946 | If the PATTERN is delimited by bracketing quotes, the REPLACEMENT has | |
3166 | pair of quotes, which may or may not be bracketing quotes, e.g., | |
3947 | its own pair of quotes, which may or may not be bracketing quotes, e.g., | |
3167 | 3948 | C<s(foo)(bar)> or C<< s<foo>/bar/ >>. A C</e> will cause the |
3168 | 3949 | replacement portion to be treated as a full-fledged Perl expression |
3169 | 3950 | and evaluated right then and there. It is, however, syntax checked at |
3170 | 3951 | compile-time. A second C<e> modifier will cause the replacement portion |
3171 | 3952 | to be C<eval>ed before being run as a Perl expression. |
3172 | 3953 | |
3173 | 3954 | =end original |
3174 | 3955 | |
3175 | ||
3956 | 空白ではない任意の区切り文字で、スラッシュを置き換えられます。 | |
3957 | 識別子として許されている文字を使うときには C<s> の後に空白を | |
3958 | 追加してください。 | |
3176 | 3959 | 先に述べたように、シングルクォートを使うと置換文字列での展開は |
3177 | 3960 | されません (C</e>修飾子を使えば可能です)。 |
3178 | Perl 4 と違って、 Perl 5 はバッククォートを通常のデリミタとして扱います | |
3961 | Perl 4 と違って、 Perl 5 はバッククォートを通常のデリミタとして扱います; | |
3179 | 3962 | 置換テキストはコマンドとして評価されません。 |
3180 | 3963 | PATTERN を括弧類で括った場合には、REPLACEMENT 用にもう一組の区切り文字を |
3181 | 用意します | |
3964 | 用意します; これは、括弧類であっても、なくてもかまいません; | |
3182 | ||
3965 | C<s(foo)(bar)> や C<< s<foo>/bar/ >>。 | |
3183 | C<< s<foo>/bar/ >>。 | |
3184 | 3966 | C</e> は置換文字列を完全な Perl の式として扱い、その場所で直ちに解釈します。 |
3185 | 3967 | しかし、これはコンパイル時に構文チェックされます。 |
3186 | 3968 | 二番目の C<e> 修飾子を指定すると、置換部分がまず Perl の式として |
3187 | 3969 | C<eval> されます。 |
3188 | 3970 | |
3189 | 3971 | =begin original |
3190 | 3972 | |
3191 | 3973 | Examples: |
3192 | 3974 | |
3193 | 3975 | =end original |
3194 | 3976 | |
3195 | 3977 | 例: |
3196 | 3978 | |
3197 | 3979 | s/\bgreen\b/mauve/g; # don't change wintergreen |
3198 | 3980 | |
3199 | 3981 | $path =~ s|/usr/bin|/usr/local/bin|; |
3200 | 3982 | |
3201 | 3983 | s/Login: $foo/Login: $bar/; # run-time pattern |
3202 | 3984 | |
3203 | 3985 | ($foo = $bar) =~ s/this/that/; # copy first, then change |
3986 | ($foo = "$bar") =~ s/this/that/; # convert to string, copy, then change | |
3987 | $foo = $bar =~ s/this/that/r; # Same as above using /r | |
3988 | $foo = $bar =~ s/this/that/r | |
3989 | =~ s/that/the other/r; # Chained substitutes using /r | |
3990 | @foo = map { s/this/that/r } @bar # /r is very useful in maps | |
3204 | 3991 | |
3205 | 3992 | $count = ($paragraph =~ s/Mister\b/Mr./g); # get change-count |
3206 | 3993 | |
3207 | 3994 | $_ = 'abc123xyz'; |
3208 | 3995 | s/\d+/$&*2/e; # yields 'abc246xyz' |
3209 | 3996 | s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz' |
3210 | 3997 | s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz' |
3211 | 3998 | |
3212 | 3999 | s/%(.)/$percent{$1}/g; # change percent escapes; no /e |
3213 | 4000 | s/%(.)/$percent{$1} || $&/ge; # expr now, so /e |
3214 | 4001 | s/^=(\w+)/pod($1)/ge; # use function call |
3215 | 4002 | |
4003 | $_ = 'abc123xyz'; | |
4004 | $a = s/abc/def/r; # $a is 'def123xyz' and | |
4005 | # $_ remains 'abc123xyz'. | |
4006 | ||
3216 | 4007 | # expand variables in $_, but dynamics only, using |
3217 | 4008 | # symbolic dereferencing |
3218 | 4009 | s/\$(\w+)/${$1}/g; |
3219 | 4010 | |
3220 | 4011 | # Add one to the value of any numbers in the string |
3221 | 4012 | s/(\d+)/1 + $1/eg; |
3222 | 4013 | |
4014 | # Titlecase words in the last 30 characters only | |
4015 | substr($str, -30) =~ s/\b(\p{Alpha}+)\b/\u\L$1/g; | |
4016 | ||
3223 | 4017 | # This will expand any embedded scalar variable |
3224 | 4018 | # (including lexicals) in $_ : First $1 is interpolated |
3225 | 4019 | # to the variable name, and then evaluated |
3226 | 4020 | s/(\$\w+)/$1/eeg; |
3227 | 4021 | |
3228 | 4022 | # Delete (most) C comments. |
3229 | 4023 | $program =~ s { |
3230 | 4024 | /\* # Match the opening delimiter. |
3231 | 4025 | .*? # Match a minimal number of characters. |
3232 | 4026 | \*/ # Match the closing delimiter. |
3233 | 4027 | } []gsx; |
3234 | 4028 | |
3235 | 4029 | s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_, expensively |
3236 | 4030 | |
3237 | 4031 | for ($variable) { # trim whitespace in $variable, cheap |
3238 | 4032 | s/^\s+//; |
3239 | 4033 | s/\s+$//; |
3240 | 4034 | } |
3241 | 4035 | |
3242 | 4036 | s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields |
3243 | 4037 | |
3244 | 4038 | =begin original |
3245 | 4039 | |
3246 | 4040 | Note the use of $ instead of \ in the last example. Unlike |
3247 | 4041 | B<sed>, we use the \<I<digit>> form in only the left hand side. |
3248 | 4042 | Anywhere else it's $<I<digit>>. |
3249 | 4043 | |
3250 | 4044 | =end original |
3251 | 4045 | |
3252 | 4046 | 最後の例で \ の代わりに $ を使っているのに注意してください。 |
3253 | 4047 | B<sed> と違って、\<I<数字>> の形式はパターンの方でのみ使用できます。 |
3254 | 4048 | その他の場所では、$<I<数字>> を使います。 |
3255 | 4049 | |
3256 | 4050 | =begin original |
3257 | 4051 | |
3258 | 4052 | Occasionally, you can't use just a C</g> to get all the changes |
3259 | 4053 | to occur that you might want. Here are two common cases: |
3260 | 4054 | |
3261 | 4055 | =end original |
3262 | 4056 | |
3263 | 4057 | ときには、C</g> を付けるだけでは、あなたが望んでいるような形で |
3264 | 4058 | すべてを変更することができないことがあります。 |
3265 | 4059 | 良くある例を 2 つ示します: |
3266 | 4060 | |
3267 | 4061 | # put commas in the right places in an integer |
3268 | 4062 | 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g; |
3269 | 4063 | |
3270 | 4064 | # expand tabs to 8-column spacing |
3271 | 4065 | 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e; |
3272 | 4066 | |
4067 | =begin original | |
4068 | ||
4069 | C<s///le> is treated as a substitution followed by the C<le> operator, not | |
4070 | the C</le> flags. This may change in a future version of Perl. It | |
4071 | produces a warning if warnings are enabled. To disambiguate, use a space | |
4072 | or change the order of the flags: | |
4073 | ||
4074 | =end original | |
4075 | ||
4076 | C<s///le> は C</le> フラグではなく、置換に引き続く C<le> 演算子として扱われます。 | |
4077 | これは将来のバージョンの Perl では変更されるかもしれません。 | |
4078 | 警告が有効の場合は警告が出力されます。 | |
4079 | 曖昧さをなくすために、空白を使うかフラグの順番を変えてください: | |
4080 | ||
4081 | s/foo/bar/ le 5; # "le" infix operator | |
4082 | s/foo/bar/el; # "e" and "l" flags | |
4083 | ||
3273 | 4084 | =back |
3274 | 4085 | |
3275 | 4086 | =head2 Quote-Like Operators |
3276 | 4087 | X<operator, quote-like> |
3277 | 4088 | |
3278 | 4089 | (クォート風演算子) |
3279 | 4090 | |
3280 | 4091 | =over 4 |
3281 | 4092 | |
3282 | 4093 | =item q/STRING/ |
3283 | 4094 | X<q> X<quote, single> X<'> X<''> |
3284 | 4095 | |
3285 | 4096 | =item 'STRING' |
3286 | 4097 | |
3287 | 4098 | =begin original |
3288 | 4099 | |
3289 | 4100 | A single-quoted, literal string. A backslash represents a backslash |
3290 | 4101 | unless followed by the delimiter or another backslash, in which case |
3291 | 4102 | the delimiter or backslash is interpolated. |
3292 | 4103 | |
3293 | 4104 | =end original |
3294 | 4105 | |
3295 | 4106 | シングルクォートされた、リテラル文字列です。 |
3296 | 4107 | バックスラッシュは、後ろに続くものが区切文字か、別のバックスラッシュで |
3297 | ある場合を除いて単なるバックスラッシュです | |
4108 | ある場合を除いて単なるバックスラッシュです; | |
3298 | 4109 | 区切文字やバックスラッシュが続く場合には、その区切文字自身もしくは |
3299 | 4110 | バックスラッシュそのものが展開されます。 |
3300 | 4111 | |
3301 | 4112 | $foo = q!I said, "You said, 'She said it.'"!; |
3302 | 4113 | $bar = q('This is it.'); |
3303 | 4114 | $baz = '\n'; # a two-character string |
3304 | 4115 | |
3305 | 4116 | =item qq/STRING/ |
3306 | 4117 | X<qq> X<quote, double> X<"> X<""> |
3307 | 4118 | |
3308 | 4119 | =item "STRING" |
3309 | 4120 | |
3310 | 4121 | =begin original |
3311 | 4122 | |
3312 | 4123 | A double-quoted, interpolated string. |
3313 | 4124 | |
3314 | 4125 | =end original |
3315 | 4126 | |
3316 | 4127 | ダブルクォートされた、リテラル文字列です。 |
3317 | 4128 | |
3318 | 4129 | $_ .= qq |
3319 | 4130 | (*** The previous line contains the naughty word "$1".\n) |
3320 | 4131 | if /\b(tcl|java|python)\b/i; # :-) |
3321 | 4132 | $baz = "\n"; # a one-character string |
3322 | 4133 | |
3323 | 4134 | =item qx/STRING/ |
3324 | 4135 | X<qx> X<`> X<``> X<backtick> |
3325 | 4136 | |
3326 | 4137 | =item `STRING` |
3327 | 4138 | |
3328 | 4139 | =begin original |
3329 | 4140 | |
3330 | 4141 | A string which is (possibly) interpolated and then executed as a |
3331 | 4142 | system command with C</bin/sh> or its equivalent. Shell wildcards, |
3332 | 4143 | pipes, and redirections will be honored. The collected standard |
3333 | 4144 | output of the command is returned; standard error is unaffected. In |
3334 | 4145 | scalar context, it comes back as a single (potentially multi-line) |
3335 | 4146 | string, or undef if the command failed. In list context, returns a |
3336 | 4147 | list of lines (however you've defined lines with $/ or |
3337 | 4148 | $INPUT_RECORD_SEPARATOR), or an empty list if the command failed. |
3338 | 4149 | |
3339 | 4150 | =end original |
3340 | 4151 | |
3341 | 4152 | 展開され、C</bin/sh> またはそれと等価なものでシステムのコマンドとして |
3342 | 4153 | 実行される(であろう)文字列です。 |
3343 | 4154 | シェルのワイルドカード、パイプ、リダイレクトが有効です。 |
3344 | そのコマンドの、標準出力を集めたものが返されます | |
4155 | そのコマンドの、標準出力を集めたものが返されます; 標準エラーは影響を | |
3345 | ||
4156 | 与えません。 | |
3346 | 4157 | スカラコンテキストでは、(複数行を含むかもしれない) |
3347 | 1 つの文字列が戻ってきます。 | |
4158 | 1 つの文字列が戻ってきます; コマンドが失敗したときは未定義値を返します。 | |
3348 | コマンドが失敗したときは未定義値を返します。 | |
3349 | 4159 | リストコンテキストでは、($/ もしくは $INPUT_RECORD_SEPARATOR を |
3350 | どのように設定していても) 行のリストを返します | |
4160 | どのように設定していても) 行のリストを返します; コマンドが失敗したときは | |
3351 | ||
4161 | 空リストを返します。 | |
3352 | 4162 | |
3353 | 4163 | =begin original |
3354 | 4164 | |
3355 | 4165 | Because backticks do not affect standard error, use shell file descriptor |
3356 | 4166 | syntax (assuming the shell supports this) if you care to address this. |
3357 | 4167 | To capture a command's STDERR and STDOUT together: |
3358 | 4168 | |
3359 | 4169 | =end original |
3360 | 4170 | |
3361 | 4171 | バッククォートは標準エラーには影響を与えないので、標準エラーを |
3362 | 4172 | 使いたい場合は(シェルが対応しているものとして)シェルのファイル記述子の |
3363 | 4173 | 文法を使ってください。 |
3364 | 4174 | コマンドの STDERR と STDOUT を共に取得したい場合は: |
3365 | 4175 | |
3366 | 4176 | $output = `cmd 2>&1`; |
3367 | 4177 | |
3368 | 4178 | =begin original |
3369 | 4179 | |
3370 | 4180 | To capture a command's STDOUT but discard its STDERR: |
3371 | 4181 | |
3372 | 4182 | =end original |
3373 | 4183 | |
3374 | 4184 | コマンドの STDOUT は取得するが STDERR は捨てる場合は: |
3375 | 4185 | |
3376 | 4186 | $output = `cmd 2>/dev/null`; |
3377 | 4187 | |
3378 | 4188 | =begin original |
3379 | 4189 | |
3380 | 4190 | To capture a command's STDERR but discard its STDOUT (ordering is |
3381 | 4191 | important here): |
3382 | 4192 | |
3383 | 4193 | =end original |
3384 | 4194 | |
3385 | 4195 | コマンドの STDERR は取得するが STDOUT は捨てる場合は |
3386 | 4196 | (ここでは順序が重要です): |
3387 | 4197 | |
3388 | 4198 | $output = `cmd 2>&1 1>/dev/null`; |
3389 | 4199 | |
3390 | 4200 | =begin original |
3391 | 4201 | |
3392 | 4202 | To exchange a command's STDOUT and STDERR in order to capture the STDERR |
3393 | 4203 | but leave its STDOUT to come out the old STDERR: |
3394 | 4204 | |
3395 | 4205 | =end original |
3396 | 4206 | |
3397 | 4207 | STDERR を取得するが、STDOUT は古い STDERR のために残しておくために |
3398 | 4208 | STDOUT と STDERR を交換するには: |
3399 | 4209 | |
3400 | 4210 | $output = `cmd 3>&1 1>&2 2>&3 3>&-`; |
3401 | 4211 | |
3402 | 4212 | =begin original |
3403 | 4213 | |
3404 | 4214 | To read both a command's STDOUT and its STDERR separately, it's easiest |
3405 | 4215 | to redirect them separately to files, and then read from those files |
3406 | 4216 | when the program is done: |
3407 | 4217 | |
3408 | 4218 | =end original |
3409 | 4219 | |
3410 | 4220 | コマンドの STDOUT と STDERR の両方を別々に読み込みたい場合、 |
3411 | 4221 | 一番簡単な方法は別々のファイルにリダイレクトし、 |
3412 | 4222 | プログラムが終了してからそのファイルを読むことです: |
3413 | 4223 | |
3414 | 4224 | system("program args 1>program.stdout 2>program.stderr"); |
3415 | 4225 | |
3416 | 4226 | =begin original |
3417 | 4227 | |
3418 | 4228 | The STDIN filehandle used by the command is inherited from Perl's STDIN. |
3419 | 4229 | For example: |
3420 | 4230 | |
3421 | 4231 | =end original |
3422 | 4232 | |
3423 | 4233 | コマンドによって使われる STDIN ファイルハンドルは Perl の STDIN を |
3424 | 4234 | 継承します。 |
3425 | 4235 | 例えば: |
3426 | 4236 | |
3427 | open | |
4237 | open(SPLAT, "stuff") || die "can't open stuff: $!"; | |
3428 | open | |
4238 | open(STDIN, "<&SPLAT") || die "can't dupe SPLAT: $!"; | |
3429 | print `sort`; | |
4239 | print STDOUT `sort`; | |
3430 | 4240 | |
3431 | 4241 | =begin original |
3432 | 4242 | |
3433 | will print the sorted contents of the file | |
4243 | will print the sorted contents of the file named F<"stuff">. | |
3434 | 4244 | |
3435 | 4245 | =end original |
3436 | 4246 | |
3437 | は | |
4247 | は F<"stuff"> という名前のファイルの内容をソートして表示します。 | |
3438 | 4248 | |
3439 | 4249 | =begin original |
3440 | 4250 | |
3441 | 4251 | Using single-quote as a delimiter protects the command from Perl's |
3442 | 4252 | double-quote interpolation, passing it on to the shell instead: |
3443 | 4253 | |
3444 | 4254 | =end original |
3445 | 4255 | |
3446 | 4256 | シングルクォートをデリミタとして使うと Perl のダブルクォート展開から |
3447 | 4257 | 保護され、そのままシェルに渡されます: |
3448 | 4258 | |
3449 | 4259 | $perl_info = qx(ps $$); # that's Perl's $$ |
3450 | 4260 | $shell_info = qx'ps $$'; # that's the new shell's $$ |
3451 | 4261 | |
3452 | 4262 | =begin original |
3453 | 4263 | |
3454 | 4264 | How that string gets evaluated is entirely subject to the command |
3455 | 4265 | interpreter on your system. On most platforms, you will have to protect |
3456 | 4266 | shell metacharacters if you want them treated literally. This is in |
3457 | 4267 | practice difficult to do, as it's unclear how to escape which characters. |
3458 | 4268 | See L<perlsec> for a clean and safe example of a manual fork() and exec() |
3459 | 4269 | to emulate backticks safely. |
3460 | 4270 | |
3461 | 4271 | =end original |
3462 | 4272 | |
3463 | 4273 | この文字列がどのように評価されるかは完全にシステムのコマンドインタプリタに |
3464 | 4274 | 依存します。 |
3465 | 4275 | ほとんどのプラットフォームでは、シェルのメタ文字をリテラルに |
3466 | 4276 | 扱ってほしい場合はそれを守る必要があります。 |
3467 | 4277 | 文字をエスケープする方法が明確ではないので、これは理論的には難しいことです。 |
3468 | 4278 | 逆クォートを安全にエミュレートするために手動で fork() と exec() を |
3469 | 4279 | 行うためのきれいで安全な例については L<perlsec> を参照してください。 |
3470 | 4280 | |
3471 | 4281 | =begin original |
3472 | 4282 | |
3473 | 4283 | On some platforms (notably DOS-like ones), the shell may not be |
3474 | 4284 | capable of dealing with multiline commands, so putting newlines in |
3475 | 4285 | the string may not get you what you want. You may be able to evaluate |
3476 | 4286 | multiple commands in a single line by separating them with the command |
3477 | 4287 | separator character, if your shell supports that (e.g. C<;> on many Unix |
3478 | 4288 | shells; C<&> on the Windows NT C<cmd> shell). |
3479 | 4289 | |
3480 | 4290 | =end original |
3481 | 4291 | |
3482 | 4292 | (特に DOS 風の)プラットフォームには、シェルが複数行のコマンドを扱うことが |
3483 | 4293 | できないものがあるので、文字列に改行を入れるとあなたの望まない結果に |
3484 | 4294 | なる場合があります。 |
3485 | 4295 | シェルが対応していれば、コマンド分割文字で分割することで |
3486 | 4296 | 1 行に複数のコマンドを入れて解釈させることができます |
3487 | 4297 | (この文字は、多くの Unix シェルでは C<;>、Windows NT C<cmd> シェルでは |
3488 | 4298 | C<&> です)。 |
3489 | 4299 | |
3490 | 4300 | =begin original |
3491 | 4301 | |
3492 | 4302 | Beginning with v5.6.0, Perl will attempt to flush all files opened for |
3493 | 4303 | output before starting the child process, but this may not be supported |
3494 | 4304 | on some platforms (see L<perlport>). To be safe, you may need to set |
3495 | 4305 | C<$|> ($AUTOFLUSH in English) or call the C<autoflush()> method of |
3496 | 4306 | C<IO::Handle> on any open handles. |
3497 | 4307 | |
3498 | 4308 | =end original |
3499 | 4309 | |
3500 | 4310 | v5.6.0 以降、Perl は子プロセスの実行前に書き込み用に開いている全ての |
3501 | 4311 | ファイルをフラッシュしようとしますが、これに対応していない |
3502 | 4312 | プラットフォームもあります(L<perlport> を参照してください)。 |
3503 | 4313 | 安全のためには、C<$|> (English モジュールでは $AUTOFLUSH)をセットするか、 |
3504 | 4314 | 開いている全てのハンドルに対して C<IO::Handle> の C<autoflush()> メソッドを |
3505 | 4315 | 呼び出す必要があります。 |
3506 | 4316 | |
3507 | 4317 | =begin original |
3508 | 4318 | |
3509 | 4319 | Beware that some command shells may place restrictions on the length |
3510 | 4320 | of the command line. You must ensure your strings don't exceed this |
3511 | 4321 | limit after any necessary interpolations. See the platform-specific |
3512 | 4322 | release notes for more details about your particular environment. |
3513 | 4323 | |
3514 | 4324 | =end original |
3515 | 4325 | |
3516 | 4326 | コマンド行の長さに制限があるコマンドシェルがあることに注意してください。 |
3517 | 4327 | 全ての必要な変換が行われた後、コマンド文字列がこの制限を越えないことを |
3518 | 4328 | 保障する必要があります。 |
3519 | 4329 | 特定の環境に関するさらなる詳細についてはプラットフォーム固有の |
3520 | 4330 | リリースノートを参照してください。 |
3521 | 4331 | |
3522 | 4332 | =begin original |
3523 | 4333 | |
3524 | 4334 | Using this operator can lead to programs that are difficult to port, |
3525 | 4335 | because the shell commands called vary between systems, and may in |
3526 | 4336 | fact not be present at all. As one example, the C<type> command under |
3527 | 4337 | the POSIX shell is very different from the C<type> command under DOS. |
3528 | 4338 | That doesn't mean you should go out of your way to avoid backticks |
3529 | 4339 | when they're the right way to get something done. Perl was made to be |
3530 | 4340 | a glue language, and one of the things it glues together is commands. |
3531 | 4341 | Just understand what you're getting yourself into. |
3532 | 4342 | |
3533 | 4343 | =end original |
3534 | 4344 | |
3535 | この演算子を使うと、プログラムの移殖が困難になります | |
4345 | この演算子を使うと、プログラムの移殖が困難になります; 呼び出されるシェル | |
3536 | ||
4346 | コマンドはシステムによって異なり、実際全く存在しないこともあるからです。 | |
3537 | 存在しないこともあるからです。 | |
3538 | 4347 | 一つの例としては、POSIX シェルの C<type> コマンドは DOS の C<type> コマンドと |
3539 | 4348 | 大きく異なっています。 |
3540 | 4349 | これは、何かを為すために正しい方法として逆クォートを使うことを |
3541 | 4350 | 避けるべきであることを意味しません。 |
3542 | 4351 | Perl は接着剤のような言語として作られ、接着されるべきものの一つは |
3543 | 4352 | コマンドです。 |
3544 | 4353 | 単にあなたが何をしようとしているかを理解しておいてください。 |
3545 | 4354 | |
3546 | 4355 | =begin original |
3547 | 4356 | |
3548 | 4357 | See L</"I/O Operators"> for more discussion. |
3549 | 4358 | |
3550 | 4359 | =end original |
3551 | 4360 | |
3552 | 4361 | さらなる議論については L</"I/O Operators"> を参照して下さい。 |
3553 | 4362 | |
3554 | 4363 | =item qw/STRING/ |
3555 | 4364 | X<qw> X<quote, list> X<quote, words> |
3556 | 4365 | |
3557 | 4366 | =begin original |
3558 | 4367 | |
3559 | 4368 | Evaluates to a list of the words extracted out of STRING, using embedded |
3560 | 4369 | whitespace as the word delimiters. It can be understood as being roughly |
3561 | 4370 | equivalent to: |
3562 | 4371 | |
3563 | 4372 | =end original |
3564 | 4373 | |
3565 | 4374 | 埋め込まれた空白を区切文字として、STRING から抜き出した単語のリストを |
3566 | 4375 | 評価します。 |
3567 | 4376 | これは、以下の式と大体同じと考えられます: |
3568 | 4377 | |
3569 | split( | |
4378 | split(" ", q/STRING/); | |
3570 | 4379 | |
3571 | 4380 | =begin original |
3572 | 4381 | |
3573 | 4382 | the differences being that it generates a real list at compile time, and |
3574 | 4383 | in scalar context it returns the last element in the list. So |
3575 | 4384 | this expression: |
3576 | 4385 | |
3577 | 4386 | =end original |
3578 | 4387 | |
3579 | 4388 | 違いは、実際のリストをコンパイル時に生成し、スカラコンテキストではリストの |
3580 | 4389 | 最後の要素を返すことです。 |
3581 | 4390 | 従って、以下の表現は: |
3582 | 4391 | |
3583 | 4392 | qw(foo bar baz) |
3584 | 4393 | |
3585 | 4394 | =begin original |
3586 | 4395 | |
3587 | 4396 | is semantically equivalent to the list: |
3588 | 4397 | |
3589 | 4398 | =end original |
3590 | 4399 | |
3591 | 4400 | 以下のリストと文法的に等価です。 |
3592 | 4401 | |
3593 | | |
4402 | "foo", "bar", "baz" | |
3594 | 4403 | |
3595 | 4404 | =begin original |
3596 | 4405 | |
3597 | 4406 | Some frequently seen examples: |
3598 | 4407 | |
3599 | 4408 | =end original |
3600 | 4409 | |
3601 | 4410 | よく行なわれる例としては以下のものです: |
3602 | 4411 | |
3603 | 4412 | use POSIX qw( setlocale localeconv ) |
3604 | 4413 | @EXPORT = qw( foo bar baz ); |
3605 | 4414 | |
3606 | 4415 | =begin original |
3607 | 4416 | |
3608 | 4417 | A common mistake is to try to separate the words with comma or to |
3609 | 4418 | put comments into a multi-line C<qw>-string. For this reason, the |
3610 | 4419 | C<use warnings> pragma and the B<-w> switch (that is, the C<$^W> variable) |
3611 | 4420 | produces warnings if the STRING contains the "," or the "#" character. |
3612 | 4421 | |
3613 | 4422 | =end original |
3614 | 4423 | |
3615 | 4424 | よくある間違いは、単語をカンマで区切ったり、複数行の C<qw> 文字列の中に |
3616 | 4425 | コメントを書いたりすることです。 |
3617 | 4426 | このために、C<usr warnings> プラグマと B<-w> スイッチ |
3618 | 4427 | (つまり、C<$^W> 変数) は STRING に "," や "#" の文字が入っていると |
3619 | 4428 | 警告を出します。 |
3620 | 4429 | |
4430 | =item tr/SEARCHLIST/REPLACEMENTLIST/cdsr | |
3622 | =item tr/SEARCHLIST/REPLACEMENTLIST/cds | |
3623 | 4431 | X<tr> X<y> X<transliterate> X</c> X</d> X</s> |
3624 | 4432 | |
3625 | =item y/SEARCHLIST/REPLACEMENTLIST/cds | |
4433 | =item y/SEARCHLIST/REPLACEMENTLIST/cdsr | |
3626 | 4434 | |
3627 | 4435 | =begin original |
3628 | 4436 | |
3629 | 4437 | Transliterates all occurrences of the characters found in the search list |
3630 | 4438 | with the corresponding character in the replacement list. It returns |
3631 | 4439 | the number of characters replaced or deleted. If no string is |
3632 | specified via the =~ or !~ operator, the $_ string is transliterated. | |
4440 | specified via the C<=~> or C<!~> operator, the $_ string is transliterated. | |
3633 | string specified with =~ must be a scalar variable, an array element, a | |
3634 | hash element, or an assignment to one of those, i.e., an lvalue.) | |
3635 | 4441 | |
3636 | 4442 | =end original |
3637 | 4443 | |
3638 | 4444 | 検索リスト (SEARCHLIST) に含まれる文字を、対応する置換リスト |
3639 | 4445 | (REPLACEMENTLIST) の文字に変換します。 |
3640 | 4446 | 置換または削除が行なわれた、文字数を返します。 |
3641 | =~ 演算子や | |
4447 | C<=~> 演算子や C<!~> 演算子で文字列が指定されていなければ、$_ の文字列が | |
3642 | 4448 | 変換されます。 |
3643 | (=~ で指定される文字列は、スカラ変数、配列要素、ハッシュ要素、 | |
3644 | あるいはこれらへの代入式といった左辺値でなければなりません。) | |
3645 | 4449 | |
3646 | 4450 | =begin original |
3647 | 4451 | |
4452 | If the C</r> (non-destructive) option is present, a new copy of the string | |
4453 | is made and its characters transliterated, and this copy is returned no | |
4454 | matter whether it was modified or not: the original string is always | |
4455 | left unchanged. The new copy is always a plain string, even if the input | |
4456 | string is an object or a tied variable. | |
4457 | ||
4458 | =end original | |
4459 | ||
4460 | C</r> (非破壊) オプションがあると、文字列の新しいコピーが作られてその | |
4461 | 文字が変換され、変更されたかどうかに関わらずこのコピーが返されます: | |
4462 | 元の文字列は常に無変更で残されます。 | |
4463 | 新しいコピーは、たとえ入力がオブジェクトや tie された変数でも、常に | |
4464 | プレーンな文字列です。 | |
4465 | ||
4466 | =begin original | |
4467 | ||
4468 | Unless the C</r> option is used, the string specified with C<=~> must be a | |
4469 | scalar variable, an array element, a hash element, or an assignment to one | |
4470 | of those; in other words, an lvalue. | |
4471 | ||
4472 | =end original | |
4473 | ||
4474 | C</r> オプションが使われない限り、C<=~> で指定される文字列は、スカラ変数、 | |
4475 | 配列要素、ハッシュ要素、あるいはこれらへの代入式といった左辺値で | |
4476 | なければなりません。 | |
4477 | ||
4478 | =begin original | |
4479 | ||
3648 | 4480 | A character range may be specified with a hyphen, so C<tr/A-J/0-9/> |
3649 | 4481 | does the same replacement as C<tr/ACEGIBDFHJ/0246813579/>. |
3650 | 4482 | For B<sed> devotees, C<y> is provided as a synonym for C<tr>. If the |
3651 | 4483 | SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST has |
3652 | its own pair of quotes, which may or may not be bracketing quotes | |
4484 | its own pair of quotes, which may or may not be bracketing quotes; | |
3653 | e | |
4485 | for example, C<tr[aeiouy][yuoiea]> or C<tr(+\-*/)/ABCD/>. | |
3654 | 4486 | |
3655 | 4487 | =end original |
3656 | 4488 | |
3657 | 文字の範囲はハイフンを使って指定できます | |
4489 | 文字の範囲はハイフンを使って指定できます; C<tr/A-J/0-9/> は | |
3658 | C<tr/A | |
4490 | C<tr/ACEGIBDFHJ/0246813579/> と同じ置換を行います。 | |
3659 | 4491 | B<sed> の信仰者のために C<y> が C<tr> の同義語として提供されています。 |
3660 | SEARCHLIST を括弧類で括った場合には、 | |
4492 | SEARCHLIST を括弧類で括った場合には、REPLACEMENTLIST 用に、もう一組の区切り | |
3661 | ||
4493 | 文字を用意します; これは、括弧類であっても、なくてもかまいません; | |
3662 | ||
4494 | 例えば、C<tr[aeiouy][yuoiea]> や C<tr(+\-*/)/ABCD/> です。 | |
3663 | 例: C<tr[A-Z][a-z]> や C<tr(+\-*/)/ABCD/>。 | |
3664 | 4495 | |
3665 | 4496 | =begin original |
3666 | 4497 | |
3667 | Note that C<tr> does B<not> do regular expression character classes | |
4498 | Note that C<tr> does B<not> do regular expression character classes such as | |
3668 | ||
4499 | C<\d> or C<\pL>. The C<tr> operator is not equivalent to the tr(1) | |
3669 | ||
4500 | utility. If you want to map strings between lower/upper cases, see | |
3670 | ||
4501 | L<perlfunc/lc> and L<perlfunc/uc>, and in general consider using the C<s> | |
3671 | ||
4502 | operator if you need regular expressions. The C<\U>, C<\u>, C<\L>, and | |
4503 | C<\l> string-interpolation escapes on the right side of a substitution | |
4504 | operator will perform correct case-mappings, but C<tr[a-z][A-Z]> will not | |
4505 | (except sometimes on legacy 7-bit data). | |
3672 | 4506 | |
3673 | 4507 | =end original |
3674 | 4508 | |
3675 | C<tr> は C<\d> や C< | |
4509 | C<tr> は C<\d> や C<\pL> といった正規表現文字クラスを | |
3676 | 4510 | B<使わない> ことに注意してください。 |
3677 | 4511 | C<tr> 演算子は tr(1) ユーティリティと等価ではありません。 |
3678 | 4512 | 文字列の大文字小文字をマップしたい場合は、 |
3679 | L<perlfunc/lc> と L<perlfunc/uc> を参照して下さい | |
4513 | L<perlfunc/lc> と L<perlfunc/uc> を参照して下さい; また正規表現が必要な | |
3680 | ||
4514 | 場合には一般的に C<s> 演算子を使うことを考慮してみてください。 | |
3681 | ||
4515 | 置換演算子の右側での C<\U>, C<\u>, C<\L>, C<\l> の文字変換エスケープは | |
4516 | 正しい大文字小文字マッピングを実行しますが、C<tr[a-z][A-Z]> は | |
4517 | (レガシーな 7 ビットデータを除いて) 動作しません。 | |
3682 | 4518 | |
3683 | 4519 | =begin original |
3684 | 4520 | |
3685 | 4521 | Note also that the whole range idea is rather unportable between |
3686 | 4522 | character sets--and even within character sets they may cause results |
3687 | 4523 | you probably didn't expect. A sound principle is to use only ranges |
3688 | 4524 | that begin from and end at either alphabets of equal case (a-e, A-E), |
3689 | 4525 | or digits (0-4). Anything else is unsafe. If in doubt, spell out the |
3690 | 4526 | character sets in full. |
3691 | 4527 | |
3692 | 4528 | =end original |
3693 | 4529 | |
3694 | 4530 | 範囲指定という考え方は文字セットが異なる場合はやや移植性に欠けることにも |
3695 | 4531 | 注意してください -- そして同じ文字セットでも恐らく期待しているのとは違う |
3696 | 4532 | 結果を引き起こすこともあります。 |
3697 | 4533 | 健全な原則としては、範囲の最初と最後をどちらもアルファベット |
3698 | 4534 | (大文字小文字も同じ)(a-e, A-E)にするか、どちらも数字にする(0-4)ことです。 |
3699 | 4535 | それ以外は全て安全ではありません。 |
3700 | 4536 | 疑わしいときは、文字セットを完全に書き出してください。 |
3701 | 4537 | |
3702 | 4538 | =begin original |
3703 | 4539 | |
3704 | 4540 | Options: |
3705 | 4541 | |
3706 | 4542 | =end original |
3707 | 4543 | |
3708 | 4544 | オプションは以下の通りです: |
3709 | 4545 | |
3710 | 4546 | =begin original |
3711 | 4547 | |
3712 | 4548 | c Complement the SEARCHLIST. |
3713 | 4549 | d Delete found but unreplaced characters. |
3714 | 4550 | s Squash duplicate replaced characters. |
4551 | r Return the modified string and leave the original string | |
4552 | untouched. | |
3715 | 4553 | |
3716 | 4554 | =end original |
3717 | 4555 | |
3718 | c SEARCHLIST を補集合にする | |
4556 | c SEARCHLIST を補集合にする。 | |
3719 | d 見つかったが置換されなかった文字を削除する | |
4557 | d 見つかったが置換されなかった文字を削除する。 | |
3720 | s 置換された文字が重なったときに圧縮する | |
4558 | s 置換された文字が重なったときに圧縮する。 | |
4559 | r 修正した結果を返し、もとの文字列はそのままにする。 | |
3721 | 4560 | |
3722 | 4561 | =begin original |
3723 | 4562 | |
3724 | 4563 | If the C</c> modifier is specified, the SEARCHLIST character set |
3725 | 4564 | is complemented. If the C</d> modifier is specified, any characters |
3726 | 4565 | specified by SEARCHLIST not found in REPLACEMENTLIST are deleted. |
3727 | 4566 | (Note that this is slightly more flexible than the behavior of some |
3728 | 4567 | B<tr> programs, which delete anything they find in the SEARCHLIST, |
3729 | 4568 | period.) If the C</s> modifier is specified, sequences of characters |
3730 | 4569 | that were transliterated to the same character are squashed down |
3731 | 4570 | to a single instance of the character. |
3732 | 4571 | |
3733 | 4572 | =end original |
3734 | 4573 | |
3735 | 4574 | C</c> 修飾子が指定されると、SEARCHLIST には補集合が指定されたものと |
3736 | 4575 | 解釈されます。 |
3737 | 4576 | C</d> 修飾子が指定されると、SEARCHLIST に指定されて、 |
3738 | 4577 | REPLACEMENTLIST に対応するものがない文字が削除されます。 |
3739 | 4578 | (これは、SEARCHLIST で見つかったものを削除する、ただそれだけの、ある種の |
3740 | 4579 | B<tr> プログラムの動作よりと比べれば、いく分柔軟なものになっています。) |
3741 | 4580 | C</s> 修飾子が指定されると、同じ文字に文字変換された文字の並びを、 |
3742 | その文字 1 文字だけに圧縮します。 | |
4581 | その文字 1 文字だけに圧縮します。 | |
3743 | 4582 | |
3744 | 4583 | =begin original |
3745 | 4584 | |
3746 | 4585 | If the C</d> modifier is used, the REPLACEMENTLIST is always interpreted |
3747 | 4586 | exactly as specified. Otherwise, if the REPLACEMENTLIST is shorter |
3748 | 4587 | than the SEARCHLIST, the final character is replicated till it is long |
3749 | 4588 | enough. If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated. |
3750 | 4589 | This latter is useful for counting characters in a class or for |
3751 | 4590 | squashing character sequences in a class. |
3752 | 4591 | |
3753 | 4592 | =end original |
3754 | 4593 | |
3755 | 4594 | C</d> 修飾子が使われると、REPLACEMENTLIST は、常に指定された通りに |
3756 | 4595 | 解釈されます。 |
3757 | 4596 | C</d> が指定されない場合で、REPLACEMENTLIST が SEARCHLIST よりも短いと、 |
3758 | 4597 | 同じ長さになるまで、REPLACEMENTLIST の最後の文字が |
3759 | 4598 | 繰り返されているものとして扱われます。 |
3760 | 4599 | REPLACEMENTLIST が空文字列でのときには、SEARCHLIST と同じになります。 |
3761 | 4600 | 後者は、ある文字クラスに含まれる文字数を数えるときや、 |
3762 | 4601 | ある文字クラスの文字の並びを圧縮するようなときに便利です。 |
3763 | 4602 | |
3764 | 4603 | =begin original |
3765 | 4604 | |
3766 | 4605 | Examples: |
3767 | 4606 | |
3768 | 4607 | =end original |
3769 | 4608 | |
3770 | 4609 | 例: |
3771 | 4610 | |
3772 | $ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case | |
4611 | $ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case ASCII | |
3773 | 4612 | |
3774 | 4613 | $cnt = tr/*/*/; # count the stars in $_ |
3775 | 4614 | |
3776 | 4615 | $cnt = $sky =~ tr/*/*/; # count the stars in $sky |
3777 | 4616 | |
3778 | 4617 | $cnt = tr/0-9//; # count the digits in $_ |
3779 | 4618 | |
3780 | 4619 | tr/a-zA-Z//s; # bookkeeper -> bokeper |
3781 | 4620 | |
3782 | 4621 | ($HOST = $host) =~ tr/a-z/A-Z/; |
4622 | $HOST = $host =~ tr/a-z/A-Z/r; # same thing | |
3783 | 4623 | |
4624 | $HOST = $host =~ tr/a-z/A-Z/r # chained with s///r | |
4625 | =~ s/:/ -p/r; | |
4626 | ||
3784 | 4627 | tr/a-zA-Z/ /cs; # change non-alphas to single space |
3785 | 4628 | |
4629 | @stripped = map tr/a-zA-Z/ /csr, @original; | |
4630 | # /r with map | |
4631 | ||
3786 | 4632 | tr [\200-\377] |
3787 | [\000-\177]; # delete 8th bit | |
4633 | [\000-\177]; # wickedly delete 8th bit | |
3788 | 4634 | |
3789 | 4635 | =begin original |
3790 | 4636 | |
3791 | 4637 | If multiple transliterations are given for a character, only the |
3792 | 4638 | first one is used: |
3793 | 4639 | |
3794 | 4640 | =end original |
3795 | 4641 | |
3796 | 4642 | 複数の文字変換が一つの文字について指定されると、最初のものだけが使われます。 |
3797 | 4643 | |
3798 | 4644 | tr/AAA/XYZ/ |
3799 | 4645 | |
3800 | 4646 | =begin original |
3801 | 4647 | |
3802 | 4648 | will transliterate any A to X. |
3803 | 4649 | |
3804 | 4650 | =end original |
3805 | 4651 | |
3806 | 4652 | は A を X に変換します。 |
3807 | 4653 | |
3808 | 4654 | =begin original |
3809 | 4655 | |
3810 | 4656 | Because the transliteration table is built at compile time, neither |
3811 | 4657 | the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote |
3812 | 4658 | interpolation. That means that if you want to use variables, you |
3813 | 4659 | must use an eval(): |
3814 | 4660 | |
3815 | 4661 | =end original |
3816 | 4662 | |
3817 | 4663 | 変換テーブルはコンパイル時に作られるので、SEARCHLIST も |
3818 | 4664 | REPLACEMENTLIST もダブルクォート展開の対象とはなりません。 |
3819 | 4665 | 変数を使いたい場合には、eval() を使わなければならないということです: |
3820 | 4666 | |
3821 | 4667 | eval "tr/$oldlist/$newlist/"; |
3822 | 4668 | die $@ if $@; |
3823 | 4669 | |
3824 | 4670 | eval "tr/$oldlist/$newlist/, 1" or die $@; |
3825 | 4671 | |
3826 | 4672 | =item <<EOF |
3827 | 4673 | X<here-doc> X<heredoc> X<here-document> X<<< << >>> |
3828 | 4674 | |
3829 | 4675 | =begin original |
3830 | 4676 | |
3831 | 4677 | A line-oriented form of quoting is based on the shell "here-document" |
3832 | 4678 | syntax. Following a C<< << >> you specify a string to terminate |
3833 | 4679 | the quoted material, and all lines following the current line down to |
3834 | 4680 | the terminating string are the value of the item. |
3835 | 4681 | |
3836 | 4682 | =end original |
3837 | 4683 | |
3838 | 4684 | クォートの行指向形式は、シェルの「ヒアドキュメント」構文を基にしています。 |
3839 | 4685 | C<< << >> に引き続いて、クォートされるテキストを終了させる文字列を指定でき、 |
3840 | 4686 | 現在の行の次の行から終端文字列までの全ての行がその項目の値となります。 |
3841 | 4687 | |
3842 | 4688 | =begin original |
3843 | 4689 | |
3844 | 4690 | The terminating string may be either an identifier (a word), or some |
3845 | 4691 | quoted text. An unquoted identifier works like double quotes. |
3846 | 4692 | There may not be a space between the C<< << >> and the identifier, |
3847 | 4693 | unless the identifier is explicitly quoted. (If you put a space it |
3848 | 4694 | will be treated as a null identifier, which is valid, and matches the |
3849 | 4695 | first empty line.) The terminating string must appear by itself |
3850 | 4696 | (unquoted and with no surrounding whitespace) on the terminating line. |
3851 | 4697 | |
3852 | 4698 | =end original |
3853 | 4699 | |
3854 | 4700 | 終端文字列には、識別子(単語) かクォートされたテキストが使えます。 |
3855 | 4701 | クォートされていない識別子は、ダブルクォートのように扱われます。 |
3856 | 4702 | 識別子がクォートされていない場合は、C<< << >> と識別子の間に |
3857 | 4703 | 空白を入れてはいけません。 |
3858 | 4704 | (もし空白を入れると、空識別子として扱われます; これは有効で、 |
3859 | 4705 | 最初の空行にマッチするようになります。) |
3860 | 4706 | 終端文字列は終端行に単体で(クォートされず、前後にも空白なしで) |
3861 | 4707 | 現れなければなりません。 |
3862 | 4708 | |
3863 | 4709 | =begin original |
3864 | 4710 | |
3865 | 4711 | If the terminating string is quoted, the type of quotes used determine |
3866 | 4712 | the treatment of the text. |
3867 | 4713 | |
3868 | 4714 | =end original |
3869 | 4715 | |
3870 | 4716 | 終端文字列がクォートされている場合には、そのクォートの種類によって、 |
3871 | 4717 | クォートされるテキストの扱い決められます。 |
3872 | 4718 | |
3873 | 4719 | =over 4 |
3874 | 4720 | |
3875 | 4721 | =item Double Quotes |
3876 | 4722 | |
3877 | 4723 | =begin original |
3878 | 4724 | |
3879 | 4725 | Double quotes indicate that the text will be interpolated using exactly |
3880 | 4726 | the same rules as normal double quoted strings. |
3881 | 4727 | |
3882 | 4728 | =end original |
3883 | 4729 | |
3884 | 4730 | ダブルクォートは、通常のダブルクォートされた文字列と全く同じ規則を使って |
3885 | 4731 | 変数展開されることを示します。 |
3886 | 4732 | |
3887 | 4733 | print <<EOF; |
3888 | 4734 | The price is $Price. |
3889 | 4735 | EOF |
3890 | 4736 | |
3891 | 4737 | print << "EOF"; # same as above |
3892 | 4738 | The price is $Price. |
3893 | 4739 | EOF |
3894 | 4740 | |
4741 | ||
3895 | 4742 | =item Single Quotes |
3896 | 4743 | |
3897 | 4744 | =begin original |
3898 | 4745 | |
3899 | 4746 | Single quotes indicate the text is to be treated literally with no |
3900 | 4747 | interpolation of its content. This is similar to single quoted |
3901 | 4748 | strings except that backslashes have no special meaning, with C<\\> |
3902 | 4749 | being treated as two backslashes and not one as they would in every |
3903 | 4750 | other quoting construct. |
3904 | 4751 | |
3905 | 4752 | =end original |
3906 | 4753 | |
3907 | 4754 | シングルクォートは、内容が展開されずにリテラルに扱われることを |
3908 | 4755 | 意味します。 |
3909 | 4756 | これはシングルクォート文字列と似ていますが、バックスラッシュは |
3910 | 4757 | 特別な意味を持たず、他の全てのクォート構造違って、C<\\> は |
3911 | 4758 | 二つのバックスラッシュとして扱われることが違います。 |
3912 | 4759 | |
3913 | 4760 | =begin original |
3914 | 4761 | |
4762 | Just as in the shell, a backslashed bareword following the C<<< << >>> | |
4763 | means the same thing as a single-quoted string does: | |
4764 | ||
4765 | =end original | |
4766 | ||
4767 | シェルでの場合と同様、C<<< << >>> に引き続いてバックスラッシュ付きの | |
4768 | 裸の単語があると、シングルクォートされた文字列と同じこととなります: | |
4769 | ||
4770 | $cost = <<'VISTA'; # hasta la ... | |
4771 | That'll be $10 please, ma'am. | |
4772 | VISTA | |
4773 | ||
4774 | $cost = <<\VISTA; # Same thing! | |
4775 | That'll be $10 please, ma'am. | |
4776 | VISTA | |
4777 | ||
4778 | =begin original | |
4779 | ||
3915 | 4780 | This is the only form of quoting in perl where there is no need |
3916 | 4781 | to worry about escaping content, something that code generators |
3917 | 4782 | can and do make good use of. |
3918 | 4783 | |
3919 | 4784 | =end original |
3920 | 4785 | |
3921 | 4786 | これは、perl において、内容のエスケープについて心配する必要のない |
3922 | 4787 | 唯一の形で、コードジェネレータがうまく使えるものです。 |
3923 | 4788 | |
3924 | 4789 | =item Backticks |
3925 | 4790 | |
3926 | 4791 | =begin original |
3927 | 4792 | |
3928 | 4793 | The content of the here doc is treated just as it would be if the |
3929 | 4794 | string were embedded in backticks. Thus the content is interpolated |
3930 | 4795 | as though it were double quoted and then executed via the shell, with |
3931 | 4796 | the results of the execution returned. |
3932 | 4797 | |
3933 | 4798 | =end original |
3934 | 4799 | |
3935 | ヒ | |
4800 | ヒアドキュメントのの内容は、文字列がバッククォートで | |
3936 | 4801 | 埋め込まれているかのように扱われます。 |
3937 | 4802 | したがって、その内容、はダブルクォートされているかのように変数展開され、 |
3938 | 4803 | その後シェル経由で実行され、実行された結果になります。 |
3939 | 4804 | |
3940 | 4805 | print << `EOC`; # execute command and get results |
3941 | 4806 | echo hi there |
3942 | 4807 | EOC |
3943 | 4808 | |
3944 | 4809 | =back |
3945 | 4810 | |
3946 | 4811 | =begin original |
3947 | 4812 | |
3948 | 4813 | It is possible to stack multiple here-docs in a row: |
3949 | 4814 | |
3950 | 4815 | =end original |
3951 | 4816 | |
3952 | 複数のヒ | |
4817 | 複数のヒアドキュメントを連続してスタックすることも可能です: | |
3953 | 4818 | |
3954 | 4819 | print <<"foo", <<"bar"; # you can stack them |
3955 | 4820 | I said foo. |
3956 | 4821 | foo |
3957 | 4822 | I said bar. |
3958 | 4823 | bar |
3959 | 4824 | |
3960 | 4825 | myfunc(<< "THIS", 23, <<'THAT'); |
3961 | 4826 | Here's a line |
3962 | 4827 | or two. |
3963 | 4828 | THIS |
3964 | 4829 | and here's another. |
3965 | 4830 | THAT |
3966 | 4831 | |
3967 | 4832 | =begin original |
3968 | 4833 | |
3969 | 4834 | Just don't forget that you have to put a semicolon on the end |
3970 | 4835 | to finish the statement, as Perl doesn't know you're not going to |
3971 | 4836 | try to do this: |
3972 | 4837 | |
3973 | 4838 | =end original |
3974 | 4839 | |
3975 | 4840 | 以下のようなことをしたいのではないということが Perl にはわからないので、 |
3976 | 4841 | 文を終わらせるためには末尾にセミコロンをつけなければならないことを |
3977 | 4842 | 忘れないで下さい: |
3978 | 4843 | |
3979 | 4844 | print <<ABC |
3980 | 4845 | 179231 |
3981 | 4846 | ABC |
3982 | 4847 | + 20; |
3983 | 4848 | |
3984 | 4849 | =begin original |
3985 | 4850 | |
3986 | 4851 | If you want to remove the line terminator from your here-docs, |
3987 | 4852 | use C<chomp()>. |
3988 | 4853 | |
3989 | 4854 | =end original |
3990 | 4855 | |
3991 | ヒ | |
4856 | ヒアドキュメントから行終端子を除去したい場合は、C<chomp()> を使ってください。 | |
3992 | 4857 | |
3993 | 4858 | chomp($string = <<'END'); |
3994 | 4859 | This is a string. |
3995 | 4860 | END |
3996 | 4861 | |
3997 | 4862 | =begin original |
3998 | 4863 | |
3999 | 4864 | If you want your here-docs to be indented with the rest of the code, |
4000 | 4865 | you'll need to remove leading whitespace from each line manually: |
4001 | 4866 | |
4002 | 4867 | =end original |
4003 | 4868 | |
4004 | 4869 | ヒアドキュメントをソースのほかの部分からインデントしたい場合、 |
4005 | 4870 | 各行の先頭の空白は手動で取り除く必要があります: |
4006 | 4871 | |
4007 | 4872 | ($quote = <<'FINIS') =~ s/^\s+//gm; |
4008 | 4873 | The Road goes ever on and on, |
4009 | 4874 | down from the door where it began. |
4010 | 4875 | FINIS |
4011 | 4876 | |
4012 | 4877 | =begin original |
4013 | 4878 | |
4014 | 4879 | If you use a here-doc within a delimited construct, such as in C<s///eg>, |
4015 | 4880 | the quoted material must come on the lines following the final delimiter. |
4016 | 4881 | So instead of |
4017 | 4882 | |
4018 | 4883 | =end original |
4019 | 4884 | |
4020 | 4885 | C<s///eg> のようなデリミタ構造の中でヒアドキュメントを使う場合、 |
4021 | 4886 | クォートされたものは最後のデリミタに引き続くものとして来なければなりません。 |
4022 | 4887 | 従って、以下のようではなく: |
4023 | 4888 | |
4024 | 4889 | s/this/<<E . 'that' |
4025 | 4890 | the other |
4026 | 4891 | E |
4027 | 4892 | . 'more '/eg; |
4028 | 4893 | |
4029 | 4894 | =begin original |
4030 | 4895 | |
4031 | 4896 | you have to write |
4032 | 4897 | |
4033 | 4898 | =end original |
4034 | 4899 | |
4035 | 4900 | 以下のように書かなければなりません: |
4036 | 4901 | |
4037 | 4902 | s/this/<<E . 'that' |
4038 | 4903 | . 'more '/eg; |
4039 | 4904 | the other |
4040 | 4905 | E |
4041 | 4906 | |
4042 | 4907 | =begin original |
4043 | 4908 | |
4044 | 4909 | If the terminating identifier is on the last line of the program, you |
4045 | 4910 | must be sure there is a newline after it; otherwise, Perl will give the |
4046 | 4911 | warning B<Can't find string terminator "END" anywhere before EOF...>. |
4047 | 4912 | |
4048 | 4913 | =end original |
4049 | 4914 | |
4050 | 4915 | プログラムの最後の行に終端識別子がある場合、その後には |
4051 | 4916 | 改行がなければなりません; さもなければ、Perl は |
4052 | 4917 | B<Can't find string terminator "END" anywhere before EOF...> という |
4053 | 4918 | 警告を出します。 |
4054 | 4919 | |
4055 | 4920 | =begin original |
4056 | 4921 | |
4057 | Additionally, | |
4922 | Additionally, quoting rules for the end-of-string identifier are | |
4058 | related to Perl's quoting rules | |
4923 | unrelated to Perl's quoting rules. C<q()>, C<qq()>, and the like are not | |
4059 | 4924 | supported in place of C<''> and C<"">, and the only interpolation is for |
4060 | 4925 | backslashing the quoting character: |
4061 | 4926 | |
4062 | 4927 | =end original |
4063 | 4928 | |
4064 | 4929 | さらに、文字列終端識別子に対するクォートルールは Perl のクォートルールとは |
4065 | 関係がありません | |
4930 | 関係がありません。 | |
4931 | C<q()>, C<qq()> および同種のものは C<''> や C<""> の | |
4066 | 4932 | 代わりのなるものには対応しておらず、文字をクォートするための |
4067 | 4933 | バックスラッシュだけが展開されます: |
4068 | 4934 | |
4069 | 4935 | print << "abc\"def"; |
4070 | 4936 | testing... |
4071 | 4937 | abc"def |
4072 | 4938 | |
4073 | 4939 | =begin original |
4074 | 4940 | |
4075 | 4941 | Finally, quoted strings cannot span multiple lines. The general rule is |
4076 | 4942 | that the identifier must be a string literal. Stick with that, and you |
4077 | 4943 | should be safe. |
4078 | 4944 | |
4079 | 4945 | =end original |
4080 | 4946 | |
4081 | 4947 | 最後に、クォートされた文字列は複数行にかかることはありません。 |
4082 | 4948 | 識別子に関する一般的なルールは、文字列リテラルでなければならいことです。 |
4083 | 4949 | これに従っていれば、安全のはずです。 |
4084 | 4950 | |
4085 | 4951 | =back |
4086 | 4952 | |
4087 | 4953 | =head2 Gory details of parsing quoted constructs |
4088 | 4954 | X<quote, gory details> |
4089 | 4955 | |
4090 | 4956 | (クォートされた構造のパースに関する詳細) |
4091 | 4957 | |
4092 | 4958 | =begin original |
4093 | 4959 | |
4094 | 4960 | When presented with something that might have several different |
4095 | 4961 | interpretations, Perl uses the B<DWIM> (that's "Do What I Mean") |
4096 | 4962 | principle to pick the most probable interpretation. This strategy |
4097 | 4963 | is so successful that Perl programmers often do not suspect the |
4098 | 4964 | ambivalence of what they write. But from time to time, Perl's |
4099 | 4965 | notions differ substantially from what the author honestly meant. |
4100 | 4966 | |
4101 | 4967 | =end original |
4102 | 4968 | |
4103 | 4969 | 何か複数の解釈が可能な表現があった場合、Perl は最も確からしい解釈を |
4104 | 4970 | 選択するために B<DWIM> ("Do What I Mean")原則を使います。 |
4105 | 4971 | この戦略は非常に成功したので、Perl プログラマはしばしば |
4106 | 4972 | 自分が書いたものの矛盾を疑いません。 |
4107 | 4973 | しかし時間がたつにつれて、Perl の概念は作者が本当に意味していたものから |
4108 | 4974 | かなり変わりました。 |
4109 | 4975 | |
4110 | 4976 | =begin original |
4111 | 4977 | |
4112 | 4978 | This section hopes to clarify how Perl handles quoted constructs. |
4113 | 4979 | Although the most common reason to learn this is to unravel labyrinthine |
4114 | 4980 | regular expressions, because the initial steps of parsing are the |
4115 | 4981 | same for all quoting operators, they are all discussed together. |
4116 | 4982 | |
4117 | 4983 | =end original |
4118 | 4984 | |
4119 | 4985 | この章では Perl がどのようにクォートされた構造を扱うかを |
4120 | 4986 | 明確にしようと思います。 |
4121 | 4987 | これを学ぼうとする最もよくある理由は正規表現の迷宮をほぐすためですが、 |
4122 | 4988 | パースの初期ステップは全てのクォート演算子で同じなので、全て同時に扱います。 |
4123 | 4989 | |
4124 | 4990 | =begin original |
4125 | 4991 | |
4126 | 4992 | The most important Perl parsing rule is the first one discussed |
4127 | 4993 | below: when processing a quoted construct, Perl first finds the end |
4128 | 4994 | of that construct, then interprets its contents. If you understand |
4129 | 4995 | this rule, you may skip the rest of this section on the first |
4130 | 4996 | reading. The other rules are likely to contradict the user's |
4131 | 4997 | expectations much less frequently than this first one. |
4132 | 4998 | |
4133 | 4999 | =end original |
4134 | 5000 | |
4135 | 5001 | Perl のパースに関するルールで最も重要なものは以下で述べているうち |
4136 | 最初のものです | |
5002 | 最初のものです: つまり、クォートされた構造を処理するときは、Perl はまずその | |
4137 | ||
5003 | 構造の最後を探して、それから中身を解釈します。 | |
4138 | 最後を探して、それから中身を解釈します。 | |
4139 | 5004 | このルールがわかれば、とりあえずはこの章の残りは読み飛ばしてもかまいません。 |
4140 | 5005 | その他のルールは最初のルールに比べてユーザーの予想に反する頻度は |
4141 | 5006 | はるかに少ないです。 |
4142 | 5007 | |
4143 | 5008 | =begin original |
4144 | 5009 | |
4145 | 5010 | Some passes discussed below are performed concurrently, but because |
4146 | 5011 | their results are the same, we consider them individually. For different |
4147 | 5012 | quoting constructs, Perl performs different numbers of passes, from |
4148 | 5013 | one to four, but these passes are always performed in the same order. |
4149 | 5014 | |
4150 | 5015 | =end original |
4151 | 5016 | |
4152 | 5017 | 以下で議論するパスには同時に実行されるものもありますが、 |
4153 | 5018 | 結果は同じことなので、別々に考えることにします。 |
4154 | 5019 | クォート構造の種類によって、Perl が実行するパスの数は |
4155 | 5020 | 1 から 4 まで異なりますが、これらのパスは常に同じ順番で実行されます。 |
4156 | 5021 | |
4157 | 5022 | =over 4 |
4158 | 5023 | |
4159 | 5024 | =item Finding the end |
4160 | 5025 | |
4161 | 5026 | (最後を探す) |
4162 | 5027 | |
4163 | 5028 | =begin original |
4164 | 5029 | |
4165 | 5030 | The first pass is finding the end of the quoted construct, where |
4166 | 5031 | the information about the delimiters is used in parsing. |
4167 | 5032 | During this search, text between the starting and ending delimiters |
4168 | 5033 | is copied to a safe location. The text copied gets delimiter-independent. |
4169 | 5034 | |
4170 | 5035 | =end original |
4171 | 5036 | |
4172 | 5037 | 最初のパスは、クォート構造の末尾を探して、パース中に使うデリミタの情報が |
4173 | 5038 | どこかを探すものです。 |
4174 | 5039 | 検索中に、開始デリミタと終了デリミタの間のテキストは安全な場所に |
4175 | 5040 | コピーされます。 |
4176 | 5041 | コピーされたテキストはデリミタに依存しません。 |
4177 | 5042 | |
4178 | 5043 | =begin original |
4179 | 5044 | |
4180 | 5045 | If the construct is a here-doc, the ending delimiter is a line |
4181 | 5046 | that has a terminating string as the content. Therefore C<<<EOF> is |
4182 | 5047 | terminated by C<EOF> immediately followed by C<"\n"> and starting |
4183 | 5048 | from the first column of the terminating line. |
4184 | 5049 | When searching for the terminating line of a here-doc, nothing |
4185 | 5050 | is skipped. In other words, lines after the here-doc syntax |
4186 | 5051 | are compared with the terminating string line by line. |
4187 | 5052 | |
4188 | 5053 | =end original |
4189 | 5054 | |
4190 | 構造がヒ | |
5055 | 構造がヒアドキュメントの場合、終了デリミタは内容として終端文字列を持つ | |
4191 | 5056 | 行です。 |
4192 | 5057 | 従って、C<<<EOF> は、C<"\n"> の直後の、終端行の最初の列から始まる C<EOF> で |
4193 | 5058 | 終端します。 |
4194 | ヒ | |
5059 | ヒアドキュメントの終端行を探すとき、読み飛ばされるものはありません。 | |
4195 | 言い換えると、ヒ | |
5060 | 言い換えると、ヒアドキュメント文法以降の行は、1 行毎に終端文字列と | |
4196 | 5061 | 比較されます。 |
4197 | 5062 | |
4198 | 5063 | =begin original |
4199 | 5064 | |
4200 | 5065 | For the constructs except here-docs, single characters are used as starting |
4201 | 5066 | and ending delimiters. If the starting delimiter is an opening punctuation |
4202 | 5067 | (that is C<(>, C<[>, C<{>, or C<< < >>), the ending delimiter is the |
4203 | 5068 | corresponding closing punctuation (that is C<)>, C<]>, C<}>, or C<< > >>). |
4204 | 5069 | If the starting delimiter is an unpaired character like C</> or a closing |
4205 | 5070 | punctuation, the ending delimiter is same as the starting delimiter. |
4206 | 5071 | Therefore a C</> terminates a C<qq//> construct, while a C<]> terminates |
4207 | 5072 | C<qq[]> and C<qq]]> constructs. |
4208 | 5073 | |
4209 | 5074 | =end original |
4210 | 5075 | |
4211 | ヒ | |
5076 | ヒアドキュメント以外の構造では、1 文字が開始デリミタと終了デリミタとして | |
4212 | 5077 | 使われます。 |
4213 | 5078 | 開始デリミタが組の開き文字(C<(>, C<[>, C<{>, C<< < >> のいずれか)の場合、 |
4214 | 5079 | 終了デリミタは対応する組の閉じ文字(つまり C<)>, C<]>, C<}>, C<< > >>) です。 |
4215 | 5080 | 開始デリミタが C</> 組になる文字や、組の閉じ文字の場合、終了デリミタは |
4216 | 5081 | 開始デリミタと同じです。 |
4217 | 5082 | 従って C</> は C<qq//> 構造を終端し、一方 C<]> は C<qq[]> と C<qq]]> 構造を |
4218 | 5083 | 終端します。 |
4219 | 5084 | |
4220 | 5085 | =begin original |
4221 | 5086 | |
4222 | 5087 | When searching for single-character delimiters, escaped delimiters |
4223 | 5088 | and C<\\> are skipped. For example, while searching for terminating C</>, |
4224 | 5089 | combinations of C<\\> and C<\/> are skipped. If the delimiters are |
4225 | 5090 | bracketing, nested pairs are also skipped. For example, while searching |
4226 | 5091 | for closing C<]> paired with the opening C<[>, combinations of C<\\>, C<\]>, |
4227 | 5092 | and C<\[> are all skipped, and nested C<[> and C<]> are skipped as well. |
4228 | 5093 | However, when backslashes are used as the delimiters (like C<qq\\> and |
4229 | 5094 | C<tr\\\>), nothing is skipped. |
4230 | 5095 | During the search for the end, backslashes that escape delimiters |
4231 | 5096 | are removed (exactly speaking, they are not copied to the safe location). |
4232 | 5097 | |
4233 | 5098 | =end original |
4234 | 5099 | |
4235 | 5100 | 1 文字のデリミタを探す場合、エスケープされたデリミタと C<\\> は |
4236 | 5101 | 読み飛ばします。 |
4237 | 5102 | 例えば、C</> を探しているときには、C<\\> と C<\/> の組み合わせを |
4238 | 5103 | 読み飛ばします。 |
4239 | 5104 | デリミタがかっこでくくられている場合は、ネストした組も読み飛ばされます。 |
4240 | 5105 | 例えば、開きの C<[> と組になる閉じの C<]> を探しているときには、 |
4241 | 5106 | C<\\>, C<\]>, C<\[> の組み合わせをは全て読み飛ばし、 |
4242 | 5107 | さらにネストした C<[>, C<]> も読み飛ばします。 |
4243 | 5108 | しかし、(C<qq\\> や C<tr\\\> のように)バックスラッシュがデリミタとして |
4244 | 5109 | 使われた場合、何も読み飛ばしません。 |
4245 | 5110 | 末尾の検索中、デリミタをエスケープするバックスラッシュは除去されます |
4246 | 5111 | (述べたとおりに正確に、安全な場所にコピーはされません)。 |
4247 | 5112 | |
4248 | 5113 | =begin original |
4249 | 5114 | |
4250 | 5115 | For constructs with three-part delimiters (C<s///>, C<y///>, and |
4251 | 5116 | C<tr///>), the search is repeated once more. |
4252 | 5117 | If the first delimiter is not an opening punctuation, three delimiters must |
4253 | 5118 | be same such as C<s!!!> and C<tr)))>, in which case the second delimiter |
4254 | 5119 | terminates the left part and starts the right part at once. |
4255 | If the left part is delimited by bracketing punctuation | |
5120 | If the left part is delimited by bracketing punctuation (that is C<()>, | |
4256 | 5121 | C<[]>, C<{}>, or C<< <> >>), the right part needs another pair of |
4257 | delimiters such as C<s(){}> and C<tr[]//>. In these cases, whitespace | |
5122 | delimiters such as C<s(){}> and C<tr[]//>. In these cases, whitespace | |
4258 | 5123 | and comments are allowed between both parts, though the comment must follow |
4259 | at least one whitespace; otherwise a character expected as the | |
5124 | at least one whitespace character; otherwise a character expected as the | |
4260 | the comment may be regarded as the starting delimiter of the right part. | |
5125 | start of the comment may be regarded as the starting delimiter of the right part. | |
4261 | 5126 | |
4262 | 5127 | =end original |
4263 | 5128 | |
4264 | 5129 | 3 つのデリミタからなる構造 (C<s///>, C<y///>, C<tr///>) の場合、 |
4265 | 5130 | 検索はもう一度繰り返されます。 |
4266 | 5131 | 最初のデリミタが開きかっこでない場合、C<s!!!> and C<tr)))> のように |
4267 | 5132 | 3 つのデリミタは同じでなければなりません; |
4268 | 5133 | この場合、2 番目のデリミタが左側の終端と右側の開始を同時に行います。 |
4269 | 5134 | 左側のデリミタがかっこを構成するもの (これは C<()>, |
4270 | 5135 | C<[]>, C<{}>, C<< <> >> のいずれか) の場合、右側も |
4271 | 5136 | C<s(){}> や C<tr[]//> のようなデリミタである必要があります。 |
4272 | 5137 | これらの場合、右側と左側の間には空白やコメントを置けますが、 |
4273 | コメントは少なくとも一つの空白の後である必要があります; | |
5138 | コメントは少なくとも一つの空白文字の後である必要があります; | |
4274 | 5139 | さもなければコメントの開始文字が右側の開始デリミタとして扱われてしまいます。 |
4275 | 5140 | |
4276 | 5141 | =begin original |
4277 | 5142 | |
4278 | 5143 | During this search no attention is paid to the semantics of the construct. |
4279 | 5144 | Thus: |
4280 | 5145 | |
4281 | 5146 | =end original |
4282 | 5147 | |
4283 | 5148 | 検索する間、構造の文脈は考慮しません。 |
4284 | 5149 | 従って: |
4285 | 5150 | |
4286 | 5151 | "$hash{"$foo/$bar"}" |
4287 | 5152 | |
4288 | 5153 | =begin original |
4289 | 5154 | |
4290 | 5155 | or: |
4291 | 5156 | |
4292 | 5157 | =end original |
4293 | 5158 | |
4294 | 5159 | または: |
4295 | 5160 | |
4296 | 5161 | m/ |
4297 | 5162 | bar # NOT a comment, this slash / terminated m//! |
4298 | 5163 | /x |
4299 | 5164 | |
4300 | 5165 | =begin original |
4301 | 5166 | |
4302 | 5167 | do not form legal quoted expressions. The quoted part ends on the |
4303 | 5168 | first C<"> and C</>, and the rest happens to be a syntax error. |
4304 | 5169 | Because the slash that terminated C<m//> was followed by a C<SPACE>, |
4305 | 5170 | the example above is not C<m//x>, but rather C<m//> with no C</x> |
4306 | 5171 | modifier. So the embedded C<#> is interpreted as a literal C<#>. |
4307 | 5172 | |
4308 | 5173 | =end original |
4309 | 5174 | |
4310 | 5175 | は正しいクォート表現ではありません。 |
4311 | 5176 | クォートは最初の C<"> や C</> で終わりとなり、残りの部分は文法エラーと |
4312 | 5177 | なります。 |
4313 | 5178 | C<m//> を終わらせているスラッシュの次に来ているのが C<空白> なので、 |
4314 | 5179 | 上の例では C<m//x> ではなく、C</x> なしの C<m//> となります。 |
4315 | 5180 | 従って、中にある C<#> はリテラルな C<#> として扱われます。 |
4316 | 5181 | |
4317 | 5182 | =begin original |
4318 | 5183 | |
4319 | 5184 | Also no attention is paid to C<\c\> (multichar control char syntax) during |
4320 | 5185 | this search. Thus the second C<\> in C<qq/\c\/> is interpreted as a part |
4321 | 5186 | of C<\/>, and the following C</> is not recognized as a delimiter. |
4322 | 5187 | Instead, use C<\034> or C<\x1c> at the end of quoted constructs. |
4323 | 5188 | |
4324 | 5189 | =end original |
4325 | 5190 | |
4326 | 5191 | この検索の間、C<\c\> (マルチバイト文字制御文法)に注意は払われません。 |
4327 | 5192 | 従って、C<qq/\c\/> の 2 番目の C<\> は C<\/> の一部として扱われ、 |
4328 | 5193 | 引き続く C</> はデリミタとして認識されません。 |
4329 | 5194 | 代わりに、クォート構造の末尾に C<\034> か C<\x1c> を使ってください。 |
4330 | 5195 | |
4331 | 5196 | =item Interpolation |
4332 | 5197 | X<interpolation> |
4333 | 5198 | |
4334 | 5199 | (展開) |
4335 | 5200 | |
4336 | 5201 | =begin original |
4337 | 5202 | |
4338 | 5203 | The next step is interpolation in the text obtained, which is now |
4339 | 5204 | delimiter-independent. There are multiple cases. |
4340 | 5205 | |
4341 | 5206 | =end original |
4342 | 5207 | |
4343 | 5208 | 次のステップは、得られた(デリミタに依存しない)テキストに対する展開です。 |
4344 | 5209 | 複数のケースがあります。 |
4345 | 5210 | |
4346 | 5211 | =over 4 |
4347 | 5212 | |
4348 | 5213 | =item C<<<'EOF'> |
4349 | 5214 | |
4350 | 5215 | =begin original |
4351 | 5216 | |
4352 | 5217 | No interpolation is performed. |
4353 | 5218 | Note that the combination C<\\> is left intact, since escaped delimiters |
4354 | 5219 | are not available for here-docs. |
4355 | 5220 | |
4356 | 5221 | =end original |
4357 | 5222 | |
4358 | 5223 | 展開は行われません。 |
4359 | 5224 | C<\\> の組み合わせはそのままであることに注意してください; |
4360 | ヒ | |
5225 | ヒアドキュメントではデリミタのエスケープはできないからです。 | |
4361 | 5226 | |
4362 | 5227 | =item C<m''>, the pattern of C<s'''> |
4363 | 5228 | |
4364 | 5229 | =begin original |
4365 | 5230 | |
4366 | 5231 | No interpolation is performed at this stage. |
4367 | 5232 | Any backslashed sequences including C<\\> are treated at the stage |
4368 | 5233 | to L</"parsing regular expressions">. |
4369 | 5234 | |
4370 | 5235 | =end original |
4371 | 5236 | |
4372 | 5237 | このステージでは展開は行われません。 |
4373 | 5238 | C<\\> を含む、バックスラッシュ付きのシーケンスは |
4374 | 5239 | L</"parsing regular expressions"> で扱われます。 |
4375 | 5240 | |
4376 | 5241 | =item C<''>, C<q//>, C<tr'''>, C<y'''>, the replacement of C<s'''> |
4377 | 5242 | |
4378 | 5243 | =begin original |
4379 | 5244 | |
4380 | 5245 | The only interpolation is removal of C<\> from pairs of C<\\>. |
4381 | 5246 | Therefore C<-> in C<tr'''> and C<y'''> is treated literally |
4382 | 5247 | as a hyphen and no character range is available. |
4383 | 5248 | C<\1> in the replacement of C<s'''> does not work as C<$1>. |
4384 | 5249 | |
4385 | 5250 | =end original |
4386 | 5251 | |
4387 | 5252 | C<\\> の組における C<\> の削除のみが行われます。 |
4388 | 5253 | 従って、C<tr'''> や C<y'''> の中にある C<-> は文字通りハイフンとして扱われ、 |
4389 | 5254 | 文字範囲は使えません。 |
4390 | 5255 | C<s'''> の置換文字列での C<\1> は C<$1> としては動作しません。 |
4391 | 5256 | |
4392 | 5257 | =item C<tr///>, C<y///> |
4393 | 5258 | |
4394 | 5259 | =begin original |
4395 | 5260 | |
4396 | 5261 | No variable interpolation occurs. String modifying combinations for |
4397 | 5262 | case and quoting such as C<\Q>, C<\U>, and C<\E> are not recognized. |
4398 | 5263 | The other escape sequences such as C<\200> and C<\t> and backslashed |
4399 | 5264 | characters such as C<\\> and C<\-> are converted to appropriate literals. |
4400 | 5265 | The character C<-> is treated specially and therefore C<\-> is treated |
4401 | 5266 | as a literal C<->. |
4402 | 5267 | |
4403 | 5268 | =end original |
4404 | 5269 | |
4405 | 5270 | 変数展開は行われません。 |
4406 | 5271 | C<\Q>, C<\U>, C<\E> のような、大文字小文字やクォートに関する、文字列を |
4407 | 5272 | 変更するような組み合わせは認識されません。 |
4408 | 5273 | C<\200> や C<\t> のようなその他のエスケープシーケンスや、 |
4409 | 5274 | C<\\> や C<\-> のようなバックスラッシュ付きの文字は、適切なリテラルに |
4410 | 5275 | 変換されます。 |
4411 | 5276 | 文字 C<-> は特別扱いされるので、C<\-> はリテラルな C<-> として扱われます。 |
4412 | 5277 | |
4413 | 5278 | =item C<"">, C<``>, C<qq//>, C<qx//>, C<< <file*glob> >>, C<<<"EOF"> |
4414 | 5279 | |
4415 | 5280 | =begin original |
4416 | 5281 | |
4417 | 5282 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> (possibly paired with C<\E>) are |
4418 | 5283 | converted to corresponding Perl constructs. Thus, C<"$foo\Qbaz$bar"> |
4419 | 5284 | is converted to C<$foo . (quotemeta("baz" . $bar))> internally. |
4420 | 5285 | The other escape sequences such as C<\200> and C<\t> and backslashed |
4421 | 5286 | characters such as C<\\> and C<\-> are replaced with appropriate |
4422 | 5287 | expansions. |
4423 | 5288 | |
4424 | 5289 | =end original |
4425 | 5290 | |
4426 | 5291 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> (おそらくは C<\E> との組)は |
4427 | 5292 | 対応する Perl 構造に変換されます。 |
4428 | 5293 | 従って、C<"$foo\Qbaz$bar"> は内部的に |
4429 | 5294 | C<$foo . (quotemeta("baz" . $bar))> に変換されます。 |
4430 | 5295 | C<\200> や C<\t> のような、その他のエスケープシーケンスや、C<\\> や |
4431 | 5296 | C<\-> のような、バックスラッシュがつけられた文字は適切な拡張に |
4432 | 5297 | 置換されます。 |
4433 | 5298 | |
4434 | 5299 | =begin original |
4435 | 5300 | |
4436 | 5301 | Let it be stressed that I<whatever falls between C<\Q> and C<\E>> |
4437 | 5302 | is interpolated in the usual way. Something like C<"\Q\\E"> has |
4438 | 5303 | no C<\E> inside. instead, it has C<\Q>, C<\\>, and C<E>, so the |
4439 | 5304 | result is the same as for C<"\\\\E">. As a general rule, backslashes |
4440 | 5305 | between C<\Q> and C<\E> may lead to counterintuitive results. So, |
4441 | 5306 | C<"\Q\t\E"> is converted to C<quotemeta("\t")>, which is the same |
4442 | 5307 | as C<"\\\t"> (since TAB is not alphanumeric). Note also that: |
4443 | 5308 | |
4444 | 5309 | =end original |
4445 | 5310 | |
4446 | 5311 | I<C<\Q> と C<\E> の間にある全てのもの> が通常の方法で展開されます。 |
4447 | C<"\Q\\E"> のようなものは内部にあるのは C<\E> では | |
5312 | C<"\Q\\E"> のようなものは内部にあるのは C<\E> ではありません。 | |
4448 | 5313 | C<\Q>, C<\\>, C<E> であるので、結果は C<"\\\\E"> と同じになります。 |
4449 | 5314 | 一般的なルールとして、C<\Q> と C<\E> の間にあるバックスラッシュは |
4450 | 5315 | 直感に反した結果になります。 |
4451 | 5316 | それで、C<"\Q\t\E"> は C<quotemeta("\t")> に変換され、これは(TAB は |
4452 | 5317 | 英数字ではないので C<"\\\t"> と同じです。 |
4453 | 5318 | 以下のようなことにも注意してください: |
4454 | 5319 | |
4455 | 5320 | $str = '\t'; |
4456 | 5321 | return "\Q$str"; |
4457 | 5322 | |
4458 | 5323 | =begin original |
4459 | 5324 | |
4460 | 5325 | may be closer to the conjectural I<intention> of the writer of C<"\Q\t\E">. |
4461 | 5326 | |
4462 | 5327 | =end original |
4463 | 5328 | |
4464 | 5329 | これは C<"\Q\t\E"> を書いた人の憶測上の I<意図> により近いです。 |
4465 | 5330 | |
4466 | 5331 | =begin original |
4467 | 5332 | |
4468 | 5333 | Interpolated scalars and arrays are converted internally to the C<join> and |
4469 | 5334 | C<.> catenation operations. Thus, C<"$foo XXX '@arr'"> becomes: |
4470 | 5335 | |
4471 | 5336 | =end original |
4472 | 5337 | |
4473 | 5338 | 展開されたスカラと配列は内部で C<join> と C<.> の結合操作に変換されます。 |
4474 | 5339 | 従って、C<"$foo XXX '@arr'"> は以下のようになります: |
4475 | 5340 | |
4476 | 5341 | $foo . " XXX '" . (join $", @arr) . "'"; |
4477 | 5342 | |
4478 | 5343 | =begin original |
4479 | 5344 | |
4480 | 5345 | All operations above are performed simultaneously, left to right. |
4481 | 5346 | |
4482 | 5347 | =end original |
4483 | 5348 | |
4484 | 5349 | 上記の全ての操作は、左から右に同時に行われます。 |
4485 | 5350 | |
4486 | 5351 | =begin original |
4487 | 5352 | |
4488 | 5353 | Because the result of C<"\Q STRING \E"> has all metacharacters |
4489 | 5354 | quoted, there is no way to insert a literal C<$> or C<@> inside a |
4490 | 5355 | C<\Q\E> pair. If protected by C<\>, C<$> will be quoted to became |
4491 | 5356 | C<"\\\$">; if not, it is interpreted as the start of an interpolated |
4492 | 5357 | scalar. |
4493 | 5358 | |
4494 | 5359 | =end original |
4495 | 5360 | |
4496 | 5361 | C<"\Q STRING \E"> の結果は全てのメタ文字がクォートされているので、 |
4497 | 5362 | C<\Q\E> の組の内側にリテラルの C<$> や C<@> を挿入する方法はありません。 |
4498 | 5363 | C<\> によって守られている場合、C<$> はクォートされて C<"\\\$"> と |
4499 | なります | |
5364 | なります; そうでない場合、これは展開されるスカラ変数の開始として | |
4500 | ||
5365 | 解釈されます。 | |
4501 | 5366 | |
4502 | 5367 | =begin original |
4503 | 5368 | |
4504 | 5369 | Note also that the interpolation code needs to make a decision on |
4505 | 5370 | where the interpolated scalar ends. For instance, whether |
4506 | 5371 | C<< "a $b -> {c}" >> really means: |
4507 | 5372 | |
4508 | 5373 | =end original |
4509 | 5374 | |
4510 | 5375 | 展開コードは、展開するスカラ変数がどこで終わるかを決定する必要が |
4511 | 5376 | あることにも注意してください。 |
4512 | 5377 | 例えば、C<< "a $b -> {c}" >> が実際には以下のどちらかになります: |
4513 | 5378 | |
4514 | 5379 | "a " . $b . " -> {c}"; |
4515 | 5380 | |
4516 | 5381 | =begin original |
4517 | 5382 | |
4518 | 5383 | or: |
4519 | 5384 | |
4520 | 5385 | =end original |
4521 | 5386 | |
4522 | 5387 | または: |
4523 | 5388 | |
4524 | 5389 | "a " . $b -> {c}; |
4525 | 5390 | |
4526 | 5391 | =begin original |
4527 | 5392 | |
4528 | 5393 | Most of the time, the longest possible text that does not include |
4529 | 5394 | spaces between components and which contains matching braces or |
4530 | 5395 | brackets. because the outcome may be determined by voting based |
4531 | 5396 | on heuristic estimators, the result is not strictly predictable. |
4532 | 5397 | Fortunately, it's usually correct for ambiguous cases. |
4533 | 5398 | |
4534 | 5399 | =end original |
4535 | 5400 | |
4536 | 5401 | ほとんどの場合、要素と、マッチする中かっこや大かっこの間に空白を含まない、 |
4537 | 5402 | 最も長いテキストになります。 |
4538 | 5403 | 出力は発見的な推定器をよる投票によって決定されるので、結果は厳密には |
4539 | 5404 | 予測できません。 |
4540 | 5405 | 幸い、紛らわしい場合でも普通は正しいです。 |
4541 | 5406 | |
4542 | 5407 | =item the replacement of C<s///> |
4543 | 5408 | |
4544 | 5409 | =begin original |
4545 | 5410 | |
4546 | 5411 | Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, and interpolation |
4547 | 5412 | happens as with C<qq//> constructs. |
4548 | 5413 | |
4549 | 5414 | =end original |
4550 | 5415 | |
4551 | 5416 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> の処理と展開が C<qq//> 構造と |
4552 | 5417 | 同じように起こります。 |
4553 | 5418 | |
4554 | 5419 | =begin original |
4555 | 5420 | |
4556 | 5421 | It is at this step that C<\1> is begrudgingly converted to C<$1> in |
4557 | 5422 | the replacement text of C<s///>, in order to correct the incorrigible |
4558 | 5423 | I<sed> hackers who haven't picked up the saner idiom yet. A warning |
4559 | 5424 | is emitted if the C<use warnings> pragma or the B<-w> command-line flag |
4560 | 5425 | (that is, the C<$^W> variable) was set. |
4561 | 5426 | |
4562 | 5427 | =end original |
4563 | 5428 | |
4564 | 5429 | このステップでは、より健全な文法をまだ導入していない、手に負えない I<sed> |
4565 | 5430 | ハッカーのために、C<s///> の置換テキストの中にある C<\1> を、しぶしぶながら |
4566 | 5431 | C<$1> に変換します。 |
4567 | 5432 | C<use warnings> プラグマやコマンドラインオプション B<-w> (これは C<$^W> |
4568 | 5433 | 変数です) がセットされていると警告が生成されます。 |
4569 | 5434 | |
4570 | 5435 | =item C<RE> in C<?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>, |
4571 | 5436 | |
4572 | 5437 | =begin original |
4573 | 5438 | |
4574 | 5439 | Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\E>, |
4575 | 5440 | and interpolation happens (almost) as with C<qq//> constructs. |
4576 | 5441 | |
4577 | 5442 | =end original |
4578 | 5443 | |
4579 | 5444 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> C<\E> の処理と展開が C<qq//> 構造と |
4580 | 5445 | (ほとんど)同じように起こります。 |
4581 | 5446 | |
4582 | 5447 | =begin original |
4583 | 5448 | |
5449 | Processing of C<\N{...}> is also done here, and compiled into an intermediate | |
5450 | form for the regex compiler. (This is because, as mentioned below, the regex | |
5451 | compilation may be done at execution time, and C<\N{...}> is a compile-time | |
5452 | construct.) | |
5453 | ||
5454 | =end original | |
5455 | ||
5456 | C<\N{...}> の処理もここで行われ、正規表現コンパイラのための中間形式に | |
5457 | コンパイルされます。 | |
5458 | (これは、後述するように、正規表現のコンパイルは実行時に行われ、 | |
5459 | C<\N{...}> はコンパイル時の構文だからです。) | |
5460 | ||
5461 | =begin original | |
5462 | ||
4584 | 5463 | However any other combinations of C<\> followed by a character |
4585 | 5464 | are not substituted but only skipped, in order to parse them |
4586 | 5465 | as regular expressions at the following step. |
4587 | 5466 | As C<\c> is skipped at this step, C<@> of C<\c@> in RE is possibly |
4588 | 5467 | treated as an array symbol (for example C<@foo>), |
4589 | 5468 | even though the same text in C<qq//> gives interpolation of C<\c@>. |
4590 | 5469 | |
4591 | 5470 | =end original |
4592 | 5471 | |
4593 | 5472 | しかし、その他の、C<\> の後に文字が続く組み合わせは置換されず、単に |
4594 | 5473 | 読み飛ばされます; これは以下のステップで正規表現としてパースするためです。 |
4595 | 5474 | ここでは C<\c> は読み飛ばされるので、正規表現中の C<\c@> の C<@> は |
4596 | 5475 | 配列のシンボル(例えば C<@foo>) と扱われる可能性があります; |
4597 | 5476 | 一方 C<qq//> 内の同じテキストは C<\c@> と展開されます。 |
4598 | 5477 | |
4599 | 5478 | =begin original |
4600 | 5479 | |
4601 | 5480 | Moreover, inside C<(?{BLOCK})>, C<(?# comment )>, and |
4602 | 5481 | a C<#>-comment in a C<//x>-regular expression, no processing is |
4603 | 5482 | performed whatsoever. This is the first step at which the presence |
4604 | 5483 | of the C<//x> modifier is relevant. |
4605 | 5484 | |
4606 | 5485 | =end original |
4607 | 5486 | |
4608 | 5487 | さらに、C<(?{BLOCK})>, C<(?# comment )>, C<//x> 正規表現での C<#> の |
4609 | 5488 | コメントの中では、どのような処理も行われません。 |
4610 | 5489 | これは C<//x> 修飾子が影響を与える最初のステップです。 |
4611 | 5490 | |
4612 | 5491 | =begin original |
4613 | 5492 | |
4614 | 5493 | Interpolation in patterns has several quirks: C<$|>, C<$(>, C<$)>, C<@+> |
4615 | 5494 | and C<@-> are not interpolated, and constructs C<$var[SOMETHING]> are |
4616 | 5495 | voted (by several different estimators) to be either an array element |
4617 | 5496 | or C<$var> followed by an RE alternative. This is where the notation |
4618 | 5497 | C<${arr[$bar]}> comes handy: C</${arr[0-9]}/> is interpreted as |
4619 | 5498 | array element C<-9>, not as a regular expression from the variable |
4620 | 5499 | C<$arr> followed by a digit, which would be the interpretation of |
4621 | 5500 | C</$arr[0-9]/>. Since voting among different estimators may occur, |
4622 | 5501 | the result is not predictable. |
4623 | 5502 | |
4624 | 5503 | =end original |
4625 | 5504 | |
4626 | 5505 | パターン内の展開ではいくつか特殊な動作をします: |
4627 | 5506 | C<$|>, C<$(>, C<$)>, C<@+>, C<@-> は展開されず、 |
4628 | 5507 | C<$var[SOMETHING]> は(いくつかの異なる推定器によって)配列の要素か |
4629 | 5508 | C<$var> の後に正規表現が続いているのかが投票されます。 |
4630 | 5509 | これは C<${arr[$bar]}> が便利になるところです: C</${arr[0-9]}/> は |
4631 | 5510 | 配列要素 C<-9> として解釈され、C</$arr[0-9]/> の場合のように C<$arr> の後に |
4632 | 5511 | 数値が続いているような正規表現としては解釈されません。 |
4633 | 5512 | 異なった推定器によって投票されることがあるので、結果は予測できません。 |
4634 | 5513 | |
4635 | 5514 | =begin original |
4636 | 5515 | |
4637 | 5516 | The lack of processing of C<\\> creates specific restrictions on |
4638 | 5517 | the post-processed text. If the delimiter is C</>, one cannot get |
4639 | 5518 | the combination C<\/> into the result of this step. C</> will |
4640 | 5519 | finish the regular expression, C<\/> will be stripped to C</> on |
4641 | 5520 | the previous step, and C<\\/> will be left as is. Because C</> is |
4642 | 5521 | equivalent to C<\/> inside a regular expression, this does not |
4643 | 5522 | matter unless the delimiter happens to be character special to the |
4644 | 5523 | RE engine, such as in C<s*foo*bar*>, C<m[foo]>, or C<?foo?>; or an |
4645 | 5524 | alphanumeric char, as in: |
4646 | 5525 | |
4647 | 5526 | =end original |
4648 | 5527 | |
4649 | 5528 | C<\\> を処理しないことにより、後処理したテキストに特定の制限があります。 |
4650 | 5529 | デリミタが C</> の場合、このステップの結果として C<\/> を得ることは |
4651 | 5530 | できません。 |
4652 | 5531 | C</> は正規表現を終わらせ、C<\/> は前のステップで C</> に展開され、 |
4653 | 5532 | C<\\/> はそのまま残されます。 |
4654 | 5533 | C</> は正規表現の中では C<\/> と等価なので、これはたまたまデリミタが |
4655 | 5534 | 正規検索エンジンにとって特別な文字の場合、つまり C<s*foo*bar*>, |
4656 | 5535 | C<m[foo]>, C<?foo?> のような場合、あるいは以下のように英数字でなければ、 |
4657 | 5536 | 問題にはなりません: |
4658 | 5537 | |
4659 | 5538 | m m ^ a \s* b mmx; |
4660 | 5539 | |
4661 | 5540 | =begin original |
4662 | 5541 | |
4663 | 5542 | In the RE above, which is intentionally obfuscated for illustration, the |
4664 | 5543 | delimiter is C<m>, the modifier is C<mx>, and after delimiter-removal the |
4665 | 5544 | RE is the same as for C<m/ ^ a \s* b /mx>. There's more than one |
4666 | 5545 | reason you're encouraged to restrict your delimiters to non-alphanumeric, |
4667 | 5546 | non-whitespace choices. |
4668 | 5547 | |
4669 | 5548 | =end original |
4670 | 5549 | |
4671 | 5550 | 上記の正規表現では、説明のために意図的にわかりにくくしていますが、 |
4672 | 5551 | デリミタは C<m> で、修飾子は C<mx> で、デリミタを取り除いた後の |
4673 | 5552 | 正規表現は C<m/ ^ a \s* b /mx> と同じです。 |
4674 | 5553 | デリミタを英数字や空白でないものに制限するべきである理由は複数あります。 |
4675 | 5554 | |
4676 | 5555 | =back |
4677 | 5556 | |
4678 | 5557 | =begin original |
4679 | 5558 | |
4680 | 5559 | This step is the last one for all constructs except regular expressions, |
4681 | 5560 | which are processed further. |
4682 | 5561 | |
4683 | 5562 | =end original |
4684 | 5563 | |
4685 | これは正規表現以外の全ての構造にとって最後のステップです | |
5564 | これは正規表現以外の全ての構造にとって最後のステップです; 正規表現は | |
4686 | ||
5565 | さらに処理が続きます。 | |
4687 | 5566 | |
4688 | 5567 | =item parsing regular expressions |
4689 | 5568 | X<regexp, parse> |
4690 | 5569 | |
4691 | 5570 | (正規表現のパース) |
4692 | 5571 | |
4693 | 5572 | =begin original |
4694 | 5573 | |
4695 | 5574 | Previous steps were performed during the compilation of Perl code, |
4696 | but this one happens at run time | |
5575 | but this one happens at run time, although it may be optimized to | |
4697 | 5576 | be calculated at compile time if appropriate. After preprocessing |
4698 | 5577 | described above, and possibly after evaluation if concatenation, |
4699 | 5578 | joining, casing translation, or metaquoting are involved, the |
4700 | 5579 | resulting I<string> is passed to the RE engine for compilation. |
4701 | 5580 | |
4702 | 5581 | =end original |
4703 | 5582 | |
4704 | 5583 | 以前のステップは Perl コードのコンパイル中に実行されますが、 |
4705 | これは実行時に起こります | |
5584 | これは実行時に起こりますが、もし適切ならコンパイル時に | |
4706 | 5585 | 計算できるように最適化されることもあります。 |
4707 | 5586 | 上記の前処理の後、そして必要なら連結、結合、大文字小文字変換、 |
4708 | 5587 | メタクォート化が行われた後、結果の I<文字列> がコンパイルのために |
4709 | 5588 | 正規表現エンジンに渡されます。 |
4710 | 5589 | |
4711 | 5590 | =begin original |
4712 | 5591 | |
4713 | 5592 | Whatever happens in the RE engine might be better discussed in L<perlre>, |
4714 | 5593 | but for the sake of continuity, we shall do so here. |
4715 | 5594 | |
4716 | 5595 | =end original |
4717 | 5596 | |
4718 | 5597 | 正規表現エンジンで起こることについては L<perlre> で議論した方が |
4719 | 5598 | よいでしょうが、継続性のために、ここでそれを行います。 |
4720 | 5599 | |
4721 | 5600 | =begin original |
4722 | 5601 | |
4723 | 5602 | This is another step where the presence of the C<//x> modifier is |
4724 | 5603 | relevant. The RE engine scans the string from left to right and |
4725 | 5604 | converts it to a finite automaton. |
4726 | 5605 | |
4727 | 5606 | =end original |
4728 | 5607 | |
4729 | 5608 | これも C<//x> 修飾子の存在が関連するステップの一つです。 |
4730 | 5609 | 正規表現エンジンは文字列を左から右にスキャンして、有限状態オートマトンに |
4731 | 5610 | 変換します。 |
4732 | 5611 | |
4733 | 5612 | =begin original |
4734 | 5613 | |
4735 | 5614 | Backslashed characters are either replaced with corresponding |
4736 | 5615 | literal strings (as with C<\{>), or else they generate special nodes |
4737 | 5616 | in the finite automaton (as with C<\b>). Characters special to the |
4738 | 5617 | RE engine (such as C<|>) generate corresponding nodes or groups of |
4739 | 5618 | nodes. C<(?#...)> comments are ignored. All the rest is either |
4740 | 5619 | converted to literal strings to match, or else is ignored (as is |
4741 | 5620 | whitespace and C<#>-style comments if C<//x> is present). |
4742 | 5621 | |
4743 | 5622 | =end original |
4744 | 5623 | |
4745 | 5624 | バックスラッシュ付きの文字は(C<\{> のように)対応するリテラル文字列に |
4746 | 5625 | 置換されるか、あるいは(C<\b> のように)有限状態オートマトンの特別な |
4747 | 5626 | ノードを生成します。 |
4748 | 5627 | (C<|> のような)正規表現エンジンにとって特別な文字は対応するノードか |
4749 | 5628 | ノードのグループを生成します。 |
5629 | C<(?#...)> コメントは無視されます。 | |
4750 | 5630 | 残りの全てはマッチするリテラル文字列に変換されるか、そうでなければ |
4751 | 5631 | (C<//x> が指定された時の空白と C<#> スタイルのコメントと同様に) |
4752 | 5632 | 無視されます。 |
4753 | 5633 | |
4754 | 5634 | =begin original |
4755 | 5635 | |
4756 | 5636 | Parsing of the bracketed character class construct, C<[...]>, is |
4757 | 5637 | rather different than the rule used for the rest of the pattern. |
4758 | 5638 | The terminator of this construct is found using the same rules as |
4759 | 5639 | for finding the terminator of a C<{}>-delimited construct, the only |
4760 | 5640 | exception being that C<]> immediately following C<[> is treated as |
4761 | 5641 | though preceded by a backslash. Similarly, the terminator of |
4762 | 5642 | C<(?{...})> is found using the same rules as for finding the |
4763 | 5643 | terminator of a C<{}>-delimited construct. |
4764 | 5644 | |
4765 | 5645 | =end original |
4766 | 5646 | |
4767 | 5647 | 文字クラス構造 C<[...]> のパースは他のパターンとはルールが異なります。 |
4768 | 5648 | この構造の終端は C<{}> でデリミタされた構造の終端を検索するのと同じルールで |
4769 | 5649 | 検索されます; 唯一の例外は、C<[> の直後の C<]> はバックスラッシュが |
4770 | 5650 | 先行しているものとして扱われます。 |
4771 | 5651 | 同様に、C<(?{...})> の終端は C<{}> でデリミタされた構造の終端を |
4772 | 5652 | 検索されるのと同じルールで検索されます。 |
4773 | 5653 | |
4774 | 5654 | =begin original |
4775 | 5655 | |
4776 | 5656 | It is possible to inspect both the string given to RE engine and the |
4777 | 5657 | resulting finite automaton. See the arguments C<debug>/C<debugcolor> |
4778 | 5658 | in the C<use L<re>> pragma, as well as Perl's B<-Dr> command-line |
4779 | 5659 | switch documented in L<perlrun/"Command Switches">. |
4780 | 5660 | |
4781 | 5661 | =end original |
4782 | 5662 | |
4783 | 5663 | 正規表現に与えられる文字列と、結果としての有限状態オートマトンの両方を |
4784 | 5664 | 検査できます。 |
4785 | 5665 | C<use L<re>> プラグマの C<debug>/C<debugcolor> 引数と、 |
4786 | 5666 | L<perlrun/"Command Switches"> に記述されている B<-Dr> コマンドライン |
4787 | 5667 | オプションを参照してください。 |
4788 | 5668 | |
4789 | 5669 | =item Optimization of regular expressions |
4790 | 5670 | X<regexp, optimization> |
4791 | 5671 | |
5672 | (正規表現の最適化) | |
5673 | ||
4792 | 5674 | =begin original |
4793 | 5675 | |
4794 | 5676 | This step is listed for completeness only. Since it does not change |
4795 | 5677 | semantics, details of this step are not documented and are subject |
4796 | 5678 | to change without notice. This step is performed over the finite |
4797 | 5679 | automaton that was generated during the previous pass. |
4798 | 5680 | |
4799 | 5681 | =end original |
4800 | 5682 | |
4801 | 5683 | このステップは完全性のためだけにリストされています。 |
4802 | 5684 | これは意味論的には変化がないので、このステップの詳細は文書化されておらず、 |
4803 | 5685 | 将来予告なしに変更されることがあります。 |
4804 | 5686 | このステップはここまでの処理で生成された有限オートマトンに対して |
4805 | 5687 | 適用されます。 |
4806 | 5688 | |
4807 | 5689 | =begin original |
4808 | 5690 | |
4809 | 5691 | It is at this stage that C<split()> silently optimizes C</^/> to |
4810 | 5692 | mean C</^/m>. |
4811 | 5693 | |
4812 | 5694 | =end original |
4813 | 5695 | |
4814 | 5696 | C<split()> で C</^/> を暗黙に C</^/m> に最適化するのもこのステップです。 |
4815 | 5697 | |
4816 | 5698 | =back |
4817 | 5699 | |
4818 | 5700 | =head2 I/O Operators |
4819 | 5701 | X<operator, i/o> X<operator, io> X<io> X<while> X<filehandle> |
4820 | 5702 | X<< <> >> X<@ARGV> |
4821 | 5703 | |
4822 | 5704 | (I/O 演算子) |
4823 | 5705 | |
4824 | 5706 | =begin original |
4825 | 5707 | |
4826 | 5708 | There are several I/O operators you should know about. |
4827 | 5709 | |
4828 | 5710 | =end original |
4829 | 5711 | |
4830 | 5712 | 知っておいた方がよい I/O 演算子もいくつかあります。 |
4831 | 5713 | |
4832 | 5714 | =begin original |
4833 | 5715 | |
4834 | 5716 | A string enclosed by backticks (grave accents) first undergoes |
4835 | 5717 | double-quote interpolation. It is then interpreted as an external |
4836 | 5718 | command, and the output of that command is the value of the |
4837 | 5719 | backtick string, like in a shell. In scalar context, a single string |
4838 | 5720 | consisting of all output is returned. In list context, a list of |
4839 | 5721 | values is returned, one per line of output. (You can set C<$/> to use |
4840 | 5722 | a different line terminator.) The command is executed each time the |
4841 | 5723 | pseudo-literal is evaluated. The status value of the command is |
4842 | 5724 | returned in C<$?> (see L<perlvar> for the interpretation of C<$?>). |
4843 | 5725 | Unlike in B<csh>, no translation is done on the return data--newlines |
4844 | 5726 | remain newlines. Unlike in any of the shells, single quotes do not |
4845 | 5727 | hide variable names in the command from interpretation. To pass a |
4846 | 5728 | literal dollar-sign through to the shell you need to hide it with a |
4847 | 5729 | backslash. The generalized form of backticks is C<qx//>. (Because |
4848 | 5730 | backticks always undergo shell expansion as well, see L<perlsec> for |
4849 | 5731 | security concerns.) |
4850 | 5732 | X<qx> X<`> X<``> X<backtick> X<glob> |
4851 | 5733 | |
4852 | 5734 | =end original |
4853 | 5735 | |
4854 | 5736 | バッククォートで括られた文字列は、まず、ダブルクォート補完のように |
4855 | 5737 | 変数の展開が行なわれます。 |
4856 | 5738 | その後、シェルでの場合と同じように、外部コマンドとして解釈され、 |
4857 | 5739 | そのコマンドの出力がこのバッククォート文字列の値となります。 |
4858 | 5740 | スカラーコンテキストでは、出力すべてを含む一個の文字列が返されます。 |
4859 | 5741 | リストコンテキストでは、出力の 1 行 1 行が個々の要素となるリストが返されます。 |
4860 | 5742 | (C<$/> を設定すれば、行の終わりを示す文字を変えることができます。) |
4861 | 5743 | コマンドは、この擬似リテラルが評価されるごとに実行されます。 |
4862 | 5744 | コマンドのステータス値は C<$?> に返されます (C<$?> の解釈については、 |
4863 | 5745 | L<perlvar> を参照してください)。 |
4864 | 5746 | B<csh> での場合とは違って、結果のデータに対する変換は行なわれず、 |
4865 | 5747 | 改行は改行のままです。 |
4866 | 5748 | どのシェルとも違って、シングルクォートがコマンド中の変数名を |
4867 | 5749 | 解釈させないようにすることはありません。 |
4868 | 5750 | シェルにリテラルなドル記号を渡すには、バックスラッシュで |
4869 | 5751 | エスケープしなければなりません。 |
4870 | 5752 | バッククォートの一般形は、C<qx//> です。 |
4871 | 5753 | (バッククォートは常にシェル展開されます。 |
4872 | 5754 | セキュリティに関しては L<perlsec> を参照して下さい) |
4873 | 5755 | |
4874 | 5756 | =begin original |
4875 | 5757 | |
4876 | 5758 | In scalar context, evaluating a filehandle in angle brackets yields |
4877 | 5759 | the next line from that file (the newline, if any, included), or |
4878 | 5760 | C<undef> at end-of-file or on error. When C<$/> is set to C<undef> |
4879 | 5761 | (sometimes known as file-slurp mode) and the file is empty, it |
4880 | 5762 | returns C<''> the first time, followed by C<undef> subsequently. |
4881 | 5763 | |
4882 | 5764 | =end original |
4883 | 5765 | |
4884 | 5766 | スカラーコンテキストで山括弧の中のファイルハンドルを評価すると、 |
4885 | 5767 | そのファイルから、次の行を読み込むことになります |
4886 | (改行があればそれも含まれます) | |
5768 | (改行があればそれも含まれます); ファイルの最後またはエラーの場合は | |
4887 | ||
5769 | C<undef> を返します。 | |
4888 | 5770 | C<$/> が C<undef> に設定されている場合(ファイル吸い込みモードと呼ばれます) |
4889 | 5771 | でファイルが空の場合、 |
4890 | 5772 | 最初は C<''> を返し、次は C<undef> を返します。 |
4891 | 5773 | |
4892 | 5774 | =begin original |
4893 | 5775 | |
4894 | 5776 | Ordinarily you must assign the returned value to a variable, but |
4895 | 5777 | there is one situation where an automatic assignment happens. If |
4896 | 5778 | and only if the input symbol is the only thing inside the conditional |
4897 | 5779 | of a C<while> statement (even if disguised as a C<for(;;)> loop), |
4898 | 5780 | the value is automatically assigned to the global variable $_, |
4899 | 5781 | destroying whatever was there previously. (This may seem like an |
4900 | 5782 | odd thing to you, but you'll use the construct in almost every Perl |
4901 | 5783 | script you write.) The $_ variable is not implicitly localized. |
4902 | 5784 | You'll have to put a C<local $_;> before the loop if you want that |
4903 | 5785 | to happen. |
4904 | 5786 | |
4905 | 5787 | =end original |
4906 | 5788 | |
4907 | 5789 | 通常は、返された値を変数に代入しなければなりませんが、自動的に |
4908 | 5790 | 代入される場合が 1 つだけあります。 |
4909 | 5791 | この入力シンボルが、while 文(C<for(;;)> の形になっていたとしても)の条件式中に |
4910 | 単独で現れた場合だけは、その値が自動的にグローバル変数 $_ に代入されます | |
5792 | 単独で現れた場合だけは、その値が自動的にグローバル変数 $_ に代入されます; | |
4911 | 5793 | 以前の値は破壊されます。 |
4912 | 5794 | (これは、奇妙に思えるかもしれませんが、ほとんどすべての Perl スクリプトで |
4913 | 5795 | これが必要になることでしょう。) |
4914 | 5796 | $_ 変数は暗黙にはローカル化されません。 |
4915 | 5797 | そうしたい場合はループの前に C<local $_;> と書く必要があります。 |
4916 | 5798 | |
4917 | 5799 | =begin original |
4918 | 5800 | |
4919 | 5801 | The following lines are equivalent: |
4920 | 5802 | |
4921 | 5803 | =end original |
4922 | 5804 | |
4923 | 5805 | 以下のものは、お互いに同値なものです: |
4924 | 5806 | |
4925 | 5807 | while (defined($_ = <STDIN>)) { print; } |
4926 | 5808 | while ($_ = <STDIN>) { print; } |
4927 | 5809 | while (<STDIN>) { print; } |
4928 | 5810 | for (;<STDIN>;) { print; } |
4929 | 5811 | print while defined($_ = <STDIN>); |
4930 | 5812 | print while ($_ = <STDIN>); |
4931 | 5813 | print while <STDIN>; |
4932 | 5814 | |
4933 | 5815 | =begin original |
4934 | 5816 | |
4935 | 5817 | This also behaves similarly, but avoids $_ : |
4936 | 5818 | |
4937 | 5819 | =end original |
4938 | 5820 | |
4939 | 5821 | 以下は同様の振る舞いをしますが、$_ を使いません: |
4940 | 5822 | |
4941 | 5823 | while (my $line = <STDIN>) { print $line } |
4942 | 5824 | |
4943 | 5825 | =begin original |
4944 | 5826 | |
4945 | 5827 | In these loop constructs, the assigned value (whether assignment |
4946 | 5828 | is automatic or explicit) is then tested to see whether it is |
4947 | 5829 | defined. The defined test avoids problems where line has a string |
4948 | 5830 | value that would be treated as false by Perl, for example a "" or |
4949 | 5831 | a "0" with no trailing newline. If you really mean for such values |
4950 | 5832 | to terminate the loop, they should be tested for explicitly: |
4951 | 5833 | |
4952 | 5834 | =end original |
4953 | 5835 | |
4954 | 5836 | これらのループ構造の中で、代入された値は (代入が自動か明示的かに関わりなく) |
4955 | 5837 | 定義されているかどうかを見るためにテストされます。 |
4956 | 5838 | 定義テストは、行が Perl にとって偽となる文字列値を持っているかどうかの |
4957 | 問題を避けます | |
5839 | 問題を避けます; 例えば newline のついていない "" や "0" です。 | |
4958 | 5840 | もし本当にこのような値でループを終了させたいときは、 |
4959 | 5841 | 以下のように明示的にテストするべきです: |
4960 | 5842 | |
4961 | 5843 | while (($_ = <STDIN>) ne '0') { ... } |
4962 | 5844 | while (<STDIN>) { last unless $_; ... } |
4963 | 5845 | |
4964 | 5846 | =begin original |
4965 | 5847 | |
4966 | In other boolean contexts, C<< < | |
5848 | In other boolean contexts, C<< <filehandle> >> without an | |
4967 | explicit C<defined> test or comparison elicit a warning if the | |
5849 | explicit C<defined> test or comparison elicits a warning if the | |
4968 | 5850 | C<use warnings> pragma or the B<-w> |
4969 | 5851 | command-line switch (the C<$^W> variable) is in effect. |
4970 | 5852 | |
4971 | 5853 | =end original |
4972 | 5854 | |
4973 | 5855 | その他の真偽値コンテキストでは、明示的な C<defined> や比較なしに |
4974 | C<< < | |
5856 | C<< <filehandle> >> を使うと、C<use warnings> プラグマや | |
4975 | 5857 | B<-w> コマンドラインスイッチ (C<$^W> 変数) が有効なときには、 |
4976 | 5858 | 警告を発生させます。 |
4977 | 5859 | |
4978 | 5860 | =begin original |
4979 | 5861 | |
4980 | 5862 | The filehandles STDIN, STDOUT, and STDERR are predefined. (The |
4981 | 5863 | filehandles C<stdin>, C<stdout>, and C<stderr> will also work except |
4982 | 5864 | in packages, where they would be interpreted as local identifiers |
4983 | 5865 | rather than global.) Additional filehandles may be created with |
4984 | 5866 | the open() function, amongst others. See L<perlopentut> and |
4985 | 5867 | L<perlfunc/open> for details on this. |
4986 | 5868 | X<stdin> X<stdout> X<sterr> |
4987 | 5869 | |
4988 | 5870 | =end original |
4989 | 5871 | |
4990 | 5872 | STDIN、STDOUT、STDERR というファイルハンドルは、あらかじめ定義されています。 |
4991 | 5873 | (C<stdin>、C<stdout>、C<stderr> というファイルハンドルも、 |
4992 | 5874 | ローカルな名前でこれらのグローバルな名前が見えなくなっている |
4993 | 5875 | パッケージを除けば、使用することができます。) |
4994 | 5876 | その他のファイルハンドルは、open() 関数などで作ることができます。 |
4995 | 5877 | これに関する詳細については L<perlopentut> と L<perlfunc/open> を |
4996 | 5878 | 参照して下さい。 |
4997 | 5879 | X<stdin> X<stdout> X<sterr> |
4998 | 5880 | |
4999 | 5881 | =begin original |
5000 | 5882 | |
5001 | 5883 | If a <FILEHANDLE> is used in a context that is looking for |
5002 | 5884 | a list, a list comprising all input lines is returned, one line per |
5003 | 5885 | list element. It's easy to grow to a rather large data space this |
5004 | 5886 | way, so use with care. |
5005 | 5887 | |
5006 | 5888 | =end original |
5007 | 5889 | |
5008 | 5890 | <FILEHANDLE> がリストを必要とするコンテキストで用いられると、 |
5009 | 5891 | 1 要素に 1 行の入力行すべてからなるリストが返されます。 |
5010 | 5892 | これを使うと簡単にかなり大きなデータになってしまいますので、 |
5011 | 5893 | 注意を要します。 |
5012 | 5894 | |
5013 | 5895 | =begin original |
5014 | 5896 | |
5015 | 5897 | <FILEHANDLE> may also be spelled C<readline(*FILEHANDLE)>. |
5016 | 5898 | See L<perlfunc/readline>. |
5017 | 5899 | |
5018 | 5900 | =end original |
5019 | 5901 | |
5020 | 5902 | <FILEHANDLE> は C<readline(*FILEHANDLE)> とも書けます。 |
5021 | 5903 | L<perlfunc/readline> を参照して下さい。 |
5022 | 5904 | |
5023 | 5905 | =begin original |
5024 | 5906 | |
5025 | 5907 | The null filehandle <> is special: it can be used to emulate the |
5026 | 5908 | behavior of B<sed> and B<awk>. Input from <> comes either from |
5027 | 5909 | standard input, or from each file listed on the command line. Here's |
5028 | 5910 | how it works: the first time <> is evaluated, the @ARGV array is |
5029 | 5911 | checked, and if it is empty, C<$ARGV[0]> is set to "-", which when opened |
5030 | 5912 | gives you standard input. The @ARGV array is then processed as a list |
5031 | 5913 | of filenames. The loop |
5032 | 5914 | |
5033 | 5915 | =end original |
5034 | 5916 | |
5035 | 5917 | ヌルファイルハンドル <> は特別で、B<sed> や B<awk> の動作を |
5036 | 5918 | エミュレートするために使われます。 |
5037 | 5919 | <> からの入力は、標準入力からか、コマンドライン上に並べられた個々の |
5038 | 5920 | ファイルから行なわれます。 |
5039 | 動作の概要は、以下のようになります | |
5921 | 動作の概要は、以下のようになります: 最初に <> が評価されると、配列 | |
5040 | ||
5922 | @ARGV が調べられ、空であれば、C<$ARGV[0]> に "-"を設定します。 | |
5041 | C<$ARGV[0]> に "-"を設定します。 | |
5042 | 5923 | これは、open されるとき標準入力となります。 |
5043 | 5924 | その後、配列 @ARGV がファイル名のリストとして処理されます。 |
5044 | 5925 | |
5045 | 5926 | while (<>) { |
5046 | 5927 | ... # code for each line |
5047 | 5928 | } |
5048 | 5929 | |
5049 | 5930 | =begin original |
5050 | 5931 | |
5051 | 5932 | is equivalent to the following Perl-like pseudo code: |
5052 | 5933 | |
5053 | 5934 | =end original |
5054 | 5935 | |
5055 | 5936 | は以下ののような Perl の擬似コードと等価です: |
5056 | 5937 | |
5057 | 5938 | unshift(@ARGV, '-') unless @ARGV; |
5058 | 5939 | while ($ARGV = shift) { |
5059 | 5940 | open(ARGV, $ARGV); |
5060 | 5941 | while (<ARGV>) { |
5061 | 5942 | ... # code for each line |
5062 | 5943 | } |
5063 | 5944 | } |
5064 | 5945 | |
5065 | 5946 | =begin original |
5066 | 5947 | |
5067 | 5948 | except that it isn't so cumbersome to say, and will actually work. |
5068 | 5949 | It really does shift the @ARGV array and put the current filename |
5069 | 5950 | into the $ARGV variable. It also uses filehandle I<ARGV> |
5070 | internally | |
5951 | internally. <> is just a synonym for <ARGV>, which | |
5071 | 5952 | is magical. (The pseudo code above doesn't work because it treats |
5072 | 5953 | <ARGV> as non-magical.) |
5073 | 5954 | |
5074 | 5955 | =end original |
5075 | 5956 | |
5076 | 5957 | 但し、わずらわしく書かなくても、動作します。 |
5077 | 5958 | 実際に @ARGV を shift しますし、その時点のファイル名を変数 $ARGV に |
5078 | 5959 | 入れています。 |
5079 | また、内部的にファイルハンドル ARGV を使ってい | |
5960 | また、内部的にファイルハンドル ARGV を使っています。 | |
5080 | <ARGV> の同義語 | |
5961 | <> は <ARGV> の同義語で、マジカルです。 | |
5081 | 5962 | (上記の擬似コードは、<ARGV> を通常のものとして扱っているので、 |
5082 | 5963 | うまく動作しません。) |
5083 | 5964 | |
5084 | 5965 | =begin original |
5085 | 5966 | |
5086 | 5967 | Since the null filehandle uses the two argument form of L<perlfunc/open> |
5087 | 5968 | it interprets special characters, so if you have a script like this: |
5088 | 5969 | |
5089 | 5970 | =end original |
5090 | 5971 | |
5091 | 5972 | 空ファイルハンドルは 2 引数の L<perlfunc/open> を使った特別な文字列なので、 |
5092 | 5973 | もし以下のようなスクリプトを書いて: |
5093 | 5974 | |
5094 | 5975 | while (<>) { |
5095 | 5976 | print; |
5096 | 5977 | } |
5097 | 5978 | |
5098 | 5979 | =begin original |
5099 | 5980 | |
5100 | 5981 | and call it with C<perl dangerous.pl 'rm -rfv *|'>, it actually opens a |
5101 | 5982 | pipe, executes the C<rm> command and reads C<rm>'s output from that pipe. |
5102 | 5983 | If you want all items in C<@ARGV> to be interpreted as file names, you |
5103 | 5984 | can use the module C<ARGV::readonly> from CPAN. |
5104 | 5985 | |
5105 | 5986 | =end original |
5106 | 5987 | |
5107 | 5988 | これを C<perl dangerous.pl 'rm -rfv *|'> として呼び出すと、実際には |
5108 | 5989 | パイプを開き、C<rm> コマンドを実行して、 C<rm> の出力をパイプから読みます。 |
5109 | 5990 | もし C<@ARGV> の全ての要素をファイル名として解釈させたいなら、 |
5110 | 5991 | CPAN にある C<ARGV::readonly> モジュールが使えます。 |
5111 | 5992 | |
5112 | 5993 | =begin original |
5113 | 5994 | |
5114 | 5995 | You can modify @ARGV before the first <> as long as the array ends up |
5115 | 5996 | containing the list of filenames you really want. Line numbers (C<$.>) |
5116 | 5997 | continue as though the input were one big happy file. See the example |
5117 | 5998 | in L<perlfunc/eof> for how to reset line numbers on each file. |
5118 | 5999 | |
5119 | 6000 | =end original |
5120 | 6001 | |
5121 | 6002 | 最終的に、@ARGV に扱いたいと思っているファイル名が含まれるのであれば、 |
5122 | 6003 | 最初に <> を評価する前に @ARGV を変更することも可能です。 |
5123 | 6004 | 行番号 (C<$.>) は、入力ファイルがあたかも 1 つの大きなファイルで |
5124 | 6005 | あるかのように、続けてカウントされます。 |
5125 | 6006 | 個々のファイルごとにリセットする方法は、L<perlfunc/eof> の例を |
5126 | 6007 | 参照してください。 |
5127 | 6008 | |
5128 | 6009 | =begin original |
5129 | 6010 | |
5130 | 6011 | If you want to set @ARGV to your own list of files, go right ahead. |
5131 | 6012 | This sets @ARGV to all plain text files if no @ARGV was given: |
5132 | 6013 | |
5133 | 6014 | =end original |
5134 | 6015 | |
5135 | 6016 | 最初から @ARGV に自分でファイルのリストを設定してもかまいません。 |
5136 | 6017 | 以下は @ARGV が与えられなかったときに全てのテキストファイルを |
5137 | 6018 | @ARGV に設定します。 |
5138 | 6019 | |
5139 | 6020 | @ARGV = grep { -f && -T } glob('*') unless @ARGV; |
5140 | 6021 | |
5141 | 6022 | =begin original |
5142 | 6023 | |
5143 | 6024 | You can even set them to pipe commands. For example, this automatically |
5144 | 6025 | filters compressed arguments through B<gzip>: |
5145 | 6026 | |
5146 | 6027 | =end original |
5147 | 6028 | |
5148 | 6029 | ここにパイプコマンドを置くことも出来ます。 |
5149 | 6030 | 例えば、以下は圧縮された引数を自動的に B<gzip> のフィルタに通します: |
5150 | 6031 | |
5151 | 6032 | @ARGV = map { /\.(gz|Z)$/ ? "gzip -dc < $_ |" : $_ } @ARGV; |
5152 | 6033 | |
5153 | 6034 | =begin original |
5154 | 6035 | |
5155 | 6036 | If you want to pass switches into your script, you can use one of the |
5156 | 6037 | Getopts modules or put a loop on the front like this: |
5157 | 6038 | |
5158 | 6039 | =end original |
5159 | 6040 | |
5160 | 6041 | スクリプトにスイッチを渡したいのであれば、Getopts モジュールを |
5161 | 6042 | 使うこともできますし、実際の処理の前にのようなループを置くこともできます。 |
5162 | 6043 | |
5163 | 6044 | while ($_ = $ARGV[0], /^-/) { |
5164 | 6045 | shift; |
5165 | 6046 | last if /^--$/; |
5166 | 6047 | if (/^-D(.*)/) { $debug = $1 } |
5167 | 6048 | if (/^-v/) { $verbose++ } |
5168 | 6049 | # ... # other switches |
5169 | 6050 | } |
5170 | 6051 | |
5171 | 6052 | while (<>) { |
5172 | 6053 | # ... # code for each line |
5173 | 6054 | } |
5174 | 6055 | |
5175 | 6056 | =begin original |
5176 | 6057 | |
5177 | 6058 | The <> symbol will return C<undef> for end-of-file only once. |
5178 | 6059 | If you call it again after this, it will assume you are processing another |
5179 | 6060 | @ARGV list, and if you haven't set @ARGV, will read input from STDIN. |
5180 | 6061 | |
5181 | 6062 | =end original |
5182 | 6063 | |
5183 | 6064 | シンボル <> がファイルの最後で C<undef> を返すのは一度きりです。 |
5184 | 6065 | そのあとでもう一度呼び出すと、新たに別の @ARGV を処理するものとみなされ、 |
5185 | 6066 | その時に @ARGV を設定しなおしていないと、STDIN からの入力を |
5186 | 6067 | 読み込むことになります。 |
5187 | 6068 | |
5188 | 6069 | =begin original |
5189 | 6070 | |
5190 | 6071 | If what the angle brackets contain is a simple scalar variable (e.g., |
5191 | 6072 | <$foo>), then that variable contains the name of the |
5192 | 6073 | filehandle to input from, or its typeglob, or a reference to the |
5193 | 6074 | same. For example: |
5194 | 6075 | |
5195 | 6076 | =end original |
5196 | 6077 | |
5197 | 6078 | 山括弧の中の文字列が (<$foo> のような) 単純スカラ変数であれば、 |
5198 | 6079 | その変数が入力を行なうファイルハンドルの名前そのもの、名前への型グロブ、 |
5199 | 6080 | 名前へのリファレンスのいずれかを示しているとみなされます。 |
6081 | 例えば: | |
5200 | 6082 | |
5201 | 6083 | $fh = \*STDIN; |
5202 | 6084 | $line = <$fh>; |
5203 | 6085 | |
5204 | 6086 | =begin original |
5205 | 6087 | |
5206 | 6088 | If what's within the angle brackets is neither a filehandle nor a simple |
5207 | 6089 | scalar variable containing a filehandle name, typeglob, or typeglob |
5208 | 6090 | reference, it is interpreted as a filename pattern to be globbed, and |
5209 | 6091 | either a list of filenames or the next filename in the list is returned, |
5210 | 6092 | depending on context. This distinction is determined on syntactic |
5211 | 6093 | grounds alone. That means C<< <$x> >> is always a readline() from |
5212 | 6094 | an indirect handle, but C<< <$hash{key}> >> is always a glob(). |
5213 | 6095 | That's because $x is a simple scalar variable, but C<$hash{key}> is |
5214 | 6096 | not--it's a hash element. Even C<< <$x > >> (note the extra space) |
5215 | 6097 | is treated as C<glob("$x ")>, not C<readline($x)>. |
5216 | 6098 | |
5217 | 6099 | =end original |
5218 | 6100 | |
5219 | 6101 | 山括弧の中の文字列がファイルハンドルでもファイルハンドル名、型グロブ、 |
5220 | 6102 | 型グロブリファレンスのいずれかが入った単純スカラ変数でもなければ、 |
5221 | 6103 | グロブを行なうファイル名のパターンと解釈され、コンテキストによって |
5222 | 6104 | ファイル名のリストか、そのリストの次のファイル名が返されます。 |
5223 | 6105 | この区別は単に構文的に行われます。 |
5224 | 6106 | C<< <$x> >> は常に間接ハンドルから readline() しますが、 |
5225 | 6107 | C<< <$hash{key}> >> は常に glob() します。 |
5226 | 6108 | $x は単純スカラー変数ですが、C<$hash{key}> は違う(ハッシュ要素)からです。 |
5227 | 6109 | C<< <$x > >> (余分な空白に注意) ですら C<readline($x)> ではなく |
5228 | 6110 | C<glob("$x ")> として扱われます。 |
5229 | 6111 | |
5230 | 6112 | =begin original |
5231 | 6113 | |
5232 | 6114 | One level of double-quote interpretation is done first, but you can't |
5233 | 6115 | say C<< <$foo> >> because that's an indirect filehandle as explained |
5234 | 6116 | in the previous paragraph. (In older versions of Perl, programmers |
5235 | 6117 | would insert curly brackets to force interpretation as a filename glob: |
5236 | 6118 | C<< <${foo}> >>. These days, it's considered cleaner to call the |
5237 | 6119 | internal function directly as C<glob($foo)>, which is probably the right |
5238 | 6120 | way to have done it in the first place.) For example: |
5239 | 6121 | |
5240 | 6122 | =end original |
5241 | 6123 | |
5242 | 6124 | まず、1 段階だけダブルクォート展開が行なわれますが、前の段落に書いた |
5243 | 6125 | 間接ファイルハンドルと同じになる、C<< <$foo> >> のようには書けません。 |
5244 | 6126 | (Perl の古いバージョンでは、ファイル名グロブと解釈させるために |
5245 | 6127 | C<< <${foo}> >> のように中括弧を入れていました。 |
5246 | 最近ではより明確にするために、C<glob($foo)> と内部関数を | |
6128 | 最近ではより明確にするために、C<glob($foo)> と内部関数を呼ぶことも | |
5247 | ||
6129 | できます; おそらく、まず、こちらの方で試すのが正解でしょう。) | |
5248 | ||
6130 | 例えば: | |
5249 | 例: | |
5250 | 6131 | |
5251 | 6132 | while (<*.c>) { |
5252 | 6133 | chmod 0644, $_; |
5253 | 6134 | } |
5254 | 6135 | |
5255 | 6136 | =begin original |
5256 | 6137 | |
5257 | 6138 | is roughly equivalent to: |
5258 | 6139 | |
5259 | 6140 | =end original |
5260 | 6141 | |
5261 | 6142 | はだいたい以下と等価です: |
5262 | 6143 | |
5263 | 6144 | open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|"); |
5264 | 6145 | while (<FOO>) { |
5265 | 6146 | chomp; |
5266 | 6147 | chmod 0644, $_; |
5267 | 6148 | } |
5268 | 6149 | |
5269 | 6150 | =begin original |
5270 | 6151 | |
5271 | 6152 | except that the globbing is actually done internally using the standard |
5272 | 6153 | C<File::Glob> extension. Of course, the shortest way to do the above is: |
5273 | 6154 | |
5274 | 6155 | =end original |
5275 | 6156 | |
5276 | 6157 | 但し実際のグロブは内部的に標準の C<File::Glob> モジュールを使います。 |
5277 | 6158 | もちろん、もっと簡単に以下のように書けます: |
5278 | 6159 | |
5279 | 6160 | chmod 0644, <*.c>; |
5280 | 6161 | |
5281 | 6162 | =begin original |
5282 | 6163 | |
5283 | 6164 | A (file)glob evaluates its (embedded) argument only when it is |
5284 | 6165 | starting a new list. All values must be read before it will start |
5285 | 6166 | over. In list context, this isn't important because you automatically |
5286 | 6167 | get them all anyway. However, in scalar context the operator returns |
5287 | 6168 | the next value each time it's called, or C<undef> when the list has |
5288 | 6169 | run out. As with filehandle reads, an automatic C<defined> is |
5289 | 6170 | generated when the glob occurs in the test part of a C<while>, |
5290 | 6171 | because legal glob returns (e.g. a file called F<0>) would otherwise |
5291 | 6172 | terminate the loop. Again, C<undef> is returned only once. So if |
5292 | 6173 | you're expecting a single value from a glob, it is much better to |
5293 | 6174 | say |
5294 | 6175 | |
5295 | 6176 | =end original |
5296 | 6177 | |
5297 | 6178 | (ファイル)グロブは新しいリストを開始するときにだけ(組み込みの)引数を |
5298 | 6179 | 評価します。 |
5299 | 6180 | 全ての値は開始する前に読み込んでいなければなりません。 |
5300 | 6181 | これはリストコンテキストでは、とにかく自動的に全てを取り込むので |
5301 | 6182 | 重要ではありません。 |
5302 | しかし、スカラ | |
6183 | しかし、スカラコンテキストではこの演算子は呼び出された時の | |
5303 | 6184 | 次の値か、リストがなくなったときには C<undef> を返します。 |
5304 | 6185 | ファイルハンドルを読み込む場合は、グロブが C<while> の条件部にある場合は |
5305 | 自動的な C<defined> が生成されます | |
6186 | 自動的な C<defined> が生成されます; | |
5306 | 6187 | なぜならそうしないと、本来の glob の返り値 (F<0> というファイル) が |
5307 | 6188 | ループを終了させるからです。 |
5308 | 6189 | ここでも、C<undef> は一度だけ返されます。 |
5309 | 6190 | 従って、もしグロブから一つの値だけを想定している場合、 |
5310 | 6191 | 以下のように書くことが: |
5311 | 6192 | |
5312 | 6193 | ($file) = <blurch*>; |
5313 | 6194 | |
5314 | 6195 | =begin original |
5315 | 6196 | |
5316 | 6197 | than |
5317 | 6198 | |
5318 | 6199 | =end original |
5319 | 6200 | |
5320 | 6201 | 以下のように書くよりはるかに良いです: |
5321 | 6202 | |
5322 | 6203 | $file = <blurch*>; |
5323 | 6204 | |
5324 | 6205 | =begin original |
5325 | 6206 | |
5326 | 6207 | because the latter will alternate between returning a filename and |
5327 | 6208 | returning false. |
5328 | 6209 | |
5329 | 6210 | =end original |
5330 | 6211 | |
5331 | 6212 | なぜなら後者はファイル名を返す場合と偽を返す場合があるからです。 |
5332 | 6213 | |
5333 | 6214 | =begin original |
5334 | 6215 | |
5335 | 6216 | If you're trying to do variable interpolation, it's definitely better |
5336 | 6217 | to use the glob() function, because the older notation can cause people |
5337 | 6218 | to become confused with the indirect filehandle notation. |
5338 | 6219 | |
5339 | 6220 | =end original |
5340 | 6221 | |
5341 | 変数変換に挑戦する場合、明らかに glob() 関数を使う方が良いです | |
6222 | 変数変換に挑戦する場合、明らかに glob() 関数を使う方が良いです; | |
5342 | 6223 | なぜなら古い表記は間接ファイルハンドル表記と混乱するかも知れないからです。 |
5343 | 6224 | |
5344 | 6225 | @files = glob("$dir/*.[ch]"); |
5345 | 6226 | @files = glob($files[$i]); |
5346 | 6227 | |
5347 | 6228 | =head2 Constant Folding |
5348 | 6229 | X<constant folding> X<folding> |
5349 | 6230 | |
5350 | 6231 | (定数の畳み込み) |
5351 | 6232 | |
5352 | 6233 | =begin original |
5353 | 6234 | |
5354 | 6235 | Like C, Perl does a certain amount of expression evaluation at |
5355 | 6236 | compile time whenever it determines that all arguments to an |
5356 | 6237 | operator are static and have no side effects. In particular, string |
5357 | 6238 | concatenation happens at compile time between literals that don't do |
5358 | 6239 | variable substitution. Backslash interpolation also happens at |
5359 | 6240 | compile time. You can say |
5360 | 6241 | |
5361 | 6242 | =end original |
5362 | 6243 | |
5363 | 6244 | C と同じように Perl でも、演算子に対するすべての引数がスタティックで、 |
5364 | 6245 | 副作用がないと判断できれば、コンパイル時に式の評価を行なってしまいます。 |
5365 | 6246 | 特に、変数置換の無いリテラルどうしの文字列連結はコンパイル時に行なわれます。 |
5366 | 6247 | バックスラッシュの解釈もコンパイル時に行なわれます。 |
6248 | 以下のように書けて、 | |
5367 | 6249 | |
5368 | 6250 | 'Now is the time for all' . "\n" . |
5369 | 6251 | 'good men to come to.' |
5370 | 6252 | |
5371 | 6253 | =begin original |
5372 | 6254 | |
5373 | 6255 | and this all reduces to one string internally. Likewise, if |
5374 | 6256 | you say |
5375 | 6257 | |
5376 | 6258 | =end original |
5377 | 6259 | |
5378 | ||
6260 | 内部的に 1 つの文字列になります。同様に | |
5379 | 6261 | |
5380 | 6262 | foreach $file (@filenames) { |
5381 | 6263 | if (-s $file > 5 + 100 * 2**16) { } |
5382 | 6264 | } |
5383 | 6265 | |
5384 | 6266 | =begin original |
5385 | 6267 | |
5386 | 6268 | the compiler will precompute the number which that expression |
5387 | 6269 | represents so that the interpreter won't have to. |
5388 | 6270 | |
5389 | 6271 | =end original |
5390 | 6272 | |
5391 | 6273 | と書くとコンパイラは、式が表わす数値をあらかじめ計算しますので、 |
5392 | 6274 | インタプリタで計算する必要がなくなっています。 |
5393 | 6275 | |
5394 | 6276 | =head2 No-ops |
5395 | 6277 | X<no-op> X<nop> |
5396 | 6278 | |
5397 | 6279 | (無実行) |
5398 | 6280 | |
5399 | 6281 | =begin original |
5400 | 6282 | |
5401 | 6283 | Perl doesn't officially have a no-op operator, but the bare constants |
5402 | 6284 | C<0> and C<1> are special-cased to not produce a warning in a void |
5403 | 6285 | context, so you can for example safely do |
5404 | 6286 | |
5405 | 6287 | =end original |
5406 | 6288 | |
5407 | 6289 | Perl は公式には無実行演算子はありませんが、裸の定数 C<0> と C<1> は |
5408 | 6290 | 特別に無効コンテキストでも警告を出さないことになっているので、 |
5409 | 6291 | 例えば安全に以下のように書けます: |
5410 | 6292 | |
5411 | 6293 | 1 while foo(); |
5412 | 6294 | |
5413 | 6295 | =head2 Bitwise String Operators |
5414 | 6296 | X<operator, bitwise, string> |
5415 | 6297 | |
5416 | 6298 | (ビット列演算子) |
5417 | 6299 | |
5418 | 6300 | =begin original |
5419 | 6301 | |
5420 | 6302 | Bitstrings of any size may be manipulated by the bitwise operators |
5421 | 6303 | (C<~ | & ^>). |
5422 | 6304 | |
5423 | 6305 | =end original |
5424 | 6306 | |
5425 | 6307 | 任意のサイズのビット列はビット単位演算子(C<~ | & ^>)で操作できます。 |
5426 | 6308 | |
5427 | 6309 | =begin original |
5428 | 6310 | |
5429 | 6311 | If the operands to a binary bitwise op are strings of different |
5430 | 6312 | sizes, B<|> and B<^> ops act as though the shorter operand had |
5431 | 6313 | additional zero bits on the right, while the B<&> op acts as though |
5432 | 6314 | the longer operand were truncated to the length of the shorter. |
5433 | 6315 | The granularity for such extension or truncation is one or more |
5434 | 6316 | bytes. |
5435 | 6317 | |
5436 | 6318 | =end original |
5437 | 6319 | |
5438 | 6320 | 二項ビット単位演算子のオペランドが異なった長さの文字列だった場合、 |
5439 | 6321 | B<|> と B<^> の演算子は短い側のオペランドの右側に追加のゼロが |
5440 | ついているとみなします | |
6322 | ついているとみなします; 一方 B<&> 演算子は長い方のオペランドが短い方に | |
5441 | ||
6323 | 切り詰められます。 | |
5442 | 6324 | この拡張や短縮の粒度はバイト単位です。 |
5443 | 6325 | |
5444 | 6326 | # ASCII-based examples |
5445 | 6327 | print "j p \n" ^ " a h"; # prints "JAPH\n" |
5446 | 6328 | print "JA" | " ph\n"; # prints "japh\n" |
5447 | 6329 | print "japh\nJunk" & '_____'; # prints "JAPH\n"; |
5448 | 6330 | print 'p N$' ^ " E<H\n"; # prints "Perl\n"; |
5449 | 6331 | |
5450 | 6332 | =begin original |
5451 | 6333 | |
5452 | 6334 | If you are intending to manipulate bitstrings, be certain that |
5453 | 6335 | you're supplying bitstrings: If an operand is a number, that will imply |
5454 | 6336 | a B<numeric> bitwise operation. You may explicitly show which type of |
5455 | 6337 | operation you intend by using C<""> or C<0+>, as in the examples below. |
5456 | 6338 | |
5457 | 6339 | =end original |
5458 | 6340 | |
5459 | 6341 | ビット列を操作したい場合は、確実にビット列が渡されるようにしてください: |
5460 | 6342 | オペランドが数字の場合、B<数値> ビット単位演算を仮定します。 |
5461 | 6343 | 明示的に演算の型を指定するときには、以下の例のように |
5462 | 6344 | C<""> か C<0+> を使ってください。 |
5463 | 6345 | |
6346 | =begin original | |
6347 | ||
5464 | 6348 | $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF) |
5465 | 6349 | $foo = '150' | 105; # yields 255 |
5466 | 6350 | $foo = 150 | '105'; # yields 255 |
5467 | 6351 | $foo = '150' | '105'; # yields string '155' (under ASCII) |
5468 | 6352 | |
6353 | =end original | |
6354 | ||
6355 | $foo = 150 | 105; # 255 になる (0x96 | 0x69 は 0xFF) | |
6356 | $foo = '150' | 105; # 255 になる | |
6357 | $foo = 150 | '105'; # 255 になる | |
6358 | $foo = '150' | '105'; # (ASCII では) 文字列 '155' になる | |
6359 | ||
6360 | =begin original | |
6361 | ||
5469 | 6362 | $baz = 0+$foo & 0+$bar; # both ops explicitly numeric |
5470 | 6363 | $biz = "$foo" ^ "$bar"; # both ops explicitly stringy |
5471 | 6364 | |
6365 | =end original | |
6366 | ||
6367 | $baz = 0+$foo & 0+$bar; # どちらのオペランドも明示的に数値 | |
6368 | $biz = "$foo" ^ "$bar"; # どちらのオペランドも明示的に文字列 | |
6369 | ||
5472 | 6370 | =begin original |
5473 | 6371 | |
5474 | 6372 | See L<perlfunc/vec> for information on how to manipulate individual bits |
5475 | 6373 | in a bit vector. |
5476 | 6374 | |
5477 | 6375 | =end original |
5478 | 6376 | |
5479 | 6377 | ビットベクタの個々のビットをどのように操作するかの情報については |
5480 | 6378 | L<perlfunc/vec> を参照して下さい。 |
5481 | 6379 | |
5482 | 6380 | =head2 Integer Arithmetic |
5483 | 6381 | X<integer> |
5484 | 6382 | |
5485 | 6383 | (整数演算) |
5486 | 6384 | |
5487 | 6385 | =begin original |
5488 | 6386 | |
5489 | 6387 | By default, Perl assumes that it must do most of its arithmetic in |
5490 | 6388 | floating point. But by saying |
5491 | 6389 | |
5492 | 6390 | =end original |
5493 | 6391 | |
5494 | 6392 | デフォルトでは、Perl は演算を浮動小数で行なわなければならないものと |
5495 | 6393 | しています。 |
5496 | 6394 | しかし、(もしそうしたいなら) |
5497 | 6395 | |
5498 | 6396 | use integer; |
5499 | 6397 | |
5500 | 6398 | =begin original |
5501 | 6399 | |
5502 | you may tell the compiler t | |
6400 | you may tell the compiler to use integer operations | |
5503 | ( | |
6401 | (see L<integer> for a detailed explanation) from here to the end of | |
5504 | An inner BLOCK may countermand this by saying | |
6402 | the enclosing BLOCK. An inner BLOCK may countermand this by saying | |
5505 | 6403 | |
5506 | 6404 | =end original |
5507 | 6405 | |
5508 | と書けば、その場所から現在の BLOCK の終わりまでは、整数演算 | |
6406 | と書けば、その場所から現在の BLOCK の終わりまでは、整数演算 | |
6407 | (詳しい説明は L<integer> を参照してください) を | |
5509 | 6408 | 行なってよいと、コンパイラに指示することができます。 |
5510 | 6409 | 内部の BLOCK で、 |
5511 | 6410 | |
5512 | 6411 | no integer; |
5513 | 6412 | |
5514 | 6413 | =begin original |
5515 | 6414 | |
5516 | 6415 | which lasts until the end of that BLOCK. Note that this doesn't |
5517 | mean everything is | |
6416 | mean everything is an integer, merely that Perl will use integer | |
5518 | operations | |
6417 | operations for arithmetic, comparison, and bitwise operators. For | |
5519 | integer>, if you take the C<sqrt(2)>, you'll | |
6418 | example, even under C<use integer>, if you take the C<sqrt(2)>, you'll | |
5520 | or so. | |
6419 | still get C<1.4142135623731> or so. | |
5521 | 6420 | |
5522 | 6421 | =end original |
5523 | 6422 | |
5524 | 6423 | と書けば、その BLOCK の終わりまでは、指示を取り消すことになります。 |
5525 | これは全てを整数だけを使って処理することを意味するわけではな | |
6424 | これは全てを整数だけを使って処理することを意味するわけではなく、 | |
5526 | ||
6425 | 単に Perl が算術、比較、ビット単位演算子で整数演算を | |
5527 | ||
6426 | するというだけであることに注意してください。 | |
5528 | というだけです。 | |
5529 | 6427 | 例えば、C<use integer> の指定があっても、C<sqrt(2)> とすると、 |
5530 | 6428 | C<1.4142135623731> といった結果が返ってきます。 |
5531 | 6429 | |
5532 | 6430 | =begin original |
5533 | 6431 | |
5534 | 6432 | Used on numbers, the bitwise operators ("&", "|", "^", "~", "<<", |
5535 | 6433 | and ">>") always produce integral results. (But see also |
5536 | 6434 | L<Bitwise String Operators>.) However, C<use integer> still has meaning for |
5537 | 6435 | them. By default, their results are interpreted as unsigned integers, but |
5538 | 6436 | if C<use integer> is in effect, their results are interpreted |
5539 | 6437 | as signed integers. For example, C<~0> usually evaluates to a large |
5540 | 6438 | integral value. However, C<use integer; ~0> is C<-1> on two's-complement |
5541 | 6439 | machines. |
5542 | 6440 | |
5543 | 6441 | =end original |
5544 | 6442 | |
5545 | 6443 | 数値を使う場合、ビット単位演算子 ("&", "|", "^", "~", "<<", ">>") は |
5546 | 常に整数の結果を生成します | |
6444 | 常に整数の結果を生成します。 | |
5547 | 参照して下さい | |
6445 | (但し L<Bitwise String Operators> も参照して下さい。) | |
5548 | 6446 | しかし、それでも C<use integer> は意味があります。 |
5549 | 6447 | デフォルトでは、これらの結果は符号なし整数として解釈されますが、 |
5550 | 6448 | C<use integer> が有効の場合は、符号付き整数として解釈されます。 |
5551 | 6449 | 例えば、C<~0> は通常大きな整数の値として評価されます。 |
5552 | 6450 | しかし、C<use integer; ~0> は 2 の補数のマシンでは C<-1> になります。 |
5553 | 6451 | |
5554 | 6452 | =head2 Floating-point Arithmetic |
5555 | 6453 | X<floating-point> X<floating point> X<float> X<real> |
5556 | 6454 | |
5557 | 6455 | (浮動小数点演算) |
5558 | 6456 | |
5559 | 6457 | =begin original |
5560 | 6458 | |
5561 | 6459 | While C<use integer> provides integer-only arithmetic, there is no |
5562 | 6460 | analogous mechanism to provide automatic rounding or truncation to a |
5563 | 6461 | certain number of decimal places. For rounding to a certain number |
5564 | 6462 | of digits, sprintf() or printf() is usually the easiest route. |
5565 | 6463 | See L<perlfaq4>. |
5566 | 6464 | |
5567 | 6465 | =end original |
5568 | 6466 | |
5569 | 6467 | C<use integer> が整数演算を提供する一方、数を特定の桁で自動的に丸めたり |
5570 | 6468 | 切り捨てたりする機構はありません。 |
5571 | 6469 | 数を丸めるには、sprintf() や printf() を使うのが一番簡単な方法です。 |
5572 | 6470 | L<perlfaq4> を参照して下さい。 |
5573 | 6471 | |
5574 | 6472 | =begin original |
5575 | 6473 | |
5576 | 6474 | Floating-point numbers are only approximations to what a mathematician |
5577 | 6475 | would call real numbers. There are infinitely more reals than floats, |
5578 | 6476 | so some corners must be cut. For example: |
5579 | 6477 | |
5580 | 6478 | =end original |
5581 | 6479 | |
5582 | 6480 | 浮動小数点数は数学者が実数と呼ぶものの近似でしかありません。 |
5583 | 6481 | 実数は浮動小数点より無限に続くので、多少角が丸められます。 |
5584 | 例: | |
6482 | 例えば: | |
5585 | 6483 | |
5586 | 6484 | printf "%.20g\n", 123456789123456789; |
5587 | 6485 | # produces 123456789123456784 |
5588 | 6486 | |
5589 | 6487 | =begin original |
5590 | 6488 | |
5591 | Testing for exact | |
6489 | Testing for exact floating-point equality or inequality is not a | |
5592 | ||
6490 | good idea. Here's a (relatively expensive) work-around to compare | |
5593 | 6491 | whether two floating-point numbers are equal to a particular number of |
5594 | 6492 | decimal places. See Knuth, volume II, for a more robust treatment of |
5595 | 6493 | this topic. |
5596 | 6494 | |
5597 | 6495 | =end original |
5598 | 6496 | |
5599 | 6497 | 浮動小数点数が等しいかどうかをちょうど同じかどうかで比較するのは |
5600 | 6498 | いいアイデアではありません。 |
5601 | 6499 | 以下に、二つの浮動小数点数が指定された桁まで等しいかどうかを |
5602 | 6500 | 比較する(比較的重い)次善の策を示します。 |
5603 | 6501 | この問題に関するより厳密な扱いについては Knuth, volume II を参照して下さい。 |
5604 | 6502 | |
5605 | 6503 | sub fp_equal { |
5606 | 6504 | my ($X, $Y, $POINTS) = @_; |
5607 | 6505 | my ($tX, $tY); |
5608 | 6506 | $tX = sprintf("%.${POINTS}g", $X); |
5609 | 6507 | $tY = sprintf("%.${POINTS}g", $Y); |
5610 | 6508 | return $tX eq $tY; |
5611 | 6509 | } |
5612 | 6510 | |
5613 | 6511 | =begin original |
5614 | 6512 | |
5615 | 6513 | The POSIX module (part of the standard perl distribution) implements |
5616 | 6514 | ceil(), floor(), and other mathematical and trigonometric functions. |
5617 | 6515 | The Math::Complex module (part of the standard perl distribution) |
5618 | 6516 | defines mathematical functions that work on both the reals and the |
5619 | 6517 | imaginary numbers. Math::Complex not as efficient as POSIX, but |
5620 | 6518 | POSIX can't work with complex numbers. |
5621 | 6519 | |
5622 | 6520 | =end original |
5623 | 6521 | |
5624 | 6522 | POSIX モジュール(Perl 標準配布パッケージの一部) は ceil(), floor() 及び |
5625 | 6523 | その他の数学関数や三角関数を実装しています。 |
5626 | 6524 | Math::Complex モジュール(Perl 標準配布パッケージの一部)は |
5627 | 6525 | 実数と虚数の両方で動作する数学関数を定義しています。 |
5628 | 6526 | Math::Complex は POSIX ほど効率的ではありませんが、 |
5629 | 6527 | POSIX は複素数は扱えません。 |
5630 | 6528 | |
5631 | 6529 | =begin original |
5632 | 6530 | |
5633 | 6531 | Rounding in financial applications can have serious implications, and |
5634 | 6532 | the rounding method used should be specified precisely. In these |
5635 | 6533 | cases, it probably pays not to trust whichever system rounding is |
5636 | 6534 | being used by Perl, but to instead implement the rounding function you |
5637 | 6535 | need yourself. |
5638 | 6536 | |
5639 | 6537 | =end original |
5640 | 6538 | |
5641 | 6539 | 金融アプリケーションにおける丸めは深刻な影響を与える可能性があり、 |
5642 | 6540 | 使用する丸めメソッドは指定された精度で行われるべきです。 |
5643 | 6541 | このような場合、Perl が使用するシステム丸めを信用せず、 |
5644 | 6542 | 代わりに自分自身で丸め関数を実装するべきです。 |
5645 | 6543 | |
5646 | 6544 | =head2 Bigger Numbers |
5647 | 6545 | X<number, arbitrary precision> |
5648 | 6546 | |
5649 | 6547 | (より大きな数) |
5650 | 6548 | |
5651 | 6549 | =begin original |
5652 | 6550 | |
5653 | The standard Math::BigInt and Math::BigFloat modules | |
6551 | The standard C<Math::BigInt>, C<Math::BigRat>, and C<Math::BigFloat> modules, | |
6552 | along with the C<bigint>, C<bigrat>, and C<bitfloat> pragmas, provide | |
5654 | 6553 | variable-precision arithmetic and overloaded operators, although |
5655 | 6554 | they're currently pretty slow. At the cost of some space and |
5656 | 6555 | considerable speed, they avoid the normal pitfalls associated with |
5657 | 6556 | limited-precision representations. |
5658 | 6557 | |
5659 | 6558 | =end original |
5660 | 6559 | |
5661 | 標準の Math::BigInt | |
6560 | 標準の C<Math::BigInt>, C<Math::BigRat>, C<Math::BigFloat> モジュールと | |
6561 | C<bigint>, C<bigrat>, C<bitfloat> プラグマは多倍長演算を提供し、 | |
5662 | 6562 | 演算子をオーバーロードしますが、これらは現在のところかなり遅いです。 |
5663 | 6563 | 多少の領域とかなりの速度を犠牲にして、桁数が制限されていることによる |
5664 | 6564 | ありがちな落とし穴を避けることができます。 |
5665 | 6565 | |
5666 | ||
6566 | use 5.010; | |
5667 | | |
6567 | use bigint; # easy interface to Math::BigInt | |
5668 | ||
6568 | $x = 123456789123456789; | |
6569 | say $x * $x; | |
6570 | +15241578780673678515622620750190521 | |
5669 | 6571 | |
5670 | | |
6572 | =begin original | |
5671 | 6573 | |
6574 | Or with rationals: | |
6575 | ||
6576 | =end original | |
6577 | ||
6578 | あるいは分数を使って: | |
6579 | ||
6580 | use 5.010; | |
6581 | use bigrat; | |
6582 | $a = 3/22; | |
6583 | $b = 4/6; | |
6584 | say "a/b is ", $a/$b; | |
6585 | say "a*b is ", $a*$b; | |
6586 | a/b is 9/44 | |
6587 | a*b is 1/11 | |
6588 | ||
5672 | 6589 | =begin original |
5673 | 6590 | |
5674 | ||
6591 | Several modules let you calculate with (bound only by memory and CPU time) | |
5675 | ||
6592 | unlimited or fixed precision. There are also some non-standard modules that | |
5676 | ||
6593 | provide faster implementations via external C libraries. | |
5677 | external C libraries. | |
5678 | 6594 | |
5679 | 6595 | =end original |
5680 | 6596 | |
5681 | 6597 | (メモリと CPU 時間のみに依存する)無制限か固定の精度での計算ができる |
5682 | 6598 | モジュールがいくつかあります。 |
5683 | 6599 | さらに外部 C ライブラリを使ってより速い実装を提供する |
5684 | 6600 | 非標準のモジュールもあります。 |
5685 | 6601 | |
5686 | 6602 | =begin original |
5687 | 6603 | |
5688 | 6604 | Here is a short, but incomplete summary: |
5689 | 6605 | |
5690 | 6606 | =end original |
5691 | 6607 | |
5692 | 6608 | 以下は短いですが不完全なリストです。 |
5693 | 6609 | |
5694 | 6610 | =begin original |
5695 | 6611 | |
5696 | ||
6612 | Math::Fraction big, unlimited fractions like 9973 / 12967 | |
5697 | ||
6613 | Math::String treat string sequences like numbers | |
5698 | ||
6614 | Math::FixedPrecision calculate with a fixed precision | |
5699 | ||
6615 | Math::Currency for currency calculations | |
5700 | ||
6616 | Bit::Vector manipulate bit vectors fast (uses C) | |
5701 | ||
6617 | Math::BigIntFast Bit::Vector wrapper for big numbers | |
5702 | ||
6618 | Math::Pari provides access to the Pari C library | |
5703 | ||
6619 | Math::BigInteger uses an external C library | |
5704 | ||
6620 | Math::Cephes uses external Cephes C library (no big numbers) | |
5705 | ||
6621 | Math::Cephes::Fraction fractions via the Cephes library | |
5706 | ||
6622 | Math::GMP another one using an external C library | |
5707 | 6623 | |
5708 | 6624 | =end original |
5709 | 6625 | |
5710 | 6626 | Math::Fraction 9973 / 12967 のような、大きくて無制限の分数 |
5711 | 6627 | Math::String 文字列を数値のように扱う |
5712 | 6628 | Math::FixedPrecision 固定精度で計算する |
5713 | 6629 | Math::Currency 通貨の計算用 |
5714 | 6630 | Bit::Vector (C を使って)ビットベクタを速く操作する |
5715 | 6631 | Math::BigIntFast 大きな数のための Bit::Vector のラッパー |
5716 | 6632 | Math::Pari Pari C ライブラリへのアクセスを提供する |
5717 | 6633 | Math::BigInteger 外部 C ライブラリを使う |
5718 | 6634 | Math::Cephes 外部の Cephes C を使う(大きな数はなし) |
5719 | 6635 | Math::Cephes::Fraction Cephes ライブラリを使った分数 |
5720 | 6636 | Math::GMP これも外部 C ライブラリを使う |
5721 | 6637 | |
5722 | 6638 | =begin original |
5723 | 6639 | |
5724 | 6640 | Choose wisely. |
5725 | 6641 | |
5726 | 6642 | =end original |
5727 | 6643 | |
5728 | 6644 | うまく選んでください。 |
5729 | 6645 | |
5730 | 6646 | =cut |
5731 | 6647 | |
5732 | 6648 | =begin meta |
5733 | 6649 | |
5734 | 6650 | Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp> (5.000) |
5735 | 6651 | Update: Kentaro Shirakata <argrath@ub32.org> (5.6.1-) |
5736 | 6652 | Status: completed |
5737 | 6653 | |
5738 | 6654 | =end meta |