オープンソース・ソフトウェアの開発とダウンロード

CVS リポジトリの参照

Contents of /perldocjp/docs/perl/5.34.0/perlop.pod

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Sat Jul 10 06:55:43 2021 UTC (2 years, 10 months ago) by argrath
Branch: MAIN
CVS Tags: HEAD
5.34.0/*

1
2 =encoding utf8
3
4 =head1 NAME
5 X<operator>
6
7 =begin original
8
9 perlop - Perl operators and precedence
10
11 =end original
12
13 perlop - Perl の演算子と優先順位
14
15 =head1 DESCRIPTION
16
17 =begin original
18
19 In Perl, the operator determines what operation is performed,
20 independent of the type of the operands. For example S<C<$x + $y>>
21 is always a numeric addition, and if C<$x> or C<$y> do not contain
22 numbers, an attempt is made to convert them to numbers first.
23
24 =end original
25
26 Perl では、演算子はどんな演算を行うかをオペランドの型と独立して決定します。
27 例えば S<C<$x + $y>> は常に数値加算で、C<$x> や C<$y> に数値でないものが
28 含まれている場合、まずそれらを数値に変換しようとします。
29
30 =begin original
31
32 This is in contrast to many other dynamic languages, where the
33 operation is determined by the type of the first argument. It also
34 means that Perl has two versions of some operators, one for numeric
35 and one for string comparison. For example S<C<$x == $y>> compares
36 two numbers for equality, and S<C<$x eq $y>> compares two strings.
37
38 =end original
39
40 これは、最初の引数の型によって演算が決定されるその他の多くの動的言語と
41 対照的です。
42 これはまた、数値比較用と文字列比較用の 2 種類の演算子があるということです。
43 例えば S<C<$x == $y>> は二つの数値の等価性を比較し、S<C<$x eq $y>> は二つの
44 文字列を比較します。
45
46 =begin original
47
48 There are a few exceptions though: C<x> can be either string
49 repetition or list repetition, depending on the type of the left
50 operand, and C<&>, C<|>, C<^> and C<~> can be either string or numeric bit
51 operations.
52
53 =end original
54
55 しかし、いくつかの例外があります: C<x> は左オペランドの型によって、
56 文字列の繰り返しの場合とリストの繰り返しの場合があります; また
57 C<&>, C<|>, C<^>, C<~> は文字列としてのビット演算と数値としてのビット演算の
58 場合があります。
59
60 =head2 Operator Precedence and Associativity
61 X<operator, precedence> X<precedence> X<associativity>
62
63 (演算子の優先順位と結合性)
64
65 =begin original
66
67 Operator precedence and associativity work in Perl more or less like
68 they do in mathematics.
69
70 =end original
71
72 Perl での演算子の優先順位と結合性は多かれ少なかれ数学のものと似ています。
73
74 =begin original
75
76 I<Operator precedence> means some operators group more tightly than others.
77 For example, in C<2 + 4 * 5>, the multiplication has higher precedence, so C<4
78 * 5> is grouped together as the right-hand operand of the addition, rather
79 than C<2 + 4> being grouped together as the left-hand operand of the
80 multiplication. It is as if the expression were written C<2 + (4 * 5)>, not
81 C<(2 + 4) * 5>. So the expression yields C<2 + 20 == 22>, rather than
82 C<6 * 5 == 30>.
83
84 =end original
85
86 I<演算子の優先順位> とは、他の演算子グループよりもしっかりと
87 結びついている演算子グループがあるということです。
88 例えば、C<2 + 4 * 5> の場合、乗算が高い優先順位を持っているので、
89 C<2 + 4> が乗算の左オペランドとしてまとめられるのではなく、
90 C<4 * 5> が加法の右オペランドとしてまとめられます。
91 これは、C<(2 + 4) * 5> ではなく、C<2 + (4 * 5)> と
92 書かれたかのようなものです。
93 従って、この式は C<6 * 5 == 30> ではなく C<2 + 20 == 22> になります。
94
95 =begin original
96
97 I<Operator associativity> defines what happens if a sequence of the same
98 operators is used one after another:
99 usually that they will be grouped at the left
100 or the right. For example, in C<9 - 3 - 2>, subtraction is left associative,
101 so C<9 - 3> is grouped together as the left-hand operand of the second
102 subtraction, rather than C<3 - 2> being grouped together as the right-hand
103 operand of the first subtraction. It is as if the expression were written
104 C<(9 - 3) - 2>, not C<9 - (3 - 2)>. So the expression yields C<6 - 2 == 4>,
105 rather than C<9 - 1 == 8>.
106
107 =end original
108
109 I<演算子の結合性> は、同じ演算子が連続して現れた場合に何が起こるかを
110 定義します: 通常はそれらが左側と結びつくか右側と結びつくかです。
111 例えば C<9 - 3 - 2>で、減法は左結合なので、
112 C<3 - 2> が最初の減法の右オペランドとしてまとめられるのではなく、
113 C<9 - 3> が 2 番目の減法の左オペランドとしてまとめられます。
114 これは、C<9 - (3 - 2)> ではなく C<(9 - 3) - 2> と書かれたかのようなものです。
115 従って、この式は C<9 - 1 == 8> ではなく C<6 - 2 == 4> になります。
116
117 =begin original
118
119 For simple operators that evaluate all their operands and then combine the
120 values in some way, precedence and associativity (and parentheses) imply some
121 ordering requirements on those combining operations. For example, in C<2 + 4 *
122 5>, the grouping implied by precedence means that the multiplication of 4 and
123 5 must be performed before the addition of 2 and 20, simply because the result
124 of that multiplication is required as one of the operands of the addition. But
125 the order of operations is not fully determined by this: in C<2 * 2 + 4 * 5>
126 both multiplications must be performed before the addition, but the grouping
127 does not say anything about the order in which the two multiplications are
128 performed. In fact Perl has a general rule that the operands of an operator
129 are evaluated in left-to-right order. A few operators such as C<&&=> have
130 special evaluation rules that can result in an operand not being evaluated at
131 all; in general, the top-level operator in an expression has control of
132 operand evaluation.
133
134 =end original
135
136 全てのオペランドを評価してから何らかの形で値を結合する
137 単純な演算子の場合、優先順位と結合性(とかっこ)はそれらの結合操作で
138 ある種の順序要求を暗示します。
139 例えば C<2 + 4 * 5> では、
140 優先順位が暗示するグループ化によって、
141 4 と 5 の乗算は
142 2 と 20 の加算の前に行われる必要があります。
143 単にこの乗法の結果が加法のオペランドの一つとして必要だからです。
144 しかし、演算の順序はこれによって完全に決定されるわけではありません:
145 C<2 * 2 + 4 * 5> では、両方の乗算は加算の前に行われなければなりませんが、
146 グループ化は二つの乗算が行われる順序については何も言っていません。
147 実際の所、Perl には、演算子のオペランドは左から右の順で
148 評価されるという一般的な規則があります。
149 C<&&=> のようないくつかの演算子は、
150 全く評価されないオペランドとなる特別な評価規則を持ちます;
151 一般的に、式の中の最上位の演算子がオペランド評価を制御します。
152
153 =begin original
154
155 Some comparison operators, as their associativity, I<chain> with some
156 operators of the same precedence (but never with operators of different
157 precedence). This chaining means that each comparison is performed
158 on the two arguments surrounding it, with each interior argument taking
159 part in two comparisons, and the comparison results are implicitly ANDed.
160 Thus S<C<"$x E<lt> $y E<lt>= $z">> behaves exactly like S<C<"$x E<lt>
161 $y && $y E<lt>= $z">>, assuming that C<"$y"> is as simple a scalar as
162 it looks. The ANDing short-circuits just like C<"&&"> does, stopping
163 the sequence of comparisons as soon as one yields false.
164
165 =end original
166
167 一部の比較演算子は、それらの優先順位と、
168 同じ優先順位を持つ一部の演算子と I<連鎖> させることができます
169 (異なる優先順位を持つ演算子とはできません)。
170 連鎖というのは、それぞれの比較はそれらの周りの二つの引数に対して
171 行われ、それぞれの内部引数は二つの比較の一部となり、比較結果は
172 暗黙に AND されるということです。
173 従って S<C<"$x E<lt> $y E<lt>= $z">> は、C<"$y"> が見た目単純な
174 スカラだと仮定すると、
175 S<C<"$x E<lt> $y && $y E<lt>= $z">> と正確に同じように振る舞います。
176 AND の短絡は C<"&&"> と同様に行われ、一つが偽となった時点で一連の連鎖は
177 停止します。
178
179 =begin original
180
181 In a chained comparison, each argument expression is evaluated at most
182 once, even if it takes part in two comparisons, but the result of the
183 evaluation is fetched for each comparison. (It is not evaluated
184 at all if the short-circuiting means that it's not required for any
185 comparisons.) This matters if the computation of an interior argument
186 is expensive or non-deterministic. For example,
187
188 =end original
189
190 連鎖比較において、それぞれの引数式は、例え二つの比較の一部となったとしても、
191 評価されるのは最大 1 回ですが、評価の結果はそれぞれの比較毎に取得されます。
192 (短絡によって比較が必要でない場合は、まったく評価されません。)
193 これは、内側の引数の計算が高価だったり非決定的だったりする場合に
194 問題になります。
195 例えば:
196
197 if($x < expensive_sub() <= $z) { ...
198
199 =begin original
200
201 is not entirely like
202
203 =end original
204
205 これは全体的に次のようなものではなく:
206
207 if($x < expensive_sub() && expensive_sub() <= $z) { ...
208
209 =begin original
210
211 but instead closer to
212
213 =end original
214
215 しかし次のものに近いです:
216
217 my $tmp = expensive_sub();
218 if($x < $tmp && $tmp <= $z) { ...
219
220 =begin original
221
222 in that the subroutine is only called once. However, it's not exactly
223 like this latter code either, because the chained comparison doesn't
224 actually involve any temporary variable (named or otherwise): there is
225 no assignment. This doesn't make much difference where the expression
226 is a call to an ordinary subroutine, but matters more with an lvalue
227 subroutine, or if the argument expression yields some unusual kind of
228 scalar by other means. For example, if the argument expression yields
229 a tied scalar, then the expression is evaluated to produce that scalar
230 at most once, but the value of that scalar may be fetched up to twice,
231 once for each comparison in which it is actually used.
232
233 =end original
234
235 ここでサブルーチンは 1 回だけ呼び出されます。
236 しかし、正確に後者のコードのようなものでもありません; なぜなら
237 連鎖比較は実際には(名前付きかどうかにかかわらず)一時変数を使わないからです;
238 代入はありません。
239 これは、式が通常のサブルーチンとして呼び出されるときにはあまり違いは
240 ありませんが、左辺値サブルーチンの呼び出しの場合や、引数式が
241 他の手段によって何らかの普通でない種類のスカラになった場合には
242 より問題になります。
243 例えば、引数式が tie されたスカラになった場合、式はスカラを作るために
244 最大 1 回評価されますが、スカラの値は、実際に使われる比較毎に 1 回、
245 合計 2 回読み込まれます。
246
247 =begin original
248
249 In this example, the expression is evaluated only once, and the tied
250 scalar (the result of the expression) is fetched for each comparison that
251 uses it.
252
253 =end original
254
255 この例で、式は 1 回だけ評価され、tie されたスカラ (式の結果) は、
256 これら使われる比較毎に取得されます。
257
258 if ($x < $tied_scalar < $z) { ...
259
260 =begin original
261
262 In the next example, the expression is evaluated only once, and the tied
263 scalar is fetched once as part of the operation within the expression.
264 The result of that operation is fetched for each comparison, which
265 normally doesn't matter unless that expression result is also magical due
266 to operator overloading.
267
268 =end original
269
270 次の例では、式は 1 回だけ評価され、 tie されたスカラは式の中の演算の
271 一部として 1 回だけ取得されます。
272 この演算の結果は比較毎に取得されます;
273 通常これは、式の結果も演算子オーバーロードによってマジカルでない限り、
274 関係ありません。
275
276 if ($x < $tied_scalar + 42 < $z) { ...
277
278 =begin original
279
280 Some operators are instead non-associative, meaning that it is a syntax
281 error to use a sequence of those operators of the same precedence.
282 For example, S<C<"$x .. $y .. $z">> is an error.
283
284 =end original
285
286 一部の演算子は非結合です; 同じ優先順位の演算子の並びを使うと
287 文法エラーになります。
288 例えば、S<C<"$x .. $y .. $z">> はエラーです。
289
290 =begin original
291
292 Perl operators have the following associativity and precedence,
293 listed from highest precedence to lowest. Operators borrowed from
294 C keep the same precedence relationship with each other, even where
295 C's precedence is slightly screwy. (This makes learning Perl easier
296 for C folks.) With very few exceptions, these all operate on scalar
297 values only, not array values.
298
299 =end original
300
301 Perl の演算子には、以下のような結合性と優先順位 (高い優先順位から
302 低いものへ並べている) があります。
303 C から持ってきた演算子の優先順位は、C での優先順位が多少おかしくても、
304 そのままにしてあります。
305 (これによって、C を使っている方が Perl に移りやすくなっています。)
306 ごく僅かな例外を別として、全ての演算子はスカラ値のみを持ち、
307 配列値を持ちません。
308
309 =begin original
310
311 left terms and list operators (leftward)
312 left ->
313 nonassoc ++ --
314 right **
315 right ! ~ ~. \ and unary + and -
316 left =~ !~
317 left * / % x
318 left + - .
319 left << >>
320 nonassoc named unary operators
321 nonassoc isa
322 chained < > <= >= lt gt le ge
323 chain/na == != eq ne <=> cmp ~~
324 left & &.
325 left | |. ^ ^.
326 left &&
327 left || //
328 nonassoc .. ...
329 right ?:
330 right = += -= *= etc. goto last next redo dump
331 left , =>
332 nonassoc list operators (rightward)
333 right not
334 left and
335 left or xor
336
337 =end original
338
339 左結合 項 リスト演算子 (左方向に対して)
340 左結合 ->
341 非結合 ++ --
342 右結合 **
343 右結合 ! ~ ~. \ 単項の+ 単項の-
344 左結合 =~ !~
345 左結合 * / % x
346 左結合 + - .
347 左結合 << >>
348 非結合 名前付き単項演算子
349 連鎖 < > <= >= lt gt le ge
350 連鎖/なし == != eq ne <=> cmp ~~
351 非結合 isa
352 左結合 & &.
353 左結合 | |. ^ ^.
354 左結合 &&
355 左結合 || //
356 非結合 .. ...
357 右結合 ?:
358 右結合 = += -= *= など; goto last next redo dump
359 左結合 , =>
360 非結合 リスト演算子 (右方向に対して)
361 右結合 not
362 左結合 and
363 左結合 or xor
364
365 =begin original
366
367 In the following sections, these operators are covered in detail, in the
368 same order in which they appear in the table above.
369
370 =end original
371
372 以下の章では、前述の表の順に、これらの演算子に関して詳述します。
373
374 =begin original
375
376 Many operators can be overloaded for objects. See L<overload>.
377
378 =end original
379
380 多くの演算子はオブジェクトでオーバーロードできます。
381 L<overload> を参照して下さい。
382
383 =head2 Terms and List Operators (Leftward)
384 X<list operator> X<operator, list> X<term>
385
386 (項とリスト演算子 (左方向))
387
388 =begin original
389
390 A TERM has the highest precedence in Perl. They include variables,
391 quote and quote-like operators, any expression in parentheses,
392 and any function whose arguments are parenthesized. Actually, there
393 aren't really functions in this sense, just list operators and unary
394 operators behaving as functions because you put parentheses around
395 the arguments. These are all documented in L<perlfunc>.
396
397 =end original
398
399 「項」は Perl でもっとも優先順位が高いものです。
400 これには、変数、クォートとクォート的な演算子、括弧で括った任意の式、
401 引数を括弧で括った任意の関数が含まれます。
402 実際には、この意味では本当の関数はなく、リスト演算子と関数のように働く
403 単項演算子が、引数を括弧で括るためそのように見えます。
404 これらはすべて L<perlfunc> に記述しています。
405
406 =begin original
407
408 If any list operator (C<print()>, etc.) or any unary operator (C<chdir()>, etc.)
409 is followed by a left parenthesis as the next token, the operator and
410 arguments within parentheses are taken to be of highest precedence,
411 just like a normal function call.
412
413 =end original
414
415 リスト演算子 (C<print()> など) や単項演算子 (C<chdir()> など) は、
416 すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、
417 通常の関数呼び出しのようにもっとも高い優先順位として扱われます。
418
419 =begin original
420
421 In the absence of parentheses, the precedence of list operators such as
422 C<print>, C<sort>, or C<chmod> is either very high or very low depending on
423 whether you are looking at the left side or the right side of the operator.
424 For example, in
425
426 =end original
427
428 括弧が無い場合には、C<print>、C<sort>、C<chmod> のようなリスト演算子の
429 優先順位は、演算子の左側をからすると非常に高く、右側からすると
430 非常に低く見えます。たとえば、
431
432 @ary = (1, 3, sort 4, 2);
433 print @ary; # prints 1324
434
435 =begin original
436
437 the commas on the right of the C<sort> are evaluated before the C<sort>,
438 but the commas on the left are evaluated after. In other words,
439 list operators tend to gobble up all arguments that follow, and
440 then act like a simple TERM with regard to the preceding expression.
441 Be careful with parentheses:
442
443 =end original
444
445 では、C<sort> の右のコンマは C<sort> よりも前に評価されますが、左側のコンマは
446 後から評価されます。
447 言い方を変えると、リスト演算子は自分の後にある引数をすべて使って処理を行ない、
448 その結果を自分の前の式に対する「項」であるかのように見せるということです。
449 ただし、括弧には気を付けないといけません:
450
451 =begin original
452
453 # These evaluate exit before doing the print:
454 print($foo, exit); # Obviously not what you want.
455 print $foo, exit; # Nor is this.
456
457 =end original
458
459 # 以下は print を行なう前に exit を評価します:
460 print($foo, exit); # 明らかにやりたいことではないでしょう。
461 print $foo, exit; # これでもない。
462
463 =begin original
464
465 # These do the print before evaluating exit:
466 (print $foo), exit; # This is what you want.
467 print($foo), exit; # Or this.
468 print ($foo), exit; # Or even this.
469
470 =end original
471
472 # 以下は exit を評価する前に print を行ないます:
473 (print $foo), exit; # これがしたかった。
474 print($foo), exit; # これでもいい。
475 print ($foo), exit; # これも OK。
476
477 =begin original
478
479 Also note that
480
481 =end original
482
483 また、
484
485 print ($foo & 255) + 1, "\n";
486
487 =begin original
488
489 probably doesn't do what you expect at first glance. The parentheses
490 enclose the argument list for C<print> which is evaluated (printing
491 the result of S<C<$foo & 255>>). Then one is added to the return value
492 of C<print> (usually 1). The result is something like this:
493
494 =end original
495
496 の動作を一目見ただけで判断するのは、難しいでしょう。
497 かっこは C<print> のために評価される引数リストを囲っています
498 (S<C<$foo & 255>> の結果が表示されます)。
499 それから C<print> の返り値 (通常は 1) に 1 が加えられます。
500 結果は以下のようになります:
501
502 =begin original
503
504 1 + 1, "\n"; # Obviously not what you meant.
505
506 =end original
507
508 1 + 1, "\n"; # 明らかにやりたいことではないでしょう。
509
510 =begin original
511
512 To do what you meant properly, you must write:
513
514 =end original
515
516 意味したいことを適切に行うには、以下のように書く必要があります:
517
518 print(($foo & 255) + 1, "\n");
519
520 =begin original
521
522 See L</Named Unary Operators> for more discussion of this.
523
524 =end original
525
526 詳しくは、L</Named Unary Operators> を参照してください。
527
528 =begin original
529
530 Also parsed as terms are the S<C<do {}>> and S<C<eval {}>> constructs, as
531 well as subroutine and method calls, and the anonymous
532 constructors C<[]> and C<{}>.
533
534 =end original
535
536 この他に「項」として解析されるものには、S<C<do {}>> や S<C<eval {}>> の
537 構成、サブルーティンやメソッドの呼び出し、無名のコンストラクタ
538 C<[]> と C<{}> があります。
539
540 =begin original
541
542 See also L</Quote and Quote-like Operators> toward the end of this section,
543 as well as L</"I/O Operators">.
544
545 =end original
546
547 後の方の L</Quote and Quote-like Operators> や
548 L</"I/O Operators"> も参照してください。
549
550 =head2 The Arrow Operator
551 X<arrow> X<dereference> X<< -> >>
552
553 (矢印演算子)
554
555 =begin original
556
557 "C<< -> >>" is an infix dereference operator, just as it is in C
558 and C++. If the right side is either a C<[...]>, C<{...}>, or a
559 C<(...)> subscript, then the left side must be either a hard or
560 symbolic reference to an array, a hash, or a subroutine respectively.
561 (Or technically speaking, a location capable of holding a hard
562 reference, if it's an array or hash reference being used for
563 assignment.) See L<perlreftut> and L<perlref>.
564
565 =end original
566
567 C や C++ と同じように "C<< -> >>" は中置の被参照演算子です。
568 右側が C<[...]>, C<{...}>, C<(...)> のいずれかの形の添字であれば、左側は配列、
569 ハッシュ、サブルーチンへのハードリファレンスか
570 シンボリックリファレンスでなければなりません。
571 (あるいは技術的には、配列またはハードリファレンスが代入可能であれば
572 ハードリファレンスを保持できる場所です。)
573 L<perlreftut> と L<perlref> を参照してください。
574
575 =begin original
576
577 Otherwise, the right side is a method name or a simple scalar
578 variable containing either the method name or a subroutine reference,
579 and the left side must be either an object (a blessed reference)
580 or a class name (that is, a package name). See L<perlobj>.
581
582 =end original
583
584 そうでなければ、右側はメソッド名かサブルーチンのリファレンスを持った
585 単純スカラ変数で、左側はオブジェクト (bless されたリファレンス) か
586 クラス名でなければなりません。
587 L<perlobj> を参照してください。
588
589 =begin original
590
591 The dereferencing cases (as opposed to method-calling cases) are
592 somewhat extended by the C<postderef> feature. For the
593 details of that feature, consult L<perlref/Postfix Dereference Syntax>.
594
595 =end original
596
597 (メソッド呼び出しの場合ではなく) デリファレンスの場合、
598 C<後置デリファレンス> (postderef) 機能によっていくらか拡張されます。
599 この機能の詳細については、L<perlref/Postfix Dereference Syntax> を
600 参照してください。
601
602 =head2 Auto-increment and Auto-decrement
603 X<increment> X<auto-increment> X<++> X<decrement> X<auto-decrement> X<-->
604
605 (インクリメントとデクリメント)
606
607 =begin original
608
609 C<"++"> and C<"--"> work as in C. That is, if placed before a variable,
610 they increment or decrement the variable by one before returning the
611 value, and if placed after, increment or decrement after returning the
612 value.
613
614 =end original
615
616 C<"++"> と C<"--"> は、C の場合と同じように動作します。
617 つまり、変数の前に置かれれば、値を返す前に変数を 1 インクリメントまたは
618 デクリメントし、後に置かれれば、値を返した後で変数を
619 インクリメントまたはデクリメントします。
620
621 $i = 0; $j = 0;
622 print $i++; # prints 0
623 print ++$j; # prints 1
624
625 =begin original
626
627 Note that just as in C, Perl doesn't define B<when> the variable is
628 incremented or decremented. You just know it will be done sometime
629 before or after the value is returned. This also means that modifying
630 a variable twice in the same statement will lead to undefined behavior.
631 Avoid statements like:
632
633 =end original
634
635 C と同様、Perl は B<いつ> 変数がインクリメントまたはデクリメントされるかは
636 定義されません。
637 値が返される前か後のどこかで行われる、ということだけがわかります。
638 これは、同じ文である変数を 2 回修正すると、振る舞いが未定義になることを
639 意味します。
640 以下のような文は避けてください:
641
642 $i = $i ++;
643 print ++ $i + $i ++;
644
645 =begin original
646
647 Perl will not guarantee what the result of the above statements is.
648
649 =end original
650
651 Perl は上記の文の結果について保障しません。
652
653 =begin original
654
655 The auto-increment operator has a little extra builtin magic to it. If
656 you increment a variable that is numeric, or that has ever been used in
657 a numeric context, you get a normal increment. If, however, the
658 variable has been used in only string contexts since it was set, and
659 has a value that is not the empty string and matches the pattern
660 C</^[a-zA-Z]*[0-9]*\z/>, the increment is done as a string, preserving each
661 character within its range, with carry:
662
663 =end original
664
665 インクリメント演算子には、ちょっと風変わりな機能が組み込まれています。
666 数値が入った変数や、数値の文脈で使われてきた変数を
667 インクリメントする場合には、通常のインクリメントとして動作します。
668 しかし、その変数が設定されてからずっと文字列の文脈でしか使われていなくて、
669 空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする値を持っているときには、
670 個々の文字の範囲を保ちながら桁あげを行なって、文字列としてインクリメントが
671 行なわれます (マジカルインクリメントと呼ばれます):
672
673 print ++($foo = "99"); # prints "100"
674 print ++($foo = "a0"); # prints "a1"
675 print ++($foo = "Az"); # prints "Ba"
676 print ++($foo = "zz"); # prints "aaa"
677
678 =begin original
679
680 C<undef> is always treated as numeric, and in particular is changed
681 to C<0> before incrementing (so that a post-increment of an undef value
682 will return C<0> rather than C<undef>).
683
684 =end original
685
686 C<undef> は常に数値として扱われ、特にインクリメントされる前には C<0> に
687 変換されます(従って、undef のポストインクリメント値は C<undef> ではなく
688 C<0> になります)。
689
690 =begin original
691
692 The auto-decrement operator is not magical.
693
694 =end original
695
696 デクリメント演算子には、マジカルなものはありません。
697
698 =head2 Exponentiation
699 X<**> X<exponentiation> X<power>
700
701 (指数演算子)
702
703 =begin original
704
705 Binary C<"**"> is the exponentiation operator. It binds even more
706 tightly than unary minus, so C<-2**4> is C<-(2**4)>, not C<(-2)**4>.
707 (This is
708 implemented using C's C<pow(3)> function, which actually works on doubles
709 internally.)
710
711 =end original
712
713 二項演算子の C<"**"> は指数演算子です。
714 この演算子は、単項のマイナスよりも結合が強い演算子で、
715 C<-2**4> は C<(-2)**4> ではなく、C<-(2**4)> と解釈されます。
716 (これは C の C<pow(3)> を使って実装されていますので、
717 内部的には double で動作します。)
718
719 =begin original
720
721 Note that certain exponentiation expressions are ill-defined:
722 these include C<0**0>, C<1**Inf>, and C<Inf**0>. Do not expect
723 any particular results from these special cases, the results
724 are platform-dependent.
725
726 =end original
727
728 一部の指数表現は明確に定義されていません:
729 これは C<0**0>, C<1**Inf>, C<Inf**0> などです。
730 これらの特殊な場合に関して特定の結果を想定しないでください;
731 結果はプラットフォーム依存です。
732
733 =head2 Symbolic Unary Operators
734 X<unary operator> X<operator, unary>
735
736 (単項演算子)
737
738 =begin original
739
740 Unary C<"!"> performs logical negation, that is, "not". See also
741 L<C<not>|/Logical Not> for a lower precedence version of this.
742 X<!>
743
744 =end original
745
746 単項演算子の C<"!"> は論理否定を行ないます; つまり「not」ということです。
747 この演算子の優先順位を低くしたものとして、
748 L<C<not>|/Logical Not> が用意されています。
749 X<!>
750
751 =begin original
752
753 Unary C<"-"> performs arithmetic negation if the operand is numeric,
754 including any string that looks like a number. If the operand is
755 an identifier, a string consisting of a minus sign concatenated
756 with the identifier is returned. Otherwise, if the string starts
757 with a plus or minus, a string starting with the opposite sign is
758 returned. One effect of these rules is that C<-bareword> is equivalent
759 to the string C<"-bareword">. If, however, the string begins with a
760 non-alphabetic character (excluding C<"+"> or C<"-">), Perl will attempt
761 to convert
762 the string to a numeric, and the arithmetic negation is performed. If the
763 string cannot be cleanly converted to a numeric, Perl will give the warning
764 B<Argument "the string" isn't numeric in negation (-) at ...>.
765 X<-> X<negation, arithmetic>
766
767 =end original
768
769 単項演算子の C<"-"> は被演算子が数値または数値に見える文字列であれば、
770 算術否定を行ないます。
771 被演算子が識別子ならば、マイナス記号にその識別子をつなげた文字列が返されます。
772 これ以外で被演算子の最初の文字がプラスかマイナスのときには、
773 その記号を逆のものに置き換えた文字列を返します。
774 この規則の結果、C<-bareword> が文字列 C<"-bareword"> に等価となります。
775 しかし、文字列が英字以外(C<"+"> と C<"-"> を除く)で始まっていると、
776 Perl は文字列を数値に変換しようとし、それから算術否定が実行されます。
777 もし文字列が明確に数値に変換できない場合、Perl は
778 B<Argument "the string" isn't numeric in negation (-) at ...> という
779 警告を出します。
780 X<-> X<negation, arithmetic>
781
782 =begin original
783
784 Unary C<"~"> performs bitwise negation, that is, 1's complement. For
785 example, S<C<0666 & ~027>> is 0640. (See also L</Integer Arithmetic> and
786 L</Bitwise String Operators>.) Note that the width of the result is
787 platform-dependent: C<~0> is 32 bits wide on a 32-bit platform, but 64
788 bits wide on a 64-bit platform, so if you are expecting a certain bit
789 width, remember to use the C<"&"> operator to mask off the excess bits.
790 X<~> X<negation, binary>
791
792 =end original
793
794 単項演算子の C<"~"> はビットごとの否定を行ないます; つまり、1 の補数を返します。
795 例えば、S<C<0666 & ~027>> は 0640 です。
796 (L</Integer Arithmetic> と L</Bitwise String Operators> も参照して下さい。)
797 結果の幅はプラットホーム依存であることに注意してください: C<~0> は
798 32 ビットプラットホームでは 32 ビット幅ですが、64 ビットプラットホームでは
799 64 ビット幅なので、特定のビット幅を仮定する場合は、
800 余分なビットをマスクするために C<"&"> 演算子を使うことを忘れないでください。
801 X<~> X<negation, binary>
802
803 =begin original
804
805 Starting in Perl 5.28, it is a fatal error to try to complement a string
806 containing a character with an ordinal value above 255.
807
808 =end original
809
810 Perl 5.28 から、値が 255 を超える文字を含む文字列の
811 補数を求めようとすると致命的エラーになります。
812
813 =begin original
814
815 If the "bitwise" feature is enabled via S<C<use
816 feature 'bitwise'>> or C<use v5.28>, then unary
817 C<"~"> always treats its argument as a number, and an
818 alternate form of the operator, C<"~.">, always treats its argument as a
819 string. So C<~0> and C<~"0"> will both give 2**32-1 on 32-bit platforms,
820 whereas C<~.0> and C<~."0"> will both yield C<"\xff">. Until Perl 5.28,
821 this feature produced a warning in the C<"experimental::bitwise"> category.
822
823 =end original
824
825 "bitwise" 機能が S<C<use feature 'bitwise'>> か C<use v5.28> で有効にされると、
826 単項の C<"~"> は常にその引数を数値として扱い、その代替形式である
827 C<"~."> はその引数を常に文字列として扱います。
828 従って 32 ビットプラットフォームでは C<~0> と C<~"0"> はどちらも
829 2**32-1 を意味しますが、C<~.0> と C<~."0"> はどちらも C<"\xff"> になります。
830 Perl 5.28 まで、この機能は C<"experimental::bitwise"> カテゴリの警告を
831 発生させていました。
832
833 =begin original
834
835 Unary C<"+"> has no effect whatsoever, even on strings. It is useful
836 syntactically for separating a function name from a parenthesized expression
837 that would otherwise be interpreted as the complete list of function
838 arguments. (See examples above under L</Terms and List Operators (Leftward)>.)
839 X<+>
840
841 =end original
842
843 単項演算子の C<"+"> は、たとえ文字列に対して用いられた場合にも、何もしません。
844 関数名に続けて括弧付きの式を書く場合に、関数の引数リストと
845 解釈されないようにするために用いることができます。
846 (下記 L</Terms and List Operators (Leftward)> の例を参照してください。)
847 X<+>
848
849 =begin original
850
851 Unary C<"\"> creates references. If its operand is a single sigilled
852 thing, it creates a reference to that object. If its operand is a
853 parenthesised list, then it creates references to the things mentioned
854 in the list. Otherwise it puts its operand in list context, and creates
855 a list of references to the scalars in the list provided by the operand.
856 See L<perlreftut>
857 and L<perlref>. Do not confuse this behavior with the behavior of
858 backslash within a string, although both forms do convey the notion
859 of protecting the next thing from interpolation.
860 X<\> X<reference> X<backslash>
861
862 =end original
863
864 単項演算子の C<"\"> はリファレンスを生成します。
865 オペランドが一つの印付きのものの場合、それへのリファレンスを作ります。
866 オペランドがかっこでくくられたリストの場合、
867 リストで言及されているものへのリファレンスを作ります。
868 さもなければオペランドをリストコンテキストとし、
869 オペランドによって提供されたリストのスカラへのリファレンスのリストを作ります。
870 L<perlreftut> と L<perlref> を参照してください。
871 この用法も文字列中のバックスラッシュも、後に続くものが展開されるのを
872 防ぐことになりますが、動作を混同しないでください。
873 X<\> X<reference> X<backslash>
874
875 =head2 Binding Operators
876 X<binding> X<operator, binding> X<=~> X<!~>
877
878 (拘束演算子)
879
880 =begin original
881
882 Binary C<"=~"> binds a scalar expression to a pattern match. Certain operations
883 search or modify the string C<$_> by default. This operator makes that kind
884 of operation work on some other string. The right argument is a search
885 pattern, substitution, or transliteration. The left argument is what is
886 supposed to be searched, substituted, or transliterated instead of the default
887 C<$_>. When used in scalar context, the return value generally indicates the
888 success of the operation. The exceptions are substitution (C<s///>)
889 and transliteration (C<y///>) with the C</r> (non-destructive) option,
890 which cause the B<r>eturn value to be the result of the substitution.
891 Behavior in list context depends on the particular operator.
892 See L</"Regexp Quote-Like Operators"> for details and L<perlretut> for
893 examples using these operators.
894
895 =end original
896
897 二項演算子の C<"=~"> は、スカラ式をパターンマッチに拘束します。
898 デフォルトで C<$_> の文字列を検索したり、変更したりする演算があります。
899 この演算子は、そのような演算を他の文字列に対して行なわせるようにするものです。
900 右引数は、検索パターン、置換、文字変換のいずれかです。
901 左引数は、デフォルトの C<$_> の代わりに検索、置換、文字変換の
902 対象となるものです。
903 スカラコンテキストで使うと、返り値は一般的に演算の結果が成功したか否かです。
904 例外は、C</r> (非破壊) オプション付きの置換 (C<s///>) と
905 文字変換 (C<y///>) です; この場合は変換した結果を返します。
906 リストコンテキストでの振る舞いは演算子に依存します。
907 詳しくは L</"Regexp Quote-Like Operators"> を、これらの演算子を使った
908 例については L<perlretut> を参照して下さい。
909
910 =begin original
911
912 If the right argument is an expression rather than a search pattern,
913 substitution, or transliteration, it is interpreted as a search pattern at run
914 time. Note that this means that its
915 contents will be interpolated twice, so
916
917 =end original
918
919 右引数が検索パターン、置換、文字変換ではなく、式であれば、
920 それは実行時に決まる検索パターンと解釈されます。
921 これは、内容が 2 回展開されることを意味することに注意してください;
922 つまり:
923
924 '\\' =~ q'\\';
925
926 =begin original
927
928 is not ok, as the regex engine will end up trying to compile the
929 pattern C<\>, which it will consider a syntax error.
930
931 =end original
932
933 は正しくありません; 正規表現エンジンは最終的にパターン C<\> を
934 コンパイルしようとして、これは文法エラーと考えるからです。
935
936 =begin original
937
938 Binary C<"!~"> is just like C<"=~"> except the return value is negated in
939 the logical sense.
940
941 =end original
942
943 二項演算子の C<"!~"> は、返される値が論理否定されることを除いて
944 C<"=~"> と同じです。
945
946 =begin original
947
948 Binary C<"!~"> with a non-destructive substitution (C<s///r>) or transliteration
949 (C<y///r>) is a syntax error.
950
951 =end original
952
953 二項演算子の C<"!~"> を非破壊置換 (C<s///r>) や変換 (C<y///r>) で使うと
954 文法エラーとなります。
955
956 =head2 Multiplicative Operators
957 X<operator, multiplicative>
958
959 (乗法演算子)
960
961 =begin original
962
963 Binary C<"*"> multiplies two numbers.
964 X<*>
965
966 =end original
967
968 二項演算子の C<"*"> は 2 つの数値の積を返します。
969 X<*>
970
971 =begin original
972
973 Binary C<"/"> divides two numbers.
974 X</> X<slash>
975
976 =end original
977
978 二項演算子の C<"/"> は 2 つの数値の商を返します。
979 X</> X<slash>
980
981 =begin original
982
983 Binary C<"%"> is the modulo operator, which computes the division
984 remainder of its first argument with respect to its second argument.
985 Given integer
986 operands C<$m> and C<$n>: If C<$n> is positive, then S<C<$m % $n>> is
987 C<$m> minus the largest multiple of C<$n> less than or equal to
988 C<$m>. If C<$n> is negative, then S<C<$m % $n>> is C<$m> minus the
989 smallest multiple of C<$n> that is not less than C<$m> (that is, the
990 result will be less than or equal to zero). If the operands
991 C<$m> and C<$n> are floating point values and the absolute value of
992 C<$n> (that is C<abs($n)>) is less than S<C<(UV_MAX + 1)>>, only
993 the integer portion of C<$m> and C<$n> will be used in the operation
994 (Note: here C<UV_MAX> means the maximum of the unsigned integer type).
995 If the absolute value of the right operand (C<abs($n)>) is greater than
996 or equal to S<C<(UV_MAX + 1)>>, C<"%"> computes the floating-point remainder
997 C<$r> in the equation S<C<($r = $m - $i*$n)>> where C<$i> is a certain
998 integer that makes C<$r> have the same sign as the right operand
999 C<$n> (B<not> as the left operand C<$m> like C function C<fmod()>)
1000 and the absolute value less than that of C<$n>.
1001 Note that when S<C<use integer>> is in scope, C<"%"> gives you direct access
1002 to the modulo operator as implemented by your C compiler. This
1003 operator is not as well defined for negative operands, but it will
1004 execute faster.
1005 X<%> X<remainder> X<modulo> X<mod>
1006
1007 =end original
1008
1009 二項演算子の C<"%"> は剰余演算子で、一つ目の引数を二つ目の引数で割ったときの
1010 余りを返します。
1011 C<$m> と C<$n> の二つの整数の被演算子を取ったとすると:
1012 C<$n> が正の場合、S<C<$m % $n>> は、C<$m> から C<$m> 以下の最大の
1013 C<$n> の倍数を引いた値です。
1014 C<$n> が負の場合、S<C<$m % $n>> は、C<$m> から C<$m> を下回らない
1015 最小の C<$n> の倍数を引いた値です(従って結果はゼロ以下になります)。
1016 オペランド C<$m> と C<$n> が浮動小数点数で、C<$n> の絶対値
1017 (つまり C<abs($n)>) が S<C<(UV_MAX + 1)>> より小さい場合、
1018 C<$m> と C<$n> の整数部のみが操作で使われます
1019 (注意: ここで C<UV_MAX> は符号なし整数の最大値を意味します)。
1020 右オペランドの絶対値 (C<abs($n)>) が S<C<(UV_MAX + 1)>> 以上の場合、
1021 C<"%"> は、S<C<($r = $m - $i*$n)>> となる浮動小数点剰余 C<$r> を計算します;
1022 ここで C<$i> は、C<$r> が右オペランド C<$n> と同じ符号 (C の
1023 関数 C<fmod()> のように左オペランド C<$m> B<ではありません>) で、
1024 絶対値が C<$n> より小さいものになるような、ある整数です。
1025 S<C<use integer>> がスコープ内にある場合、
1026 C<"%"> は C コンパイラで実装された剰余演算子を使います。
1027 この演算子は被演算子が負の場合の挙動が不確実ですが、
1028 より高速です。
1029 X<%> X<remainder> X<modulo> X<mod>
1030
1031 =begin original
1032
1033 Binary C<x> is the repetition operator. In scalar context, or if the
1034 left operand is neither enclosed in parentheses nor a C<qw//> list,
1035 it performs a string repetition. In that case it supplies scalar
1036 context to the left operand, and returns a string consisting of the
1037 left operand string repeated the number of times specified by the right
1038 operand. If the C<x> is in list context, and the left operand is either
1039 enclosed in parentheses or a C<qw//> list, it performs a list repetition.
1040 In that case it supplies list context to the left operand, and returns
1041 a list consisting of the left operand list repeated the number of times
1042 specified by the right operand.
1043 If the right operand is zero or negative (raising a warning on
1044 negative), it returns an empty string
1045 or an empty list, depending on the context.
1046 X<x>
1047
1048 =end original
1049
1050 二項演算子の C<"x"> は繰り返し演算子です。
1051 スカラコンテキストまたは左辺値がかっこで囲まれたり
1052 C<qw//> リストであったりしない場合は、文字列の繰り返しを実行します。
1053 この場合、左オペランドにスカラコンテキストを提供し、
1054 右オペランドによって指定された回数繰り返された左オペランド文字列からなる
1055 文字列を返します。
1056 C<x> がリストコンテキストで、
1057 左オペランドがかっこでくくられているか
1058 C<qw//> リストの場合、
1059 リスト繰り返しを実行します。
1060 この場合、これは左オペランドへリストコンテキストを提供し、
1061 右オペランドによって指定された回数繰り返された
1062 左オペランドのリストからなるリストを返します。
1063 右オペランドが 0 か負数の場合(負数の場合は警告が出ます)、
1064 コンテキストによって空文字列か空リストを返します。
1065 X<x>
1066
1067 print '-' x 80; # print row of dashes
1068
1069 print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
1070
1071 @ones = (1) x 80; # a list of 80 1's
1072 @ones = (5) x @ones; # set all elements to 5
1073
1074 =head2 Additive Operators
1075 X<operator, additive>
1076
1077 (加法演算子)
1078
1079 =begin original
1080
1081 Binary C<"+"> returns the sum of two numbers.
1082 X<+>
1083
1084 =end original
1085
1086 二項演算子の C<"+"> は 2 つの数値の和を返します。
1087 X<+>
1088
1089 =begin original
1090
1091 Binary C<"-"> returns the difference of two numbers.
1092 X<->
1093
1094 =end original
1095
1096 二項演算子の C<"-"> は 2 つの数値の差を返します。
1097 X<->
1098
1099 =begin original
1100
1101 Binary C<"."> concatenates two strings.
1102 X<string, concatenation> X<concatenation>
1103 X<cat> X<concat> X<concatenate> X<.>
1104
1105 =end original
1106
1107 二項演算子の C<"."> は 2 つの文字列を連結します。
1108 X<string, concatenation> X<concatenation>
1109 X<cat> X<concat> X<concatenate> X<.>
1110
1111 =head2 Shift Operators
1112 X<shift operator> X<operator, shift> X<<< << >>>
1113 X<<< >> >>> X<right shift> X<left shift> X<bitwise shift>
1114 X<shl> X<shr> X<shift, right> X<shift, left>
1115
1116 (シフト演算子)
1117
1118 =begin original
1119
1120 Binary C<<< "<<" >>> returns the value of its left argument shifted left by the
1121 number of bits specified by the right argument. Arguments should be
1122 integers. (See also L</Integer Arithmetic>.)
1123
1124 =end original
1125
1126 二項演算子の C<<< "<<" >>> は左引数の値を、右引数で示すビット数だけ、
1127 左にシフトした値を返します。
1128 引数は整数でなければなりません。
1129 (L</Integer Arithmetic> も参照して下さい。)
1130
1131 =begin original
1132
1133 Binary C<<< ">>" >>> returns the value of its left argument shifted right by
1134 the number of bits specified by the right argument. Arguments should
1135 be integers. (See also L</Integer Arithmetic>.)
1136
1137 =end original
1138
1139 二項演算子の C<<< ">>" >>> は左引数の値を、右引数で示すビット数だけ、
1140 右にシフトした値を返します。
1141 引数は整数でなければなりません。
1142 (L</Integer Arithmetic> も参照して下さい。)
1143
1144 =begin original
1145
1146 If S<C<use integer>> (see L</Integer Arithmetic>) is in force then
1147 signed C integers are used (I<arithmetic shift>), otherwise unsigned C
1148 integers are used (I<logical shift>), even for negative shiftees.
1149 In arithmetic right shift the sign bit is replicated on the left,
1150 in logical shift zero bits come in from the left.
1151
1152 =end original
1153
1154 S<C<use integer>> (L</Integer Arithmetic> を参照してください)が有効な場合、
1155 C の符号付き整数が使われ(I<算術シフト>)、そうでない場合は(例え負のシフトでも)
1156 C の符号なし整数が使われます(I<論理シフト>)。
1157 算術右シフトでは符号ビットは左側に複製され、論理シフトでは 0 ビットが
1158 左側から来ます。
1159
1160 =begin original
1161
1162 Either way, the implementation isn't going to generate results larger
1163 than the size of the integer type Perl was built with (32 bits or 64 bits).
1164
1165 =end original
1166
1167 どちらの場合も、この実装は Perl がビルドされた整数型のサイズ(32 ビットか
1168 64 ビット)よりも大きい結果を生成することはありません。
1169
1170 =begin original
1171
1172 Shifting by negative number of bits means the reverse shift: left
1173 shift becomes right shift, right shift becomes left shift. This is
1174 unlike in C, where negative shift is undefined.
1175
1176 =end original
1177
1178 負数のビットシフトは逆シフトを意味します: 左シフトは右シフトに鳴り、
1179 右シフトは左シフトになります。
1180 これは、負のシフトが未定義である C とは異なります。
1181
1182 =begin original
1183
1184 Shifting by more bits than the size of the integers means most of the
1185 time zero (all bits fall off), except that under S<C<use integer>>
1186 right overshifting a negative shiftee results in -1. This is unlike
1187 in C, where shifting by too many bits is undefined. A common C
1188 behavior is "shift by modulo wordbits", so that for example
1189
1190 =end original
1191
1192 整数のサイズ以上のビット数のシフトは、ほとんどの場合 0 (全てのビットが落ちる)
1193 になります; 例外として、S<C<use integer>> の基で、負の値を超過シフトすると
1194 -1 になります。
1195 これは、多すぎるビット数のシフトは未定義である C とは異なります。
1196 一般的な C の振る舞いは「ワードビット数を法としてシフトする」なので、
1197 例えば次のようになります
1198
1199 1 >> 64 == 1 >> (64 % 64) == 1 >> 0 == 1 # Common C behavior.
1200
1201 =begin original
1202
1203 but that is completely accidental.
1204
1205 =end original
1206
1207 しかしこれは完全に偶然です。
1208
1209 =begin original
1210
1211 If you get tired of being subject to your platform's native integers,
1212 the S<C<use bigint>> pragma neatly sidesteps the issue altogether:
1213
1214 =end original
1215
1216 プラットフォームのネイティブな整数の影響に疲れたなら、S<C<use bigint>>
1217 プラグマは問題を完全にうまく回避します:
1218
1219 print 20 << 20; # 20971520
1220 print 20 << 40; # 5120 on 32-bit machines,
1221 # 21990232555520 on 64-bit machines
1222 use bigint;
1223 print 20 << 100; # 25353012004564588029934064107520
1224
1225 =head2 Named Unary Operators
1226 X<operator, named unary>
1227
1228 (名前付き単項演算子)
1229
1230 =begin original
1231
1232 The various named unary operators are treated as functions with one
1233 argument, with optional parentheses.
1234
1235 =end original
1236
1237 さまざまな名前付き単項演算子が、引数を 1 つ持ち、括弧が省略可能な、
1238 関数として扱われます。
1239
1240 =begin original
1241
1242 If any list operator (C<print()>, etc.) or any unary operator (C<chdir()>, etc.)
1243 is followed by a left parenthesis as the next token, the operator and
1244 arguments within parentheses are taken to be of highest precedence,
1245 just like a normal function call. For example,
1246 because named unary operators are higher precedence than C<||>:
1247
1248 =end original
1249
1250 リスト演算子 (C<print()> など) や単項演算子 (C<chdir()> など) は、
1251 すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、
1252 通常の関数呼び出しのようにもっとも高い優先順位として扱われます。
1253 たとえば、名前つき単項演算子は C<||> より優先順位が高いので、
1254 以下のようになります:
1255
1256 chdir $foo || die; # (chdir $foo) || die
1257 chdir($foo) || die; # (chdir $foo) || die
1258 chdir ($foo) || die; # (chdir $foo) || die
1259 chdir +($foo) || die; # (chdir $foo) || die
1260
1261 =begin original
1262
1263 but, because C<"*"> is higher precedence than named operators:
1264
1265 =end original
1266
1267 しかし C<"*"> は名前つき演算子より優先順位が高いので、以下のようになります:
1268
1269 chdir $foo * 20; # chdir ($foo * 20)
1270 chdir($foo) * 20; # (chdir $foo) * 20
1271 chdir ($foo) * 20; # (chdir $foo) * 20
1272 chdir +($foo) * 20; # chdir ($foo * 20)
1273
1274 rand 10 * 20; # rand (10 * 20)
1275 rand(10) * 20; # (rand 10) * 20
1276 rand (10) * 20; # (rand 10) * 20
1277 rand +(10) * 20; # rand (10 * 20)
1278
1279 =begin original
1280
1281 Regarding precedence, the filetest operators, like C<-f>, C<-M>, etc. are
1282 treated like named unary operators, but they don't follow this functional
1283 parenthesis rule. That means, for example, that C<-f($file).".bak"> is
1284 equivalent to S<C<-f "$file.bak">>.
1285 X<-X> X<filetest> X<operator, filetest>
1286
1287 =end original
1288
1289 優先順位に関して、C<-f> や C<-M> のようなファイルテスト演算子は、名前付き
1290 単項演算子として扱われますが、この関数のかっこルールは適用されません。
1291 これは、例えば C<-f($file).".bak"> は S<C<-f "$file.bak">> と等価であることを
1292 意味します。
1293 X<-X> X<filetest> X<operator, filetest>
1294
1295 =begin original
1296
1297 See also L</"Terms and List Operators (Leftward)">.
1298
1299 =end original
1300
1301 L</"Terms and List Operators (Leftward)"> も参照して下さい。
1302
1303 =head2 Relational Operators
1304 X<relational operator> X<operator, relational>
1305
1306 (比較演算子)
1307
1308 =begin original
1309
1310 Perl operators that return true or false generally return values
1311 that can be safely used as numbers. For example, the relational
1312 operators in this section and the equality operators in the next
1313 one return C<1> for true and a special version of the defined empty
1314 string, C<"">, which counts as a zero but is exempt from warnings
1315 about improper numeric conversions, just as S<C<"0 but true">> is.
1316
1317 =end original
1318
1319 真か偽の値を返す Perl 演算子は一般的に安全に数値として使える値を返します。
1320 例えば、この節の関係演算子と次の節の等価演算子は、真および、
1321 S<C<"0 but true">> と同様、ゼロとカウントされるけれども不適切な数値変換に
1322 関する警告の出ない、定義された空文字列 C<""> の特別版に対して C<1> を
1323 返します。
1324
1325 =begin original
1326
1327 Binary C<< "<" >> returns true if the left argument is numerically less than
1328 the right argument.
1329 X<< < >>
1330
1331 =end original
1332
1333 二項演算子の C<< "<" >> は左引数が数値的に右引数よりも小さければ、
1334 真を返します。
1335 X<< < >>
1336
1337 =begin original
1338
1339 Binary C<< ">" >> returns true if the left argument is numerically greater
1340 than the right argument.
1341 X<< > >>
1342
1343 =end original
1344
1345 二項演算子の C<< ">" >> は左引数が数値的に右引数よりも大きければ、
1346 真を返します。
1347 X<< > >>
1348
1349 =begin original
1350
1351 Binary C<< "<=" >> returns true if the left argument is numerically less than
1352 or equal to the right argument.
1353 X<< <= >>
1354
1355 =end original
1356
1357 二項演算子の C<< "<=" >> は左引数が数値的に右引数よりも小さいか等しければ、
1358 真を返します。
1359 X<< <= >>
1360
1361 =begin original
1362
1363 Binary C<< ">=" >> returns true if the left argument is numerically greater
1364 than or equal to the right argument.
1365 X<< >= >>
1366
1367 =end original
1368
1369 二項演算子の C<< ">=" >> は左引数が数値的に右引数よりも大きいか等しければ、
1370 真を返します。
1371 X<< >= >>
1372
1373 =begin original
1374
1375 Binary C<"lt"> returns true if the left argument is stringwise less than
1376 the right argument.
1377 X<< lt >>
1378
1379 =end original
1380
1381 二項演算子の C<"lt"> は左引数が文字列的に右引数よりも小さければ、真を返します。
1382 X<< lt >>
1383
1384 =begin original
1385
1386 Binary C<"gt"> returns true if the left argument is stringwise greater
1387 than the right argument.
1388 X<< gt >>
1389
1390 =end original
1391
1392 二項演算子の C<"gt"> は左引数が文字列的に右引数よりも大きければ、真を返します。
1393 X<< gt >>
1394
1395 =begin original
1396
1397 Binary C<"le"> returns true if the left argument is stringwise less than
1398 or equal to the right argument.
1399 X<< le >>
1400
1401 =end original
1402
1403 二項演算子の C<"le"> は左引数が文字列的に右引数よりも小さいか等しければ、
1404 真を返します。
1405 X<< le >>
1406
1407 =begin original
1408
1409 Binary C<"ge"> returns true if the left argument is stringwise greater
1410 than or equal to the right argument.
1411 X<< ge >>
1412
1413 =end original
1414
1415 二項演算子の C<"ge"> は左引数が文字列的に右引数よりも大きいか等しければ、
1416 真を返します。
1417 X<< ge >>
1418
1419 =begin original
1420
1421 A sequence of relational operators, such as S<C<"$x E<lt> $y E<lt>=
1422 $z">>, performs chained comparisons, in the manner described above in
1423 the section L</"Operator Precedence and Associativity">.
1424 Beware that they do not chain with equality operators, which have lower
1425 precedence.
1426
1427 =end original
1428
1429 S<C<"$x E<lt> $y E<lt>= $z">> のような比較演算子の並びは、
1430 L</"Operator Precedence and Associativity"> 節で述べたような形で
1431 連鎖比較を実行します。
1432 より低い優先順位を持つ等価演算子とは連鎖しないことに注意してください。
1433
1434 =head2 Equality Operators
1435 X<equality> X<equal> X<equals> X<operator, equality>
1436
1437 (等価演算子)
1438
1439 =begin original
1440
1441 Binary C<< "==" >> returns true if the left argument is numerically equal to
1442 the right argument.
1443 X<==>
1444
1445 =end original
1446
1447 二項演算子の C<< "==" >> は左引数が数値的に右引数と等しければ、真を返します。
1448 X<==>
1449
1450 =begin original
1451
1452 Binary C<< "!=" >> returns true if the left argument is numerically not equal
1453 to the right argument.
1454 X<!=>
1455
1456 =end original
1457
1458 二項演算子の C<< "!=" >> は左引数が数値的に右引数と等しくなければ、真を
1459 返します。
1460 X<!=>
1461
1462 =begin original
1463
1464 Binary C<"eq"> returns true if the left argument is stringwise equal to
1465 the right argument.
1466 X<eq>
1467
1468 =end original
1469
1470 二項演算子の C<"eq"> は左引数が文字列的に右引数と等しければ、真を返します。
1471 X<eq>
1472
1473 =begin original
1474
1475 Binary C<"ne"> returns true if the left argument is stringwise not equal
1476 to the right argument.
1477 X<ne>
1478
1479 =end original
1480
1481 二項演算子の C<"ne"> は左引数が文字列的に右引数と等しくなければ、真を
1482 返します。
1483 X<ne>
1484
1485 =begin original
1486
1487 A sequence of the above equality operators, such as S<C<"$x == $y ==
1488 $z">>, performs chained comparisons, in the manner described above in
1489 the section L</"Operator Precedence and Associativity">.
1490 Beware that they do not chain with relational operators, which have
1491 higher precedence.
1492
1493 =end original
1494
1495 S<C<"$x == $y == $z">> のような上述の等価演算子の並びは、
1496 L</"Operator Precedence and Associativity"> 節で述べたような形で
1497 連鎖比較を実行します。
1498 より高い優先順位を持つ比較演算子とは連鎖しないことに注意してください。
1499
1500 =begin original
1501
1502 Binary C<< "<=>" >> returns -1, 0, or 1 depending on whether the left
1503 argument is numerically less than, equal to, or greater than the right
1504 argument. If your platform supports C<NaN>'s (not-a-numbers) as numeric
1505 values, using them with C<< "<=>" >> returns undef. C<NaN> is not
1506 C<< "<" >>, C<< "==" >>, C<< ">" >>, C<< "<=" >> or C<< ">=" >> anything
1507 (even C<NaN>), so those 5 return false. S<C<< NaN != NaN >>> returns
1508 true, as does S<C<NaN !=> I<anything else>>. If your platform doesn't
1509 support C<NaN>'s then C<NaN> is just a string with numeric value 0.
1510 X<< <=> >>
1511 X<spaceship>
1512
1513 =end original
1514
1515 二項演算子の C<< "<=>" >> は左引数が数値的に右引数より小さいか、等しいか、
1516 大きいかに従って、-1, 0, 1 を返します。
1517 数値として C<NaN> (非数) に対応しているプラットフォームでは、
1518 それに対して C<< "<=>" >> を使うと undef を返します。
1519 C<NaN> はどの値に対しても(C<NaN> に対してでさえも) C<< "<" >>, C<< "==" >>,
1520 C<< ">" >>, C<< "<=" >>, C<< ">=" >> のいずれも成立しないので、これらは全て
1521 偽となります。
1522 S<C<< NaN != NaN >>> は真を返しますが、
1523 S<C<NaN !=> I<その他のどの値でも>> です。
1524 C<NaN> に対応していないプラットフォームでは、C<NaN> は単に数としての値 0 を
1525 持つ文字列です。
1526 X<< <=> >> X<spaceship>
1527
1528 $ perl -le '$x = "NaN"; print "No NaN support here" if $x == $x'
1529 $ perl -le '$x = "NaN"; print "NaN support here" if $x != $x'
1530
1531 =begin original
1532
1533 (Note that the L<bigint>, L<bigrat>, and L<bignum> pragmas all
1534 support C<"NaN">.)
1535
1536 =end original
1537
1538 (L<bigint>, L<bigrat>, L<bignum> プラグマは全て C<"NaN"> に対応していることに
1539 注意してください。)
1540
1541 =begin original
1542
1543 Binary C<"cmp"> returns -1, 0, or 1 depending on whether the left
1544 argument is stringwise less than, equal to, or greater than the right
1545 argument.
1546 X<cmp>
1547
1548 =end original
1549
1550 二項演算子の C<"cmp"> は左引数が文字列的に右引数より小さいか、
1551 等しいか、大きいかに従って、-1, 0, 1 を返します。
1552 X<cmp>
1553
1554 =begin original
1555
1556 Binary C<"~~"> does a smartmatch between its arguments. Smart matching
1557 is described in the next section.
1558 X<~~>
1559
1560 =end original
1561
1562 二項演算子の C<"~~"> はその引数に対してスマートマッチングを行います。
1563 スマートマッチングについては次の節で述べられています。
1564 X<~~>
1565
1566 =begin original
1567
1568 The two-sided ordering operators C<"E<lt>=E<gt>"> and C<"cmp">, and the
1569 smartmatch operator C<"~~">, are non-associative with respect to each
1570 other and with respect to the equality operators of the same precedence.
1571
1572 =end original
1573
1574 両面順序演算子 C<"E<lt>=E<gt>">, C<"cmp"> および、
1575 スマートマッチング演算子 C<"~~"> は、互いに、および同じ優先順位の
1576 演算子に対しては非結合です。
1577
1578 =begin original
1579
1580 C<"lt">, C<"le">, C<"ge">, C<"gt"> and C<"cmp"> use the collation (sort)
1581 order specified by the current C<LC_COLLATE> locale if a S<C<use
1582 locale>> form that includes collation is in effect. See L<perllocale>.
1583 Do not mix these with Unicode,
1584 only use them with legacy 8-bit locale encodings.
1585 The standard C<L<Unicode::Collate>> and
1586 C<L<Unicode::Collate::Locale>> modules offer much more powerful
1587 solutions to collation issues.
1588
1589 =end original
1590
1591 C<"lt">, C<"le">, C<"ge">, C<"gt">, C<"cmp"> は照合を含む
1592 S<C<use locale>> 型式が有効な場合は、現在の C<LC_COLLATE> ロケールで
1593 指定された照合(ソート)順が使われます。
1594 L<perllocale> を参照して下さい。
1595 これらを Unicode と混ぜないでください;
1596 伝統的な 8 ビットロケールコーディングでのみ使ってください。
1597 標準の C<L<Unicode::Collate>> と C<L<Unicode::Collate::Locale>> モジュールは、
1598 照合問題に関する遥かに強力な解決法を提供します。
1599
1600 =begin original
1601
1602 For case-insensitive comparisons, look at the L<perlfunc/fc> case-folding
1603 function, available in Perl v5.16 or later:
1604
1605 =end original
1606
1607 大文字小文字を無視した比較に関しては、Perl v5.16 以降で利用可能な
1608 L<perlfunc/fc> 大文字小文字畳み込み関数を参照してください:
1609
1610 if ( fc($x) eq fc($y) ) { ... }
1611
1612 =head2 Class Instance Operator
1613 X<isa operator>
1614
1615 (クラスインスタンス演算子)
1616
1617 =begin original
1618
1619 Binary C<isa> evaluates to true when the left argument is an object instance of
1620 the class (or a subclass derived from that class) given by the right argument.
1621 If the left argument is not defined, not a blessed object instance, nor does
1622 not derive from the class given by the right argument, the operator evaluates
1623 as false. The right argument may give the class either as a bareword or a
1624 scalar expression that yields a string class name:
1625
1626 =end original
1627
1628 二項演算子の C<isa> は、左側の引数が右側の引数で与えられた
1629 クラス(またはこのクラスから派生した下位クラス)の
1630 オブジェクトインスタンスである場合に真と評価されます。
1631 左側の引数が、未定義、bless されたオブジェクトインスタンスではない、
1632 右側の引数で与えられたクラスから派生していない、のいずれかの場合、
1633 この演算子は偽として評価されます。
1634 右側の引数は、裸の単語か、クラス名の文字列となるスカラ式を与えます:
1635
1636 if( $obj isa Some::Class ) { ... }
1637
1638 if( $obj isa "Different::Class" ) { ... }
1639 if( $obj isa $name_of_class ) { ... }
1640
1641 =begin original
1642
1643 This is an experimental feature and is available from Perl 5.31.6 when enabled
1644 by C<use feature 'isa'>. It emits a warning in the C<experimental::isa>
1645 category.
1646
1647 =end original
1648
1649 これは Perl 5.31.6 からの実験的な機能で、C<use feature 'isa'> によって
1650 有効化されると利用可能になります。
1651 これは C<experimental::isa> カテゴリの警告を出力します。
1652
1653 =head2 Smartmatch Operator
1654
1655 (スマートマッチング演算子)
1656
1657 =begin original
1658
1659 First available in Perl 5.10.1 (the 5.10.0 version behaved differently),
1660 binary C<~~> does a "smartmatch" between its arguments. This is mostly
1661 used implicitly in the C<when> construct described in L<perlsyn>, although
1662 not all C<when> clauses call the smartmatch operator. Unique among all of
1663 Perl's operators, the smartmatch operator can recurse. The smartmatch
1664 operator is L<experimental|perlpolicy/experimental> and its behavior is
1665 subject to change.
1666
1667 =end original
1668
1669 Perl 5.10.1 で最初に現れた (5.10.0 版は異なった振る舞いでした)、2 項 C<~~> は
1670 引数に対する「スマートマッチング」を行います。
1671 これはほとんど L<perlsyn> で記述されている C<when> 構文で暗黙に使われますが、
1672 C<when> 節だけがスマートマッチング演算子を呼び出すわけではありません。
1673 全ての Perl の演算子の中で唯一、スマートマッチング演算子は再帰できます。
1674 スマートマッチング演算子は L<実験的|perlpolicy/experimental> で、その振る舞いは
1675 変更されることがあります。
1676
1677 =begin original
1678
1679 It is also unique in that all other Perl operators impose a context
1680 (usually string or numeric context) on their operands, autoconverting
1681 those operands to those imposed contexts. In contrast, smartmatch
1682 I<infers> contexts from the actual types of its operands and uses that
1683 type information to select a suitable comparison mechanism.
1684
1685 =end original
1686
1687 また、その他全ての Perl 演算子はそのオペランドにコンテキスト(通常は
1688 文字列または数値コンテキスト)を割り当てて、オペランドを割り当てた
1689 コンテキストに自動変換します。
1690 一方、スマートマッチングはそのオペランドの実際の型からコンテキストを
1691 I<推論> して、適切な比較機構を選択するためにその型情報を使います。
1692
1693 =begin original
1694
1695 The C<~~> operator compares its operands "polymorphically", determining how
1696 to compare them according to their actual types (numeric, string, array,
1697 hash, etc.). Like the equality operators with which it shares the same
1698 precedence, C<~~> returns 1 for true and C<""> for false. It is often best
1699 read aloud as "in", "inside of", or "is contained in", because the left
1700 operand is often looked for I<inside> the right operand. That makes the
1701 order of the operands to the smartmatch operand often opposite that of
1702 the regular match operator. In other words, the "smaller" thing is usually
1703 placed in the left operand and the larger one in the right.
1704
1705 =end original
1706
1707 C<~~> 演算子はオペランドを「多態的に」比較します; どのように比較するかの
1708 決定は、実際の型 (数値、文字列、配列、ハッシュなど) に基づきます。
1709 同じ優先順位を共有する等価演算子のように、
1710 C<~~> は真では 1 を、偽では C<""> を返します。
1711 これはしばしば "in", "inside of", "is contained in" と呼ぶのが最良です;
1712 なぜなら左オペランドはしばしば右オペランドの I<内側> を探すからです。
1713 これにより、スマートマッチングオペランドへのオペランドの順序はしばしば
1714 正規表現演算子のものと逆になります。
1715 言い換えると、「より小さい」ものが普通は左オペランドに置かれ、より
1716 大きいものが右側に置かれます。
1717
1718 =begin original
1719
1720 The behavior of a smartmatch depends on what type of things its arguments
1721 are, as determined by the following table. The first row of the table
1722 whose types apply determines the smartmatch behavior. Because what
1723 actually happens is mostly determined by the type of the second operand,
1724 the table is sorted on the right operand instead of on the left.
1725
1726 =end original
1727
1728 スマートマッチングの振る舞いは、次の表で決定されるように、引数がどんな
1729 型かに依存します。
1730 型が適用される表の最初の行は、スマートマッチングの振る舞いを決定します。
1731 実際に何が起こるかはほとんどの場合 2 番目のオペランドの型で決定されるので、
1732 表は左ではなく右オペランドでソートされています。
1733
1734 =begin original
1735
1736 Left Right Description and pseudocode
1737 ===============================================================
1738 Any undef check whether Any is undefined
1739 like: !defined Any
1740
1741 =end original
1742
1743 左 右 説明と擬似コード
1744 ===============================================================
1745 Any undef Any が未定義かどうか調べる
1746 like: !defined Any
1747
1748 =begin original
1749
1750 Any Object invoke ~~ overloading on Object, or die
1751
1752 =end original
1753
1754 Any Object Object に対する ~~ オーバーロードを起動するか die
1755
1756 =begin original
1757
1758 Right operand is an ARRAY:
1759
1760 =end original
1761
1762 右被演算子が 配列:
1763
1764 =begin original
1765
1766 Left Right Description and pseudocode
1767 ===============================================================
1768 ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2]
1769 like: (ARRAY1[0] ~~ ARRAY2[0])
1770 && (ARRAY1[1] ~~ ARRAY2[1]) && ...
1771 HASH ARRAY any ARRAY elements exist as HASH keys
1772 like: grep { exists HASH->{$_} } ARRAY
1773 Regexp ARRAY any ARRAY elements pattern match Regexp
1774 like: grep { /Regexp/ } ARRAY
1775 undef ARRAY undef in ARRAY
1776 like: grep { !defined } ARRAY
1777 Any ARRAY smartmatch each ARRAY element[3]
1778 like: grep { Any ~~ $_ } ARRAY
1779
1780 =end original
1781
1782 左 右 説明と擬似コード
1783 ===============================================================
1784 ARRAY1 ARRAY2 ARRAY1 と ARRAY2 の組の要素に対して再帰 [2]
1785 like: (ARRAY1[0] ~~ ARRAY2[0])
1786 && (ARRAY1[1] ~~ ARRAY2[1]) && ...
1787 HASH ARRAY いずれかの ARRAY 要素が HASH キーに存在するか
1788 like: grep { exists HASH->{$_} } ARRAY
1789 Regexp ARRAY いずれかの ARRAY 要素が Regexp でマッチングするか
1790 like: grep { /Regexp/ } ARRAY
1791 undef ARRAY ARRAY 内の undef
1792 like: grep { !defined } ARRAY
1793 Any ARRAY それぞれの ARRAY 要素に対してスマートマッチング [3]
1794 like: grep { Any ~~ $_ } ARRAY
1795
1796 =begin original
1797
1798 Right operand is a HASH:
1799
1800 =end original
1801
1802 右被演算子がハッシュ:
1803
1804 =begin original
1805
1806 Left Right Description and pseudocode
1807 ===============================================================
1808 HASH1 HASH2 all same keys in both HASHes
1809 like: keys HASH1 ==
1810 grep { exists HASH2->{$_} } keys HASH1
1811 ARRAY HASH any ARRAY elements exist as HASH keys
1812 like: grep { exists HASH->{$_} } ARRAY
1813 Regexp HASH any HASH keys pattern match Regexp
1814 like: grep { /Regexp/ } keys HASH
1815 undef HASH always false (undef can't be a key)
1816 like: 0 == 1
1817 Any HASH HASH key existence
1818 like: exists HASH->{Any}
1819
1820 =end original
1821
1822 左 右 説明と擬似コード
1823 ===============================================================
1824 HASH1 HASH2 HASH1 と HASH2 両方が全て同じキー
1825 like: keys HASH1 ==
1826 grep { exists HASH2->{$_} } keys HASH1
1827 ARRAY HASH いずれかの ARRAY 要素が HASH キーに存在するか
1828 like: grep { exists HASH->{$_} } ARRAY
1829 Regexp HASH いずれかの HASH キーが Regexp でマッチングするか
1830 like: grep { /Regexp/ } keys HASH
1831 undef HASH 常に偽 (undef はキーになれない)
1832 like: 0 == 1
1833 Any HASH HASH キーが存在するか
1834 like: exists HASH->{Any}
1835
1836 =begin original
1837
1838 Right operand is CODE:
1839
1840 =end original
1841
1842 右被演算子がコードリファレンス:
1843
1844 =begin original
1845
1846 Left Right Description and pseudocode
1847 ===============================================================
1848 ARRAY CODE sub returns true on all ARRAY elements[1]
1849 like: !grep { !CODE->($_) } ARRAY
1850 HASH CODE sub returns true on all HASH keys[1]
1851 like: !grep { !CODE->($_) } keys HASH
1852 Any CODE sub passed Any returns true
1853 like: CODE->(Any)
1854
1855 =end original
1856
1857 左 右 説明と擬似コード
1858 ===============================================================
1859 ARRAY CODE 全ての ARRAY 要素に対してサブルーチンが真を返す [1]
1860 like: !grep { !CODE->($_) } ARRAY
1861 HASH CODE 全ての HASH キーに対してサブルーチンが真を返す [1]
1862 like: !grep { !CODE->($_) } keys HASH
1863 Any CODE サブルーチンに Any を渡して真を返す
1864 like: CODE->(Any)
1865
1866 =begin original
1867
1868 Right operand is a Regexp:
1869
1870 =end original
1871
1872 右被演算子が正規表現:
1873
1874 =begin original
1875
1876 Left Right Description and pseudocode
1877 ===============================================================
1878 ARRAY Regexp any ARRAY elements match Regexp
1879 like: grep { /Regexp/ } ARRAY
1880 HASH Regexp any HASH keys match Regexp
1881 like: grep { /Regexp/ } keys HASH
1882 Any Regexp pattern match
1883 like: Any =~ /Regexp/
1884
1885 =end original
1886
1887 左 右 説明と擬似コード
1888 ===============================================================
1889 ARRAY Regexp いずれかの ARRAY 要素が Regexp にマッチングするか
1890 like: grep { /Regexp/ } ARRAY
1891 HASH Regexp いずれかの HASH キーが Regexp にマッチングするか
1892 like: grep { /Regexp/ } keys HASH
1893 Any Regexp パターンマッチング
1894 like: Any =~ /Regexp/
1895
1896 =begin original
1897
1898 Other:
1899
1900 =end original
1901
1902 その他:
1903
1904 =begin original
1905
1906 Left Right Description and pseudocode
1907 ===============================================================
1908 Object Any invoke ~~ overloading on Object,
1909 or fall back to...
1910
1911 =end original
1912
1913 左 右 説明と擬似コード
1914 ===============================================================
1915 Object Any Object に対して ~~ のオーバーロードを起動、
1916 あるいは次にフォールバック…
1917
1918 =begin original
1919
1920 Any Num numeric equality
1921 like: Any == Num
1922 Num nummy[4] numeric equality
1923 like: Num == nummy
1924 undef Any check whether undefined
1925 like: !defined(Any)
1926 Any Any string equality
1927 like: Any eq Any
1928
1929 =end original
1930
1931 Any Num 数値の等価性
1932 like: Any == Num
1933 Num nummy[4] 数値の等価性
1934 like: Num == nummy
1935 undef Any 未定義かどうかを調べる
1936 like: !defined(Any)
1937 Any Any 文字列の等価性
1938 like: Any eq Any
1939
1940 =begin original
1941
1942 Notes:
1943
1944 =end original
1945
1946 注意:
1947
1948 =over
1949
1950 =item 1.
1951 Empty hashes or arrays match.
1952
1953 (1. 空ハッシュや配列はマッチングします。)
1954
1955 =item 2.
1956 That is, each element smartmatches the element of the same index in the other array.[3]
1957
1958 (2. つまり、それぞれの要素は他の配列の同じインデックスの要素とスマートマッチングします。[3])
1959
1960 =item 3.
1961 If a circular reference is found, fall back to referential equality.
1962
1963 (3. 循環参照が見つかると、参照の等価性にフォールバックします。)
1964
1965 =item 4.
1966 Either an actual number, or a string that looks like one.
1967
1968 (4. 実際の数値か、数値に見える文字列のどちらかです。)
1969
1970 =back
1971
1972 =begin original
1973
1974 The smartmatch implicitly dereferences any non-blessed hash or array
1975 reference, so the C<I<HASH>> and C<I<ARRAY>> entries apply in those cases.
1976 For blessed references, the C<I<Object>> entries apply. Smartmatches
1977 involving hashes only consider hash keys, never hash values.
1978
1979 =end original
1980
1981 スマートマッチングは bless されていないハッシュや配列のリファレンスを暗黙に
1982 デリファレンスするので、それらの場合では C<I<HASH>> と C<I<ARRAY>> の
1983 エントリが適用されます。
1984 bless されたリファレンスでは、C<I<Object>> エントリが適用されます。
1985 ハッシュに関連するスマートマッチングはキーのみを考慮し、ハッシュの値は
1986 考慮しません。
1987
1988 =begin original
1989
1990 The "like" code entry is not always an exact rendition. For example, the
1991 smartmatch operator short-circuits whenever possible, but C<grep> does
1992 not. Also, C<grep> in scalar context returns the number of matches, but
1993 C<~~> returns only true or false.
1994
1995 =end original
1996
1997 "like" コードエントリは常に正確な処理を行うわけではありません。
1998 例えば、スマートマッチング演算子は可能なら短絡しますが、C<grep> はしません。
1999 また、スカラコンテキストでは C<grep> はマッチングした数を返しますが、
2000 C<~~> は真か偽かのみを返します。
2001
2002 =begin original
2003
2004 Unlike most operators, the smartmatch operator knows to treat C<undef>
2005 specially:
2006
2007 =end original
2008
2009 ほとんどの演算子と異なり、スマートマッチング演算子は C<undef> を特別に
2010 扱う方法を知っています:
2011
2012 use v5.10.1;
2013 @array = (1, 2, 3, undef, 4, 5);
2014 say "some elements undefined" if undef ~~ @array;
2015
2016 =begin original
2017
2018 Each operand is considered in a modified scalar context, the modification
2019 being that array and hash variables are passed by reference to the
2020 operator, which implicitly dereferences them. Both elements
2021 of each pair are the same:
2022
2023 =end original
2024
2025 それぞれのオペランドは修正されたスカラコンテキストと考えられます;
2026 修正というのは、配列とハッシュの変数は演算子にリファレンスが渡され、
2027 暗黙にデリファレンスされます。
2028 それぞれの組のどちらの要素も同じです:
2029
2030 use v5.10.1;
2031
2032 my %hash = (red => 1, blue => 2, green => 3,
2033 orange => 4, yellow => 5, purple => 6,
2034 black => 7, grey => 8, white => 9);
2035
2036 my @array = qw(red blue green);
2037
2038 say "some array elements in hash keys" if @array ~~ %hash;
2039 say "some array elements in hash keys" if \@array ~~ \%hash;
2040
2041 say "red in array" if "red" ~~ @array;
2042 say "red in array" if "red" ~~ \@array;
2043
2044 say "some keys end in e" if /e$/ ~~ %hash;
2045 say "some keys end in e" if /e$/ ~~ \%hash;
2046
2047 =begin original
2048
2049 Two arrays smartmatch if each element in the first array smartmatches
2050 (that is, is "in") the corresponding element in the second array,
2051 recursively.
2052
2053 =end original
2054
2055 二つの配列は、最初の配列のそれぞれの要素が、二つ目の配列の対応する要素に
2056 (つまり "in") 再帰的にスマートマッチングするときに、スマートマッチングします。
2057
2058 use v5.10.1;
2059 my @little = qw(red blue green);
2060 my @bigger = ("red", "blue", [ "orange", "green" ] );
2061 if (@little ~~ @bigger) { # true!
2062 say "little is contained in bigger";
2063 }
2064
2065 =begin original
2066
2067 Because the smartmatch operator recurses on nested arrays, this
2068 will still report that "red" is in the array.
2069
2070 =end original
2071
2072 スマートマッチング演算子はネストした配列を再帰するので、これは "red" が
2073 配列にいると報告するままです。
2074
2075 use v5.10.1;
2076 my @array = qw(red blue green);
2077 my $nested_array = [[[[[[[ @array ]]]]]]];
2078 say "red in array" if "red" ~~ $nested_array;
2079
2080 =begin original
2081
2082 If two arrays smartmatch each other, then they are deep
2083 copies of each others' values, as this example reports:
2084
2085 =end original
2086
2087 二つの配列が互いにスマートマッチングすると、次の例が報告しているように、
2088 互いの値のディープコピーになります:
2089
2090 use v5.12.0;
2091 my @a = (0, 1, 2, [3, [4, 5], 6], 7);
2092 my @b = (0, 1, 2, [3, [4, 5], 6], 7);
2093
2094 if (@a ~~ @b && @b ~~ @a) {
2095 say "a and b are deep copies of each other";
2096 }
2097 elsif (@a ~~ @b) {
2098 say "a smartmatches in b";
2099 }
2100 elsif (@b ~~ @a) {
2101 say "b smartmatches in a";
2102 }
2103 else {
2104 say "a and b don't smartmatch each other at all";
2105 }
2106
2107 =begin original
2108
2109 If you were to set S<C<$b[3] = 4>>, then instead of reporting that "a and b
2110 are deep copies of each other", it now reports that C<"b smartmatches in a">.
2111 That's because the corresponding position in C<@a> contains an array that
2112 (eventually) has a 4 in it.
2113
2114 =end original
2115
2116 S<C<$b[3] = 4>> に設定すると、"a and b are deep copies of each other" と
2117 報告されるのではなく、C<"b smartmatches in a"> と報告されます。
2118 これは、C<@a> の対応する位置には(最終的に)中に 4 がある配列を
2119 含んでいるからです。
2120
2121 =begin original
2122
2123 Smartmatching one hash against another reports whether both contain the
2124 same keys, no more and no less. This could be used to see whether two
2125 records have the same field names, without caring what values those fields
2126 might have. For example:
2127
2128 =end original
2129
2130 あるハッシュを他のものとスマートマッチングすると、両方に同じキーが
2131 含まれているかどうかを報告し、それ以上でもそれ以下でもありません。
2132 これは、値を気にせずに、二つのレコードが同じフィールド名を持っているか
2133 どうかを見るのに使えるかもしれません。
2134 例えば:
2135
2136 use v5.10.1;
2137 sub make_dogtag {
2138 state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 };
2139
2140 my ($class, $init_fields) = @_;
2141
2142 die "Must supply (only) name, rank, and serial number"
2143 unless $init_fields ~~ $REQUIRED_FIELDS;
2144
2145 ...
2146 }
2147
2148 =begin original
2149
2150 However, this only does what you mean if C<$init_fields> is indeed a hash
2151 reference. The condition C<$init_fields ~~ $REQUIRED_FIELDS> also allows the
2152 strings C<"name">, C<"rank">, C<"serial_num"> as well as any array reference
2153 that contains C<"name"> or C<"rank"> or C<"serial_num"> anywhere to pass
2154 through.
2155
2156 =end original
2157
2158 しかし、これは C<$init_fields> が確かにハッシュリファレンスである場合にのみ
2159 考えている通りに動作します。
2160 C<$init_fields ~~ $REQUIRED_FIELDS> という条件は、文字列
2161 C<"name">, C<"rank">, C<"serial_num"> および、
2162 C<"name">, C<"rank">, C<"serial_num"> のいずれかを含むような
2163 配列リファレンスでも成立します。
2164
2165 =begin original
2166
2167 The smartmatch operator is most often used as the implicit operator of a
2168 C<when> clause. See the section on "Switch Statements" in L<perlsyn>.
2169
2170 =end original
2171
2172 スマートマッチング演算子は C<when> 節の暗黙の演算子としてもっともよく
2173 使われます。
2174 L<perlsyn> の "Switch Statements" の節を参照してください。
2175
2176 =head3 Smartmatching of Objects
2177
2178 (オブジェクトのスマートマッチング)
2179
2180 =begin original
2181
2182 To avoid relying on an object's underlying representation, if the
2183 smartmatch's right operand is an object that doesn't overload C<~~>,
2184 it raises the exception "C<Smartmatching a non-overloaded object
2185 breaks encapsulation>". That's because one has no business digging
2186 around to see whether something is "in" an object. These are all
2187 illegal on objects without a C<~~> overload:
2188
2189 =end original
2190
2191 オブジェクトの基となる表現に依存することを避けるために、スマートマッチングの
2192 右オペランドが C<~~> をオーバーロードしないオブジェクトなら、例外
2193 "C<Smartmatching a non-overloaded object breaks encapsulation>" が発生します。
2194 これは、何かがオブジェクトに「含まれている」かどうかを知るために
2195 調べる筋合いではないからです。
2196 次のものは C<~~> のオーバーロードがなければ全て不正です:
2197
2198 %hash ~~ $object
2199 42 ~~ $object
2200 "fred" ~~ $object
2201
2202 =begin original
2203
2204 However, you can change the way an object is smartmatched by overloading
2205 the C<~~> operator. This is allowed to
2206 extend the usual smartmatch semantics.
2207 For objects that do have an C<~~> overload, see L<overload>.
2208
2209 =end original
2210
2211 しかし、C<~~> 演算子をオーバーロードすることにオブジェクトが
2212 スマートマッチングする方法を変更できます。
2213 これにより通常のスマートマッチングの意味論を拡張できます。
2214 C<~~> のオーバーロードを持つオブジェクトについては、L<overload> を
2215 参照してください。
2216
2217 =begin original
2218
2219 Using an object as the left operand is allowed, although not very useful.
2220 Smartmatching rules take precedence over overloading, so even if the
2221 object in the left operand has smartmatch overloading, this will be
2222 ignored. A left operand that is a non-overloaded object falls back on a
2223 string or numeric comparison of whatever the C<ref> operator returns. That
2224 means that
2225
2226 =end original
2227
2228 左オペランドにオブジェクトを使うことは許されていますが、あまり
2229 有用ではありません。
2230 スマートマッチングの規則はオーバーロードより優先順位が高いので、例え
2231 左オペランドのオブジェクトがスマートマッチングのオーバーロードを
2232 持っていても、無視されます。
2233 左オペランドがオーバーロードされていないオブジェクトなら、C<ref> 演算子が
2234 返したものに従って、文字または数値比較にフォールバックします。
2235 これは、
2236
2237 $object ~~ X
2238
2239 =begin original
2240
2241 does I<not> invoke the overload method with C<I<X>> as an argument.
2242 Instead the above table is consulted as normal, and based on the type of
2243 C<I<X>>, overloading may or may not be invoked. For simple strings or
2244 numbers, "in" becomes equivalent to this:
2245
2246 =end original
2247
2248 は C<I<X>> を引数としてオーバーロードメソッドを起動 I<しない> ということです。
2249 通常通り前述の表を見て C<I<X>> の型に依存するのではなく、オーバーロードは
2250 起動されるかもしれませんし、されないかもしれません。
2251 単純な文字列や数値については、"in" は以下と等価になります:
2252
2253 $object ~~ $number ref($object) == $number
2254 $object ~~ $string ref($object) eq $string
2255
2256 =begin original
2257
2258 For example, this reports that the handle smells IOish
2259 (but please don't really do this!):
2260
2261 =end original
2262
2263 例えば、これは "handle smells IOish" と報告します (しかし実際にこれを
2264 しないでください!):
2265
2266 use IO::Handle;
2267 my $fh = IO::Handle->new();
2268 if ($fh ~~ /\bIO\b/) {
2269 say "handle smells IOish";
2270 }
2271
2272 =begin original
2273
2274 That's because it treats C<$fh> as a string like
2275 C<"IO::Handle=GLOB(0x8039e0)">, then pattern matches against that.
2276
2277 =end original
2278
2279 これは、C<$fh> を C<"IO::Handle=GLOB(0x8039e0)"> のような文字列として扱い、
2280 それからパターンはこれに対してマッチングするからです。
2281
2282 =head2 Bitwise And
2283 X<operator, bitwise, and> X<bitwise and> X<&>
2284
2285 (ビットごとの AND)
2286
2287 =begin original
2288
2289 Binary C<"&"> returns its operands ANDed together bit by bit. Although no
2290 warning is currently raised, the result is not well defined when this operation
2291 is performed on operands that aren't either numbers (see
2292 L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String Operators>).
2293
2294 =end original
2295
2296 二項演算子の C<"&"> は、両オペランドのビットごとに論理積をとって、
2297 その結果を返します。
2298 数値 (L</Integer Arithmetic>) 参照) でもビット文字列
2299 (L</Bitwise String Operators> 参照) でもないオペランドに対してこの演算を
2300 実行した場合、現在のところ警告は出ませんが、結果は未定義です。
2301
2302 =begin original
2303
2304 Note that C<"&"> has lower priority than relational operators, so for example
2305 the parentheses are essential in a test like
2306
2307 =end original
2308
2309 C<"&"> は関係演算子より優先順位が低いので、例えば以下のようなテストでは、
2310 かっこが重要です:
2311
2312 print "Even\n" if ($x & 1) == 0;
2313
2314 =begin original
2315
2316 If the "bitwise" feature is enabled via S<C<use feature 'bitwise'>> or
2317 C<use v5.28>, then this operator always treats its operands as numbers.
2318 Before Perl 5.28 this feature produced a warning in the
2319 C<"experimental::bitwise"> category.
2320
2321 =end original
2322
2323 S<C<use feature 'bitwise'>> か C<use v5.28> によって "bitwise" 機能が
2324 有効になっている場合、この演算子はオペランドを常に数値として扱います。
2325 Perl 5.28 より前ではこの機能は C<"experimental::bitwise"> カテゴリの
2326 警告が出力されていました。
2327
2328 =head2 Bitwise Or and Exclusive Or
2329 X<operator, bitwise, or> X<bitwise or> X<|> X<operator, bitwise, xor>
2330 X<bitwise xor> X<^>
2331
2332 (ビットごとの OR と XOR)
2333
2334 =begin original
2335
2336 Binary C<"|"> returns its operands ORed together bit by bit.
2337
2338 =end original
2339
2340 二項演算子の C<"|"> は、両オペランドのビットごとに論理和をとって、
2341 その結果を返します。
2342
2343 =begin original
2344
2345 Binary C<"^"> returns its operands XORed together bit by bit.
2346
2347 =end original
2348
2349 二項演算子の C<"^"> は、両オペランドのビットごとに排他論理和をとって、
2350 その結果を返します。
2351
2352 =begin original
2353
2354 Although no warning is currently raised, the results are not well
2355 defined when these operations are performed on operands that aren't either
2356 numbers (see L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String
2357 Operators>).
2358
2359 =end original
2360
2361 数値 (L</Integer Arithmetic>) 参照) でもビット文字列
2362 (L</Bitwise String Operators> 参照) でもないオペランドに対してこれらの演算を
2363 実行した場合、現在のところ警告は出ませんが、結果は未定義です。
2364
2365 =begin original
2366
2367 Note that C<"|"> and C<"^"> have lower priority than relational operators, so
2368 for example the parentheses are essential in a test like
2369
2370 =end original
2371
2372 C<"|"> と C<"^"> は関係演算子より優先順位が低いので、例えば以下のような
2373 テストでは、かっこが重要です:
2374
2375 print "false\n" if (8 | 2) != 10;
2376
2377 =begin original
2378
2379 If the "bitwise" feature is enabled via S<C<use feature 'bitwise'>> or
2380 C<use v5.28>, then this operator always treats its operands as numbers.
2381 Before Perl 5.28. this feature produced a warning in the
2382 C<"experimental::bitwise"> category.
2383
2384 =end original
2385
2386 S<C<use feature 'bitwise'>> か C<use v5.28> によって "bitwise" 機能が
2387 有効になっている場合、この演算子はオペランドを常に数値として扱います。
2388 Perl 5.28 より前ではこの機能は C<"experimental::bitwise"> カテゴリの
2389 警告が出力されていました。
2390
2391 =head2 C-style Logical And
2392 X<&&> X<logical and> X<operator, logical, and>
2393
2394 (C スタイルの論理積)
2395
2396 =begin original
2397
2398 Binary C<"&&"> performs a short-circuit logical AND operation. That is,
2399 if the left operand is false, the right operand is not even evaluated.
2400 Scalar or list context propagates down to the right operand if it
2401 is evaluated.
2402
2403 =end original
2404
2405 二項演算子の C<"&&"> は、短絡の論理積演算を行ないます。
2406 つまり、左被演算子が偽であれば、右被演算子は評価さえ
2407 行なわれないということです。
2408 評価される場合には、スカラーかリストかというコンテキストは、
2409 右被演算子にも及びます。
2410
2411 =head2 C-style Logical Or
2412 X<||> X<operator, logical, or>
2413
2414 (C スタイルの論理和)
2415
2416 =begin original
2417
2418 Binary C<"||"> performs a short-circuit logical OR operation. That is,
2419 if the left operand is true, the right operand is not even evaluated.
2420 Scalar or list context propagates down to the right operand if it
2421 is evaluated.
2422
2423 =end original
2424
2425 二項演算子の C<"||"> は、短絡の論理和演算を行ないます。
2426 つまり、左被演算子が真であれば、右被演算子は評価さえ
2427 行なわれないということです。
2428 評価される場合には、スカラーかリストかというコンテキストは、
2429 右被演算子にも及びます。
2430
2431 =head2 Logical Defined-Or
2432 X<//> X<operator, logical, defined-or>
2433
2434 (論理定義性和)
2435
2436 =begin original
2437
2438 Although it has no direct equivalent in C, Perl's C<//> operator is related
2439 to its C-style "or". In fact, it's exactly the same as C<||>, except that it
2440 tests the left hand side's definedness instead of its truth. Thus,
2441 S<C<< EXPR1 // EXPR2 >>> returns the value of C<< EXPR1 >> if it's defined,
2442 otherwise, the value of C<< EXPR2 >> is returned.
2443 (C<< EXPR1 >> is evaluated in scalar context, C<< EXPR2 >>
2444 in the context of C<< // >> itself). Usually,
2445 this is the same result as S<C<< defined(EXPR1) ? EXPR1 : EXPR2 >>> (except that
2446 the ternary-operator form can be used as a lvalue, while S<C<< EXPR1 // EXPR2 >>>
2447 cannot). This is very useful for
2448 providing default values for variables. If you actually want to test if
2449 at least one of C<$x> and C<$y> is defined, use S<C<defined($x // $y)>>.
2450
2451 =end original
2452
2453 C では直接等価なものはありませんが、Perl の C<//> 演算子は C スタイル
2454 論理和に関連しています。
2455 実際、左辺の真偽ではなく定義されているかを判定することを除けば
2456 C<||> と同じです。
2457 従って S<C<< EXPR1 // EXPR2 >>> は、C<< EXPR1 >> が定義されていればその値を
2458 返し、さもなければ、C<< EXPR2 >> の値を返します。
2459 (C<< EXPR1 >> はスカラコンテキストで、C<< EXPR2 >> は C<< // >> 自身の
2460 コンテキストで評価されます。)
2461 普通はこれは S<C<< defined(EXPR1) ? EXPR1 : EXPR2 >>> と同じ結果になり
2462 完全に等価です (例外は 3 項演算子形式は左辺値として使えますが、
2463 S<C<< EXPR1 // EXPR2 >>> は使えません)。
2464 これは、変数に対するデフォルト値を設定するのにとても有用です。
2465 実際に、C<$x> と C<$y> の少なくとも片方が定義されているかを判定したいなら、
2466 S<C<defined($x // $y)>> を使ってください。
2467
2468 =begin original
2469
2470 The C<||>, C<//> and C<&&> operators return the last value evaluated
2471 (unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably
2472 portable way to find out the home directory might be:
2473
2474 =end original
2475
2476 C<||>, C<//>, C<&&> 演算子は、(C のように 単に 0 や 1 を返すのではなく)
2477 最後に評価された値を返します。
2478 これにより、かなり一般的に使えるホームディレクトリを探す方法は:
2479
2480 $home = $ENV{HOME}
2481 // $ENV{LOGDIR}
2482 // (getpwuid($<))[7]
2483 // die "You're homeless!\n";
2484
2485 =begin original
2486
2487 In particular, this means that you shouldn't use this
2488 for selecting between two aggregates for assignment:
2489
2490 =end original
2491
2492 特に、これは代入のために二つの集合を選択するためには
2493 使うべきではないことを意味します。
2494
2495 @a = @b || @c; # This doesn't do the right thing
2496 @a = scalar(@b) || @c; # because it really means this.
2497 @a = @b ? @b : @c; # This works fine, though.
2498
2499 =begin original
2500
2501 As alternatives to C<&&> and C<||> when used for
2502 control flow, Perl provides the C<and> and C<or> operators (see below).
2503 The short-circuit behavior is identical. The precedence of C<"and">
2504 and C<"or"> is much lower, however, so that you can safely use them after a
2505 list operator without the need for parentheses:
2506
2507 =end original
2508
2509 Perl では、フロー制御に使う場合の C<&&> と C<||> の同義語として、
2510 C<and> 演算子と C<or> 演算子が用意されています (下記参照)。
2511 短絡の動作は全く同じです。
2512 しかし、C<"and"> と C<"or"> の優先順位はかなり低くしてあるので、引数に括弧を
2513 使っていないリスト演算子のあとに続けて使う場合にも、
2514 安心して使うことができます:
2515
2516 unlink "alpha", "beta", "gamma"
2517 or gripe(), next LINE;
2518
2519 =begin original
2520
2521 With the C-style operators that would have been written like this:
2522
2523 =end original
2524
2525 C スタイルの演算子では以下のように書く必要があります。
2526
2527 unlink("alpha", "beta", "gamma")
2528 || (gripe(), next LINE);
2529
2530 =begin original
2531
2532 It would be even more readable to write that this way:
2533
2534 =end original
2535
2536 次のようにして書き込みをより読みやすくすることもできます:
2537
2538 unless(unlink("alpha", "beta", "gamma")) {
2539 gripe();
2540 next LINE;
2541 }
2542
2543 =begin original
2544
2545 Using C<"or"> for assignment is unlikely to do what you want; see below.
2546
2547 =end original
2548
2549 代入で C<"or"> を使うと、したいことと違うことになります; 以下を
2550 参照して下さい。
2551
2552 =head2 Range Operators
2553 X<operator, range> X<range> X<..> X<...>
2554
2555 (範囲演算子)
2556
2557 =begin original
2558
2559 Binary C<".."> is the range operator, which is really two different
2560 operators depending on the context. In list context, it returns a
2561 list of values counting (up by ones) from the left value to the right
2562 value. If the left value is greater than the right value then it
2563 returns the empty list. The range operator is useful for writing
2564 S<C<foreach (1..10)>> loops and for doing slice operations on arrays. In
2565 the current implementation, no temporary array is created when the
2566 range operator is used as the expression in C<foreach> loops, but older
2567 versions of Perl might burn a lot of memory when you write something
2568 like this:
2569
2570 =end original
2571
2572 二項演算子の C<".."> は範囲演算子で、使われるコンテキストによって
2573 異なる動作をする 2 つの演算子を合わせたものです。
2574 リストコンテキストでは、左の値から右の値まで (1 づつ昇順で) 数えあげた値から
2575 なるリストを返します。
2576 左側の値が右側の値より大きい場合は、空リストを返します。
2577 範囲演算子は、S<C<foreach (1..10)>> のようなループを書くときや、
2578 配列のスライス演算を行なうときに便利です。
2579 現状の実装では、C<foreach> ループの式の中で範囲演算子を使っても
2580 一時配列は作りませんが、古い Perl は以下のようなことを書くと、
2581 大量のメモリを消費することになります:
2582
2583 for (1 .. 1_000_000) {
2584 # code
2585 }
2586
2587 =begin original
2588
2589 The range operator also works on strings, using the magical
2590 auto-increment, see below.
2591
2592 =end original
2593
2594 範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します;
2595 以下を参照してください。
2596
2597 =begin original
2598
2599 In scalar context, C<".."> returns a boolean value. The operator is
2600 bistable, like a flip-flop, and emulates the line-range (comma)
2601 operator of B<sed>, B<awk>, and various editors. Each C<".."> operator
2602 maintains its own boolean state, even across calls to a subroutine
2603 that contains it. It is false as long as its left operand is false.
2604 Once the left operand is true, the range operator stays true until the
2605 right operand is true, I<AFTER> which the range operator becomes false
2606 again. It doesn't become false till the next time the range operator
2607 is evaluated. It can test the right operand and become false on the
2608 same evaluation it became true (as in B<awk>), but it still returns
2609 true once. If you don't want it to test the right operand until the
2610 next evaluation, as in B<sed>, just use three dots (C<"...">) instead of
2611 two. In all other regards, C<"..."> behaves just like C<".."> does.
2612
2613 =end original
2614
2615 スカラコンテキストで使われたときには、C<".."> は真偽値を返します。
2616 この演算子は、フリップフロップのように 2 値安定で、
2617 B<sed> や B<awk> や多くのエディタでの行範囲 (コンマ) 演算子を
2618 エミュレートするものとなります。
2619 各々の C<".."> 演算子は、例えそれを含むサブルーチンの呼び出しを
2620 またいでも、それぞれに独立して自分の真偽状態を管理します。
2621 はじめは、左被演算子が偽である間、演算全体も偽となっています。
2622 いったん左被演算子が真になると、範囲演算子は、右被演算子が真である間、
2623 真を返すようになります; 範囲演算子が再び偽になった I<後> です。
2624 次に範囲演算子が評価されるまでは、偽とはなりません。
2625 (B<awk> でのように) 真となった、その評価の中で右被演算子をテストし、
2626 偽とすることができますが、1 度は真を返すことになります。
2627 B<sed> でのように、次に評価されるまで右被演算子をテストしたくなければ、
2628 2 個のドットの代わりに 3 つのドット (C<"...">) を使ってください。
2629 その他の点では、C<"..."> は C<".."> と同様に振舞います.
2630
2631 =begin original
2632
2633 The right operand is not evaluated while the operator is in the
2634 "false" state, and the left operand is not evaluated while the
2635 operator is in the "true" state. The precedence is a little lower
2636 than || and &&. The value returned is either the empty string for
2637 false, or a sequence number (beginning with 1) for true. The sequence
2638 number is reset for each range encountered. The final sequence number
2639 in a range has the string C<"E0"> appended to it, which doesn't affect
2640 its numeric value, but gives you something to search for if you want
2641 to exclude the endpoint. You can exclude the beginning point by
2642 waiting for the sequence number to be greater than 1.
2643
2644 =end original
2645
2646 右被演算子は、演算子の状態が「偽」である間は評価されることがなく、
2647 左被演算子は、演算子の状態が「真」である間は評価されることがありません。
2648 優先順位は、|| と && の少し下です。
2649 偽としては空文字列が返され、
2650 真としては (1 から始まる) 通し番号が返されます。
2651 この通し番号は、新たに範囲が始まるごとにリセットされます。
2652 範囲の最後の通し番号には、文字列 C<"E0"> が末尾につけられます; これは、
2653 数値としては何の影響もありませんが、範囲の終わりで何か特別なことをしたい
2654 場合に、目印として使うことができます。
2655 範囲の始まりを除きたい場合には、通し番号が 1 よりも大きくなるのを
2656 待てばよいでしょう。
2657
2658 =begin original
2659
2660 If either operand of scalar C<".."> is a constant expression,
2661 that operand is considered true if it is equal (C<==>) to the current
2662 input line number (the C<$.> variable).
2663
2664 =end original
2665
2666 スカラの C<".."> の被演算子が定数表現であるときは、その被演算子は暗黙に、
2667 現在の入力行番号(変数 C<$.>) と等しい(C<==>) 場合に真となります。
2668
2669 =begin original
2670
2671 To be pedantic, the comparison is actually S<C<int(EXPR) == int(EXPR)>>,
2672 but that is only an issue if you use a floating point expression; when
2673 implicitly using C<$.> as described in the previous paragraph, the
2674 comparison is S<C<int(EXPR) == int($.)>> which is only an issue when C<$.>
2675 is set to a floating point value and you are not reading from a file.
2676 Furthermore, S<C<"span" .. "spat">> or S<C<2.18 .. 3.14>> will not do what
2677 you want in scalar context because each of the operands are evaluated
2678 using their integer representation.
2679
2680 =end original
2681
2682 とても細かい話をすると、比較は実際には S<C<int(EXPR) == int(EXPR)>> ですが、
2683 これは浮動小数点数を使うときにだけ問題になります; 前の段落で記述したように
2684 明示的に C<$.> を使ったとき、比較は S<C<int(EXPR) == int($.)>> となり、
2685 C<$.> に浮動小数点数がセットされ、ファイルから読み込みを行わない場合にのみ
2686 問題になります。
2687 さらに、S<C<"span" .. "spat">> や S<C<2.18 .. 3.14>> は、それぞれの
2688 オペランドが整数表現を使って評価されるため、スカラコンテキストでは
2689 望みどおりの結果になりません。
2690
2691 =begin original
2692
2693 Examples:
2694
2695 =end original
2696
2697 例:
2698
2699 =begin original
2700
2701 As a scalar operator:
2702
2703 =end original
2704
2705 スカラ演算子として:
2706
2707 if (101 .. 200) { print; } # print 2nd hundred lines, short for
2708 # if ($. == 101 .. $. == 200) { print; }
2709
2710 next LINE if (1 .. /^$/); # skip header lines, short for
2711 # next LINE if ($. == 1 .. /^$/);
2712 # (typically in a loop labeled LINE)
2713
2714 s/^/> / if (/^$/ .. eof()); # quote body
2715
2716 # parse mail messages
2717 while (<>) {
2718 $in_header = 1 .. /^$/;
2719 $in_body = /^$/ .. eof;
2720 if ($in_header) {
2721 # do something
2722 } else { # in body
2723 # do something else
2724 }
2725 } continue {
2726 close ARGV if eof; # reset $. each file
2727 }
2728
2729 =begin original
2730
2731 Here's a simple example to illustrate the difference between
2732 the two range operators:
2733
2734 =end original
2735
2736 以下は二つの範囲演算子の違いを示す単純な例です:
2737
2738 @lines = (" - Foo",
2739 "01 - Bar",
2740 "1 - Baz",
2741 " - Quux");
2742
2743 foreach (@lines) {
2744 if (/0/ .. /1/) {
2745 print "$_\n";
2746 }
2747 }
2748
2749 =begin original
2750
2751 This program will print only the line containing "Bar". If
2752 the range operator is changed to C<...>, it will also print the
2753 "Baz" line.
2754
2755 =end original
2756
2757 このプログラムは "Bar" を含む行だけを表示します。
2758 範囲演算子を C<...> に変更すると、"Baz" の行も表示します。
2759
2760 =begin original
2761
2762 And now some examples as a list operator:
2763
2764 =end original
2765
2766 これはリスト演算子の例です:
2767
2768 =begin original
2769
2770 for (101 .. 200) { print } # print $_ 100 times
2771 @foo = @foo[0 .. $#foo]; # an expensive no-op
2772 @foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
2773
2774 =end original
2775
2776 for (101 .. 200) { print } # $_ を 100 回出力
2777 @foo = @foo[0 .. $#foo]; # 高価な no-op
2778 @foo = @foo[$#foo-4 .. $#foo]; # 末尾の 5 アイテムをスライス
2779
2780 =begin original
2781
2782 Because each operand is evaluated in integer form, S<C<2.18 .. 3.14>> will
2783 return two elements in list context.
2784
2785 =end original
2786
2787 それぞれのオペランドは整数の形で評価されるので、S<C<2.18 .. 3.14>> は
2788 リストコンテキストでは二つの要素を返します。
2789
2790 =begin original
2791
2792 @list = (2.18 .. 3.14); # same as @list = (2 .. 3);
2793
2794 =end original
2795
2796 @list = (2.18 .. 3.14); # @list = (2 .. 3); と同じ
2797
2798 =begin original
2799
2800 The range operator in list context can make use of the magical
2801 auto-increment algorithm if both operands are strings, subject to the
2802 following rules:
2803
2804 =end original
2805
2806 リストコンテキストでの範囲演算子は、両方のオペランドが
2807 文字列であるときには、次の規則に従って、マジカルインクリメントの
2808 機能が使えます。
2809
2810 =over
2811
2812 =item *
2813
2814 =begin original
2815
2816 With one exception (below), if both strings look like numbers to Perl,
2817 the magic increment will not be applied, and the strings will be treated
2818 as numbers (more specifically, integers) instead.
2819
2820 =end original
2821
2822 一つの例外(後述)を除いて、
2823 両側の文字列が Perl にとって数値に見える場合、
2824 マジカルインクリメントは適用されず、代わりにその文字列は数値として
2825 (より正確には、整数として)扱われます。
2826
2827 =begin original
2828
2829 For example, C<"-2".."2"> is the same as C<-2..2>, and
2830 C<"2.18".."3.14"> produces C<2, 3>.
2831
2832 =end original
2833
2834 例えば、C<"-2".."2"> は C<-2..2> と同じで、
2835 C<"2.18".."3.14"> は C<2, 3> を生成します。
2836
2837 =item *
2838
2839 =begin original
2840
2841 The exception to the above rule is when the left-hand string begins with
2842 C<0> and is longer than one character, in this case the magic increment
2843 I<will> be applied, even though strings like C<"01"> would normally look
2844 like a number to Perl.
2845
2846 =end original
2847
2848 前述の規則の例外として、
2849 左側の文字列が C<0> で始まっていて 2 文字以上の場合、
2850 C<"01"> のような文字列は通常 Perl にとって数値に見えるにも関わらず、
2851 この場合はマジカルインクリメントは I<適用されます> 。
2852
2853 =begin original
2854
2855 For example, C<"01".."04"> produces C<"01", "02", "03", "04">, and
2856 C<"00".."-1"> produces C<"00"> through C<"99"> - this may seem
2857 surprising, but see the following rules for why it works this way.
2858 To get dates with leading zeros, you can say:
2859
2860 =end original
2861
2862 例えば、C<"01".."04"> は C<"01", "02", "03", "04"> を生成し、
2863 C<"00".."-1"> は C<"00"> から C<"99"> を生成します - これは驚きがあるかも
2864 しれませんが、なぜこれがこのように動作するのかについては
2865 次の規則を見てください。
2866 先頭に 0 があるものから日付を取り出すために、次のようにできます:
2867
2868 @z2 = ("01" .. "31");
2869 print $z2[$mday];
2870
2871 =begin original
2872
2873 If you want to force strings to be interpreted as numbers, you could say
2874
2875 =end original
2876
2877 文字列が数値として解釈することを強制したいなら、次のようにできます:
2878
2879 @numbers = ( 0+$first .. 0+$last );
2880
2881 =begin original
2882
2883 B<Note:> In Perl versions 5.30 and below, I<any> string on the left-hand
2884 side beginning with C<"0">, including the string C<"0"> itself, would
2885 cause the magic string increment behavior. This means that on these Perl
2886 versions, C<"0".."-1"> would produce C<"0"> through C<"99">, which was
2887 inconsistent with C<0..-1>, which produces the empty list. This also means
2888 that C<"0".."9"> now produces a list of integers instead of a list of
2889 strings.
2890
2891 =end original
2892
2893 B<注意:> Perl バージョン 5.30 以下では、左側が C<"0"> で始まる
2894 I<任意の> 文字列 (文字列 C<"0"> 自体を含む)は、マジカル文字列
2895 インクリメントを引き起こします。
2896 つまり、これらの Perl バージョンでは、C<"0".."-1"> は C<"0"> から
2897 C<"99"> までを生成しますが、これは空のリストを生成する C<0..-1> と
2898 矛盾していました。
2899 また、C<"0"."9"> では、文字列のリストではなく整数のリストが
2900 生成されるということです。
2901
2902 =item *
2903
2904 =begin original
2905
2906 If the initial value specified isn't part of a magical increment
2907 sequence (that is, a non-empty string matching C</^[a-zA-Z]*[0-9]*\z/>),
2908 only the initial value will be returned.
2909
2910 =end original
2911
2912 指定された初期値がマジカルインクリメント処理の一部でない場合
2913 (つまり、C</^[a-zA-Z]*[0-9]*\z/> にマッチングする、空でない文字列の場合)、
2914 初期値のみが返されます。
2915
2916 =begin original
2917
2918 For example, C<"ax".."az"> produces C<"ax", "ay", "az">, but
2919 C<"*x".."az"> produces only C<"*x">.
2920
2921 =end original
2922
2923 例えば、C<"ax".."az"> は C<"ax", "ay", "az"> を生成しますが、
2924 C<"*x".."az"> は C<"*x"> だけを生成します。
2925
2926 =item *
2927
2928 =begin original
2929
2930 For other initial values that are strings that do follow the rules of the
2931 magical increment, the corresponding sequence will be returned.
2932
2933 =end original
2934
2935 マジカルインクリメントの規則に従う文字列がもう片方の初期値の場合、
2936 対応する並びが返されます。
2937
2938 =begin original
2939
2940 For example, you can say
2941
2942 =end original
2943
2944 例えば以下のように書くと:
2945
2946 @alphabet = ("A" .. "Z");
2947
2948 =begin original
2949
2950 to get all normal letters of the English alphabet, or
2951
2952 =end original
2953
2954 英語の大文字すべてを得られますし:
2955
2956 $hexdigit = (0 .. 9, "a" .. "f")[$num & 15];
2957
2958 =begin original
2959
2960 to get a hexadecimal digit.
2961
2962 =end original
2963
2964 と書けば、16 進の数字が得られます。
2965
2966 =item *
2967
2968 =begin original
2969
2970 If the final value specified is not in the sequence that the magical
2971 increment would produce, the sequence goes until the next value would
2972 be longer than the final value specified. If the length of the final
2973 string is shorter than the first, the empty list is returned.
2974
2975
2976 =end original
2977
2978 マジカルインクリメントによって得られる値の中に指定した最終値に
2979 ちょうど一致するものが見つからないような場合には、
2980 マジカルインクリメントによって得られる次の値の文字列長が、
2981 最終値として指定した値のものより長くなるまでインクリメントが続けられます。
2982 最終値の文字列の長さが最初のものより短い場合、
2983 空リストが返されます。
2984
2985 =begin original
2986
2987 For example, C<"a".."--"> is the same as C<"a".."zz">, C<"0".."xx">
2988 produces C<"0"> through C<"99">, and C<"aaa".."--"> returns the empty
2989 list.
2990
2991 =end original
2992
2993 例えば、C<"a".."--"> は C<"a".."zz"> と同じで、
2994 C<"0".."xx"> は C<"0"> から C<"99"> を出力し、
2995 C<"aaa".."--"> は空リストを返します。
2996
2997 =back
2998
2999 =begin original
3000
3001 As of Perl 5.26, the list-context range operator on strings works as expected
3002 in the scope of L<< S<C<"use feature 'unicode_strings">>|feature/The
3003 'unicode_strings' feature >>. In previous versions, and outside the scope of
3004 that feature, it exhibits L<perlunicode/The "Unicode Bug">: its behavior
3005 depends on the internal encoding of the range endpoint.
3006
3007 =end original
3008
3009 Perl 5.26 から、文字列に対するリストコンテキスト範囲演算子は、
3010 L<< S<C<"use feature 'unicode_strings">>|feature/The
3011 'unicode_strings' feature >> のスコープの中で想定通りに
3012 動作するようになりました。
3013 以前のバージョン、およびこの機能のスコープの外側では、
3014 これは L<perlunicode/The "Unicode Bug"> を起こしていました:
3015 その振る舞いは範囲の両端の内部エンコーディングに依存していました。
3016
3017 =begin original
3018
3019 Because the magical increment only works on non-empty strings matching
3020 C</^[a-zA-Z]*[0-9]*\z/>, the following will only return an alpha:
3021
3022 =end original
3023
3024 マジカルインクリメントは C</^[a-zA-Z]*[0-9]*\z/> にマッチングする
3025 空でない文字列でのみ動作するので、
3026 以下はαのみを返します:
3027
3028 use charnames "greek";
3029 my @greek_small = ("\N{alpha}" .. "\N{omega}");
3030
3031 =begin original
3032
3033 To get the 25 traditional lowercase Greek letters, including both sigmas,
3034 you could use this instead:
3035
3036 =end original
3037
3038 両方のシグマを含む、25 文字の伝統的な小文字のギリシャ文字を得るためには、
3039 代わりに以下のようにしてください:
3040
3041 use charnames "greek";
3042 my @greek_small = map { chr } ( ord("\N{alpha}")
3043 ..
3044 ord("\N{omega}")
3045 );
3046
3047 =begin original
3048
3049 However, because there are I<many> other lowercase Greek characters than
3050 just those, to match lowercase Greek characters in a regular expression,
3051 you could use the pattern C</(?:(?=\p{Greek})\p{Lower})+/> (or the
3052 L<experimental feature|perlrecharclass/Extended Bracketed Character
3053 Classes> C<S</(?[ \p{Greek} & \p{Lower} ])+/>>).
3054
3055 =end original
3056
3057 しかし、小文字のギリシャ文字はここに書いたものよりも I<たくさん> あるので、
3058 正規表現で小文字のギリシャ文字にマッチングさせるためには、
3059 C</(?:(?=\p{Greek})\p{Lower})+/> というパターン (または
3060 L<実験的機能|perlrecharclass/Extended Bracketed Character Classes> である
3061 C<S</(?[ \p{Greek} & \p{Lower} ])+/>>) を使います。
3062
3063 =head2 Conditional Operator
3064 X<operator, conditional> X<operator, ternary> X<ternary> X<?:>
3065
3066 (条件演算子)
3067
3068 =begin original
3069
3070 Ternary C<"?:"> is the conditional operator, just as in C. It works much
3071 like an if-then-else. If the argument before the C<?> is true, the
3072 argument before the C<:> is returned, otherwise the argument after the
3073 C<:> is returned. For example:
3074
3075 =end original
3076
3077 三項演算子の C<"?:"> は、C の場合と同じ条件演算子です。
3078 これは、if-then-else のように働きます。
3079 C<"?"> の前の引数が真であれば C<":"> の前の引数が返されますが、
3080 真でなければ、C<":"> の後の引数が返されます。
3081 例えば:
3082
3083 printf "I have %d dog%s.\n", $n,
3084 ($n == 1) ? "" : "s";
3085
3086 =begin original
3087
3088 Scalar or list context propagates downward into the 2nd
3089 or 3rd argument, whichever is selected.
3090
3091 =end original
3092
3093 スカラコンテキストかリストコンテキストかという状況は、
3094 選択された 2 番目もしくは 3 番目の引数にまで伝わります。
3095
3096 =begin original
3097
3098 $x = $ok ? $y : $z; # get a scalar
3099 @x = $ok ? @y : @z; # get an array
3100 $x = $ok ? @y : @z; # oops, that's just a count!
3101
3102 =end original
3103
3104 $x = $ok ? $y : $z; # スカラを取る
3105 @x = $ok ? @y : @z; # 配列を取る
3106 $x = $ok ? @y : @z; # うわ、これは単なる数です!
3107
3108 =begin original
3109
3110 The operator may be assigned to if both the 2nd and 3rd arguments are
3111 legal lvalues (meaning that you can assign to them):
3112
3113 =end original
3114
3115 2 番目と 3 番目の引数双方が左辺値 (代入可能ということ)であれば、
3116 この演算子に代入を行なうこともできます:
3117
3118 ($x_or_y ? $x : $y) = $z;
3119
3120 =begin original
3121
3122 Because this operator produces an assignable result, using assignments
3123 without parentheses will get you in trouble. For example, this:
3124
3125 =end original
3126
3127 この演算子は代入可能な結果を生み出すので、
3128 括弧なしで代入を行うとおかしくなるかもしれません。
3129 例えばこれは:
3130
3131 $x % 2 ? $x += 10 : $x += 2
3132
3133 =begin original
3134
3135 Really means this:
3136
3137 =end original
3138
3139 以下を意味し:
3140
3141 (($x % 2) ? ($x += 10) : $x) += 2
3142
3143 =begin original
3144
3145 Rather than this:
3146
3147 =end original
3148
3149 以下のようにはなりません:
3150
3151 ($x % 2) ? ($x += 10) : ($x += 2)
3152
3153 =begin original
3154
3155 That should probably be written more simply as:
3156
3157 =end original
3158
3159 恐らく以下のようにもっと単純に書くべきでしょう:
3160
3161 $x += ($x % 2) ? 10 : 2;
3162
3163 =head2 Assignment Operators
3164 X<assignment> X<operator, assignment> X<=> X<**=> X<+=> X<*=> X<&=>
3165 X<<< <<= >>> X<&&=> X<-=> X</=> X<|=> X<<< >>= >>> X<||=> X<//=> X<.=>
3166 X<%=> X<^=> X<x=> X<&.=> X<|.=> X<^.=>
3167
3168 (代入演算子)
3169
3170 =begin original
3171
3172 C<"="> is the ordinary assignment operator.
3173
3174 =end original
3175
3176 C<"="> は通常の代入演算子です。
3177
3178 =begin original
3179
3180 Assignment operators work as in C. That is,
3181
3182 =end original
3183
3184 代入演算子は C の場合と同様の働きをします。
3185 つまり、
3186
3187 $x += 2;
3188
3189 =begin original
3190
3191 is equivalent to
3192
3193 =end original
3194
3195 は以下と等価です:
3196
3197 $x = $x + 2;
3198
3199 =begin original
3200
3201 although without duplicating any side effects that dereferencing the lvalue
3202 might trigger, such as from C<tie()>. Other assignment operators work similarly.
3203 The following are recognized:
3204
3205 =end original
3206
3207 しかし、C<tie()> のようなもので起こる左辺値の被参照による
3208 副作用が 2 回起こることはありません。
3209 他の代入演算も同様に働きます。
3210 以下のものが認識されます:
3211
3212 **= += *= &= &.= <<= &&=
3213 -= /= |= |.= >>= ||=
3214 .= %= ^= ^.= //=
3215 x=
3216
3217 =begin original
3218
3219 Although these are grouped by family, they all have the precedence
3220 of assignment. These combined assignment operators can only operate on
3221 scalars, whereas the ordinary assignment operator can assign to arrays,
3222 hashes, lists and even references. (See L<"Context"|perldata/Context>
3223 and L<perldata/List value constructors>, and L<perlref/Assigning to
3224 References>.)
3225
3226 =end original
3227
3228 グループ分けしてありますが、これらはいずれも代入演算子として
3229 同じ優先順位となっています。
3230 これらの複合代入演算子はスカラとしてのみ動作しますが、一方
3231 通常の代入演算子は配列、スカラ、リスト、リファレンスに代入できます。
3232 (L<"Context"|perldata/Context>, L<perldata/List value constructors>,
3233 L<perlref/Assigning to References> を参照してください。)
3234
3235 =begin original
3236
3237 Unlike in C, the scalar assignment operator produces a valid lvalue.
3238 Modifying an assignment is equivalent to doing the assignment and
3239 then modifying the variable that was assigned to. This is useful
3240 for modifying a copy of something, like this:
3241
3242 =end original
3243
3244 C と違って、スカラ代入演算子は有効な左辺値を作り出します。
3245 代入を修正することは、代入を行なってから、その代入された変数を修正するのと
3246 同じことになります。
3247 これは、以下のように何かのコピーを変更したいときに便利です:
3248
3249 ($tmp = $global) =~ tr/13579/24680/;
3250
3251 =begin original
3252
3253 Although as of 5.14, that can be also be accomplished this way:
3254
3255 =end original
3256
3257 しかし 5.14 現在、これは次のようにしてもできるようになりました:
3258
3259 use v5.14;
3260 $tmp = ($global =~ tr/13579/24680/r);
3261
3262 =begin original
3263
3264 Likewise,
3265
3266 =end original
3267
3268 同様に、
3269
3270 ($x += 2) *= 3;
3271
3272 =begin original
3273
3274 is equivalent to
3275
3276 =end original
3277
3278 は以下と等価です:
3279
3280 $x += 2;
3281 $x *= 3;
3282
3283 =begin original
3284
3285 Similarly, a list assignment in list context produces the list of
3286 lvalues assigned to, and a list assignment in scalar context returns
3287 the number of elements produced by the expression on the right hand
3288 side of the assignment.
3289
3290 =end original
3291
3292 同様に、リストコンテキストでのリストへの代入は代入可能な左辺値のリストとなり、
3293 スカラコンテキストでのリストへの代入は代入の右側の式で作成された
3294 要素の数を返します。
3295
3296 =begin original
3297
3298 The three dotted bitwise assignment operators (C<&.=> C<|.=> C<^.=>) are new in
3299 Perl 5.22. See L</Bitwise String Operators>.
3300
3301 =end original
3302
3303 三つのドット付きビット単位代入演算子 (C<&.=> C<|.=> C<^.=>) は
3304 Perl 5.22 からの新しいものです。
3305 L</Bitwise String Operators> を参照してください。
3306
3307 =head2 Comma Operator
3308 X<comma> X<operator, comma> X<,>
3309
3310 (コンマ演算子)
3311
3312 =begin original
3313
3314 Binary C<","> is the comma operator. In scalar context it evaluates
3315 its left argument, throws that value away, then evaluates its right
3316 argument and returns that value. This is just like C's comma operator.
3317
3318 =end original
3319
3320 二項演算子の C<","> はコンマ演算子です。
3321 スカラコンテキストではその左引数を評価し、その値を捨てて、
3322 それから右引数を評価し、その値を返します。
3323 これはちょうど、C のコンマ演算子と同じです。
3324
3325 =begin original
3326
3327 In list context, it's just the list argument separator, and inserts
3328 both its arguments into the list. These arguments are also evaluated
3329 from left to right.
3330
3331 =end original
3332
3333 リストコンテキストでは、これは単にリスト引数の区切り文字で、
3334 双方の引数をそのリストに挿入する働きがあります。
3335 これらの引数も左から右に評価されます。
3336
3337 =begin original
3338
3339 The C<< => >> operator (sometimes pronounced "fat comma") is a synonym
3340 for the comma except that it causes a
3341 word on its left to be interpreted as a string if it begins with a letter
3342 or underscore and is composed only of letters, digits and underscores.
3343 This includes operands that might otherwise be interpreted as operators,
3344 constants, single number v-strings or function calls. If in doubt about
3345 this behavior, the left operand can be quoted explicitly.
3346
3347 =end original
3348
3349 C<< => >> 演算子(時々「ファットコンマ」と発音されます)はコンマ演算子の
3350 同義語ですが、もし左側の単語が文字か下線で始まっていて、かつ文字、数字、
3351 下線でのみ構成されている場合、これを文字列として扱うという効果もあります。
3352 これには他の場所では演算子、定数、v-文字列、関数呼び出しとして扱われる
3353 オペランドを含みます。
3354 この振る舞いについて迷うことがあるなら、左オペランドを明示的に
3355 クォートすることも出来ます。
3356
3357 =begin original
3358
3359 Otherwise, the C<< => >> operator behaves exactly as the comma operator
3360 or list argument separator, according to context.
3361
3362 =end original
3363
3364 さもなければ、C<< => >> 演算子はコンテキストによって、
3365 カンマ演算子かリスト引数の区切り文字と全く同様に振る舞います。
3366
3367 =begin original
3368
3369 For example:
3370
3371 =end original
3372
3373 例えば:
3374
3375 use constant FOO => "something";
3376
3377 my %h = ( FOO => 23 );
3378
3379 =begin original
3380
3381 is equivalent to:
3382
3383 =end original
3384
3385 は、以下と等価です:
3386
3387 my %h = ("FOO", 23);
3388
3389 =begin original
3390
3391 It is I<NOT>:
3392
3393 =end original
3394
3395 これは I<違います>:
3396
3397 my %h = ("something", 23);
3398
3399 =begin original
3400
3401 The C<< => >> operator is helpful in documenting the correspondence
3402 between keys and values in hashes, and other paired elements in lists.
3403
3404 =end original
3405
3406 C<< => >> 演算子は、ハッシュのキーと値や、その他のリスト中の組となる
3407 要素の関係を表現するのに便利です。
3408
3409 %hash = ( $key => $value );
3410 login( $username => $password );
3411
3412 =begin original
3413
3414 The special quoting behavior ignores precedence, and hence may apply to
3415 I<part> of the left operand:
3416
3417 =end original
3418
3419 特殊なクォートの振る舞いは優先順位を無視し、従って左オペランドの I<一部> に
3420 適用されることがあります:
3421
3422 print time.shift => "bbb";
3423
3424 =begin original
3425
3426 That example prints something like C<"1314363215shiftbbb">, because the
3427 C<< => >> implicitly quotes the C<shift> immediately on its left, ignoring
3428 the fact that C<time.shift> is the entire left operand.
3429
3430 =end original
3431
3432 この例は C<"1314363215shiftbbb"> のようなものを表示します; なぜなら
3433 C<< => >> は暗黙にすぐ左にある C<shift> をクォートし、 C<time.shift> 全体が
3434 左オペランドであるという事実を無視するからです。
3435
3436 =head2 List Operators (Rightward)
3437 X<operator, list, rightward> X<list operator>
3438
3439 (リスト演算子 (右方向))
3440
3441 =begin original
3442
3443 On the right side of a list operator, the comma has very low precedence,
3444 such that it controls all comma-separated expressions found there.
3445 The only operators with lower precedence are the logical operators
3446 C<"and">, C<"or">, and C<"not">, which may be used to evaluate calls to list
3447 operators without the need for parentheses:
3448
3449 =end original
3450
3451 リスト演算子の右側のものにとって、カンマはとても低い優先順位になります;
3452 これによってコンマで区切った式をリスト演算子の引数として
3453 置くことができます。
3454 これよりも優先順位が低いものは、論理演算子の C<"and">, C<"or">,
3455 C<"not"> のみで、括弧を付けないリスト演算子の呼び出しを評価するために使えます:
3456
3457 open HANDLE, "< :encoding(UTF-8)", "filename"
3458 or die "Can't open: $!\n";
3459
3460 =begin original
3461
3462 However, some people find that code harder to read than writing
3463 it with parentheses:
3464
3465 =end original
3466
3467 しかし、かっこ付きで書くよりもコードが読みにくいという人もいます:
3468
3469 open(HANDLE, "< :encoding(UTF-8)", "filename")
3470 or die "Can't open: $!\n";
3471
3472 =begin original
3473
3474 in which case you might as well just use the more customary C<"||"> operator:
3475
3476 =end original
3477
3478 この場合、より慣習的な C<"||"> 演算子も使えます:
3479
3480 open(HANDLE, "< :encoding(UTF-8)", "filename")
3481 || die "Can't open: $!\n";
3482
3483 =begin original
3484
3485 See also discussion of list operators in L</Terms and List Operators (Leftward)>.
3486
3487 =end original
3488
3489 L</Terms and List Operators (Leftward)> のリスト演算子の議論も参照して下さい。
3490
3491 =head2 Logical Not
3492 X<operator, logical, not> X<not>
3493
3494 (論理否定)
3495
3496 =begin original
3497
3498 Unary C<"not"> returns the logical negation of the expression to its right.
3499 It's the equivalent of C<"!"> except for the very low precedence.
3500
3501 =end original
3502
3503 単項演算子の C<"not"> は右側に来る式の否定を返します。
3504 これは、優先順位がずっと低いことを除いては C<"!"> と等価です。
3505
3506 =head2 Logical And
3507 X<operator, logical, and> X<and>
3508
3509 (論理積)
3510
3511 =begin original
3512
3513 Binary C<"and"> returns the logical conjunction of the two surrounding
3514 expressions. It's equivalent to C<&&> except for the very low
3515 precedence. This means that it short-circuits: the right
3516 expression is evaluated only if the left expression is true.
3517
3518 =end original
3519
3520 二項演算子の C<"and"> は両側の式の論理積を返します。
3521 これは、優先順位がずっと低いことを除けば C<&&> と等価です。
3522 つまり、これも短絡演算を行ない、右側の式は左側の式が
3523 「真」であった場合にのみ評価されます。
3524
3525 =head2 Logical or and Exclusive Or
3526 X<operator, logical, or> X<operator, logical, xor>
3527 X<operator, logical, exclusive or>
3528 X<or> X<xor>
3529
3530 (論理和と排他論理和)
3531
3532 =begin original
3533
3534 Binary C<"or"> returns the logical disjunction of the two surrounding
3535 expressions. It's equivalent to C<||> except for the very low precedence.
3536 This makes it useful for control flow:
3537
3538 =end original
3539
3540 二項演算子の C<"or"> は両側の式の論理和を返します。
3541 これは、優先順位がずっと低いことを除いて C<||> と等価です。
3542 これはフローを制御するのに有用です:
3543
3544 print FH $data or die "Can't write to FH: $!";
3545
3546 =begin original
3547
3548 This means that it short-circuits: the right expression is evaluated
3549 only if the left expression is false. Due to its precedence, you must
3550 be careful to avoid using it as replacement for the C<||> operator.
3551 It usually works out better for flow control than in assignments:
3552
3553 =end original
3554
3555 つまり、これも短絡演算を行ない、右側の式は左側の式が
3556 「偽」であった場合にのみ評価されます。
3557 優先度の関係で、これを C<||> 演算子の置き換えに使うのは慎重に
3558 避けなければなりません。
3559 これは普通代入よりも、フローの制御でうまく動作します:
3560
3561 =begin original
3562
3563 $x = $y or $z; # bug: this is wrong
3564 ($x = $y) or $z; # really means this
3565 $x = $y || $z; # better written this way
3566
3567 =end original
3568
3569 $x = $y or $z; # バグ: これは間違い
3570 ($x = $y) or $z; # 本当にしたいこと
3571 $x = $y || $z; # こう書いた方がいい
3572
3573 =begin original
3574
3575 However, when it's a list-context assignment and you're trying to use
3576 C<||> for control flow, you probably need C<"or"> so that the assignment
3577 takes higher precedence.
3578
3579 =end original
3580
3581 しかし、代入がリストコンテキストの時に C<||> をフロー制御に使おうとする場合、
3582 代入により大きな優先順位を持たせるために C<"or"> が必要かもしれません。
3583
3584 =begin original
3585
3586 @info = stat($file) || die; # oops, scalar sense of stat!
3587 @info = stat($file) or die; # better, now @info gets its due
3588
3589 =end original
3590
3591 @info = stat($file) || die; # うわ、stat がスカラの意味だ!
3592 @info = stat($file) or die; # よりよい; @info はその目的を果たす
3593
3594 =begin original
3595
3596 Then again, you could always use parentheses.
3597
3598 =end original
3599
3600 もちろん、常に括弧をつけてもよいです。
3601
3602 =begin original
3603
3604 Binary C<"xor"> returns the exclusive-OR of the two surrounding expressions.
3605 It cannot short-circuit (of course).
3606
3607 =end original
3608
3609 二項演算子の C<"xor"> は両側の式の排他論理和を返します。
3610 これは (もちろん) 短絡できません。
3611
3612 =begin original
3613
3614 There is no low precedence operator for defined-OR.
3615
3616 =end original
3617
3618 定義性論理和の低優先順位版はありません。
3619
3620 =head2 C Operators Missing From Perl
3621 X<operator, missing from perl> X<&> X<*>
3622 X<typecasting> X<(TYPE)>
3623
3624 (Perl にない C の演算子)
3625
3626 =begin original
3627
3628 Here is what C has that Perl doesn't:
3629
3630 =end original
3631
3632 C にあって Perl に無いものは以下の通りです:
3633
3634 =over 8
3635
3636 =item unary &
3637
3638 =begin original
3639
3640 Address-of operator. (But see the C<"\"> operator for taking a reference.)
3641
3642 =end original
3643
3644 アドレス演算子。
3645 (しかし C<"\"> 演算子がリファレンスのために用いられます。)
3646
3647 =item unary *
3648
3649 =begin original
3650
3651 Dereference-address operator. (Perl's prefix dereferencing
3652 operators are typed: C<$>, C<@>, C<%>, and C<&>.)
3653
3654 =end original
3655
3656 被アドレス参照演算子。
3657 (Perl の被参照プリフィクス演算子が型づけを行ないます:
3658 C<$>, C<@>, C<%>, C<&>。)
3659
3660 =item (TYPE)
3661
3662 =begin original
3663
3664 Type-casting operator.
3665
3666 =end original
3667
3668 型のキャスト演算子。
3669
3670 =back
3671
3672 =head2 Quote and Quote-like Operators
3673 X<operator, quote> X<operator, quote-like> X<q> X<qq> X<qx> X<qw> X<m>
3674 X<qr> X<s> X<tr> X<'> X<''> X<"> X<""> X<//> X<`> X<``> X<<< << >>>
3675 X<escape sequence> X<escape>
3676
3677 (クォートとクォート風の演算子)
3678
3679 =begin original
3680
3681 While we usually think of quotes as literal values, in Perl they
3682 function as operators, providing various kinds of interpolating and
3683 pattern matching capabilities. Perl provides customary quote characters
3684 for these behaviors, but also provides a way for you to choose your
3685 quote character for any of them. In the following table, a C<{}> represents
3686 any pair of delimiters you choose.
3687
3688 =end original
3689
3690 クォートはリテラル値であると考えるのが普通ですが、Perl において、
3691 クォートは演算子として働き、さまざまな展開やパターンマッチの機能を
3692 持っています。
3693 そのような動作をさせるのに、Perl は慣習的にクォート文字を使っていますが、
3694 どの種類のクォートも、自分でクォート文字を選べるようになっています。
3695 以下の表では、{} がその選んだ区切文字のペアを示しています。
3696
3697 =begin original
3698
3699 Customary Generic Meaning Interpolates
3700 '' q{} Literal no
3701 "" qq{} Literal yes
3702 `` qx{} Command yes*
3703 qw{} Word list no
3704 // m{} Pattern match yes*
3705 qr{} Pattern yes*
3706 s{}{} Substitution yes*
3707 tr{}{} Transliteration no (but see below)
3708 y{}{} Transliteration no (but see below)
3709 <<EOF here-doc yes*
3710
3711 =end original
3712
3713 通常記法 汎用記法 意味 展開
3714 =================================================
3715 '' q{} リテラル 不可
3716 "" qq{} リテラル 可
3717 `` qx{} コマンド 可 *
3718 qw{} 単語リスト 不可
3719 // m{} パターンマッチ 可 *
3720 qr{} パターン 可 *
3721 s{}{} 置換 可 *
3722 tr{}{} 変換 不可 (但し以下を参照のこと)
3723 y{}{} 変換 不可 (但し以下を参照のこと)
3724 <<EOF ヒアドキュメント 可 *
3725
3726 =begin original
3727
3728 * unless the delimiter is ''.
3729
3730 =end original
3731
3732 * '' がデリミタでない場合のみ
3733
3734 =begin original
3735
3736 Non-bracketing delimiters use the same character fore and aft, but the four
3737 sorts of ASCII brackets (round, angle, square, curly) all nest, which means
3738 that
3739
3740 =end original
3741
3742 選んだ区切文字が括弧の類でない場合には、前後の文字として同一のものを
3743 使いますが、4 つの ASCII のかっこ ((), <>, [], {}) の場合にはネストできます;
3744 つまり、以下のものは、
3745
3746 q{foo{bar}baz}
3747
3748 =begin original
3749
3750 is the same as
3751
3752 =end original
3753
3754 以下と同じです。
3755
3756 'foo{bar}baz'
3757
3758 =begin original
3759
3760 Note, however, that this does not always work for quoting Perl code:
3761
3762 =end original
3763
3764 しかし、以下のコードはクォートされた Perl コードでは
3765 いつも正しく動くわけではないことに注意してください:
3766
3767 $s = q{ if($x eq "}") ... }; # WRONG
3768
3769 =begin original
3770
3771 is a syntax error. The C<L<Text::Balanced>> module (standard as of v5.8,
3772 and from CPAN before then) is able to do this properly.
3773
3774 =end original
3775
3776 これは文法エラーとなります。
3777 C<L<Text::Balanced>> モジュール(Perl 5.8 からは標準配布、それ以前は CPAN に
3778 あります)はこれを適切に行います。
3779
3780 =begin original
3781
3782 There can (and in some cases, must) be whitespace between the operator
3783 and the quoting
3784 characters, except when C<#> is being used as the quoting character.
3785 C<q#foo#> is parsed as the string C<foo>, while S<C<q #foo#>> is the
3786 operator C<q> followed by a comment. Its argument will be taken
3787 from the next line. This allows you to write:
3788
3789 =end original
3790
3791 演算子とクォート文字の間に空白を置くことも出来ます (そして場合によっては
3792 必須です); ただし、C<#> をクォート文字として使う場合は例外です。
3793 C<q#foo#> は文字列 C<foo> としてパースされますが、
3794 S<C<q #foo#>> は C<q> 演算子の後にコメントがあるとみなされます。
3795 この引数は次の行から取られます。つまり、以下のように書けます:
3796
3797 =begin original
3798
3799 s {foo} # Replace foo
3800 {bar} # with bar.
3801
3802 =end original
3803
3804 s {foo} # foo を
3805 {bar} # bar で置き換える
3806
3807 =begin original
3808
3809 The cases where whitespace must be used are when the quoting character
3810 is a word character (meaning it matches C</\w/>):
3811
3812 =end original
3813
3814 空白が使われなければならないのは、クォート文字が単語文字
3815 (つまり C</\w/> にマッチングする)の場合です:
3816
3817 q XfooX # Works: means the string 'foo'
3818 qXfooX # WRONG!
3819
3820 =begin original
3821
3822 The following escape sequences are available in constructs that interpolate,
3823 and in transliterations whose delimiters aren't single quotes (C<"'">).
3824 In all the ones with braces, any number of blanks and/or tabs adjoining
3825 and within the braces are allowed (and ignored).
3826 X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}>
3827 X<\o{}>
3828
3829 =end original
3830
3831 以下のエスケープシーケンスが、
3832 区切り文字がシングルクォート (C<"'">) でない展開と文字変換の構文で
3833 利用可能です。
3834 中かっこつきのもの全ては、中かっこの中で隣接している任意の数の空白や
3835 タブが許されます(そして無視されます)。
3836 X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}>
3837 X<\o{}>
3838
3839 =begin original
3840
3841 Sequence Note Description
3842 \t tab (HT, TAB)
3843 \n newline (NL)
3844 \r return (CR)
3845 \f form feed (FF)
3846 \b backspace (BS)
3847 \a alarm (bell) (BEL)
3848 \e escape (ESC)
3849 \x{263A} [1,8] hex char (example shown: SMILEY)
3850 \x{ 263A } Same, but shows optional blanks inside and
3851 adjoining the braces
3852 \x1b [2,8] restricted range hex char (example: ESC)
3853 \N{name} [3] named Unicode character or character sequence
3854 \N{U+263D} [4,8] Unicode character (example: FIRST QUARTER MOON)
3855 \c[ [5] control char (example: chr(27))
3856 \o{23072} [6,8] octal char (example: SMILEY)
3857 \033 [7,8] restricted range octal char (example: ESC)
3858
3859 =end original
3860
3861 シーケンス 注意 説明
3862 \t タブ (HT, TAB)
3863 \n 改行 (NL)
3864 \r 復帰 (CR)
3865 \f 改ページ (FF)
3866 \b バックスペース (BS)
3867 \a アラーム (BEL)
3868 \e エスケープ (ESC)
3869 \x{263a} [1,8] 16 進文字 (例: SMILEY)
3870 \x{ 263A } 同様、しかし中かっこの内側に隣接している
3871 オプションの空白を示している
3872 \x1b [2,8] 範囲制限された 16 進数で表した文字 (例: ESC)
3873 \N{name} [3] 名前つき Unicode 文字または文字シーケンス
3874 \N{U+263D} [4,8] Unicode 文字 (例: FIRST QUARTER MOON)
3875 \c[ [5] 制御文字 (例: chr(27))
3876 \o{23072} [6,8] 8 進文字 (例: SMILEY)
3877 \033 [7,8] 範囲制限された 8 進文字 (例: ESC)
3878
3879 =begin original
3880
3881 Note that any escape sequence using braces inside interpolated
3882 constructs may have optional blanks (tab or space characters) adjoining
3883 with and inside of the braces, as illustrated above by the second
3884 S<C<\x{ }>> example.
3885
3886 =end original
3887
3888 変数置換構文の中の中かっこを使ったエスケープシーケンスは、
3889 前述の 2 番目の S<C<\x{ }>> の例で図示したように、中かっこの内側隣接の
3890 位置にオプションの空白(タブまたはスペース文字)を置くことができます。
3891
3892 =over 4
3893
3894 =item [1]
3895
3896 =begin original
3897
3898 The result is the character specified by the hexadecimal number between
3899 the braces. See L</[8]> below for details on which character.
3900
3901 =end original
3902
3903 結果は中かっこで囲まれた 16 進数で指定された文字です。
3904 その文字に関する詳細については以下の L</[8]> を参照してください。
3905
3906 =begin original
3907
3908 Blanks (tab or space characters) may separate the number from either or
3909 both of the braces.
3910
3911 =end original
3912
3913 空白 (タブまたはスペース文字) は、片方または両方の中かっこから数字を
3914 分離します。
3915
3916 =begin original
3917
3918 Otherwise, only hexadecimal digits are valid between the braces. If an
3919 invalid character is encountered, a warning will be issued and the
3920 invalid character and all subsequent characters (valid or invalid)
3921 within the braces will be discarded.
3922
3923 =end original
3924
3925 さもなければ、中かっこの中には 16 進数字のみが妥当です。
3926 不正な文字に遭遇すると、警告が発生し、中かっこの内側の不正な文字と
3927 それ以降の文字(妥当でも不正でも)は捨てられます。
3928
3929 =begin original
3930
3931 If there are no valid digits between the braces, the generated character is
3932 the NULL character (C<\x{00}>). However, an explicit empty brace (C<\x{}>)
3933 will not cause a warning (currently).
3934
3935 =end original
3936
3937 中かっこの中に妥当な文字がなければ、生成される文字は NULL 文字
3938 (C<\x{00}>) です。
3939 しかし、明示的な空の中かっこ (C<\x{}>) は(今のところ)警告を出しません。
3940
3941 =item [2]
3942
3943 =begin original
3944
3945 The result is the character specified by the hexadecimal number in the range
3946 0x00 to 0xFF. See L</[8]> below for details on which character.
3947
3948 =end original
3949
3950 結果は 0x00 から 0xFF の範囲の 16 進数で指定された文字です。
3951 その文字に関する詳細については以下の L</[8]> を参照してください。
3952
3953 =begin original
3954
3955 Only hexadecimal digits are valid following C<\x>. When C<\x> is followed
3956 by fewer than two valid digits, any valid digits will be zero-padded. This
3957 means that C<\x7> will be interpreted as C<\x07>, and a lone C<"\x"> will be
3958 interpreted as C<\x00>. Except at the end of a string, having fewer than
3959 two valid digits will result in a warning. Note that although the warning
3960 says the illegal character is ignored, it is only ignored as part of the
3961 escape and will still be used as the subsequent character in the string.
3962 For example:
3963
3964 =end original
3965
3966 C<\x> に引き続くのは 16 進数字のみが妥当です。
3967 C<\x> に引き続く妥当な数字が 2 桁ない場合、妥当な数字はゼロで
3968 パッディングされます。
3969 これは、C<\x7> は C<\x07> と解釈され、単独の C<"\x"> は C<\x00> と
3970 解釈されるということです。
3971 文字列の末尾を例外として、妥当な数字が 2 桁ない場合は警告が発生します。
3972 警告は不正な文字が無視されると言うにも関わらず、エスケープの一部のみが
3973 無視され、文字列中の引き続く文字は使われるままであることに注意してください。
3974 例えば:
3975
3976 Original Result Warns?
3977 "\x7" "\x07" no
3978 "\x" "\x00" no
3979 "\x7q" "\x07q" yes
3980 "\xq" "\x00q" yes
3981
3982 =item [3]
3983
3984 =begin original
3985
3986 The result is the Unicode character or character sequence given by I<name>.
3987 See L<charnames>.
3988
3989 =end original
3990
3991 結果は I<name> で指定される Unicode 文字または文字の並びです。
3992 L<charnames> を参照してください。
3993
3994 =item [4]
3995
3996 =begin original
3997
3998 S<C<\N{U+I<hexadecimal number>}>> means the Unicode character whose Unicode code
3999 point is I<hexadecimal number>.
4000
4001 =end original
4002
4003 S<C<\N{U+I<hexadecimal number>}>> は、Unicode 符号位置が
4004 I<hexadecimal number> の Unicode 文字を意味します。
4005
4006 =item [5]
4007
4008 =begin original
4009
4010 The character following C<\c> is mapped to some other character as shown in the
4011 table:
4012
4013 =end original
4014
4015 C<\c> に引き続く文字は以下の表に示すように他の文字にマッピングされます:
4016
4017 Sequence Value
4018 \c@ chr(0)
4019 \cA chr(1)
4020 \ca chr(1)
4021 \cB chr(2)
4022 \cb chr(2)
4023 ...
4024 \cZ chr(26)
4025 \cz chr(26)
4026 \c[ chr(27)
4027 # See below for chr(28)
4028 \c] chr(29)
4029 \c^ chr(30)
4030 \c_ chr(31)
4031 \c? chr(127) # (on ASCII platforms; see below for link to
4032 # EBCDIC discussion)
4033
4034 =begin original
4035
4036 In other words, it's the character whose code point has had 64 xor'd with
4037 its uppercase. C<\c?> is DELETE on ASCII platforms because
4038 S<C<ord("?") ^ 64>> is 127, and
4039 C<\c@> is NULL because the ord of C<"@"> is 64, so xor'ing 64 itself produces 0.
4040
4041 =end original
4042
4043 言い換えると、符号位置を 64 で xor して大文字にした文字です。
4044 S<C<ord("?") ^ 64>> は 127 なので C<\c?> は ASCII プラットフォームでは
4045 DELETE で、C<"@"> は 64 のために
4046 64 で xor すると 0 になるので C<\c@> は NUL です。
4047
4048 =begin original
4049
4050 Also, C<\c\I<X>> yields S<C< chr(28) . "I<X>">> for any I<X>, but cannot come at the
4051 end of a string, because the backslash would be parsed as escaping the end
4052 quote.
4053
4054 =end original
4055
4056 また、C<\c\I<X>> は任意の I<X> について S<C< chr(28) . "I<X>">> となりますが、
4057 文字列の末尾には来ません; 逆スラッシュは末尾のクォートをエスケープするように
4058 パースされるからです。
4059
4060 =begin original
4061
4062 On ASCII platforms, the resulting characters from the list above are the
4063 complete set of ASCII controls. This isn't the case on EBCDIC platforms; see
4064 L<perlebcdic/OPERATOR DIFFERENCES> for a full discussion of the
4065 differences between these for ASCII versus EBCDIC platforms.
4066
4067 =end original
4068
4069 ASCII プラットフォームでは、上述の一覧からの結果の文字は ASCII 制御文字の
4070 完全な集合です。
4071 これは EBCDIC プラットフォームには当てはまりません; これに関する
4072 ASCII プラットフォームと EBCDIC プラットフォームとの違いの完全な議論については
4073 L<perlebcdic/OPERATOR DIFFERENCES> を参照してください。
4074
4075 =begin original
4076
4077 Use of any other character following the C<"c"> besides those listed above is
4078 discouraged, and as of Perl v5.20, the only characters actually allowed
4079 are the printable ASCII ones, minus the left brace C<"{">. What happens
4080 for any of the allowed other characters is that the value is derived by
4081 xor'ing with the seventh bit, which is 64, and a warning raised if
4082 enabled. Using the non-allowed characters generates a fatal error.
4083
4084 =end original
4085
4086 C<"c"> に引き続いて上述した以外の文字を使うことは非推奨であり、
4087 as of Perl v5.20, the only characters actually allowed
4088 are the printable ASCII ones, minus the left brace C<"{">.
4089 許されている他の文字を置いたときに起こることは、値が第 7 ビット;
4090 つまり 64 で xor を取ったものになり、
4091 有効の場合は警告が発生します。
4092 許されていない文字を使うと致命的エラーが発生します。
4093
4094 =begin original
4095
4096 To get platform independent controls, you can use C<\N{...}>.
4097
4098 =end original
4099
4100 プラットフォーム非依存の制御文字を得るには、C<\N{...}> を使ってください。
4101
4102 =item [6]
4103
4104 =begin original
4105
4106 The result is the character specified by the octal number between the braces.
4107 See L</[8]> below for details on which character.
4108
4109 =end original
4110
4111 結果は中かっこで囲まれた 8 進数で指定された文字です。
4112 その文字に関する詳細については以下の L</[8]> を参照してください。
4113
4114 =begin original
4115
4116 Blanks (tab or space characters) may separate the number from either or
4117 both of the braces.
4118
4119 =end original
4120
4121 空白 (タブまたはスペース文字) は、片方または両方の中かっこから数字を
4122 分離します。
4123
4124 =begin original
4125
4126 Otherwise, if a character that isn't an octal digit is encountered, a
4127 warning is raised, and the value is based on the octal digits before it,
4128 discarding it and all following characters up to the closing brace. It
4129 is a fatal error if there are no octal digits at all.
4130
4131 =end original
4132
4133 さもなければ、8 進数でない文字に遭遇すると、警告が発生し、値はそこまでの
4134 8 進数字を基として、それ以降閉じ中かっこまでの全ての文字を捨てます。
4135 8 進数字がまったくないと致命的エラーになります。
4136
4137 =item [7]
4138
4139 =begin original
4140
4141 The result is the character specified by the three-digit octal number in the
4142 range 000 to 777 (but best to not use above 077, see next paragraph). See
4143 L</[8]> below for details on which character.
4144
4145 =end original
4146
4147 結果は範囲 000 から 777 までの 3 桁の 8 進数で指定される文字です
4148 (しかし 077 より上は使わないのが最良です; 次の段落を参照してください)。
4149 その文字に関する詳細については以下の L</[8]> を参照してください。
4150
4151 =begin original
4152
4153 Some contexts allow 2 or even 1 digit, but any usage without exactly
4154 three digits, the first being a zero, may give unintended results. (For
4155 example, in a regular expression it may be confused with a backreference;
4156 see L<perlrebackslash/Octal escapes>.) Starting in Perl 5.14, you may
4157 use C<\o{}> instead, which avoids all these problems. Otherwise, it is best to
4158 use this construct only for ordinals C<\077> and below, remembering to pad to
4159 the left with zeros to make three digits. For larger ordinals, either use
4160 C<\o{}>, or convert to something else, such as to hex and use C<\N{U+}>
4161 (which is portable between platforms with different character sets) or
4162 C<\x{}> instead.
4163
4164 =end original
4165
4166 一部のコンテキストでは 2 桁や、1 桁ですら許されますが、正確に 3 桁かつ
4167 先頭が 0、以外の使い方は意図していない結果をもたらすかもしれません。
4168 (例えば、正規表現中では後方参照で混乱するかもしれません;
4169 L<perlrebackslash/Octal escapes> を参照してください。)
4170 Perl 5.14 から、代わりに C<\o{}> を使えます; これはこれらすべての問題を
4171 避けられます。
4172 さもなければ、この構文を値 C<\077> 以下でのみ使用するのが最良です;
4173 3 桁にするために左側にゼロをパッディングするのを忘れないでください。
4174 より大きな値では、C<\o{}> を使うか、代わりに 16 進数にして
4175 C<\N{U+}>
4176 (これは異なった文字集合のプラットフォーム間で移植性があります) を使うか、
4177 または C<\x{}> を
4178 使うような、他のものに変換してください。
4179
4180 =item [8]
4181
4182 =begin original
4183
4184 Several constructs above specify a character by a number. That number
4185 gives the character's position in the character set encoding (indexed from 0).
4186 This is called synonymously its ordinal, code position, or code point. Perl
4187 works on platforms that have a native encoding currently of either ASCII/Latin1
4188 or EBCDIC, each of which allow specification of 256 characters. In general, if
4189 the number is 255 (0xFF, 0377) or below, Perl interprets this in the platform's
4190 native encoding. If the number is 256 (0x100, 0400) or above, Perl interprets
4191 it as a Unicode code point and the result is the corresponding Unicode
4192 character. For example C<\x{50}> and C<\o{120}> both are the number 80 in
4193 decimal, which is less than 256, so the number is interpreted in the native
4194 character set encoding. In ASCII the character in the 80th position (indexed
4195 from 0) is the letter C<"P">, and in EBCDIC it is the ampersand symbol C<"&">.
4196 C<\x{100}> and C<\o{400}> are both 256 in decimal, so the number is interpreted
4197 as a Unicode code point no matter what the native encoding is. The name of the
4198 character in the 256th position (indexed by 0) in Unicode is
4199 C<LATIN CAPITAL LETTER A WITH MACRON>.
4200
4201 =end original
4202
4203 上述のいくつかの構造は文字を数値で指定しています。
4204 この値は文字集合エンコーディングで (0 から始めた) 文字の位置を指定します。
4205 これは同意語として序数(ordinal)、コード位置(code position)、
4206 符号位置(code point)と呼ばれます。
4207 Perl は現在のところ、それぞれ 256 文字を定義している ASCII/Latin1 または
4208 EBCDIC のどちらかのネイティブエンコーディングを持つプラットフォームで
4209 動作します。
4210 一般的に、数値が 255 (0xFF, 0377) 以下なら、Perl はこれをプラットフォームの
4211 ネイティブエンコーディングと解釈します。
4212 数値が 256 (0x100, 0400) 以上なら、Perl はこれを Unicode 符号位置と解釈し、
4213 結果は対応する Unicode 文字となります。
4214 例えば C<\x{50}> と C<\o{120}> はどちらも 10 進数では 80 で、これは 256 より
4215 小さいので、数値はネイティブ文字集合エンコーディングとして解釈されます。
4216 ASCII では (0 から始めて) 80 番目の位置の文字は C<"P"> で、EBCDIC では
4217 アンパサンド記号 C<"&"> です。
4218 C<\x{100}> と C<\o{400}> はどちらも 10 進数では 256 なので、数値は
4219 ネイティブエンコーディングが何かに関わらず Unicode 符号位置として
4220 解釈されます。
4221 Unicode での (0 から始めて) 256 番目の位置の文字の名前は
4222 C<LATIN CAPITAL LETTER A WITH MACRON> です。
4223
4224 =begin original
4225
4226 An exception to the above rule is that S<C<\N{U+I<hex number>}>> is
4227 always interpreted as a Unicode code point, so that C<\N{U+0050}> is C<"P"> even
4228 on EBCDIC platforms.
4229
4230 =end original
4231
4232 上述の規則の例外として、S<C<\N{U+I<16 進数>}>> は常に Unicode 符号位置として
4233 解釈されるので、C<\N{U+0050}> は EBCDIC プラットフォームでも C<"P"> です。
4234
4235 =back
4236
4237 =begin original
4238
4239 B<NOTE>: Unlike C and other languages, Perl has no C<\v> escape sequence for
4240 the vertical tab (VT, which is 11 in both ASCII and EBCDIC), but you may
4241 use C<\N{VT}>, C<\ck>, C<\N{U+0b}>, or C<\x0b>. (C<\v>
4242 does have meaning in regular expression patterns in Perl, see L<perlre>.)
4243
4244 =end original
4245
4246 B<注意>: C やその他の言語と違って、Perl は垂直タブ (VT - ASCII と EBCDIC の
4247 両方で 11) のための \v エスケープシーケンスはありませんが、C<\N{VT}>, C<\ck>,
4248 C<\N{U+0b}>, C<\x0b> が使えます。
4249 (C<\v> は Perl の正規表現パターンでは意味があります; L<perlre> を
4250 参照してください。)
4251
4252 =begin original
4253
4254 The following escape sequences are available in constructs that interpolate,
4255 but not in transliterations.
4256 X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> X<\F>
4257
4258 =end original
4259
4260 以下のエスケープシーケンスが展開と文字変換の構文で利用可能です。
4261 X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q>
4262 X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> X<\F>
4263
4264 =begin original
4265
4266 \l lowercase next character only
4267 \u titlecase (not uppercase!) next character only
4268 \L lowercase all characters till \E or end of string
4269 \U uppercase all characters till \E or end of string
4270 \F foldcase all characters till \E or end of string
4271 \Q quote (disable) pattern metacharacters till \E or
4272 end of string
4273 \E end either case modification or quoted section
4274 (whichever was last seen)
4275
4276 =end original
4277
4278 \l 次の文字だけを小文字にする
4279 \u 次の文字だけをタイトル文字(大文字ではありません!)にする
4280 \L \E か文字列の末尾まで小文字にする
4281 \U \E か文字列の末尾まで大文字にする
4282 \F \E か文字列の末尾まで畳み込み文字にする
4283 \Q \E か文字列の末尾までパターンメタ文字をクォート(無効化)する
4284 \E 大文字小文字変換かクォート部分(どちらか最後に現れたもの)を
4285 終了させる
4286
4287 =begin original
4288
4289 See L<perlfunc/quotemeta> for the exact definition of characters that
4290 are quoted by C<\Q>.
4291
4292 =end original
4293
4294 C<\Q> でクォートされる文字の正確な定義については L<perlfunc/quotemeta> を
4295 参照してください。
4296
4297 =begin original
4298
4299 C<\L>, C<\U>, C<\F>, and C<\Q> can stack, in which case you need one
4300 C<\E> for each. For example:
4301
4302 =end original
4303
4304 C<\L>, C<\U>, C<\F>, C<\Q> はスタックできます; この場合それぞれに対して
4305 C<\E> が必要です。
4306 例えば:
4307
4308 say"This \Qquoting \ubusiness \Uhere isn't quite\E done yet,\E is it?";
4309 This quoting\ Business\ HERE\ ISN\'T\ QUITE\ done\ yet\, is it?
4310
4311 =begin original
4312
4313 If a S<C<use locale>> form that includes C<LC_CTYPE> is in effect (see
4314 L<perllocale>), the case map used by C<\l>, C<\L>, C<\u>, and C<\U> is
4315 taken from the current locale. If Unicode (for example, C<\N{}> or code
4316 points of 0x100 or beyond) is being used, the case map used by C<\l>,
4317 C<\L>, C<\u>, and C<\U> is as defined by Unicode. That means that
4318 case-mapping a single character can sometimes produce a sequence of
4319 several characters.
4320 Under S<C<use locale>>, C<\F> produces the same results as C<\L>
4321 for all locales but a UTF-8 one, where it instead uses the Unicode
4322 definition.
4323
4324 =end original
4325
4326 C<LC_CTYPE> を含む S<C<use locale>> 型式が有効の場合(L<perllocale> を
4327 参照して下さい)、C<\l>, C<\L>, C<\u>, C<\U> で使われる
4328 大文字小文字テーブルは現在のロケールのものが使われます。
4329 (例えば、C<\N{}> や、0x100 以上の符号位置の) Unicode が
4330 使われている場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる大文字小文字
4331 テーブルは Unicode で定義されているものになります。
4332 これは、単一の文字の大文字小文字マッピングは複数の文字の並びを生成することが
4333 あるということです。
4334 S<C<use locale>> の基では、C<\F> は、Unicode の定義が使われる UTF-8 以外の
4335 全てのロケールにおいて、C<\L> と同じ結果を生成します。
4336
4337 =begin original
4338
4339 All systems use the virtual C<"\n"> to represent a line terminator,
4340 called a "newline". There is no such thing as an unvarying, physical
4341 newline character. It is only an illusion that the operating system,
4342 device drivers, C libraries, and Perl all conspire to preserve. Not all
4343 systems read C<"\r"> as ASCII CR and C<"\n"> as ASCII LF. For example,
4344 on the ancient Macs (pre-MacOS X) of yesteryear, these used to be reversed,
4345 and on systems without a line terminator,
4346 printing C<"\n"> might emit no actual data. In general, use C<"\n"> when
4347 you mean a "newline" for your system, but use the literal ASCII when you
4348 need an exact character. For example, most networking protocols expect
4349 and prefer a CR+LF (C<"\015\012"> or C<"\cM\cJ">) for line terminators,
4350 and although they often accept just C<"\012">, they seldom tolerate just
4351 C<"\015">. If you get in the habit of using C<"\n"> for networking,
4352 you may be burned some day.
4353 X<newline> X<line terminator> X<eol> X<end of line>
4354 X<\n> X<\r> X<\r\n>
4355
4356 =end original
4357
4358 全てのシステムでは "newline" と呼ばれる行端末子を表現するために
4359 仮想的な C<"\n"> が用いられます。
4360 普遍の、物理的な "newline" 文字と言うものはありません。
4361 オペレーティングシステム、デバイスドライバ、C ライブラリ、Perl が全て
4362 協力して保存しようとすると言うのは単なる幻想です。
4363 全てのシステムで C<"\r"> を ASCII CR として、また C<"\n"> を
4364 ASCII LF として読み込むわけではありません。
4365 例えば昔の Mac (MacOS X 以前)ではこれらは保存され、行端末子のない
4366 システムでは、C<"\n"> を print しても実際のデータは何も出力しません。
4367 一般に、システムで "newline" を意味したいときには C<"\n"> を使いますが、
4368 正確な文字が必要な場合はリテラルな ASCII を使います。
4369 例えば、ほとんどのネットワークプロトコルでは行端末子として
4370 CR+LF (C<"\015\012"> または C<"\cM\cJ">) を予想し、また好みますが、
4371 しばしば C<"\012"> だけでも許容し、さらに時々は C<"\015"> だけでも認めます。
4372 もしネットワーク関係で C<"\n"> を使う習慣がついていると、
4373 いつか痛い目を見ることになるでしょう。
4374 X<newline> X<line terminator> X<eol> X<end of line>
4375 X<\n> X<\r> X<\r\n>
4376
4377 =begin original
4378
4379 For constructs that do interpolate, variables beginning with "C<$>"
4380 or "C<@>" are interpolated. Subscripted variables such as C<$a[3]> or
4381 C<< $href->{key}[0] >> are also interpolated, as are array and hash slices.
4382 But method calls such as C<< $obj->meth >> are not.
4383
4384 =end original
4385
4386 展開が行なわれる構文では、"C<$>" や "C<@>" で始まる変数が展開されます。
4387 C<$a[3]> や C<< $href->{key}[0] >> のような添え字付き変数もまた
4388 配列やハッシュのスライスのように展開されます。
4389 しかし、C<< $obj->meth >> のようなメソッド呼び出しは展開されません。
4390
4391 =begin original
4392
4393 Interpolating an array or slice interpolates the elements in order,
4394 separated by the value of C<$">, so is equivalent to interpolating
4395 S<C<join $", @array>>. "Punctuation" arrays such as C<@*> are usually
4396 interpolated only if the name is enclosed in braces C<@{*}>, but the
4397 arrays C<@_>, C<@+>, and C<@-> are interpolated even without braces.
4398
4399 =end original
4400
4401 配列やスライスの展開は、要素を順番に、C<$"> の値で分割して展開されるので、
4402 S<C<join $", @array>> の展開と等価です。
4403 C<@*> のような「句読点」配列は普通は名前が C<@{*}> のように中かっこで
4404 囲われている場合にのみ展開されますが、配列 C<@_>, C<@+>, C<@-> は
4405 中かっこなしでも展開されます。
4406
4407 =begin original
4408
4409 For double-quoted strings, the quoting from C<\Q> is applied after
4410 interpolation and escapes are processed.
4411
4412 =end original
4413
4414 ダブルクォートされた文字列では、C<\Q> からのクォートは文字変換と
4415 エスケープが処理された後に適用されます。
4416
4417 "abc\Qfoo\tbar$s\Exyz"
4418
4419 =begin original
4420
4421 is equivalent to
4422
4423 =end original
4424
4425 は以下と等価です:
4426
4427 "abc" . quotemeta("foo\tbar$s") . "xyz"
4428
4429 =begin original
4430
4431 For the pattern of regex operators (C<qr//>, C<m//> and C<s///>),
4432 the quoting from C<\Q> is applied after interpolation is processed,
4433 but before escapes are processed. This allows the pattern to match
4434 literally (except for C<$> and C<@>). For example, the following matches:
4435
4436 =end original
4437
4438 正規表現演算子 (C<qr//>, C<m//>, C<s///>) のパターンでは、C<\Q> による
4439 クォートは変数展開が行われた後、エスケープが処理される前に適用されます。
4440 これによりパターンを (C<$> と C<@> 以外は) リテラルにマッチングできます。
4441 例えば、以下はマッチングします:
4442
4443 '\s\t' =~ /\Q\s\t/
4444
4445 =begin original
4446
4447 Because C<$> or C<@> trigger interpolation, you'll need to use something
4448 like C</\Quser\E\@\Qhost/> to match them literally.
4449
4450 =end original
4451
4452 C<$> や C<@> は変換を引き起こすので、リテラルにマッチングさせるためには
4453 C<m/\Quser\E\@\Qhost/> などという風に書く必要があります。
4454
4455 =begin original
4456
4457 Patterns are subject to an additional level of interpretation as a
4458 regular expression. This is done as a second pass, after variables are
4459 interpolated, so that regular expressions may be incorporated into the
4460 pattern from the variables. If this is not what you want, use C<\Q> to
4461 interpolate a variable literally.
4462
4463 =end original
4464
4465 パターンはさらに、正規表現として展開が行なわれます。
4466 これは、変数が展開された後の 2 回目のパスで行なわれるので、変数に正規表現を
4467 含めておき、パターンの中へ展開することができます。
4468 もし、そうしたくないのであれば、C<\Q> を使うと変数の内容を文字通りに
4469 展開することができます。
4470
4471 =begin original
4472
4473 Apart from the behavior described above, Perl does not expand
4474 multiple levels of interpolation. In particular, contrary to the
4475 expectations of shell programmers, back-quotes do I<NOT> interpolate
4476 within double quotes, nor do single quotes impede evaluation of
4477 variables when used within double quotes.
4478
4479 =end original
4480
4481 上記の振る舞いを除けば、Perl は 複数の段階を踏んで展開を行ないません。
4482 特に、シェルのプログラマの期待とは裏腹に、バッククォートはダブルクォートの
4483 中では展開されませんし、シングルクォートがダブルクォートの中で使われても、
4484 変数の展開を妨げることは I<ありません>。
4485
4486 =head2 Regexp Quote-Like Operators
4487 X<operator, regexp>
4488
4489 (正規表現のクォート風の演算子)
4490
4491 =begin original
4492
4493 Here are the quote-like operators that apply to pattern
4494 matching and related activities.
4495
4496 =end original
4497
4498 以下はパターンマッチングと関連する行動に関するクォート風の演算子です。
4499
4500 =over 8
4501
4502 =item C<qr/I<STRING>/msixpodualn>
4503 X<qr> X</i> X</m> X</o> X</s> X</x> X</p>
4504
4505 =begin original
4506
4507 This operator quotes (and possibly compiles) its I<STRING> as a regular
4508 expression. I<STRING> is interpolated the same way as I<PATTERN>
4509 in C<m/I<PATTERN>/>. If C<"'"> is used as the delimiter, no variable
4510 interpolation is done. Returns a Perl value which may be used instead of the
4511 corresponding C</I<STRING>/msixpodualn> expression. The returned value is a
4512 normalized version of the original pattern. It magically differs from
4513 a string containing the same characters: C<ref(qr/x/)> returns "Regexp";
4514 however, dereferencing it is not well defined (you currently get the
4515 normalized version of the original pattern, but this may change).
4516
4517 =end original
4518
4519 この演算子は I<STRING> を正規表現としてクォートします
4520 (そして可能ならコンパイルします)。
4521 I<STRING> は C<m/I<PATTERN>/> 内の I<PATTERN> と同様に文字変換されます。
4522 C<"'"> がデリミタとして使用された場合、変数展開は行われません。
4523 対応する C</I<STRING>/msixpodualn> 表現の代わりに使われた Perl の値を返します。
4524 返り値は元のパターンを正規化したものです。
4525 これは不思議なことに、同じ文字を含む文字列とは異なります:
4526 C<ref(qr/x/)> は "Regexp" を返します; しかし、これのデリファレンスは
4527 定義されていません (現在の所元のパターンの正規化版を返しますが、これは
4528 変更されるかもしれません)。
4529
4530 =begin original
4531
4532 For example,
4533
4534 =end original
4535
4536 例えば:
4537
4538 $rex = qr/my.STRING/is;
4539 print $rex; # prints (?si-xm:my.STRING)
4540 s/$rex/foo/;
4541
4542 =begin original
4543
4544 is equivalent to
4545
4546 =end original
4547
4548 は以下と等価です:
4549
4550 s/my.STRING/foo/is;
4551
4552 =begin original
4553
4554 The result may be used as a subpattern in a match:
4555
4556 =end original
4557
4558 結果はマッチのサブパターンとして使えます:
4559
4560 =begin original
4561
4562 $re = qr/$pattern/;
4563 $string =~ /foo${re}bar/; # can be interpolated in other
4564 # patterns
4565 $string =~ $re; # or used standalone
4566 $string =~ /$re/; # or this way
4567
4568 =end original
4569
4570 $re = qr/$pattern/;
4571 $string =~ /foo${re}bar/; # 他のパターンに展開できる
4572 $string =~ $re; # または単独で使う
4573 $string =~ /$re/; # またはこのようにする
4574
4575 =begin original
4576
4577 Since Perl may compile the pattern at the moment of execution of the C<qr()>
4578 operator, using C<qr()> may have speed advantages in some situations,
4579 notably if the result of C<qr()> is used standalone:
4580
4581 =end original
4582
4583 Perl は C<qr()> 演算子を実行する瞬間にパターンをコンパイルするので、
4584 C<qr()> を使うことでいくつかの場面で速度的に有利になります;
4585 特に C<qr()> の結果が独立して使われる場合に有利になります。
4586
4587 sub match {
4588 my $patterns = shift;
4589 my @compiled = map qr/$_/i, @$patterns;
4590 grep {
4591 my $success = 0;
4592 foreach my $pat (@compiled) {
4593 $success = 1, last if /$pat/;
4594 }
4595 $success;
4596 } @_;
4597 }
4598
4599 =begin original
4600
4601 Precompilation of the pattern into an internal representation at
4602 the moment of C<qr()> avoids the need to recompile the pattern every
4603 time a match C</$pat/> is attempted. (Perl has many other internal
4604 optimizations, but none would be triggered in the above example if
4605 we did not use C<qr()> operator.)
4606
4607 =end original
4608
4609 C<qr()> の時点でパターンを内部表現にプリコンパイルすることにより、
4610 C</$pat/> を試みる毎に毎回パターンを再コンパイルするのを
4611 避けることができます。
4612 (Perl はその他にも多くの内部最適化を行いますが、上の例で C<qr()> 演算子を
4613 使わなかった場合はどの最適化も行われません。)
4614
4615 =begin original
4616
4617 Options (specified by the following modifiers) are:
4618
4619 =end original
4620
4621 (以下の修飾子で指定される)オプションは以下の通りです:
4622
4623 =begin original
4624
4625 m Treat string as multiple lines.
4626 s Treat string as single line. (Make . match a newline)
4627 i Do case-insensitive pattern matching.
4628 x Use extended regular expressions; specifying two
4629 x's means \t and the SPACE character are ignored within
4630 square-bracketed character classes
4631 p When matching preserve a copy of the matched string so
4632 that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be
4633 defined (ignored starting in v5.20) as these are always
4634 defined starting in that release
4635 o Compile pattern only once.
4636 a ASCII-restrict: Use ASCII for \d, \s, \w and [[:posix:]]
4637 character classes; specifying two a's adds the further
4638 restriction that no ASCII character will match a
4639 non-ASCII one under /i.
4640 l Use the current run-time locale's rules.
4641 u Use Unicode rules.
4642 d Use Unicode or native charset, as in 5.12 and earlier.
4643 n Non-capture mode. Don't let () fill in $1, $2, etc...
4644
4645 =end original
4646
4647 m 文字列を複数行として扱う。
4648 s 文字列を一行として扱う。 (. が 改行にマッチングするようにする)
4649 i パターンマッチにおいて大文字小文字を区別しない。
4650 x 拡張正規表現を使う; x を二つ指定すると、
4651 \t と SPACE 文字は大かっこ文字クラスの中では無視されます。
4652 p マッチング時にマッチングした文字列を保存するので、
4653 ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} が定義される。
4654 (v5.20 から無視される) このリリースから常に定義される
4655 o 一度だけコンパイルする。
4656 a ASCII 制限: \d, \s, \w, [[:posix:]] 文字クラスに ASCII を使う;
4657 a を二つ指定すると、/i で ASCII 文字が非 ASCII 文字に
4658 マッチングしないようなさらなる制限を追加する
4659 l 現在の実行時ロケールの規則を使う。
4660 u Unicode の規則を使う
4661 d 5.12 以降かどうかで、Unicode かネイティブな文字集合を使う
4662 n 非捕捉モード。() で $1, $2 などを埋めない
4663
4664 =begin original
4665
4666 If a precompiled pattern is embedded in a larger pattern then the effect
4667 of C<"msixpluadn"> will be propagated appropriately. The effect that the
4668 C</o> modifier has is not propagated, being restricted to those patterns
4669 explicitly using it.
4670
4671 =end original
4672
4673 プリコンパイルされたパターンがより大きいパターンに組み込まれている場合、
4674 C<"msixpluadn"> の効果は適切に伝播します。
4675 C</o> 修飾子の効果は伝播せず、明示的に使われたパターンに制限されます。
4676
4677 =begin original
4678
4679 The C</a>, C</d>, C</l>, and C</u> modifiers (added in Perl 5.14)
4680 control the character set rules, but C</a> is the only one you are likely
4681 to want to specify explicitly; the other three are selected
4682 automatically by various pragmas.
4683
4684 =end original
4685
4686 (Perl 5.14 で追加された) C</a>, C</d>, C</l>, C</u> 修飾子は 、
4687 文字集合の規則を制御しますが、明示的に指定したいと思いそうなものは
4688 C</a> だけでしょう;
4689 その他の三つはさまざまなプラグマによって自動的に選択されます。
4690
4691 =begin original
4692
4693 See L<perlre> for additional information on valid syntax for I<STRING>, and
4694 for a detailed look at the semantics of regular expressions. In
4695 particular, all modifiers except the largely obsolete C</o> are further
4696 explained in L<perlre/Modifiers>. C</o> is described in the next section.
4697
4698 =end original
4699
4700 I<STRING> として有効な文法に関する追加の情報と、正規表現の意味論に関する
4701 詳細については、L<perlre> を参照してください。
4702 特に、かなり古いものである C</o> 以外の全ての修飾子については
4703 L<perlre/Modifiers> でさらに説明されています。
4704 C</o> は次の節に記述されています。
4705
4706 =item C<m/I<PATTERN>/msixpodualngc>
4707 X<m> X<operator, match>
4708 X<regexp, options> X<regexp> X<regex, options> X<regex>
4709 X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c>
4710
4711 =item C</I<PATTERN>/msixpodualngc>
4712
4713 =begin original
4714
4715 Searches a string for a pattern match, and in scalar context returns
4716 true if it succeeds, false if it fails. If no string is specified
4717 via the C<=~> or C<!~> operator, the C<$_> string is searched. (The
4718 string specified with C<=~> need not be an lvalue--it may be the
4719 result of an expression evaluation, but remember the C<=~> binds
4720 rather tightly.) See also L<perlre>.
4721
4722 =end original
4723
4724 パターンマッチで文字列検索を行ない、スカラコンテキストでは成功したときは真、
4725 失敗したときは偽を返します。
4726 C<=~> 演算子か C<!~> 演算子で検索対象の文字列を示さなかったときには、
4727 C<$_> の文字列が検索対象となります。
4728 (C<=~> で指定される文字列は、左辺値である必要はありません--
4729 式を評価した結果でもかまいませんが、C<=~> の優先順位がいくぶん高いことに
4730 注意してください。)
4731 L<perlre> も参照してください。
4732
4733 =begin original
4734
4735 Options are as described in C<qr//> above; in addition, the following match
4736 process modifiers are available:
4737
4738 =end original
4739
4740 オプションは上述した C<qr//> に記述されています; さらに、以下の
4741 マッチング処理修飾子が利用可能です:
4742
4743 =begin original
4744
4745 g Match globally, i.e., find all occurrences.
4746 c Do not reset search position on a failed match when /g is
4747 in effect.
4748
4749 =end original
4750
4751 g グローバルにマッチング、つまり、すべてを探し出す。
4752 c /g が有効なとき、マッチングに失敗しても検索位置をリセットしない。
4753
4754 =begin original
4755
4756 If C<"/"> is the delimiter then the initial C<m> is optional. With the C<m>
4757 you can use any pair of non-whitespace (ASCII) characters
4758 as delimiters. This is particularly useful for matching path names
4759 that contain C<"/">, to avoid LTS (leaning toothpick syndrome). If C<"?"> is
4760 the delimiter, then a match-only-once rule applies,
4761 described in C<m?I<PATTERN>?> below. If C<"'"> (single quote) is the delimiter,
4762 no variable interpolation is performed on the I<PATTERN>.
4763 When using a delimiter character valid in an identifier, whitespace is required
4764 after the C<m>.
4765
4766 =end original
4767
4768 区切文字が C<"/"> のときには、最初の C<m> は付けても付けなくてもかまいません。
4769 C<m> を付けるときには、(ASCII の)空白でもない、任意の文字のペアを
4770 区切文字として使うことができます。
4771 これは特に、C<"/"> を含むパス名にパターンマッチングを行なうときに、
4772 LTS (傾斜楊枝症候群) を避けるために便利でしょう。
4773 "?" がデリミタなら、後述する C<m?I<PATTERN>?> にある「一度だけマッチング」
4774 ルールが適用されます。
4775 C<"'"> (シングルクォート) がデリミタの場合、I<PATTERN> に対する変数展開は
4776 行われません。
4777 識別子として有効な区切り文字を使う場合、C<m> の後に空白が必要です。
4778
4779 =begin original
4780
4781 I<PATTERN> may contain variables, which will be interpolated
4782 every time the pattern search is evaluated, except
4783 for when the delimiter is a single quote. (Note that C<$(>, C<$)>, and
4784 C<$|> are not interpolated because they look like end-of-string tests.)
4785 Perl will not recompile the pattern unless an interpolated
4786 variable that it contains changes. You can force Perl to skip the
4787 test and never recompile by adding a C</o> (which stands for "once")
4788 after the trailing delimiter.
4789 Once upon a time, Perl would recompile regular expressions
4790 unnecessarily, and this modifier was useful to tell it not to do so, in the
4791 interests of speed. But now, the only reasons to use C</o> are one of:
4792
4793 =end original
4794
4795 I<PATTERN> には、変数が含まれていてもよく、パターンが評価されるごとに、
4796 (デリミタがシングルクォートでない限り)変数は展開され
4797 (パターンが再コンパイルされ) ます。
4798 (変数 C<$(>, C<$)>, C<$|> は文字列の終わりを調べるパターンであると
4799 解釈されるので、展開されません。)
4800 Perl は展開された変数の値が変更されない限りパターンを再コンパイルしません。
4801 デリミタに引き続いて C</o> ("once" を意味します) を追加することで、
4802 テストを飛ばして再コンパイルしないようにすることができます。
4803 昔々、Perl は不必要に正規表現を再コンパイルしていたので、速度に関心が
4804 ある場合は再コンパイルしないようにするためにこの修飾子は有用でした。
4805 しかし今では、C</o> を使う理由は以下のいずれかだけです:
4806
4807 =over
4808
4809 =item 1
4810
4811 =begin original
4812
4813 The variables are thousands of characters long and you know that they
4814 don't change, and you need to wring out the last little bit of speed by
4815 having Perl skip testing for that. (There is a maintenance penalty for
4816 doing this, as mentioning C</o> constitutes a promise that you won't
4817 change the variables in the pattern. If you do change them, Perl won't
4818 even notice.)
4819
4820 =end original
4821
4822 変数が数千文字の長さで、これが変更されないことが分かっており、これに対する
4823 テストを飛ばすことであともう少しだけ速度を稼ぐ必要がある。
4824 (こうすることには保守上のペナルティがあります; なぜなら C</o> と
4825 言及することでパターン内の変数を変更しないことを約束したことになるからです。
4826 変更しても、Perl は気づきもしません。)
4827
4828 =item 2
4829
4830 =begin original
4831
4832 you want the pattern to use the initial values of the variables
4833 regardless of whether they change or not. (But there are saner ways
4834 of accomplishing this than using C</o>.)
4835
4836 =end original
4837
4838 変数が変更されようが変更されまいが、変数の初期値を使ったパターンがほしい。
4839 (しかしこれを達成するための、C</o> を使うよりもまともな方法があります。)
4840
4841 =item 3
4842
4843 =begin original
4844
4845 If the pattern contains embedded code, such as
4846
4847 =end original
4848
4849 以下のようにパターンに組み込みコードが含まれている場合
4850
4851 use re 'eval';
4852 $code = 'foo(?{ $x })';
4853 /$code/
4854
4855 =begin original
4856
4857 then perl will recompile each time, even though the pattern string hasn't
4858 changed, to ensure that the current value of C<$x> is seen each time.
4859 Use C</o> if you want to avoid this.
4860
4861 =end original
4862
4863 C<$x> の現在の値を毎回確認するために、例えパターン文字列が
4864 変更されていなくても、毎回再コンパイルされます。
4865 これを避けたい場合は C</o> を使ってください。
4866
4867 =back
4868
4869 =begin original
4870
4871 The bottom line is that using C</o> is almost never a good idea.
4872
4873 =end original
4874
4875 結論としては、C</o> を使うことがいい考えであることはほとんどありません。
4876
4877 =item The empty pattern C<//>
4878
4879 (空パターン C<//>)
4880
4881 =begin original
4882
4883 If the I<PATTERN> evaluates to the empty string, the last
4884 I<successfully> matched regular expression is used instead. In this
4885 case, only the C<g> and C<c> flags on the empty pattern are honored;
4886 the other flags are taken from the original pattern. If no match has
4887 previously succeeded, this will (silently) act instead as a genuine
4888 empty pattern (which will always match).
4889
4890 =end original
4891
4892 I<PATTERN> を評価した結果が空文字列となった場合には、最後にマッチに
4893 I<成功した> 正規表現が、代わりに使われます。
4894 この場合、空パターンに対して C<g> の C<c> フラグだけが有効です;
4895 その他のフラグは元のパターンから取られます。
4896 以前に成功したマッチングがない場合、これは(暗黙に)真の空パターンとして
4897 動作します(つまり常にマッチングします)。
4898
4899 =begin original
4900
4901 Note that it's possible to confuse Perl into thinking C<//> (the empty
4902 regex) is really C<//> (the defined-or operator). Perl is usually pretty
4903 good about this, but some pathological cases might trigger this, such as
4904 C<$x///> (is that S<C<($x) / (//)>> or S<C<$x // />>?) and S<C<print $fh //>>
4905 (S<C<print $fh(//>> or S<C<print($fh //>>?). In all of these examples, Perl
4906 will assume you meant defined-or. If you meant the empty regex, just
4907 use parentheses or spaces to disambiguate, or even prefix the empty
4908 regex with an C<m> (so C<//> becomes C<m//>).
4909
4910 =end original
4911
4912 Perl が C<//> (空正規表現) と C<//> (定義性和演算子) を混同する
4913 可能性があることに注意してください。
4914 Perl は普通これをかなりうまく処理しますが、C<$x///> (S<C<($x) / (//)>>
4915 それとも S<C<$x // />>?) や S<C<print $fh //>> (S<C<print $fh(//>>
4916 それとも S<C<print($fh //>>?) のような病的な状況ではこれが起こりえます。
4917 これらの例の全てでは、Perl は定義性和を意味していると仮定します。
4918 もし空正規表現を意味したいなら、あいまいさをなくすために単に
4919 かっこや空白を使うか、空正規表現に接頭辞 C<m> を付けてください
4920 (つまり C<//> を C<m//> にします)。
4921
4922 =item Matching in list context
4923
4924 (リストコンテキストでのマッチング)
4925
4926 =begin original
4927
4928 If the C</g> option is not used, C<m//> in list context returns a
4929 list consisting of the subexpressions matched by the parentheses in the
4930 pattern, that is, (C<$1>, C<$2>, C<$3>...) (Note that here C<$1> etc. are
4931 also set). When there are no parentheses in the pattern, the return
4932 value is the list C<(1)> for success.
4933 With or without parentheses, an empty list is returned upon failure.
4934
4935 =end original
4936
4937 C</g> オプションが使われなかった場合、リストコンテキストでのC<m//>は
4938 パターンの中の括弧で括られた部分列にマッチしたもので構成されるリストを
4939 返します; これは、(C<$1>, C<$2>, C<$3>, ...) ということです
4940 (この場合、C<$1> なども設定されます)。
4941 パターンに括弧がない場合は、返り値は成功時はリスト C<(1)> です。
4942 括弧のあるなしに関わらず、失敗時は空リストを返します。
4943
4944 =begin original
4945
4946 Examples:
4947
4948 =end original
4949
4950 例:
4951
4952 open(TTY, "+</dev/tty")
4953 || die "can't access /dev/tty: $!";
4954
4955 <TTY> =~ /^y/i && foo(); # do foo if desired
4956
4957 if (/Version: *([0-9.]*)/) { $version = $1; }
4958
4959 next if m#^/usr/spool/uucp#;
4960
4961 # poor man's grep
4962 $arg = shift;
4963 while (<>) {
4964 print if /$arg/o; # compile only once (no longer needed!)
4965 }
4966
4967 if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
4968
4969 =begin original
4970
4971 This last example splits C<$foo> into the first two words and the
4972 remainder of the line, and assigns those three fields to C<$F1>, C<$F2>, and
4973 C<$Etc>. The conditional is true if any variables were assigned; that is,
4974 if the pattern matched.
4975
4976 =end original
4977
4978 最後の例は、C<$foo> を最初の 2 つの単語と行の残りに分解し、
4979 C<$F1> と C<$F2> と C<$Etc> に代入しています。
4980 変数に代入されれば、すなわちパターンがマッチすれば、
4981 if の条件が真となります。
4982
4983 =begin original
4984
4985 The C</g> modifier specifies global pattern matching--that is,
4986 matching as many times as possible within the string. How it behaves
4987 depends on the context. In list context, it returns a list of the
4988 substrings matched by any capturing parentheses in the regular
4989 expression. If there are no parentheses, it returns a list of all
4990 the matched strings, as if there were parentheses around the whole
4991 pattern.
4992
4993 =end original
4994
4995 C</g> 修飾子は、グローバルなパターンマッチを指定するもので、
4996 文字列の中で可能な限りたくさんマッチを行ないます。
4997 この動作は、コンテキストに依存します。
4998 リストコンテキストでは、正規表現内の括弧付けされたものにマッチした
4999 部分文字列のリストが返されます。
5000 括弧がなければ、パターン全体を括弧で括っていたかのように、
5001 すべてのマッチした文字列のリストが返されます。
5002
5003 =begin original
5004
5005 In scalar context, each execution of C<m//g> finds the next match,
5006 returning true if it matches, and false if there is no further match.
5007 The position after the last match can be read or set using the C<pos()>
5008 function; see L<perlfunc/pos>. A failed match normally resets the
5009 search position to the beginning of the string, but you can avoid that
5010 by adding the C</c> modifier (for example, C<m//gc>). Modifying the target
5011 string also resets the search position.
5012
5013 =end original
5014
5015 スカラコンテキストでは、C<m//g> を実行する毎に次のマッチを探します;
5016 マッチした場合は真を返し、もうマッチしなくなったら偽を返します。
5017 最後のマッチの位置は C<pos()> 関数で読み出しや設定ができます;
5018 L<perlfunc/pos> を参照して下さい。
5019 マッチに失敗すると通常は検索位置を文字列の先頭にリセットしますが、
5020 C</c> 修飾子をつける(例えば C<m//gc>)ことでこれを防ぐことができます。
5021 ターゲットとなる文字列が変更された場合も検索位置はリセットされます。
5022
5023 =item C<\G I<assertion>>
5024
5025 (C<\G アサート>)
5026
5027 =begin original
5028
5029 You can intermix C<m//g> matches with C<m/\G.../g>, where C<\G> is a
5030 zero-width assertion that matches the exact position where the
5031 previous C<m//g>, if any, left off. Without the C</g> modifier, the
5032 C<\G> assertion still anchors at C<pos()> as it was at the start of
5033 the operation (see L<perlfunc/pos>), but the match is of course only
5034 attempted once. Using C<\G> without C</g> on a target string that has
5035 not previously had a C</g> match applied to it is the same as using
5036 the C<\A> assertion to match the beginning of the string. Note also
5037 that, currently, C<\G> is only properly supported when anchored at the
5038 very beginning of the pattern.
5039
5040 =end original
5041
5042 C<m//g> マッチを C<m/\G.../g> と混ぜることもできます; C<\G> は前回の
5043 C<m//g> があればその同じ位置でマッチするゼロ文字幅のアサートです。
5044 C</g> 修飾子なしの場合、C<\G> アサートは操作の最初としてC<pos()> に
5045 固定しますが、(L<perlfunc/pos> 参照) マッチはもちろん一度だけ試されます。
5046 以前に C</g> マッチを適用していないターゲット文字列に対して C</g> なしで
5047 C<\G> を使うと、文字列の先頭にマッチする C<\A> アサートを使うのと
5048 同じことになります。
5049 C<\G> は現在のところ、パターンのまさに先頭を示す場合にのみ正しく
5050 対応することにも注意してください。
5051
5052 =begin original
5053
5054 Examples:
5055
5056 =end original
5057
5058 例:
5059
5060 # list context
5061 ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
5062
5063 # scalar context
5064 local $/ = "";
5065 while ($paragraph = <>) {
5066 while ($paragraph =~ /\p{Ll}['")]*[.!?]+['")]*\s/g) {
5067 $sentences++;
5068 }
5069 }
5070 say $sentences;
5071
5072 =begin original
5073
5074 Here's another way to check for sentences in a paragraph:
5075
5076 =end original
5077
5078 以下は段落内の文をチェックするためのもう一つの方法です:
5079
5080 =begin original
5081
5082 my $sentence_rx = qr{
5083 (?: (?<= ^ ) | (?<= \s ) ) # after start-of-string or
5084 # whitespace
5085 \p{Lu} # capital letter
5086 .*? # a bunch of anything
5087 (?<= \S ) # that ends in non-
5088 # whitespace
5089 (?<! \b [DMS]r ) # but isn't a common abbr.
5090 (?<! \b Mrs )
5091 (?<! \b Sra )
5092 (?<! \b St )
5093 [.?!] # followed by a sentence
5094 # ender
5095 (?= $ | \s ) # in front of end-of-string
5096 # or whitespace
5097 }sx;
5098 local $/ = "";
5099 while (my $paragraph = <>) {
5100 say "NEW PARAGRAPH";
5101 my $count = 0;
5102 while ($paragraph =~ /($sentence_rx)/g) {
5103 printf "\tgot sentence %d: <%s>\n", ++$count, $1;
5104 }
5105 }
5106
5107 =end original
5108
5109 my $sentence_rx = qr{
5110 (?: (?<= ^ ) | (?<= \s ) ) # 文字列の先頭か空白の後
5111 \p{Lu} # 大文字
5112 .*? # なんでも
5113 (?<= \S ) # 空白以外で終わる
5114 (?<! \b [DMS]r ) # しかし一般的な省略形ではない
5115 (?<! \b Mrs )
5116 (?<! \b Sra )
5117 (?<! \b St )
5118 [.?!] # 引き続いて文を終わらせるものが
5119 (?= $ | \s ) # 文字列の末尾か空白の前に
5120 }sx;
5121 local $/ = "";
5122 while (my $paragraph = <>) {
5123 say "NEW PARAGRAPH";
5124 my $count = 0;
5125 while ($paragraph =~ /($sentence_rx)/g) {
5126 printf "\tgot sentence %d: <%s>\n", ++$count, $1;
5127 }
5128 }
5129
5130 =begin original
5131
5132 Here's how to use C<m//gc> with C<\G>:
5133
5134 =end original
5135
5136 以下は C<m//gc> を C<\G> で使う方法です:
5137
5138 $_ = "ppooqppqq";
5139 while ($i++ < 2) {
5140 print "1: '";
5141 print $1 while /(o)/gc; print "', pos=", pos, "\n";
5142 print "2: '";
5143 print $1 if /\G(q)/gc; print "', pos=", pos, "\n";
5144 print "3: '";
5145 print $1 while /(p)/gc; print "', pos=", pos, "\n";
5146 }
5147 print "Final: '$1', pos=",pos,"\n" if /\G(.)/;
5148
5149 =begin original
5150
5151 The last example should print:
5152
5153 =end original
5154
5155 最後のものは以下のものを表示するはずです:
5156
5157 1: 'oo', pos=4
5158 2: 'q', pos=5
5159 3: 'pp', pos=7
5160 1: '', pos=7
5161 2: 'q', pos=8
5162 3: '', pos=8
5163 Final: 'q', pos=8
5164
5165 =begin original
5166
5167 Notice that the final match matched C<q> instead of C<p>, which a match
5168 without the C<\G> anchor would have done. Also note that the final match
5169 did not update C<pos>. C<pos> is only updated on a C</g> match. If the
5170 final match did indeed match C<p>, it's a good bet that you're running an
5171 ancient (pre-5.6.0) version of Perl.
5172
5173 =end original
5174
5175 C<\G> なしでのマッチが行われたため、最後のマッチでは C<p> ではなく
5176 C<q> がマッチすることに注意してください。
5177 また、最後のマッチは C<pos> を更新しないことに注意してください。
5178 C<pos> は C</g> マッチでのみ更新されます。
5179 もし最後のマッチで C<p> にマッチした場合、かなりの確率で
5180 とても古い (5.6.0 以前の) Perl で実行しているはずです。
5181
5182 =begin original
5183
5184 A useful idiom for C<lex>-like scanners is C</\G.../gc>. You can
5185 combine several regexps like this to process a string part-by-part,
5186 doing different actions depending on which regexp matched. Each
5187 regexp tries to match where the previous one leaves off.
5188
5189 =end original
5190
5191 C<lex> 風にスキャンするために便利な指定は C</\G.../gc> です。
5192 文字列を部分ごとに処理するためにいくつかの正規表現をつなげて、どの正規表現に
5193 マッチングしたかによって異なる処理をすることができます。
5194 それぞれの正規表現は前の正規表現が飛ばした部分に対してマッチングを試みます。
5195
5196 $_ = <<'EOL';
5197 $url = URI::URL->new( "http://example.com/" );
5198 die if $url eq "xXx";
5199 EOL
5200
5201 LOOP: {
5202 print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
5203 print(" lowercase"), redo LOOP
5204 if /\G\p{Ll}+\b[,.;]?\s*/gc;
5205 print(" UPPERCASE"), redo LOOP
5206 if /\G\p{Lu}+\b[,.;]?\s*/gc;
5207 print(" Capitalized"), redo LOOP
5208 if /\G\p{Lu}\p{Ll}+\b[,.;]?\s*/gc;
5209 print(" MiXeD"), redo LOOP if /\G\pL+\b[,.;]?\s*/gc;
5210 print(" alphanumeric"), redo LOOP
5211 if /\G[\p{Alpha}\pN]+\b[,.;]?\s*/gc;
5212 print(" line-noise"), redo LOOP if /\G\W+/gc;
5213 print ". That's all!\n";
5214 }
5215
5216 =begin original
5217
5218 Here is the output (split into several lines):
5219
5220 =end original
5221
5222 出力は以下のようになります(何行かに分割しています):
5223
5224 line-noise lowercase line-noise UPPERCASE line-noise UPPERCASE
5225 line-noise lowercase line-noise lowercase line-noise lowercase
5226 lowercase line-noise lowercase lowercase line-noise lowercase
5227 lowercase line-noise MiXeD line-noise. That's all!
5228
5229 =item C<m?I<PATTERN>?msixpodualngc>
5230 X<?> X<operator, match-once>
5231
5232 =begin original
5233
5234 This is just like the C<m/I<PATTERN>/> search, except that it matches
5235 only once between calls to the C<reset()> operator. This is a useful
5236 optimization when you want to see only the first occurrence of
5237 something in each file of a set of files, for instance. Only C<m??>
5238 patterns local to the current package are reset.
5239
5240 =end original
5241
5242 これは、C<reset()> 演算子を呼び出すごとに 1 度だけしかマッチングしないことを
5243 除いては C<m/I<PATTERN>/> による検索と全く同じです。
5244 たとえば、ファイルの集まりの中で個々のファイルについて、あるものを
5245 探すとき、最初の 1 つだけの存在がわかれば良いのであれば、この機能を
5246 使って最適化をはかることができます。
5247 現在のパッケージにローカルとなっている C<m??> のパターンだけが
5248 リセットされます。
5249
5250 while (<>) {
5251 if (m?^$?) {
5252 # blank line between header and body
5253 }
5254 } continue {
5255 reset if eof; # clear m?? status for next file
5256 }
5257
5258 =begin original
5259
5260 Another example switched the first "latin1" encoding it finds
5261 to "utf8" in a pod file:
5262
5263 =end original
5264
5265 次の例は、pod ファイル中の最初の "latin1" エンコーディングを "utf8" に
5266 切り替えます:
5267
5268 s//utf8/ if m? ^ =encoding \h+ \K latin1 ?x;
5269
5270 =begin original
5271
5272 The match-once behavior is controlled by the match delimiter being
5273 C<?>; with any other delimiter this is the normal C<m//> operator.
5274
5275 =end original
5276
5277 一度だけマッチングという振る舞いはマッチングデリミタが C<?> かどうかで
5278 制御されます: その他のデリミタの場合はこれは通常の C<m//> 演算子です。
5279
5280 =begin original
5281
5282 In the past, the leading C<m> in C<m?I<PATTERN>?> was optional, but omitting it
5283 would produce a deprecation warning. As of v5.22.0, omitting it produces a
5284 syntax error. If you encounter this construct in older code, you can just add
5285 C<m>.
5286
5287 =end original
5288
5289 過去には、C<m?PATTERN?> の先頭の C<m> は省略可能でしたが、これを省略すると
5290 廃止予定警告が出ていました。
5291 v5.22.0 から、これを省略すると文法エラーが発生します。
5292 もし古いコードでこの構文に遭遇した場合は、単に C<m> を追加してください。
5293
5294 =item C<s/I<PATTERN>/I<REPLACEMENT>/msixpodualngcer>
5295 X<s> X<substitute> X<substitution> X<replace> X<regexp, replace>
5296 X<regexp, substitute> X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> X</e> X</r>
5297
5298 =begin original
5299
5300 Searches a string for a pattern, and if found, replaces that pattern
5301 with the replacement text and returns the number of substitutions
5302 made. Otherwise it returns false (a value that is both an empty string (C<"">)
5303 and numeric zero (C<0>) as described in L</Relational Operators>).
5304
5305 =end original
5306
5307 文字列中でパターンを検索し、もし見つかれば、置換テキストで置き換え、
5308 置換した数を返します。
5309 見つからなければ、偽 (空文字列 (C<"">) と数値のゼロ (C<0> の両方) を
5310 返します。
5311
5312 =begin original
5313
5314 If the C</r> (non-destructive) option is used then it runs the
5315 substitution on a copy of the string and instead of returning the
5316 number of substitutions, it returns the copy whether or not a
5317 substitution occurred. The original string is never changed when
5318 C</r> is used. The copy will always be a plain string, even if the
5319 input is an object or a tied variable.
5320
5321 =end original
5322
5323 C</r> (非破壊) オプションが使われると、文字列のコピーに対して置換が行われ、
5324 置換された数ではなく、置換が行われたかどうかにかかわらずこのコピーが
5325 返されます。
5326 C</r> が使われた場合、元の文字列は決して変更されません。
5327 コピーは、たとえ入力がオブジェクトや tie された変数でも、常に
5328 プレーンな文字列です。
5329
5330 =begin original
5331
5332 If no string is specified via the C<=~> or C<!~> operator, the C<$_>
5333 variable is searched and modified. Unless the C</r> option is used,
5334 the string specified must be a scalar variable, an array element, a
5335 hash element, or an assignment to one of those; that is, some sort of
5336 scalar lvalue.
5337
5338 =end original
5339
5340 C<=~> 演算子や C<!~> 演算子によって文字列が指定されていなければ、
5341 変数 C<$_> が検索され、修正されます。
5342 C</r> が指定されていない限り、
5343 C<=~> で指定される文字列は、スカラ変数、配列要素、ハッシュ要素、
5344 あるいは、これらへの代入式といったある種の
5345 スカラ左辺値でなければなりません。
5346
5347 =begin original
5348
5349 If the delimiter chosen is a single quote, no variable interpolation is
5350 done on either the I<PATTERN> or the I<REPLACEMENT>. Otherwise, if the
5351 I<PATTERN> contains a C<$> that looks like a variable rather than an
5352 end-of-string test, the variable will be interpolated into the pattern
5353 at run-time. If you want the pattern compiled only once the first time
5354 the variable is interpolated, use the C</o> option. If the pattern
5355 evaluates to the empty string, the last successfully executed regular
5356 expression is used instead. See L<perlre> for further explanation on these.
5357
5358 =end original
5359
5360 シングルクォートを区切り文字として使った場合には、
5361 I<PATTERN> にも I<REPLACEMENT> にも変数展開を行ないません。
5362 それ以外の場合、文字列の最後を表わすものには見えない C<$> が
5363 I<PATTERN> に含まれると、実行時に変数がパターン内に展開されます。
5364 最初に変数が展開されるときにだけパターンのコンパイルを行ないたいときには、
5365 C</o> オプションを使ってください。
5366 パターンの評価結果が空文字列になった場合には、最後に成功した正規表現が
5367 代わりに使われます。
5368 これについてさらに詳しくは、L<perlre> を参照してください。
5369
5370 =begin original
5371
5372 Options are as with C<m//> with the addition of the following replacement
5373 specific options:
5374
5375 =end original
5376
5377 オプションは、C<m//> のものに加えて、以下の置換固有のものがあります:
5378
5379 =begin original
5380
5381 e Evaluate the right side as an expression.
5382 ee Evaluate the right side as a string then eval the
5383 result.
5384 r Return substitution and leave the original string
5385 untouched.
5386
5387 =end original
5388
5389 e 式の右側の評価を行なう。
5390 ee 右側を文字列として評価して、その結果を評価する。
5391 r 置換した結果を返し、もとの文字列はそのままにする。
5392
5393 =begin original
5394
5395 Any non-whitespace delimiter may replace the slashes. Add space after
5396 the C<s> when using a character allowed in identifiers. If single quotes
5397 are used, no interpretation is done on the replacement string (the C</e>
5398 modifier overrides this, however). Note that Perl treats backticks
5399 as normal delimiters; the replacement text is not evaluated as a command.
5400 If the I<PATTERN> is delimited by bracketing quotes, the I<REPLACEMENT> has
5401 its own pair of quotes, which may or may not be bracketing quotes, for example,
5402 C<s(foo)(bar)> or C<< s<foo>/bar/ >>. A C</e> will cause the
5403 replacement portion to be treated as a full-fledged Perl expression
5404 and evaluated right then and there. It is, however, syntax checked at
5405 compile-time. A second C<e> modifier will cause the replacement portion
5406 to be C<eval>ed before being run as a Perl expression.
5407
5408 =end original
5409
5410 空白ではない任意の区切り文字で、スラッシュを置き換えられます。
5411 識別子として許されている文字を使うときには C<s> の後に空白を
5412 追加してください。
5413 先に述べたように、シングルクォートを使うと置換文字列での展開は
5414 されません (C</e>修飾子を使えば可能です)。
5415 バッククォートを通常のデリミタとして扱うことに注意してください;
5416 置換テキストはコマンドとして評価されません。
5417 I<PATTERN> を括弧類で括った場合には、I<REPLACEMENT> 用にもう一組の区切り文字を
5418 用意します; これは、括弧類であっても、なくてもかまいません;
5419 C<s(foo)(bar)> や C<< s<foo>/bar/ >>。
5420 C</e> は置換文字列を完全な Perl の式として扱い、その場所で直ちに解釈します。
5421 しかし、これはコンパイル時に構文チェックされます。
5422 二番目の C<e> 修飾子を指定すると、置換部分がまず Perl の式として
5423 C<eval> されます。
5424
5425 =begin original
5426
5427 Examples:
5428
5429 =end original
5430
5431 例:
5432
5433 s/\bgreen\b/mauve/g; # don't change wintergreen
5434
5435 $path =~ s|/usr/bin|/usr/local/bin|;
5436
5437 s/Login: $foo/Login: $bar/; # run-time pattern
5438
5439 ($foo = $bar) =~ s/this/that/; # copy first, then
5440 # change
5441 ($foo = "$bar") =~ s/this/that/; # convert to string,
5442 # copy, then change
5443 $foo = $bar =~ s/this/that/r; # Same as above using /r
5444 $foo = $bar =~ s/this/that/r
5445 =~ s/that/the other/r; # Chained substitutes
5446 # using /r
5447 @foo = map { s/this/that/r } @bar # /r is very useful in
5448 # maps
5449
5450 $count = ($paragraph =~ s/Mister\b/Mr./g); # get change-cnt
5451
5452 $_ = 'abc123xyz';
5453 s/\d+/$&*2/e; # yields 'abc246xyz'
5454 s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz'
5455 s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz'
5456
5457 s/%(.)/$percent{$1}/g; # change percent escapes; no /e
5458 s/%(.)/$percent{$1} || $&/ge; # expr now, so /e
5459 s/^=(\w+)/pod($1)/ge; # use function call
5460
5461 $_ = 'abc123xyz';
5462 $x = s/abc/def/r; # $x is 'def123xyz' and
5463 # $_ remains 'abc123xyz'.
5464
5465 # expand variables in $_, but dynamics only, using
5466 # symbolic dereferencing
5467 s/\$(\w+)/${$1}/g;
5468
5469 # Add one to the value of any numbers in the string
5470 s/(\d+)/1 + $1/eg;
5471
5472 # Titlecase words in the last 30 characters only
5473 substr($str, -30) =~ s/\b(\p{Alpha}+)\b/\u\L$1/g;
5474
5475 # This will expand any embedded scalar variable
5476 # (including lexicals) in $_ : First $1 is interpolated
5477 # to the variable name, and then evaluated
5478 s/(\$\w+)/$1/eeg;
5479
5480 # Delete (most) C comments.
5481 $program =~ s {
5482 /\* # Match the opening delimiter.
5483 .*? # Match a minimal number of characters.
5484 \*/ # Match the closing delimiter.
5485 } []gsx;
5486
5487 s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_,
5488 # expensively
5489
5490 for ($variable) { # trim whitespace in $variable,
5491 # cheap
5492 s/^\s+//;
5493 s/\s+$//;
5494 }
5495
5496 s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields
5497
5498 $foo !~ s/A/a/g; # Lowercase all A's in $foo; return
5499 # 0 if any were found and changed;
5500 # otherwise return 1
5501
5502 =begin original
5503
5504 Note the use of C<$> instead of C<\> in the last example. Unlike
5505 B<sed>, we use the \<I<digit>> form only in the left hand side.
5506 Anywhere else it's $<I<digit>>.
5507
5508 =end original
5509
5510 最後の例で C<\> の代わりに C<$> を使っているのに注意してください。
5511 B<sed> と違って、\<I<数字>> の形式はパターンの方でのみ使用できます。
5512 その他の場所では、$<I<数字>> を使います。
5513
5514 =begin original
5515
5516 Occasionally, you can't use just a C</g> to get all the changes
5517 to occur that you might want. Here are two common cases:
5518
5519 =end original
5520
5521 ときには、C</g> を付けるだけでは、あなたが望んでいるような形で
5522 すべてを変更することができないことがあります。
5523 良くある例を 2 つ示します:
5524
5525 # put commas in the right places in an integer
5526 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;
5527
5528 # expand tabs to 8-column spacing
5529 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
5530
5531 =begin original
5532
5533 X</c>While C<s///> accepts the C</c> flag, it has no effect beyond
5534 producing a warning if warnings are enabled.
5535
5536 =end original
5537
5538 X</c>
5539 C<s///> は C</c> フラグを受け付けますが、
5540 警告が有効の場合に警告を出す以外の効果はありません。
5541
5542 =back
5543
5544 =head2 Quote-Like Operators
5545 X<operator, quote-like>
5546
5547 (クォート風演算子)
5548
5549 =over 4
5550
5551 =item C<q/I<STRING>/>
5552 X<q> X<quote, single> X<'> X<''>
5553
5554 =item C<'I<STRING>'>
5555
5556 =begin original
5557
5558 A single-quoted, literal string. A backslash represents a backslash
5559 unless followed by the delimiter or another backslash, in which case
5560 the delimiter or backslash is interpolated.
5561
5562 =end original
5563
5564 シングルクォートされた、リテラル文字列です。
5565 バックスラッシュは、後ろに続くものが区切文字か、別のバックスラッシュで
5566 ある場合を除いて単なるバックスラッシュです;
5567 区切文字やバックスラッシュが続く場合には、その区切文字自身もしくは
5568 バックスラッシュそのものが展開されます。
5569
5570 $foo = q!I said, "You said, 'She said it.'"!;
5571 $bar = q('This is it.');
5572 $baz = '\n'; # a two-character string
5573
5574 =item C<qq/I<STRING>/>
5575 X<qq> X<quote, double> X<"> X<"">
5576
5577 =item C<"I<STRING>">
5578
5579 =begin original
5580
5581 A double-quoted, interpolated string.
5582
5583 =end original
5584
5585 ダブルクォートされた、リテラル文字列です。
5586
5587 $_ .= qq
5588 (*** The previous line contains the naughty word "$1".\n)
5589 if /\b(tcl|java|python)\b/i; # :-)
5590 $baz = "\n"; # a one-character string
5591
5592 =item C<qx/I<STRING>/>
5593 X<qx> X<`> X<``> X<backtick>
5594
5595 =item C<`I<STRING>`>
5596
5597 =begin original
5598
5599 A string which is (possibly) interpolated and then executed as a
5600 system command, via F</bin/sh> or its equivalent if required. Shell
5601 wildcards, pipes, and redirections will be honored. Similarly to
5602 C<system>, if the string contains no shell metacharacters then it will
5603 executed directly. The collected standard output of the command is
5604 returned; standard error is unaffected. In scalar context, it comes
5605 back as a single (potentially multi-line) string, or C<undef> if the
5606 shell (or command) could not be started. In list context, returns a
5607 list of lines (however you've defined lines with C<$/> or
5608 C<$INPUT_RECORD_SEPARATOR>), or an empty list if the shell (or command)
5609 could not be started.
5610
5611 =end original
5612
5613 展開され、必要なら F</bin/sh> またはそれと等価なものでシステムの
5614 コマンドとして実行される(であろう)文字列です。
5615 シェルのワイルドカード、パイプ、リダイレクトが有効です。
5616 C<system> と同様、文字列にシェルメタ文字が含まれていないときは、
5617 直接実行されます。
5618 そのコマンドの、標準出力を集めたものが返されます; 標準エラーは影響を
5619 与えません。
5620 スカラコンテキストでは、(複数行を含むかもしれない)
5621 1 つの文字列が戻ってきます;
5622 シェル (またはコマンド) が開始できなかったときは C<undef> を返します。
5623 リストコンテキストでは、(C<$/> もしくは C<$INPUT_RECORD_SEPARATOR> を
5624 どのように設定していても) 行のリストを返します;
5625 シェル (またはコマンド) が開始できなかったときは
5626 空リストを返します。
5627
5628 =begin original
5629
5630 Because backticks do not affect standard error, use shell file descriptor
5631 syntax (assuming the shell supports this) if you care to address this.
5632 To capture a command's STDERR and STDOUT together:
5633
5634 =end original
5635
5636 バッククォートは標準エラーには影響を与えないので、標準エラーを
5637 使いたい場合は(シェルが対応しているものとして)シェルのファイル記述子の
5638 文法を使ってください。
5639 コマンドの STDERR と STDOUT を共に取得したい場合は:
5640
5641 $output = `cmd 2>&1`;
5642
5643 =begin original
5644
5645 To capture a command's STDOUT but discard its STDERR:
5646
5647 =end original
5648
5649 コマンドの STDOUT は取得するが STDERR は捨てる場合は:
5650
5651 $output = `cmd 2>/dev/null`;
5652
5653 =begin original
5654
5655 To capture a command's STDERR but discard its STDOUT (ordering is
5656 important here):
5657
5658 =end original
5659
5660 コマンドの STDERR は取得するが STDOUT は捨てる場合は
5661 (ここでは順序が重要です):
5662
5663 $output = `cmd 2>&1 1>/dev/null`;
5664
5665 =begin original
5666
5667 To exchange a command's STDOUT and STDERR in order to capture the STDERR
5668 but leave its STDOUT to come out the old STDERR:
5669
5670 =end original
5671
5672 STDERR を取得するが、STDOUT は古い STDERR のために残しておくために
5673 STDOUT と STDERR を交換するには:
5674
5675 $output = `cmd 3>&1 1>&2 2>&3 3>&-`;
5676
5677 =begin original
5678
5679 To read both a command's STDOUT and its STDERR separately, it's easiest
5680 to redirect them separately to files, and then read from those files
5681 when the program is done:
5682
5683 =end original
5684
5685 コマンドの STDOUT と STDERR の両方を別々に読み込みたい場合、
5686 一番簡単な方法は別々のファイルにリダイレクトし、
5687 プログラムが終了してからそのファイルを読むことです:
5