perlop > 5.38.0 との差分

perlop 5.38.0 と 5.10.1 の差分

11
2=encoding utf8
2=encoding euc-jp
33
44=head1 NAME
55X<operator>
66
77=begin original
88
99perlop - Perl operators and precedence
1010
1111=end original
1212
1313perlop - Perl の演算子と優先順位
1414
1515=head1 DESCRIPTION
1616
17=begin original
18
19In Perl, the operator determines what operation is performed,
20independent of the type of the operands. For example S<C<$x + $y>>
21is always a numeric addition, and if C<$x> or C<$y> do not contain
22numbers, an attempt is made to convert them to numbers first.
23
24=end original
25
26Perl では、演算子はどんな演算を行うかをオペランドの型と独立して決定します。
27例えば S<C<$x + $y>> は常に数値加算で、C<$x> や C<$y> に数値でないものが
28含まれている場合、まずそれらを数値に変換しようとします。
29
30=begin original
31
32This is in contrast to many other dynamic languages, where the
33operation is determined by the type of the first argument. It also
34means that Perl has two versions of some operators, one for numeric
35and one for string comparison. For example S<C<$x == $y>> compares
36two 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
48There are a few exceptions though: C<x> can be either string
49repetition or list repetition, depending on the type of the left
50operand, and C<&>, C<|>, C<^> and C<~> can be either string or numeric bit
51operations.
52
53=end original
54
55しかし、いくつかの例外があります: C<x> は左オペランドの型によって、
56文字列の繰り返しの場合とリストの繰り返しの場合があります; また
57C<&>, C<|>, C<^>, C<~> は文字列としてのビット演算と数値としてのビット演算の
58場合があります。
59
6017=head2 Operator Precedence and Associativity
6118X<operator, precedence> X<precedence> X<associativity>
6219
6320(演算子の優先順位と結合性)
6421
6522=begin original
6623
6724Operator precedence and associativity work in Perl more or less like
6825they do in mathematics.
6926
7027=end original
7128
7229Perl での演算子の優先順位と結合性は多かれ少なかれ数学のものと似ています。
7330
7431=begin original
7532
76I<Operator precedence> means some operators group more tightly than others.
33I<Operator precedence> means some operators are evaluated before
77For example, in C<2 + 4 * 5>, the multiplication has higher precedence, so C<4
34others. For example, in C<2 + 4 * 5>, the multiplication has higher
78* 5> is grouped together as the right-hand operand of the addition, rather
35precedence so C<4 * 5> is evaluated first yielding C<2 + 20 ==
79than C<2 + 4> being grouped together as the left-hand operand of the
3622> and not C<6 * 5 == 30>.
80multiplication. It is as if the expression were written C<2 + (4 * 5)>, not
81C<(2 + 4) * 5>. So the expression yields C<2 + 20 == 22>, rather than
82C<6 * 5 == 30>.
8337
8438=end original
8539
86I<演算子の優先順位> とは、他の演算子グループよりもしっかりと
40I<演算子の優先順位> とは、他の演算子より先に評価される演算子が
87結びついている演算子グループがあるということです。
41あるということです。
8842例えば、C<2 + 4 * 5> の場合、乗算が高い優先順位を持っているので、
89C<2 + 4> が乗算の左オペランドとしてまとめらるのではなく、
43C<4 * 5> が先に評価さ、結果は C<6 * 5 == 30> ではなく、
90C<4 * 5> が加法の右オペランドしてまとめられます。
44C<2 + 20 == 22> となります。
91これは、C<(2 + 4) * 5> ではなく、C<2 + (4 * 5)> と
92書かれたかのようなものです。
93従って、この式は C<6 * 5 == 30> ではなく C<2 + 20 == 22> になります。
9445
9546=begin original
9647
97I<Operator associativity> defines what happens if a sequence of the same
48I<Operator associativity> defines what happens if a sequence of the
98operators is used one after another:
49same operators is used one after another: whether the evaluator will
99usually that they will be grouped at the left
50evaluate the left operations first or the right. For example, in C<8
100or the right. For example, in C<9 - 3 - 2>, subtraction is left associative,
51- 4 - 2>, subtraction is left associative so Perl evaluates the
101so C<9 - 3> is grouped together as the left-hand operand of the second
52expression left to right. C<8 - 4> is evaluated first making the
102subtraction, rather than C<3 - 2> being grouped together as the right-hand
53expression C<4 - 2 == 2> and not C<8 - 2 == 6>.
103operand of the first subtraction. It is as if the expression were written
104C<(9 - 3) - 2>, not C<9 - (3 - 2)>. So the expression yields C<6 - 2 == 4>,
105rather than C<9 - 1 == 8>.
10654
10755=end original
10856
10957I<演算子の結合性> は、同じ演算子が連続して現れた場合に何が起こるかを
110定義します: 通常はそれらが左側と結びつくか右側と結びつくかです。
58定義します: 評価器が左側を先に評価するか右側を先に評価するかです。
111例えば C<9 - 3 - 2>、減は左結合なので、
59例えばC<8 - 4 - 2> の場合、減は左結合なので、Perl は式を左から
112C<3 - 2> が最初の減法のオペランドととめられるのではなく、
60に評価します。
113C<9 - 3> が 2 番目の減法の左オペランドとしてまとめられます。
61C<8 - 4> が先に評価されるので、C<8 - 2 == 6> ではなく
114これは、C<9 - (3 - 2)> ではなく C<(9 - 3) - 2> と書かれたかのようものです。
62C<4 - 2 == 2> となります。
115従って、この式は C<9 - 1 == 8> ではなく C<6 - 2 == 4> になります。
11663
11764=begin original
11865
119For simple operators that evaluate all their operands and then combine the
120values in some way, precedence and associativity (and parentheses) imply some
121ordering requirements on those combining operations. For example, in C<2 + 4 *
1225>, the grouping implied by precedence means that the multiplication of 4 and
1235 must be performed before the addition of 2 and 20, simply because the result
124of that multiplication is required as one of the operands of the addition. But
125the order of operations is not fully determined by this: in C<2 * 2 + 4 * 5>
126both multiplications must be performed before the addition, but the grouping
127does not say anything about the order in which the two multiplications are
128performed. In fact Perl has a general rule that the operands of an operator
129are evaluated in left-to-right order. A few operators such as C<&&=> have
130special evaluation rules that can result in an operand not being evaluated at
131all; in general, the top-level operator in an expression has control of
132operand evaluation.
133
134=end original
135
136全てのオペランドを評価してから何らかの形で値を結合する
137単純な演算子の場合、優先順位と結合性(とかっこ)はそれらの結合操作で
138ある種の順序要求を暗示します。
139例えば C<2 + 4 * 5> では、
140優先順位が暗示するグループ化によって、
1414 と 5 の乗算は
1422 と 20 の加算の前に行われる必要があります;
143単にこの乗法の結果が加法のオペランドの一つとして必要だからです。
144しかし、演算の順序はこれによって完全に決定されるわけではありません:
145C<2 * 2 + 4 * 5> では、両方の乗算は加算の前に行われなければなりませんが、
146グループ化は二つの乗算が行われる順序については何も言っていません。
147実際の所、Perl には、演算子のオペランドは左から右の順で
148評価されるという一般的な規則があります。
149C<&&=> のようないくつかの演算子は、
150全く評価されないオペランドとなる特別な評価規則を持ちます;
151一般的に、式の中の最上位の演算子がオペランド評価を制御します。
152
153=begin original
154
155Some comparison operators, as their associativity, I<chain> with some
156operators of the same precedence (but never with operators of different
157precedence). This chaining means that each comparison is performed
158on the two arguments surrounding it, with each interior argument taking
159part in two comparisons, and the comparison results are implicitly ANDed.
160Thus 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
162it looks. The ANDing short-circuits just like C<"&&"> does, stopping
163the 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スカラだと仮定すると、
175S<C<"$x E<lt> $y && $y E<lt>= $z">> と正確に同じように振る舞います。
176AND の短絡は C<"&&"> と同様に行われ、一つが偽となった時点で一連の連鎖は
177停止します。
178
179=begin original
180
181In a chained comparison, each argument expression is evaluated at most
182once, even if it takes part in two comparisons, but the result of the
183evaluation is fetched for each comparison. (It is not evaluated
184at all if the short-circuiting means that it's not required for any
185comparisons.) This matters if the computation of an interior argument
186is 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
201is not entirely like
202
203=end original
204
205これは全体的に次のようなものではなく:
206
207 if($x < expensive_sub() && expensive_sub() <= $z) { ...
208
209=begin original
210
211but 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
222in that the subroutine is only called once. However, it's not exactly
223like this latter code either, because the chained comparison doesn't
224actually involve any temporary variable (named or otherwise): there is
225no assignment. This doesn't make much difference where the expression
226is a call to an ordinary subroutine, but matters more with an lvalue
227subroutine, or if the argument expression yields some unusual kind of
228scalar by other means. For example, if the argument expression yields
229a tied scalar, then the expression is evaluated to produce that scalar
230at most once, but the value of that scalar may be fetched up to twice,
231once 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
249In this example, the expression is evaluated only once, and the tied
250scalar (the result of the expression) is fetched for each comparison that
251uses it.
252
253=end original
254
255この例で、式は 1 回だけ評価され、tie されたスカラ (式の結果) は、
256これら使われる比較毎に取得されます。
257
258 if ($x < $tied_scalar < $z) { ...
259
260=begin original
261
262In the next example, the expression is evaluated only once, and the tied
263scalar is fetched once as part of the operation within the expression.
264The result of that operation is fetched for each comparison, which
265normally doesn't matter unless that expression result is also magical due
266to 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
280Some operators are instead non-associative, meaning that it is a syntax
281error to use a sequence of those operators of the same precedence.
282For 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
29266Perl operators have the following associativity and precedence,
29367listed from highest precedence to lowest. Operators borrowed from
29468C keep the same precedence relationship with each other, even where
29569C's precedence is slightly screwy. (This makes learning Perl easier
29670for C folks.) With very few exceptions, these all operate on scalar
29771values only, not array values.
29872
29973=end original
30074
30175Perl の演算子には、以下のような結合性と優先順位 (高い優先順位から
30276低いものへ並べている) があります。
30377C から持ってきた演算子の優先順位は、C での優先順位が多少おかしくても、
30478そのままにしてあります。
30579(これによって、C を使っている方が Perl に移りやすくなっています。)
30680ごく僅かな例外を別として、全ての演算子はスカラ値のみを持ち、
30781配列値を持ちません。
30882
30983=begin original
31084
31185 left terms and list operators (leftward)
31286 left ->
31387 nonassoc ++ --
31488 right **
315 right ! ~ ~. \ and unary + and -
89 right ! ~ \ and unary + and -
31690 left =~ !~
31791 left * / % x
31892 left + - .
31993 left << >>
32094 nonassoc named unary operators
321 nonassoc isa
95 nonassoc < > <= >= lt gt le ge
322 chained < > <= >= lt gt le ge
96 nonassoc == != <=> eq ne cmp ~~
323 chain/na == != eq ne <=> cmp ~~
97 left &
324 left & &.
98 left | ^
325 left | |. ^ ^.
32699 left &&
327100 left || //
328101 nonassoc .. ...
329102 right ?:
330 right = += -= *= etc. goto last next redo dump
103 right = += -= *= etc.
331104 left , =>
332105 nonassoc list operators (rightward)
333106 right not
334107 left and
335108 left or xor
336109
337110=end original
338111
339112 左結合 項 リスト演算子 (左方向に対して)
340113 左結合 ->
341114 非結合 ++ --
342115 右結合 **
343 右結合 ! ~ ~. \ 単項の+ 単項の-
116 右結合 ! ~ \ 単項の+ 単項の-
344117 左結合 =~ !~
345118 左結合 * / % x
346119 左結合 + - .
347120 左結合 << >>
348121 非結合 名前付き単項演算子
349 連鎖 < > <= >= lt gt le ge
122 非結合 < > <= >= lt gt le ge
350 連鎖/なし == != eq ne <=> cmp ~~
123 非結合 == != <=> eq ne cmp ~~
351 結合 isa
124 結合 &
352 左結合 & &.
125 左結合 | ^
353 左結合 | |. ^ ^.
354126 左結合 &&
355127 左結合 || //
356128 非結合 .. ...
357129 右結合 ?:
358 右結合 = += -= *= など; goto last next redo dump
130 右結合 = += -= *= などの代入演算子
359131 左結合 , =>
360132 非結合 リスト演算子 (右方向に対して)
361133 右結合 not
362134 左結合 and
363135 左結合 or xor
364136
365137=begin original
366138
367In the following sections, these operators are covered in detail, in the
139In the following sections, these operators are covered in precedence order.
368same order in which they appear in the table above.
369140
370141=end original
371142
372以下の章では、前述の表の順に、これらの演算子に関して詳ます。
143以下の章では、演算子は優先順位の順されています。
373144
374145=begin original
375146
376147Many operators can be overloaded for objects. See L<overload>.
377148
378149=end original
379150
380151多くの演算子はオブジェクトでオーバーロードできます。
381152L<overload> を参照して下さい。
382153
383154=head2 Terms and List Operators (Leftward)
384155X<list operator> X<operator, list> X<term>
385156
386157(項とリスト演算子 (左方向))
387158
388159=begin original
389160
390161A TERM has the highest precedence in Perl. They include variables,
391162quote and quote-like operators, any expression in parentheses,
392163and any function whose arguments are parenthesized. Actually, there
393164aren't really functions in this sense, just list operators and unary
394165operators behaving as functions because you put parentheses around
395166the arguments. These are all documented in L<perlfunc>.
396167
397168=end original
398169
399170「項」は Perl でもっとも優先順位が高いものです。
400171これには、変数、クォートとクォート的な演算子、括弧で括った任意の式、
401172引数を括弧で括った任意の関数が含まれます。
402173実際には、この意味では本当の関数はなく、リスト演算子と関数のように働く
403174単項演算子が、引数を括弧で括るためそのように見えます。
404175これらはすべて L<perlfunc> に記述しています。
405176
406177=begin original
407178
408If any list operator (C<print()>, etc.) or any unary operator (C<chdir()>, etc.)
179If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
409180is followed by a left parenthesis as the next token, the operator and
410181arguments within parentheses are taken to be of highest precedence,
411182just like a normal function call.
412183
413184=end original
414185
415リスト演算子 (C<print()> など) や単項演算子 (C<chdir()> など) は、
186もし、リスト演算子 (print() など) や単項演算子 (chdir() など)
416すべて次トークンとして開き括弧が続く、その演算子と括弧内の引数は、
187名前後に開き括弧が続く場合には、その演算子と括弧内の引数は、
417通常の関数呼び出しのようにもっとも高い優先順位として扱われます。
188通常の関数呼び出しのようにもっとも高い優先順位で処理されます。
418189
419190=begin original
420191
421192In the absence of parentheses, the precedence of list operators such as
422193C<print>, C<sort>, or C<chmod> is either very high or very low depending on
423194whether you are looking at the left side or the right side of the operator.
424195For example, in
425196
426197=end original
427198
428199括弧が無い場合には、C<print>、C<sort>、C<chmod> のようなリスト演算子の
429200優先順位は、演算子の左側をからすると非常に高く、右側からすると
430201非常に低く見えます。たとえば、
431202
432203 @ary = (1, 3, sort 4, 2);
433204 print @ary; # prints 1324
434205
435206=begin original
436207
437the commas on the right of the C<sort> are evaluated before the C<sort>,
208the commas on the right of the sort are evaluated before the sort,
438209but the commas on the left are evaluated after. In other words,
439210list operators tend to gobble up all arguments that follow, and
440211then act like a simple TERM with regard to the preceding expression.
441212Be careful with parentheses:
442213
443214=end original
444215
445では、C<sort> の右のコンマは C<sort> よりも前に評価されますが、左のコンマは
216では、sort の右のコンマは sort よりも前に評価されます (右から見ると
446後から評価されます
217sort の優先順位が低い) が、左側のコンマは sort のあとに評価されます
218(左側から見ると sort の方が優先順位が高くなっている)。
447219言い方を変えると、リスト演算子は自分の後にある引数をすべて使って処理を行ない、
448220その結果を自分の前の式に対する「項」であるかのように見せるということです。
449221ただし、括弧には気を付けないといけません:
450222
451223=begin original
452224
453225 # These evaluate exit before doing the print:
454226 print($foo, exit); # Obviously not what you want.
455227 print $foo, exit; # Nor is this.
456228
457229=end original
458230
459231 # 以下は print を行なう前に exit を評価します:
460232 print($foo, exit); # 明らかにやりたいことではないでしょう。
461233 print $foo, exit; # これでもない。
462234
463235=begin original
464236
465237 # These do the print before evaluating exit:
466238 (print $foo), exit; # This is what you want.
467239 print($foo), exit; # Or this.
468240 print ($foo), exit; # Or even this.
469241
470242=end original
471243
472244 # 以下は exit を評価する前に print を行ないます:
473245 (print $foo), exit; # これがしたかった。
474246 print($foo), exit; # これでもいい。
475247 print ($foo), exit; # これも OK。
476248
477249=begin original
478250
479251Also note that
480252
481253=end original
482254
483255また、
484256
485257 print ($foo & 255) + 1, "\n";
486258
487259=begin original
488260
489261probably doesn't do what you expect at first glance. The parentheses
490262enclose the argument list for C<print> which is evaluated (printing
491the result of S<C<$foo & 255>>). Then one is added to the return value
263the result of C<$foo & 255>). Then one is added to the return value
492264of C<print> (usually 1). The result is something like this:
493265
494266=end original
495267
496268の動作を一目見ただけで判断するのは、難しいでしょう。
497269かっこは C<print> のために評価される引数リストを囲っています
498(S<C<$foo & 255>> の結果が表示されます)。
270(C<$foo & 255> の結果が表示されます)。
499271それから C<print> の返り値 (通常は 1) に 1 が加えられます。
500272結果は以下のようになります:
501273
502=begin original
503
504274 1 + 1, "\n"; # Obviously not what you meant.
505275
506=end original
507
508 1 + 1, "\n"; # 明らかにやりたいことではないでしょう。
509
510276=begin original
511277
512278To do what you meant properly, you must write:
513279
514280=end original
515281
516282意味したいことを適切に行うには、以下のように書く必要があります:
517283
518284 print(($foo & 255) + 1, "\n");
519285
520286=begin original
521287
522See L</Named Unary Operators> for more discussion of this.
288See L<Named Unary Operators> for more discussion of this.
523289
524290=end original
525291
526詳しくは、L</Named Unary Operators> を参照してください。
292詳しくは、L<Named Unary Operators> を参照してください。
527293
528294=begin original
529295
530Also parsed as terms are the S<C<do {}>> and S<C<eval {}>> constructs, as
296Also parsed as terms are the C<do {}> and C<eval {}> constructs, as
531297well as subroutine and method calls, and the anonymous
532298constructors C<[]> and C<{}>.
533299
534300=end original
535301
536この他に「項」として解析されるものには、S<C<do {}>>S<C<eval {}>> の
302この他に「項」として解析されるものには、C<do {}> や C<eval {}> の
537303構成、サブルーティンやメソッドの呼び出し、無名のコンストラクタ
538304C<[]> と C<{}> があります。
539305
540306=begin original
541307
542See also L</Quote and Quote-like Operators> toward the end of this section,
308See also L<Quote and Quote-like Operators> toward the end of this section,
543309as well as L</"I/O Operators">.
544310
545311=end original
546312
547後の方の L</Quote and Quote-like Operators>
313後の方のL<Quote and Quote-like Operators>や
548L</"I/O Operators"> も参照してください。
314L</"I/O Operators">も参照してください。
549315
550316=head2 The Arrow Operator
551317X<arrow> X<dereference> X<< -> >>
552318
553319(矢印演算子)
554320
555321=begin original
556322
557323"C<< -> >>" is an infix dereference operator, just as it is in C
558324and C++. If the right side is either a C<[...]>, C<{...}>, or a
559325C<(...)> subscript, then the left side must be either a hard or
560326symbolic reference to an array, a hash, or a subroutine respectively.
561327(Or technically speaking, a location capable of holding a hard
562328reference, if it's an array or hash reference being used for
563329assignment.) See L<perlreftut> and L<perlref>.
564330
565331=end original
566332
567333C や C++ と同じように "C<< -> >>" は中置の被参照演算子です。
568右側が C<[...]>, C<{...}>, C<(...)> のいずれかの形の添字であれば、左側は配列、
334右側が C<[...]>, C<{...}>,
569ハッシュ、サブルーチンへのハードリファレンスか
335C<(...)> のいずれかの形の添字であれば、左側は配列、ハッシュ、
570シンボリックリファレンスでなければなりません。
336サブルーチンへのハードリファレンスかシンボリックリファレンス (あるいは
571(あるいは技術的には、配列またはハードリファレンスが代入可能であれば
337技術的には、配列またはハードリファレンスが代入可能であれば
572ハードリファレンスを保持できる場所です。)
338ハードリファレンスを保持できる場所)
573L<perlreftut> と L<perlref> を参照してください。
339でなければなりません。L<perlreftut> と L<perlref> を参照してください。
574340
575341=begin original
576342
577343Otherwise, the right side is a method name or a simple scalar
578344variable containing either the method name or a subroutine reference,
579and (if it is a method name) the left side must be either an object (a
345and the left side must be either an object (a blessed reference)
580blessed reference) or a class name (that is, a package name). See
346or a class name (that is, a package name). See L<perlobj>.
581L<perlobj>.
582347
583348=end original
584349
585350そうでなければ、右側はメソッド名かサブルーチンのリファレンスを持った
586単純スカラ変数で、
351単純スカラ変数で、左側はオブジェクト (bless されたリファレンス) か
587(メソッド名なら) 左側はオブジェクト (bless されたリファレンス) か
588352クラス名でなければなりません。
589353L<perlobj> を参照してください。
590354
591=begin original
592
593The dereferencing cases (as opposed to method-calling cases) are
594somewhat extended by the C<postderef> feature. For the
595details of that feature, consult L<perlref/Postfix Dereference Syntax>.
596
597=end original
598
599(メソッド呼び出しの場合ではなく) デリファレンスの場合、
600C<後置デリファレンス> (postderef) 機能によっていくらか拡張されます。
601この機能の詳細については、L<perlref/Postfix Dereference Syntax> を
602参照してください。
603
604355=head2 Auto-increment and Auto-decrement
605356X<increment> X<auto-increment> X<++> X<decrement> X<auto-decrement> X<-->
606357
607358(インクリメントとデクリメント)
608359
609360=begin original
610361
611C<"++"> and C<"--"> work as in C. That is, if placed before a variable,
362"++" and "--" work as in C. That is, if placed before a variable,
612363they increment or decrement the variable by one before returning the
613364value, and if placed after, increment or decrement after returning the
614365value.
615366
616367=end original
617368
618C<"++">C<"--"> は、C の場合と同じように動作します。
369"++" と "--" は、C の場合と同じように動作します。
619370つまり、変数の前に置かれれば、値を返す前に変数を 1 インクリメントまたは
620371デクリメントし、後に置かれれば、値を返した後で変数を
621372インクリメントまたはデクリメントします。
622373
623374 $i = 0; $j = 0;
624375 print $i++; # prints 0
625376 print ++$j; # prints 1
626377
627378=begin original
628379
629380Note that just as in C, Perl doesn't define B<when> the variable is
630incremented or decremented. You just know it will be done sometime
381incremented or decremented. You just know it will be done sometime
631before or after the value is returned. This also means that modifying
382before or after the value is returned. This also means that modifying
632a variable twice in the same statement will lead to undefined behavior.
383a variable twice in the same statement will lead to undefined behaviour.
633384Avoid statements like:
634385
635386=end original
636387
637388C と同様、Perl は B<いつ> 変数がインクリメントまたはデクリメントされるかは
638389定義されません。
639390値が返される前か後のどこかで行われる、ということだけがわかります。
640391これは、同じ文である変数を 2 回修正すると、振る舞いが未定義になることを
641392意味します。
642393以下のような文は避けてください:
643394
644395 $i = $i ++;
645396 print ++ $i + $i ++;
646397
647398=begin original
648399
649400Perl will not guarantee what the result of the above statements is.
650401
651402=end original
652403
653404Perl は上記の文の結果について保障しません。
654405
655406=begin original
656407
657408The auto-increment operator has a little extra builtin magic to it. If
658409you increment a variable that is numeric, or that has ever been used in
659410a numeric context, you get a normal increment. If, however, the
660411variable has been used in only string contexts since it was set, and
661412has a value that is not the empty string and matches the pattern
662413C</^[a-zA-Z]*[0-9]*\z/>, the increment is done as a string, preserving each
663414character within its range, with carry:
664415
665416=end original
666417
667418インクリメント演算子には、ちょっと風変わりな機能が組み込まれています。
668419数値が入った変数や、数値の文脈で使われてきた変数を
669420インクリメントする場合には、通常のインクリメントとして動作します。
670しかし、その変数が設定されてからずっと文字列の文脈でしか使われていなくて、
421しかし、その変数が設定されてからずっと文字列の文脈で
671空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする値を持っているときには、
422しか使われていなくて、空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする
672個々の文字の範囲を保ちながら桁あげを行なって、文字列としてインクリメントが
423値を持っているときには、個々の文字の範囲を保ちながら桁あげを行なって、
673行なわれます (マジカルインクリメントと呼ばれます):
424文字列としてインクリメントが行なわれます (マジカルインクリメントと呼ばれます):
674425
675 print ++($foo = "99"); # prints "100"
426 print ++($foo = '99'); # prints '100'
676 print ++($foo = "a0"); # prints "a1"
427 print ++($foo = 'a0'); # prints 'a1'
677 print ++($foo = "Az"); # prints "Ba"
428 print ++($foo = 'Az'); # prints 'Ba'
678 print ++($foo = "zz"); # prints "aaa"
429 print ++($foo = 'zz'); # prints 'aaa'
679430
680431=begin original
681432
682433C<undef> is always treated as numeric, and in particular is changed
683434to C<0> before incrementing (so that a post-increment of an undef value
684435will return C<0> rather than C<undef>).
685436
686437=end original
687438
688439C<undef> は常に数値として扱われ、特にインクリメントされる前には C<0> に
689440変換されます(従って、undef のポストインクリメント値は C<undef> ではなく
690441C<0> になります)。
691442
692443=begin original
693444
694445The auto-decrement operator is not magical.
695446
696447=end original
697448
698449デクリメント演算子には、マジカルなものはありません。
699450
700451=head2 Exponentiation
701452X<**> X<exponentiation> X<power>
702453
703454(指数演算子)
704455
705456=begin original
706457
707Binary C<"**"> is the exponentiation operator. It binds even more
458Binary "**" is the exponentiation operator. It binds even more
708tightly than unary minus, so C<-2**4> is C<-(2**4)>, not C<(-2)**4>.
459tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is
709(This is
460implemented using C's pow(3) function, which actually works on doubles
710implemented using C's C<pow(3)> function, which actually works on doubles
711461internally.)
712462
713463=end original
714464
715二項演算子の C<"**"> は指数演算子です。
465二項演算子の "**" は指数演算子です。
716466この演算子は、単項のマイナスよりも結合が強い演算子で、
717C<-2**4>C<(-2)**4> ではなく、C<-(2**4)> と解釈されます。
467-2**4 は (-2)**4 ではなく、-(2**4) と解釈されます。
718(これは C の C<pow(3)> を使って実装されていますので、
468(これは C の pow(3) を使って実装されていますので、
719469内部的には double で動作します。)
720470
721=begin original
722
723Note that certain exponentiation expressions are ill-defined:
724these include C<0**0>, C<1**Inf>, and C<Inf**0>. Do not expect
725any particular results from these special cases, the results
726are platform-dependent.
727
728=end original
729
730一部の指数表現は明確に定義されていません:
731これは C<0**0>, C<1**Inf>, C<Inf**0> などです。
732これらの特殊な場合に関して特定の結果を想定しないでください;
733結果はプラットフォーム依存です。
734
735471=head2 Symbolic Unary Operators
736472X<unary operator> X<operator, unary>
737473
738474(単項演算子)
739475
740476=begin original
741477
742Unary C<"!"> performs logical negation, that is, "not". See also
478Unary "!" performs logical negation, i.e., "not". See also C<not> for a lower
743L<C<not>|/Logical Not> for a lower precedence version of this.
479precedence version of this.
744480X<!>
745481
746482=end original
747483
748単項演算子の C<"!"> は論理否定を行ないます; つまり「not」ということです。
484単項演算子の "!" は論理否定を行ないます。
749の演算子の優先順位を低くしたものして、
485つまり 「not」 ということです。
750L<C<not>|/Logical Not> が用意されています。
486この演算子の優先順位を低くしたものとして、C<not> が用意されています。
751487X<!>
752488
753489=begin original
754490
755Unary C<"-"> performs arithmetic negation if the operand is numeric,
491Unary "-" performs arithmetic negation if the operand is numeric. If
756including any string that looks like a number. If the operand is
492the operand is an identifier, a string consisting of a minus sign
757an identifier, a string consisting of a minus sign concatenated
493concatenated with the identifier is returned. Otherwise, if the string
758with the identifier is returned. Otherwise, if the string starts
494starts with a plus or minus, a string starting with the opposite sign
759with a plus or minus, a string starting with the opposite sign is
495is returned. One effect of these rules is that -bareword is equivalent
760returned. One effect of these rules is that C<-bareword> is equivalent
496to the string "-bareword". If, however, the string begins with a
761to the string C<"-bareword">. If, however, the string begins with a
497non-alphabetic character (excluding "+" or "-"), Perl will attempt to convert
762non-alphabetic character (excluding C<"+"> or C<"-">), Perl will attempt
498the string to a numeric and the arithmetic negation is performed. If the
763to convert
764the string to a numeric, and the arithmetic negation is performed. If the
765499string cannot be cleanly converted to a numeric, Perl will give the warning
766500B<Argument "the string" isn't numeric in negation (-) at ...>.
767501X<-> X<negation, arithmetic>
768502
769503=end original
770504
771単項演算子の C<"-"> は被演算子が数値または数値に見える文字列であれば、
505単項演算子の "-" は被演算子が数値であれば、算術否定を行ないます。
772術否定います。
506被演子が識別子ならば、マイナス記号にその識別子げた
773被演算子が識別子ならば、マイナス記号にその識別子をつなげた文字列が返されます。
507文字列が返されます。
774508これ以外で被演算子の最初の文字がプラスかマイナスのときには、
775509その記号を逆のものに置き換えた文字列を返します。
776この規則の結果、C<-bareword> が文字列 C<"-bareword"> に等価となります。
510この規則の結果、-bareword が文字列 "-bareword" に等価となります。
777しかし、文字列が英字以外(C<"+">C<"-"> を除く)で始まっていると、
511しかし、文字列が英字以外("+" と "-" を除く)で始まっていると、
778512Perl は文字列を数値に変換しようとし、それから算術否定が実行されます。
779513もし文字列が明確に数値に変換できない場合、Perl は
780514B<Argument "the string" isn't numeric in negation (-) at ...> という
781515警告を出します。
782516X<-> X<negation, arithmetic>
783517
784518=begin original
785519
786Unary C<"~"> performs bitwise negation, that is, 1's complement. For
520Unary "~" performs bitwise negation, i.e., 1's complement. For
787example, S<C<0666 & ~027>> is 0640. (See also L</Integer Arithmetic> and
521example, C<0666 & ~027> is 0640. (See also L<Integer Arithmetic> and
788L</Bitwise String Operators>.) Note that the width of the result is
522L<Bitwise String Operators>.) Note that the width of the result is
789platform-dependent: C<~0> is 32 bits wide on a 32-bit platform, but 64
523platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64
790524bits wide on a 64-bit platform, so if you are expecting a certain bit
791width, remember to use the C<"&"> operator to mask off the excess bits.
525width, remember to use the & operator to mask off the excess bits.
792526X<~> X<negation, binary>
793527
794528=end original
795529
796単項演算子の C<"~"> はビットごとの否定を行ないます; つまり、1 の補数を返します。
530単項演算子の "~" はビットごとの否定を行ないます。
797例えばS<C<0666 & ~027>> は 0640 です。
531つまり1 の補数を返します。
798(L</Integer Arithmetic> L</Bitwise String Operators> も参照して下さい)
532例えば、C<0666 & ~027> 0640 です
799結果の幅はプラットホーム依存であるこに注意してください: C<~0>
533(L<Integer Arithmetic> L<Bitwise String Operators> も参照して下さい。)
80032 ビットプラットホームでは 32 ビットですが、64 ビットプラットホームで
534結果のプラットホーム依存あることに注意してください。
80164 ット幅なの、特定のビット幅を仮定る場合は
535~0 は 32-bit プラットホームは 32 ビット幅
802余分なビットをマスクするために C<"&"> 演算子を使うことを忘れないください。
53664-bit プラットホームでは 64 ビット幅すので、
537特定のビット幅を仮定する場合は、
538余分なビットをマスクするために & 演算子を使うことを忘れないでください。
803539X<~> X<negation, binary>
804540
805541=begin original
806542
807Starting in Perl 5.28, it is a fatal error to try to complement a string
543Unary "+" has no effect whatsoever, even on strings. It is useful
808containing a character with an ordinal value above 255.
809
810=end original
811
812Perl 5.28 から、値が 255 を超える文字を含む文字列の
813補数を求めようとすると致命的エラーになります。
814
815=begin original
816
817If the "bitwise" feature is enabled via S<C<use
818feature 'bitwise'>> or C<use v5.28>, then unary
819C<"~"> always treats its argument as a number, and an
820alternate form of the operator, C<"~.">, always treats its argument as a
821string. So C<~0> and C<~"0"> will both give 2**32-1 on 32-bit platforms,
822whereas C<~.0> and C<~."0"> will both yield C<"\xff">. Until Perl 5.28,
823this feature produced a warning in the C<"experimental::bitwise"> category.
824
825=end original
826
827"bitwise" 機能が S<C<use feature 'bitwise'>> か C<use v5.28> で有効にされると、
828単項の C<"~"> は常にその引数を数値として扱い、その代替形式である
829C<"~."> はその引数を常に文字列として扱います。
830従って 32 ビットプラットフォームでは C<~0> と C<~"0"> はどちらも
8312**32-1 を意味しますが、C<~.0> と C<~."0"> はどちらも C<"\xff"> になります。
832Perl 5.28 まで、この機能は C<"experimental::bitwise"> カテゴリの警告を
833発生させていました。
834
835=begin original
836
837Unary C<"+"> has no effect whatsoever, even on strings. It is useful
838544syntactically for separating a function name from a parenthesized expression
839545that would otherwise be interpreted as the complete list of function
840arguments. (See examples above under L</Terms and List Operators (Leftward)>.)
546arguments. (See examples above under L<Terms and List Operators (Leftward)>.)
841547X<+>
842548
843549=end original
844550
845単項演算子の C<"+"> は、たとえ文字列に対して用いられた場合にも、何もしません。
551単項演算子の "+" は、たとえ文字列に対して用いられた場合にも、何もしません。
846552関数名に続けて括弧付きの式を書く場合に、関数の引数リストと
847553解釈されないようにするために用いることができます。
848(下記 L</Terms and List Operators (Leftward)> の例を参照してください。)
554(下記 L<Terms and List Operators (Leftward)> の例を参照してください。)
849555X<+>
850556
851557=begin original
852558
853Unary C<"\"> creates references. If its operand is a single sigilled
559Unary "\" creates a reference to whatever follows it. See L<perlreftut>
854thing, it creates a reference to that object. If its operand is a
855parenthesised list, then it creates references to the things mentioned
856in the list. Otherwise it puts its operand in list context, and creates
857a list of references to the scalars in the list provided by the operand.
858See L<perlreftut>
859560and L<perlref>. Do not confuse this behavior with the behavior of
860561backslash within a string, although both forms do convey the notion
861562of protecting the next thing from interpolation.
862563X<\> X<reference> X<backslash>
863564
864565=end original
865566
866単項演算子の C<"\"> はリファレンスを生成します。
567単項演算子の "\" はその後に続くものへのリファレンスを生成します。
867オペランドが一つの印付きのものの場合、それへのリファレンスを作ります。
868オペランドがかっこでくくられたリストの場合、
869リストで言及されているものへのリファレンスを作ります。
870さもなければオペランドをリストコンテキストとし、
871オペランドによって提供されたリストのスカラへのリファレンスのリストを作ります。
872568L<perlreftut> と L<perlref> を参照してください。
873569この用法も文字列中のバックスラッシュも、後に続くものが展開されるのを
874570防ぐことになりますが、動作を混同しないでください。
875571X<\> X<reference> X<backslash>
876572
877573=head2 Binding Operators
878574X<binding> X<operator, binding> X<=~> X<!~>
879575
880576(拘束演算子)
881577
882578=begin original
883579
884Binary C<"=~"> binds a scalar expression to a pattern match. Certain operations
580Binary "=~" binds a scalar expression to a pattern match. Certain operations
885search or modify the string C<$_> by default. This operator makes that kind
581search or modify the string $_ by default. This operator makes that kind
886582of operation work on some other string. The right argument is a search
887583pattern, substitution, or transliteration. The left argument is what is
888584supposed to be searched, substituted, or transliterated instead of the default
889C<$_>. When used in scalar context, the return value generally indicates the
585$_. When used in scalar context, the return value generally indicates the
890success of the operation. The exceptions are substitution (C<s///>)
586success of the operation. Behavior in list context depends on the particular
891and transliteration (C<y///>) with the C</r> (non-destructive) option,
587operator. See L</"Regexp Quote-Like Operators"> for details and
892which cause the B<r>eturn value to be the result of the substitution.
588L<perlretut> for examples using these operators.
893Behavior in list context depends on the particular operator.
894See L</"Regexp Quote-Like Operators"> for details and L<perlretut> for
895examples using these operators.
896589
897590=end original
898591
899二項演算子の C<"=~"> は、スカラ式をパターンマッチに拘束します。
592二項演算子の "=~" は、スカラ式をパターンマッチに拘束します。
900デフォルトで C<$_> の文字列を検索したり、変更したりする演算があります。
593デフォルトで $_ の文字列を検索したり、変更したりする演算があります。
901594この演算子は、そのような演算を他の文字列に対して行なわせるようにするものです。
902595右引数は、検索パターン、置換、文字変換のいずれかです。
903左引数は、デフォルトの C<$_> の代わりに検索、置換、文字変換の
596左引数は、デフォルトの $_ の代わりに検索、置換、文字変換の対象となるものです。
904対象となるものです。
905597スカラコンテキストで使うと、返り値は一般的に演算の結果が成功したか否かです。
906例外は、C</r> (非破壊) オプション付きの置換 (C<s///>) と
907文字変換 (C<y///>) です; この場合は変換した結果を返します。
908598リストコンテキストでの振る舞いは演算子に依存します。
909599詳しくは L</"Regexp Quote-Like Operators"> を、これらの演算子を使った
910600例については L<perlretut> を参照して下さい。
911601
912602=begin original
913603
914604If the right argument is an expression rather than a search pattern,
915605substitution, or transliteration, it is interpreted as a search pattern at run
916time. Note that this means that its
606time. Note that this means that its contents will be interpolated twice, so
917contents will be interpolated twice, so
918607
919608=end original
920609
921610右引数が検索パターン、置換、文字変換ではなく、式であれば、
922611それは実行時に決まる検索パターンと解釈されます。
923612これは、内容が 2 回展開されることを意味することに注意してください;
924613つまり:
925614
926 '\\' =~ q'\\';
615 '\\' =~ q'\\';
927616
928617=begin original
929618
930619is not ok, as the regex engine will end up trying to compile the
931620pattern C<\>, which it will consider a syntax error.
932621
933622=end original
934623
935624は正しくありません; 正規表現エンジンは最終的にパターン C<\> を
936625コンパイルしようとして、これは文法エラーと考えるからです。
937626
938627=begin original
939628
940Binary C<"!~"> is just like C<"=~"> except the return value is negated in
629Binary "!~" is just like "=~" except the return value is negated in
941630the logical sense.
942631
943632=end original
944633
945二項演算子の C<"!~"> は、返される値が論理否定されることを除いて
634二項演算子の "!~" は、返される値が論理否定されることを除いて
946C<"=~"> と同じです。
635"=~" と同じです。
947636
948=begin original
949
950Binary C<"!~"> with a non-destructive substitution (C<s///r>) or transliteration
951(C<y///r>) is a syntax error.
952
953=end original
954
955二項演算子の C<"!~"> を非破壊置換 (C<s///r>) や変換 (C<y///r>) で使うと
956文法エラーとなります。
957
958637=head2 Multiplicative Operators
959638X<operator, multiplicative>
960639
961640(乗法演算子)
962641
963642=begin original
964643
965Binary C<"*"> multiplies two numbers.
644Binary "*" multiplies two numbers.
966645X<*>
967646
968647=end original
969648
970二項演算子の C<"*"> は 2 つの数値の積を返します。
649二項演算子の "*" は 2 つの数値の積を返します。
971650X<*>
972651
973652=begin original
974653
975Binary C<"/"> divides two numbers.
654Binary "/" divides two numbers.
976655X</> X<slash>
977656
978657=end original
979658
980二項演算子の C<"/"> は 2 つの数値の商を返します。
659二項演算子の "/" は 2 つの数値の商を返します。
981660X</> X<slash>
982661
983662=begin original
984663
985Binary C<"%"> is the modulo operator, which computes the division
664Binary "%" is the modulo operator, which computes the division
986665remainder of its first argument with respect to its second argument.
987666Given integer
988operands C<$m> and C<$n>: If C<$n> is positive, then S<C<$m % $n>> is
667operands C<$a> and C<$b>: If C<$b> is positive, then C<$a % $b> is
989C<$m> minus the largest multiple of C<$n> less than or equal to
668C<$a> minus the largest multiple of C<$b> less than or equal to
990C<$m>. If C<$n> is negative, then S<C<$m % $n>> is C<$m> minus the
669C<$a>. If C<$b> is negative, then C<$a % $b> is C<$a> minus the
991smallest multiple of C<$n> that is not less than C<$m> (that is, the
670smallest multiple of C<$b> that is not less than C<$a> (i.e. the
992671result will be less than or equal to zero). If the operands
993C<$m> and C<$n> are floating point values and the absolute value of
672C<$a> and C<$b> are floating point values and the absolute value of
994C<$n> (that is C<abs($n)>) is less than S<C<(UV_MAX + 1)>>, only
673C<$b> (that is C<abs($b)>) is less than C<(UV_MAX + 1)>, only
995the integer portion of C<$m> and C<$n> will be used in the operation
674the integer portion of C<$a> and C<$b> will be used in the operation
996675(Note: here C<UV_MAX> means the maximum of the unsigned integer type).
997If the absolute value of the right operand (C<abs($n)>) is greater than
676If the absolute value of the right operand (C<abs($b)>) is greater than
998or equal to S<C<(UV_MAX + 1)>>, C<"%"> computes the floating-point remainder
677or equal to C<(UV_MAX + 1)>, "%" computes the floating-point remainder
999C<$r> in the equation S<C<($r = $m - $i*$n)>> where C<$i> is a certain
678C<$r> in the equation C<($r = $a - $i*$b)> where C<$i> is a certain
1000679integer that makes C<$r> have the same sign as the right operand
1001C<$n> (B<not> as the left operand C<$m> like C function C<fmod()>)
680C<$b> (B<not> as the left operand C<$a> like C function C<fmod()>)
1002and the absolute value less than that of C<$n>.
681and the absolute value less than that of C<$b>.
1003Note that when S<C<use integer>> is in scope, C<"%"> gives you direct access
682Note that when C<use integer> is in scope, "%" gives you direct access
1004683to the modulo operator as implemented by your C compiler. This
1005684operator is not as well defined for negative operands, but it will
1006685execute faster.
1007686X<%> X<remainder> X<modulo> X<mod>
1008687
1009688=end original
1010689
1011二項演算子の C<"%"> は剰余演算子で、一つ目の引数を二つ目の引数で割ったときの
690二項演算子の "%" は剰余演算子で、一つ目の引数を二つ目の引数で割ったときの
1012691余りを返します。
1013C<$m> と C<$n> の二つの整数の被演算子を取ったとすると:
692C<$a> と C<$b> の二つの整数の被演算子を取ったとすると:
1014C<$n> が正の場合、S<C<$m % $n>> は、C<$m> から C<$m> 以下の最大の
693C<$b> が正の場合、C<$a % $b> は、C<$a> から C<$a> 以下の最大の
1015C<$n> の倍数を引いた値です。
694C<$b> の倍数を引いた値です。
1016C<$n> が負の場合、S<C<$m % $n>> は、C<$m> から C<$m> を下回らない
695C<$b> が負の場合、C<$a % $b> は、C<$a> から C<$a> を下回らない
1017最小の C<$n> の倍数を引いた値です(従って結果はゼロ以下になります)
696最小の C<$b> の倍数を引いた値です(従って結果はゼロ以下になります。)
1018オペランド C<$m> と C<$n> が浮動小数点数で、C<$n> の絶対値
697オペランド C<$a> と C<$b> が浮動小数点数で、C<$b> の絶対値
1019(つまり C<abs($n)>) が S<C<(UV_MAX + 1)>> より小さい場合、
698(つまり C<abs($b)>) が C<(UV_MAX + 1)> より小さい場合、
1020C<$m> と C<$n> の整数部のみが操作で使われます
699C<$a> と C<$b> の整数部のみが操作で使われます
1021700(注意: ここで C<UV_MAX> は符号なし整数の最大値を意味します)。
1022右オペランドの絶対値 (C<abs($n)>) が S<C<(UV_MAX + 1)>> 以上の場合、
701右オペランドの絶対値 (C<abs($b)>) が C<(UV_MAX + 1)> 以上の場合、
1023C<"%"> は、S<C<($r = $m - $i*$n)>> となる浮動小数点剰余 C<$r> を計算します;
702"%" は、C<($r = $a - $i*$b)> となる浮動小数点剰余 C<$r> を計算します;
1024ここで C<$i> は、C<$r> が右オペランド C<$n> と同じ符号 (C の
703ここで C<$i> は、C<$r> が右オペランド C<$b> と同じ符号 (C の
1025関数 C<fmod()> のように左オペランド C<$m> B<ではありません>) で、
704関数 C<fmod()> のように左オペランド C<$a> B<ではありません>) で、
1026絶対値が C<$n> より小さいものになるような、ある整数です。
705絶対値が C<$b> より小さいものになるような、ある整数です。
1027S<C<use integer>> がスコープ内にある場合、
706C<use integer> がスコープ内にある場合、
1028C<"%"> は C コンパイラで実装された剰余演算子を使います。
707"%" は C コンパイラで実装された剰余演算子を使います。
1029708この演算子は被演算子が負の場合の挙動が不確実ですが、
1030709より高速です。
1031710X<%> X<remainder> X<modulo> X<mod>
1032711
1033712=begin original
1034713
1035Binary C<x> is the repetition operator. In scalar context, or if the
714Binary "x" is the repetition operator. In scalar context or if the left
1036left operand is neither enclosed in parentheses nor a C<qw//> list,
715operand is not enclosed in parentheses, it returns a string consisting
1037it performs a string repetition. In that case it supplies scalar
716of the left operand repeated the number of times specified by the right
1038context to the left operand, and returns a string consisting of the
717operand. In list context, if the left operand is enclosed in
1039left operand string repeated the number of times specified by the right
718parentheses or is a list formed by C<qw/STRING/>, it repeats the list.
1040operand. If the C<x> is in list context, and the left operand is either
719If the right operand is zero or negative, it returns an empty string
1041enclosed in parentheses or a C<qw//> list, it performs a list repetition.
1042In that case it supplies list context to the left operand, and returns
1043a list consisting of the left operand list repeated the number of times
1044specified by the right operand.
1045If the right operand is zero or negative (raising a warning on
1046negative), it returns an empty string
1047720or an empty list, depending on the context.
1048721X<x>
1049722
1050723=end original
1051724
1052二項演算子の C<"x"> は繰り返し演算子です。
725二項演算子の "x" は繰り返し演算子です。
1053スカラコンテキストまたは左辺値がかっこ囲またり
726スカラコンテキストまたは左辺値が括弧括らていない場合は、
1054C<qw//> リストであったりしない場合は、文字列の繰り返しを実行します。
727左被演算子を右被演算子に示す数だけ繰り返したもので構成される
1055この場合、左オペランドにスカラコンテキストを提供し、
1056右オペランドによって指定された回数繰り返された左オペランド文字列からなる
1057728文字列を返します。
1058C<x> がリストコンテキストで、
729リストコンテキストで左被演算子が括弧で括られているか、C<qw/STRING> の
1059左オペランドがかっこでくくられているか
730形のリストの場合、リストを繰り返します。
1060C<qw//> リストの場合、
731右オペランドが 0 か負数の場合、コンテキストによって、空文字列か空リストを
1061リスト繰りしを実行します。
732返します。
1062この場合、これは左オペランドへリストコンテキストを提供し、
1063右オペランドによって指定された回数繰り返された
1064左オペランドのリストからなるリストを返します。
1065右オペランドが 0 か負数の場合(負数の場合は警告が出ます)、
1066コンテキストによって空文字列か空リストを返します。
1067733X<x>
1068734
1069735 print '-' x 80; # print row of dashes
1070736
1071737 print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
1072738
1073739 @ones = (1) x 80; # a list of 80 1's
1074740 @ones = (5) x @ones; # set all elements to 5
1075741
742
1076743=head2 Additive Operators
1077744X<operator, additive>
1078745
1079746(加法演算子)
1080747
1081748=begin original
1082749
1083Binary C<"+"> returns the sum of two numbers.
750Binary "+" returns the sum of two numbers.
1084751X<+>
1085752
1086753=end original
1087754
1088二項演算子の C<"+"> は 2 つの数値の和を返します。
755二項演算子の "+" は 2 つの数値の和を返します。
1089756X<+>
1090757
1091758=begin original
1092759
1093Binary C<"-"> returns the difference of two numbers.
760Binary "-" returns the difference of two numbers.
1094761X<->
1095762
1096763=end original
1097764
1098二項演算子の C<"-"> は 2 つの数値の差を返します。
765二項演算子の "-" は 2 つの数値の差を返します。
1099766X<->
1100767
1101768=begin original
1102769
1103Binary C<"."> concatenates two strings.
770Binary "." concatenates two strings.
1104771X<string, concatenation> X<concatenation>
1105772X<cat> X<concat> X<concatenate> X<.>
1106773
1107774=end original
1108775
1109二項演算子の C<"."> は 2 つの文字列を連結します。
776二項演算子の "." は 2 つの文字列を連結します。
1110777X<string, concatenation> X<concatenation>
1111778X<cat> X<concat> X<concatenate> X<.>
1112779
1113780=head2 Shift Operators
1114781X<shift operator> X<operator, shift> X<<< << >>>
1115782X<<< >> >>> X<right shift> X<left shift> X<bitwise shift>
1116783X<shl> X<shr> X<shift, right> X<shift, left>
1117784
1118785(シフト演算子)
1119786
1120787=begin original
1121788
1122Binary C<<< "<<" >>> returns the value of its left argument shifted left by the
789Binary "<<" returns the value of its left argument shifted left by the
1123790number of bits specified by the right argument. Arguments should be
1124integers. (See also L</Integer Arithmetic>.)
791integers. (See also L<Integer Arithmetic>.)
1125792
1126793=end original
1127794
1128二項演算子の C<<< "<<" >>> は左引数の値を、右引数で示すビット数だけ、
795二項演算子の "<<" は左引数の値を、右引数で示すビット数だけ、
1129796左にシフトした値を返します。
1130797引数は整数でなければなりません。
1131(L</Integer Arithmetic> も参照して下さい。)
798(L<Integer Arithmetic> も参照して下さい。)
1132799
1133800=begin original
1134801
1135Binary C<<< ">>" >>> returns the value of its left argument shifted right by
802Binary ">>" returns the value of its left argument shifted right by
1136803the number of bits specified by the right argument. Arguments should
1137be integers. (See also L</Integer Arithmetic>.)
804be integers. (See also L<Integer Arithmetic>.)
1138805
1139806=end original
1140807
1141二項演算子の C<<< ">>" >>> は左引数の値を、右引数で示すビット数だけ、
808二項演算子の ">>" は左引数の値を、右引数で示すビット数だけ、
1142809右にシフトした値を返します。
1143810引数は整数でなければなりません。
1144(L</Integer Arithmetic> も参照して下さい。)
811(L<Integer Arithmetic> も参照して下さい。)
1145812
1146813=begin original
1147814
1148If S<C<use integer>> (see L</Integer Arithmetic>) is in force then
815Note that both "<<" and ">>" in Perl are implemented directly using
1149signed C integers are used (I<arithmetic shift>), otherwise unsigned C
816"<<" and ">>" in C. If C<use integer> (see L<Integer Arithmetic>) is
1150integers are used (I<logical shift>), even for negative shiftees.
817in force then signed C integers are used, else unsigned C integers are
1151In arithmetic right shift the sign bit is replicated on the left,
818used. Either way, the implementation isn't going to generate results
1152in logical shift zero bits come in from the left.
819larger than the size of the integer type Perl was built with (32 bits
820or 64 bits).
1153821
1154822=end original
1155823
1156S<C<use integer>> (L</Integer Arithmetic> を参照してください)が有効な場合、
824Perl での "<<" と ">>" は C での "<<" と ">>"直接利用して
1157C の符号付き整数が使わ(I<算術シフト>)、そうでな場合は(例え負のシフトでも)
825実装さることに注意してください。
1158C の符号なし整数が使われます(I<論理シフト>)
826C<use integer> (L<Integer Arithmetic> を参照してください)が有効な場合、
1159算術右シフトでは符号ビットは左側に複製され、論理シフトでは 0 ビット
827C の符号付き整数が使われ、そうない場合C の符号なし整数使われます。
1160左側から来ます。
1161
1162=begin original
1163
1164Either way, the implementation isn't going to generate results larger
1165than the size of the integer type Perl was built with (32 bits or 64 bits).
1166
1167=end original
1168
1169828どちらの場合も、この実装は Perl がビルドされた整数型のサイズ(32 ビットか
117082964 ビット)よりも大きい結果を生成することはありません。
1171830
1172831=begin original
1173832
1174Shifting by negative number of bits means the reverse shift: left
833The result of overflowing the range of the integers is undefined
1175shift becomes right shift, right shift becomes left shift. This is
834because it is undefined also in C. In other words, using 32-bit
1176unlike in C, where negative shift is undefined.
835integers, C<< 1 << 32 >> is undefined. Shifting by a negative number
836of bits is also undefined.
1177837
1178838=end original
1179839
1180数のビットシフトは逆シフト意味します: 左シ右シフトに鳴り
840数の範囲オーバーローした場合の結果は、C でも未定義なので、未定義です。
1181右シフトは左シフトになります。
841言い換えると、32 ビッ整数を使っているとき、C<< 1 << 32 >> 未定義です。
1182これは、負のシフト未定義である C とは異なります。
842シフトするビット数として負の数を指定した場合も未定義です。
1183843
1184=begin original
1185
1186Shifting by more bits than the size of the integers means most of the
1187time zero (all bits fall off), except that under S<C<use integer>>
1188right overshifting a negative shiftee results in -1. This is unlike
1189in C, where shifting by too many bits is undefined. A common C
1190behavior is "shift by modulo wordbits", so that for example
1191
1192=end original
1193
1194整数のサイズ以上のビット数のシフトは、ほとんどの場合 0 (全てのビットが落ちる)
1195になります; 例外として、S<C<use integer>> の基で、負の値を超過シフトすると
1196-1 になります。
1197これは、多すぎるビット数のシフトは未定義である C とは異なります。
1198一般的な C の振る舞いは「ワードビット数を法としてシフトする」なので、
1199例えば次のようになります
1200
1201 1 >> 64 == 1 >> (64 % 64) == 1 >> 0 == 1 # Common C behavior.
1202
1203=begin original
1204
1205but that is completely accidental.
1206
1207=end original
1208
1209しかしこれは完全に偶然です。
1210
1211=begin original
1212
1213If you get tired of being subject to your platform's native integers,
1214the S<C<use bigint>> pragma neatly sidesteps the issue altogether:
1215
1216=end original
1217
1218プラットフォームのネイティブな整数の影響に疲れたなら、S<C<use bigint>>
1219プラグマは問題を完全にうまく回避します:
1220
1221 print 20 << 20; # 20971520
1222 print 20 << 40; # 5120 on 32-bit machines,
1223 # 21990232555520 on 64-bit machines
1224 use bigint;
1225 print 20 << 100; # 25353012004564588029934064107520
1226
1227844=head2 Named Unary Operators
1228845X<operator, named unary>
1229846
1230847(名前付き単項演算子)
1231848
1232849=begin original
1233850
1234851The various named unary operators are treated as functions with one
1235852argument, with optional parentheses.
1236853
1237854=end original
1238855
1239856さまざまな名前付き単項演算子が、引数を 1 つ持ち、括弧が省略可能な、
1240857関数として扱われます。
1241858
1242859=begin original
1243860
1244If any list operator (C<print()>, etc.) or any unary operator (C<chdir()>, etc.)
861If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
1245862is followed by a left parenthesis as the next token, the operator and
1246863arguments within parentheses are taken to be of highest precedence,
1247864just like a normal function call. For example,
1248because named unary operators are higher precedence than C<||>:
865because named unary operators are higher precedence than ||:
1249866
1250867=end original
1251868
1252リスト演算子 (C<print()> など) や単項演算子 (C<chdir()> など) は、
869リスト演算子 (print() など) や単項演算子 (chdir() など) は、
1253870すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、
1254871通常の関数呼び出しのようにもっとも高い優先順位として扱われます。
1255たとえば、名前つき単項演算子は C<||> より優先順位が高いので、
872たとえば、名前つき単項演算子は || より優先順位が高いので、
1256873以下のようになります:
1257874
1258875 chdir $foo || die; # (chdir $foo) || die
1259876 chdir($foo) || die; # (chdir $foo) || die
1260877 chdir ($foo) || die; # (chdir $foo) || die
1261878 chdir +($foo) || die; # (chdir $foo) || die
1262879
1263880=begin original
1264881
1265but, because C<"*"> is higher precedence than named operators:
882but, because * is higher precedence than named operators:
1266883
1267884=end original
1268885
1269しかし C<"*"> は名前つき演算子より優先順位が高いので、以下のようになります:
886しかし * は名前つき演算子より優先順位が高いので、以下のようになります:
1270887
1271888 chdir $foo * 20; # chdir ($foo * 20)
1272889 chdir($foo) * 20; # (chdir $foo) * 20
1273890 chdir ($foo) * 20; # (chdir $foo) * 20
1274891 chdir +($foo) * 20; # chdir ($foo * 20)
1275892
1276893 rand 10 * 20; # rand (10 * 20)
1277894 rand(10) * 20; # (rand 10) * 20
1278895 rand (10) * 20; # (rand 10) * 20
1279896 rand +(10) * 20; # rand (10 * 20)
1280897
1281898=begin original
1282899
1283900Regarding precedence, the filetest operators, like C<-f>, C<-M>, etc. are
1284901treated like named unary operators, but they don't follow this functional
1285902parenthesis rule. That means, for example, that C<-f($file).".bak"> is
1286equivalent to S<C<-f "$file.bak">>.
903equivalent to C<-f "$file.bak">.
1287904X<-X> X<filetest> X<operator, filetest>
1288905
1289906=end original
1290907
1291908優先順位に関して、C<-f> や C<-M> のようなファイルテスト演算子は、名前付き
1292909単項演算子として扱われますが、この関数のかっこルールは適用されません。
1293これは、例えば C<-f($file).".bak"> は S<C<-f "$file.bak">> と等価であることを
910これは、例えば C<-f($file).".bak"> は C<-f "$file.bak"> と等価であることを
1294911意味します。
1295912X<-X> X<filetest> X<operator, filetest>
1296913
1297914=begin original
1298915
1299See also L</"Terms and List Operators (Leftward)">.
916See also L<"Terms and List Operators (Leftward)">.
1300917
1301918=end original
1302919
1303L</"Terms and List Operators (Leftward)"> も参照して下さい。
920L<"Terms and List Operators (Leftward)"> も参照して下さい。
1304921
1305922=head2 Relational Operators
1306923X<relational operator> X<operator, relational>
1307924
1308925(比較演算子)
1309926
1310927=begin original
1311928
1312Perl operators that return true or false generally return values
929Binary "<" returns true if the left argument is numerically less than
1313that can be safely used as numbers. For example, the relational
1314operators in this section and the equality operators in the next
1315one return C<1> for true and a special version of the defined empty
1316string, C<"">, which counts as a zero but is exempt from warnings
1317about improper numeric conversions, just as S<C<"0 but true">> is.
1318
1319=end original
1320
1321真か偽の値を返す Perl 演算子は一般的に安全に数値として使える値を返します。
1322例えば、この節の関係演算子と次の節の等価演算子は、真および、
1323S<C<"0 but true">> と同様、ゼロとカウントされるけれども不適切な数値変換に
1324関する警告の出ない、定義された空文字列 C<""> の特別版に対して C<1> を
1325返します。
1326
1327=begin original
1328
1329Binary C<< "<" >> returns true if the left argument is numerically less than
1330930the right argument.
1331931X<< < >>
1332932
1333933=end original
1334934
1335二項演算子の C<< "<" >> は左引数が数値的に右引数よりも小さければ、
935二項演算子の "<" は左引数が数値的に右引数よりも小さければ、
1336936真を返します。
1337X<< < >>
1338937
1339938=begin original
1340939
1341Binary C<< ">" >> returns true if the left argument is numerically greater
940Binary ">" returns true if the left argument is numerically greater
1342941than the right argument.
1343942X<< > >>
1344943
1345944=end original
1346945
1347二項演算子の C<< ">" >> は左引数が数値的に右引数よりも大きければ、
946二項演算子の ">" は左引数が数値的に右引数よりも大きければ、
1348947真を返します。
1349X<< > >>
1350948
1351949=begin original
1352950
1353Binary C<< "<=" >> returns true if the left argument is numerically less than
951Binary "<=" returns true if the left argument is numerically less than
1354952or equal to the right argument.
1355953X<< <= >>
1356954
1357955=end original
1358956
1359二項演算子の C<< "<=" >> は左引数が数値的に右引数よりも小さいか等しければ、
957二項演算子の "<=" は左引数が数値的に右引数よりも小さいか等しければ、
1360958真を返します。
1361X<< <= >>
1362959
1363960=begin original
1364961
1365Binary C<< ">=" >> returns true if the left argument is numerically greater
962Binary ">=" returns true if the left argument is numerically greater
1366963than or equal to the right argument.
1367964X<< >= >>
1368965
1369966=end original
1370967
1371二項演算子の C<< ">=" >> は左引数が数値的に右引数よりも大きいか等しければ、
968二項演算子の ">=" は左引数が数値的に右引数よりも大きいか等しければ、
1372969真を返します。
1373X<< >= >>
1374970
1375971=begin original
1376972
1377Binary C<"lt"> returns true if the left argument is stringwise less than
973Binary "lt" returns true if the left argument is stringwise less than
1378974the right argument.
1379975X<< lt >>
1380976
1381977=end original
1382978
1383二項演算子の C<"lt"> は左引数が文字列的に右引数よりも小さければ、真を返します。
979二項演算子の "lt" は左引数が文字列的に右引数よりも小さければ、
1384X<< lt >>
980真を返します。
1385981
1386982=begin original
1387983
1388Binary C<"gt"> returns true if the left argument is stringwise greater
984Binary "gt" returns true if the left argument is stringwise greater
1389985than the right argument.
1390986X<< gt >>
1391987
1392988=end original
1393989
1394二項演算子の C<"gt"> は左引数が文字列的に右引数よりも大きければ、真を返します。
990二項演算子の "gt" は左引数が文字列的に右引数よりも大きければ、
1395X<< gt >>
991真を返します。
1396992
1397993=begin original
1398994
1399Binary C<"le"> returns true if the left argument is stringwise less than
995Binary "le" returns true if the left argument is stringwise less than
1400996or equal to the right argument.
1401997X<< le >>
1402998
1403999=end original
14041000
1405二項演算子の C<"le"> は左引数が文字列的に右引数よりも小さいか等しければ、
1001二項演算子の "le" は左引数が文字列的に右引数よりも小さいか等しければ、
14061002真を返します。
1407X<< le >>
14081003
14091004=begin original
14101005
1411Binary C<"ge"> returns true if the left argument is stringwise greater
1006Binary "ge" returns true if the left argument is stringwise greater
14121007than or equal to the right argument.
14131008X<< ge >>
14141009
14151010=end original
14161011
1417二項演算子の C<"ge"> は左引数が文字列的に右引数よりも大きいか等しければ、
1012二項演算子の "ge" は左引数が文字列的に右引数よりも大きいか等しければ、
14181013真を返します。
1419X<< ge >>
14201014
1421=begin original
1422
1423A sequence of relational operators, such as S<C<"$x E<lt> $y E<lt>=
1424$z">>, performs chained comparisons, in the manner described above in
1425the section L</"Operator Precedence and Associativity">.
1426Beware that they do not chain with equality operators, which have lower
1427precedence.
1428
1429=end original
1430
1431S<C<"$x E<lt> $y E<lt>= $z">> のような比較演算子の並びは、
1432L</"Operator Precedence and Associativity"> 節で述べたような形で
1433連鎖比較を実行します。
1434より低い優先順位を持つ等価演算子とは連鎖しないことに注意してください。
1435
14361015=head2 Equality Operators
14371016X<equality> X<equal> X<equals> X<operator, equality>
14381017
14391018(等価演算子)
14401019
14411020=begin original
14421021
1443Binary C<< "==" >> returns true if the left argument is numerically equal to
1022Binary "==" returns true if the left argument is numerically equal to
14441023the right argument.
14451024X<==>
14461025
14471026=end original
14481027
1449二項演算子の C<< "==" >> は左引数が数値的に右引数と等しければ、真を返します。
1028二項演算子の "==" は左引数が数値的に右引数と等しければ、
1450X<==>
1029真を返します。
14511030
14521031=begin original
14531032
1454Binary C<< "!=" >> returns true if the left argument is numerically not equal
1033Binary "!=" returns true if the left argument is numerically not equal
14551034to the right argument.
14561035X<!=>
14571036
14581037=end original
14591038
1460二項演算子の C<< "!=" >> は左引数が数値的に右引数と等しくなければ、真を
1039二項演算子の "!=" は左引数が数値的に右引数と等しくなければ、
1461返します。
1040真を返します。
1462X<!=>
14631041
14641042=begin original
14651043
1466Binary C<"eq"> returns true if the left argument is stringwise equal to
1044Binary "<=>" returns -1, 0, or 1 depending on whether the left
1467the right argument.
1045argument is numerically less than, equal to, or greater than the right
1468X<eq>
1046argument. If your platform supports NaNs (not-a-numbers) as numeric
1047values, using them with "<=>" returns undef. NaN is not "<", "==", ">",
1048"<=" or ">=" anything (even NaN), so those 5 return false. NaN != NaN
1049returns true, as does NaN != anything else. If your platform doesn't
1050support NaNs then NaN is just a string with numeric value 0.
1051X<< <=> >> X<spaceship>
14691052
14701053=end original
14711054
1472二項演算子の C<"eq"> は左引数が文字列的に右引数等しければ真を返します。
1055二項演算子の "<=>" は左引数が数値的に右引数より小さいか、等しいか
1473X<eq>
1056大きいかに従って、-1, 0, 1 を返します。
1057数値として NaN (非数) に対応しているプラットフォームでは、
1058NaN に対して "<=>" を使うと undef を返します。
1059NaN はどの値に対しても(NaN に対してでさえも) "<", "==", ">",
1060"<=", ">=" のいずれも成立しないので、これらは全て偽となります。
1061NaN != NaN は真を返しますが、その他のどの値に対しても != は偽を返します。
1062NaN に対応していないプラットフォームでは、NaN は単に数としての値 0 を持つ
1063文字列です。
14741064
1475=begin original
1065 perl -le '$a = "NaN"; print "No NaN support here" if $a == $a'
1066 perl -le '$a = "NaN"; print "NaN support here" if $a != $a'
14761067
1477Binary C<"ne"> returns true if the left argument is stringwise not equal
1478to the right argument.
1479X<ne>
1480
1481=end original
1482
1483二項演算子の C<"ne"> は左引数が文字列的に右引数と等しくなければ、真を
1484返します。
1485X<ne>
1486
14871068=begin original
14881069
1489A sequence of the above equality operators, such as S<C<"$x == $y ==
1070Binary "eq" returns true if the left argument is stringwise equal to
1490$z">>, performs chained comparisons, in the manner described above in
1071the right argument.
1491the section L</"Operator Precedence and Associativity">.
1072X<eq>
1492Beware that they do not chain with relational operators, which have
1493higher precedence.
14941073
14951074=end original
14961075
1497S<C<"$x == $y == $z">> のような上述の等価演算子の並びは、
1076二項演算子の "eq" 左引数が文字列的に右引数と等しければ
1498L</"Operator Precedence and Associativity"> 節で述べたような形で
1077真を返します。
1499連鎖比較を実行します。
1500より高い優先順位を持つ比較演算子とは連鎖しないことに注意してください。
15011078
15021079=begin original
15031080
1504Binary C<< "<=>" >> returns -1, 0, or 1 depending on whether the left
1081Binary "ne" returns true if the left argument is stringwise not equal
1505argument is numerically less than, equal to, or greater than the right
1082to the right argument.
1506argument. If your platform supports C<NaN>'s (not-a-numbers) as numeric
1083X<ne>
1507values, using them with C<< "<=>" >> returns undef. C<NaN> is not
1508C<< "<" >>, C<< "==" >>, C<< ">" >>, C<< "<=" >> or C<< ">=" >> anything
1509(even C<NaN>), so those 5 return false. S<C<< NaN != NaN >>> returns
1510true, as does S<C<NaN !=> I<anything else>>. If your platform doesn't
1511support C<NaN>'s then C<NaN> is just a string with numeric value 0.
1512X<< <=> >>
1513X<spaceship>
15141084
15151085=end original
15161086
1517二項演算子の C<< "<=>" >> は左引数が数値的に右引数より小さいか、等しいか
1087二項演算子の "ne" は左引数が文字列的に右引数等しくなければ
1518大きいかに従って、-1, 0, 1 を返します。
1088を返します。
1519数値として C<NaN> (非数) に対応しているプラットフォームでは、
1520それに対して C<< "<=>" >> を使うと undef を返します。
1521C<NaN> はどの値に対しても(C<NaN> に対してでさえも) C<< "<" >>, C<< "==" >>,
1522C<< ">" >>, C<< "<=" >>, C<< ">=" >> のいずれも成立しないので、これらは全て
1523偽となります。
1524S<C<< NaN != NaN >>> は真を返しますが、
1525S<C<NaN !=> I<その他のどの値でも>> です。
1526C<NaN> に対応していないプラットフォームでは、C<NaN> は単に数としての値 0 を
1527持つ文字列です。
1528X<< <=> >> X<spaceship>
15291089
1530 $ perl -le '$x = "NaN"; print "No NaN support here" if $x == $x'
1531 $ perl -le '$x = "NaN"; print "NaN support here" if $x != $x'
1532
15331090=begin original
15341091
1535(Note that the L<bigint>, L<bigrat>, and L<bignum> pragmas all
1092Binary "cmp" returns -1, 0, or 1 depending on whether the left
1536support C<"NaN">.)
1537
1538=end original
1539
1540(L<bigint>, L<bigrat>, L<bignum> プラグマは全て C<"NaN"> に対応していることに
1541注意してください。)
1542
1543=begin original
1544
1545Binary C<"cmp"> returns -1, 0, or 1 depending on whether the left
15461093argument is stringwise less than, equal to, or greater than the right
15471094argument.
1095X<cmp>
15481096
15491097=end original
15501098
1551二項演算子の C<"cmp"> は左引数が文字列的に右引数より小さいか、
1099二項演算子の "cmp" は左引数が文字列的に右引数より小さいか、
15521100等しいか、大きいかに従って、-1, 0, 1 を返します。
15531101
15541102=begin original
15551103
1556Here we can see the difference between <=> and cmp,
1104Binary "~~" does a smart match between its arguments. Smart matching
1105is described in L<perlsyn/"Smart matching in detail">.
1558=end original
1559
1560ここで、<=> と cmp の違いを示します:
1561
1562 print 10 <=> 2 #prints 1
1563 print 10 cmp 2 #prints -1
1564
1565=begin original
1566
1567(likewise between gt and >, lt and <, etc.)
1568X<cmp>
1569
1570=end original
1571
1572(gt と >, lt と <, なども同様です)
1573X<cmp>
1574
1575=begin original
1576
1577Binary C<"~~"> does a smartmatch between its arguments. Smart matching
1578is described in the next section.
15791106X<~~>
15801107
15811108=end original
15821109
1583二項演算子の C<"~~">その引数に対してスマートマッチングを行います。
1110二項演算子の "~~" はスマートマッチングとして働きます。
1584スマートマッチングについては次の節述べられています。
1111スマートマッチングについては L<perlsyn/"Smart matching in detail">
1112述べられています。
15851113X<~~>
15861114
15871115=begin original
15881116
1589The two-sided ordering operators C<"E<lt>=E<gt>"> and C<"cmp">, and the
1117"lt", "le", "ge", "gt" and "cmp" use the collation (sort) order specified
1590smartmatch operator C<"~~">, are non-associative with respect to each
1118by the current locale if C<use locale> is in effect. See L<perllocale>.
1591other and with respect to the equality operators of the same precedence.
15921119
15931120=end original
15941121
1595両面順序演算子 C<"E<lt>=E<gt>">, C<"cmp"> および、
1122"lt", "le", "ge", "gt", "cmp" は C<use locale> が有効な場合は
1596スマートマッチング演算子 C<"~~"> は、互いに、および同じ優先位の
1123現在のロケルで指定された辞書(ソー)が使われます。
1597演算子に対しては非結合です。
1598
1599=begin original
1600
1601C<"lt">, C<"le">, C<"ge">, C<"gt"> and C<"cmp"> use the collation (sort)
1602order specified by the current C<LC_COLLATE> locale if a S<C<use
1603locale>> form that includes collation is in effect. See L<perllocale>.
1604Do not mix these with Unicode,
1605only use them with legacy 8-bit locale encodings.
1606The standard C<L<Unicode::Collate>> and
1607C<L<Unicode::Collate::Locale>> modules offer much more powerful
1608solutions to collation issues.
1609
1610=end original
1611
1612C<"lt">, C<"le">, C<"ge">, C<"gt">, C<"cmp"> は照合を含む
1613S<C<use locale>> 型式が有効な場合は、現在の C<LC_COLLATE> ロケールで
1614指定された照合(ソート)順が使われます。
16151124L<perllocale> を参照して下さい。
1616これらを Unicode と混ぜないでください;
1617伝統的な 8 ビットロケールコーディングでのみ使ってください。
1618標準の C<L<Unicode::Collate>> と C<L<Unicode::Collate::Locale>> モジュールは、
1619照合問題に関する遥かに強力な解決法を提供します。
16201125
1621=begin original
1622
1623For case-insensitive comparisons, look at the L<perlfunc/fc> case-folding
1624function, available in Perl v5.16 or later:
1625
1626=end original
1627
1628大文字小文字を無視した比較に関しては、Perl v5.16 以降で利用可能な
1629L<perlfunc/fc> 大文字小文字畳み込み関数を参照してください:
1630
1631 if ( fc($x) eq fc($y) ) { ... }
1632
1633=head2 Class Instance Operator
1634X<isa operator>
1635
1636(クラスインスタンス演算子)
1637
1638=begin original
1639
1640Binary C<isa> evaluates to true when the left argument is an object instance of
1641the class (or a subclass derived from that class) given by the right argument.
1642If the left argument is not defined, not a blessed object instance, nor does
1643not derive from the class given by the right argument, the operator evaluates
1644as false. The right argument may give the class either as a bareword or a
1645scalar expression that yields a string class name:
1646
1647=end original
1648
1649二項演算子の C<isa> は、左側の引数が右側の引数で与えられた
1650クラス(またはこのクラスから派生した下位クラス)の
1651オブジェクトインスタンスである場合に真と評価されます。
1652左側の引数が、未定義、bless されたオブジェクトインスタンスではない、
1653右側の引数で与えられたクラスから派生していない、のいずれかの場合、
1654この演算子は偽として評価されます。
1655右側の引数は、裸の単語か、クラス名の文字列となるスカラ式を与えます:
1656
1657 if( $obj isa Some::Class ) { ... }
1658
1659 if( $obj isa "Different::Class" ) { ... }
1660 if( $obj isa $name_of_class ) { ... }
1661
1662=begin original
1663
1664This feature is available from Perl 5.31.6 onwards when enabled by
1665C<use feature 'isa'>. This feature is enabled automatically by a
1666C<use v5.36> (or higher) declaration in the current scope.
1667
1668=end original
1669
1670これは Perl 5.31.6 からの機能で、C<use feature 'isa'> によって
1671有効化されると利用可能になります。
1672この機能は、現在のスコープで C<use v5.36> (またはそれ以上の) 宣言をすることで
1673自動的に有効になります。
1674
1675=head2 Smartmatch Operator
1676
1677(スマートマッチング演算子)
1678
1679=begin original
1680
1681First available in Perl 5.10.1 (the 5.10.0 version behaved differently),
1682binary C<~~> does a "smartmatch" between its arguments. This is mostly
1683used implicitly in the C<when> construct described in L<perlsyn>, although
1684not all C<when> clauses call the smartmatch operator. Unique among all of
1685Perl's operators, the smartmatch operator can recurse. The smartmatch
1686operator is L<experimental|perlpolicy/experimental> and its behavior is
1687subject to change.
1688
1689=end original
1690
1691Perl 5.10.1 で最初に現れた (5.10.0 版は異なった振る舞いでした)、2 項 C<~~> は
1692引数に対する「スマートマッチング」を行います。
1693これはほとんど L<perlsyn> で記述されている C<when> 構文で暗黙に使われますが、
1694C<when> 節だけがスマートマッチング演算子を呼び出すわけではありません。
1695全ての Perl の演算子の中で唯一、スマートマッチング演算子は再帰できます。
1696スマートマッチング演算子は L<実験的|perlpolicy/experimental> で、その振る舞いは
1697変更されることがあります。
1698
1699=begin original
1700
1701It is also unique in that all other Perl operators impose a context
1702(usually string or numeric context) on their operands, autoconverting
1703those operands to those imposed contexts. In contrast, smartmatch
1704I<infers> contexts from the actual types of its operands and uses that
1705type information to select a suitable comparison mechanism.
1706
1707=end original
1708
1709また、その他全ての Perl 演算子はそのオペランドにコンテキスト(通常は
1710文字列または数値コンテキスト)を割り当てて、オペランドを割り当てた
1711コンテキストに自動変換します。
1712一方、スマートマッチングはそのオペランドの実際の型からコンテキストを
1713I<推論> して、適切な比較機構を選択するためにその型情報を使います。
1714
1715=begin original
1716
1717The C<~~> operator compares its operands "polymorphically", determining how
1718to compare them according to their actual types (numeric, string, array,
1719hash, etc.). Like the equality operators with which it shares the same
1720precedence, C<~~> returns 1 for true and C<""> for false. It is often best
1721read aloud as "in", "inside of", or "is contained in", because the left
1722operand is often looked for I<inside> the right operand. That makes the
1723order of the operands to the smartmatch operand often opposite that of
1724the regular match operator. In other words, the "smaller" thing is usually
1725placed in the left operand and the larger one in the right.
1726
1727=end original
1728
1729C<~~> 演算子はオペランドを「多態的に」比較します; どのように比較するかの
1730決定は、実際の型 (数値、文字列、配列、ハッシュなど) に基づきます。
1731同じ優先順位を共有する等価演算子のように、
1732C<~~> は真では 1 を、偽では C<""> を返します。
1733これはしばしば "in", "inside of", "is contained in" と呼ぶのが最良です;
1734なぜなら左オペランドはしばしば右オペランドの I<内側> を探すからです。
1735これにより、スマートマッチングオペランドへのオペランドの順序はしばしば
1736正規表現演算子のものと逆になります。
1737言い換えると、「より小さい」ものが普通は左オペランドに置かれ、より
1738大きいものが右側に置かれます。
1739
1740=begin original
1741
1742The behavior of a smartmatch depends on what type of things its arguments
1743are, as determined by the following table. The first row of the table
1744whose types apply determines the smartmatch behavior. Because what
1745actually happens is mostly determined by the type of the second operand,
1746the table is sorted on the right operand instead of on the left.
1747
1748=end original
1749
1750スマートマッチングの振る舞いは、次の表で決定されるように、引数がどんな
1751型かに依存します。
1752型が適用される表の最初の行は、スマートマッチングの振る舞いを決定します。
1753実際に何が起こるかはほとんどの場合 2 番目のオペランドの型で決定されるので、
1754表は左ではなく右オペランドでソートされています。
1755
1756=begin original
1757
1758 Left Right Description and pseudocode
1759 ===============================================================
1760 Any undef check whether Any is undefined
1761 like: !defined Any
1762
1763=end original
1764
1765 左 右 説明と擬似コード
1766 ===============================================================
1767 Any undef Any が未定義かどうか調べる
1768 like: !defined Any
1769
1770=begin original
1771
1772 Any Object invoke ~~ overloading on Object, or die
1773
1774=end original
1775
1776 Any Object Object に対する ~~ オーバーロードを起動するか die
1777
1778=begin original
1779
1780 Right operand is an ARRAY:
1781
1782=end original
1783
1784 右被演算子が 配列:
1785
1786=begin original
1787
1788 Left Right Description and pseudocode
1789 ===============================================================
1790 ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2]
1791 like: (ARRAY1[0] ~~ ARRAY2[0])
1792 && (ARRAY1[1] ~~ ARRAY2[1]) && ...
1793 HASH ARRAY any ARRAY elements exist as HASH keys
1794 like: grep { exists HASH->{$_} } ARRAY
1795 Regexp ARRAY any ARRAY elements pattern match Regexp
1796 like: grep { /Regexp/ } ARRAY
1797 undef ARRAY undef in ARRAY
1798 like: grep { !defined } ARRAY
1799 Any ARRAY smartmatch each ARRAY element[3]
1800 like: grep { Any ~~ $_ } ARRAY
1801
1802=end original
1803
1804 左 右 説明と擬似コード
1805 ===============================================================
1806 ARRAY1 ARRAY2 ARRAY1 と ARRAY2 の組の要素に対して再帰 [2]
1807 like: (ARRAY1[0] ~~ ARRAY2[0])
1808 && (ARRAY1[1] ~~ ARRAY2[1]) && ...
1809 HASH ARRAY いずれかの ARRAY 要素が HASH キーに存在するか
1810 like: grep { exists HASH->{$_} } ARRAY
1811 Regexp ARRAY いずれかの ARRAY 要素が Regexp でマッチングするか
1812 like: grep { /Regexp/ } ARRAY
1813 undef ARRAY ARRAY 内の undef
1814 like: grep { !defined } ARRAY
1815 Any ARRAY それぞれの ARRAY 要素に対してスマートマッチング [3]
1816 like: grep { Any ~~ $_ } ARRAY
1817
1818=begin original
1819
1820 Right operand is a HASH:
1821
1822=end original
1823
1824 右被演算子がハッシュ:
1825
1826=begin original
1827
1828 Left Right Description and pseudocode
1829 ===============================================================
1830 HASH1 HASH2 all same keys in both HASHes
1831 like: keys HASH1 ==
1832 grep { exists HASH2->{$_} } keys HASH1
1833 ARRAY HASH any ARRAY elements exist as HASH keys
1834 like: grep { exists HASH->{$_} } ARRAY
1835 Regexp HASH any HASH keys pattern match Regexp
1836 like: grep { /Regexp/ } keys HASH
1837 undef HASH always false (undef cannot be a key)
1838 like: 0 == 1
1839 Any HASH HASH key existence
1840 like: exists HASH->{Any}
1841
1842=end original
1843
1844 左 右 説明と擬似コード
1845 ===============================================================
1846 HASH1 HASH2 HASH1 と HASH2 両方が全て同じキー
1847 like: keys HASH1 ==
1848 grep { exists HASH2->{$_} } keys HASH1
1849 ARRAY HASH いずれかの ARRAY 要素が HASH キーに存在するか
1850 like: grep { exists HASH->{$_} } ARRAY
1851 Regexp HASH いずれかの HASH キーが Regexp でマッチングするか
1852 like: grep { /Regexp/ } keys HASH
1853 undef HASH 常に偽 (undef はキーになれない)
1854 like: 0 == 1
1855 Any HASH HASH キーが存在するか
1856 like: exists HASH->{Any}
1857
1858=begin original
1859
1860 Right operand is CODE:
1861
1862=end original
1863
1864 右被演算子がコードリファレンス:
1865
1866=begin original
1867
1868 Left Right Description and pseudocode
1869 ===============================================================
1870 ARRAY CODE sub returns true on all ARRAY elements[1]
1871 like: !grep { !CODE->($_) } ARRAY
1872 HASH CODE sub returns true on all HASH keys[1]
1873 like: !grep { !CODE->($_) } keys HASH
1874 Any CODE sub passed Any returns true
1875 like: CODE->(Any)
1876
1877=end original
1878
1879 左 右 説明と擬似コード
1880 ===============================================================
1881 ARRAY CODE 全ての ARRAY 要素に対してサブルーチンが真を返す [1]
1882 like: !grep { !CODE->($_) } ARRAY
1883 HASH CODE 全ての HASH キーに対してサブルーチンが真を返す [1]
1884 like: !grep { !CODE->($_) } keys HASH
1885 Any CODE サブルーチンに Any を渡して真を返す
1886 like: CODE->(Any)
1887
1888=begin original
1889
1890 Right operand is a Regexp:
1891
1892=end original
1893
1894 右被演算子が正規表現:
1895
1896=begin original
1897
1898 Left Right Description and pseudocode
1899 ===============================================================
1900 ARRAY Regexp any ARRAY elements match Regexp
1901 like: grep { /Regexp/ } ARRAY
1902 HASH Regexp any HASH keys match Regexp
1903 like: grep { /Regexp/ } keys HASH
1904 Any Regexp pattern match
1905 like: Any =~ /Regexp/
1906
1907=end original
1908
1909 左 右 説明と擬似コード
1910 ===============================================================
1911 ARRAY Regexp いずれかの ARRAY 要素が Regexp にマッチングするか
1912 like: grep { /Regexp/ } ARRAY
1913 HASH Regexp いずれかの HASH キーが Regexp にマッチングするか
1914 like: grep { /Regexp/ } keys HASH
1915 Any Regexp パターンマッチング
1916 like: Any =~ /Regexp/
1917
1918=begin original
1919
1920 Other:
1921
1922=end original
1923
1924 その他:
1925
1926=begin original
1927
1928 Left Right Description and pseudocode
1929 ===============================================================
1930 Object Any invoke ~~ overloading on Object,
1931 or fall back to...
1932
1933=end original
1934
1935 左 右 説明と擬似コード
1936 ===============================================================
1937 Object Any Object に対して ~~ のオーバーロードを起動、
1938 あるいは次にフォールバック…
1939
1940=begin original
1941
1942 Any Num numeric equality
1943 like: Any == Num
1944 Num nummy[4] numeric equality
1945 like: Num == nummy
1946 undef Any check whether undefined
1947 like: !defined(Any)
1948 Any Any string equality
1949 like: Any eq Any
1950
1951=end original
1952
1953 Any Num 数値の等価性
1954 like: Any == Num
1955 Num nummy[4] 数値の等価性
1956 like: Num == nummy
1957 undef Any 未定義かどうかを調べる
1958 like: !defined(Any)
1959 Any Any 文字列の等価性
1960 like: Any eq Any
1961
1962=begin original
1963
1964Notes:
1965
1966=end original
1967
1968注意:
1969
1970=over
1971
1972=item 1.
1973Empty hashes or arrays match.
1974
1975(1. 空ハッシュや配列はマッチングします。)
1976
1977=item 2.
1978That is, each element smartmatches the element of the same index in the other array.[3]
1979
1980(2. つまり、それぞれの要素は他の配列の同じインデックスの要素とスマートマッチングします。[3])
1981
1982=item 3.
1983If a circular reference is found, fall back to referential equality.
1984
1985(3. 循環参照が見つかると、参照の等価性にフォールバックします。)
1986
1987=item 4.
1988Either an actual number, or a string that looks like one.
1989
1990(4. 実際の数値か、数値に見える文字列のどちらかです。)
1991
1992=back
1993
1994=begin original
1995
1996The smartmatch implicitly dereferences any non-blessed hash or array
1997reference, so the C<I<HASH>> and C<I<ARRAY>> entries apply in those cases.
1998For blessed references, the C<I<Object>> entries apply. Smartmatches
1999involving hashes only consider hash keys, never hash values.
2000
2001=end original
2002
2003スマートマッチングは bless されていないハッシュや配列のリファレンスを暗黙に
2004デリファレンスするので、それらの場合では C<I<HASH>> と C<I<ARRAY>> の
2005エントリが適用されます。
2006bless されたリファレンスでは、C<I<Object>> エントリが適用されます。
2007ハッシュに関連するスマートマッチングはキーのみを考慮し、ハッシュの値は
2008考慮しません。
2009
2010=begin original
2011
2012The "like" code entry is not always an exact rendition. For example, the
2013smartmatch operator short-circuits whenever possible, but C<grep> does
2014not. Also, C<grep> in scalar context returns the number of matches, but
2015C<~~> returns only true or false.
2016
2017=end original
2018
2019"like" コードエントリは常に正確な処理を行うわけではありません。
2020例えば、スマートマッチング演算子は可能なら短絡しますが、C<grep> はしません。
2021また、スカラコンテキストでは C<grep> はマッチングした数を返しますが、
2022C<~~> は真か偽かのみを返します。
2023
2024=begin original
2025
2026Unlike most operators, the smartmatch operator knows to treat C<undef>
2027specially:
2028
2029=end original
2030
2031ほとんどの演算子と異なり、スマートマッチング演算子は C<undef> を特別に
2032扱う方法を知っています:
2033
2034 use v5.10.1;
2035 @array = (1, 2, 3, undef, 4, 5);
2036 say "some elements undefined" if undef ~~ @array;
2037
2038=begin original
2039
2040Each operand is considered in a modified scalar context, the modification
2041being that array and hash variables are passed by reference to the
2042operator, which implicitly dereferences them. Both elements
2043of each pair are the same:
2044
2045=end original
2046
2047それぞれのオペランドは修正されたスカラコンテキストと考えられます;
2048修正というのは、配列とハッシュの変数は演算子にリファレンスが渡され、
2049暗黙にデリファレンスされます。
2050それぞれの組のどちらの要素も同じです:
2051
2052 use v5.10.1;
2053
2054 my %hash = (red => 1, blue => 2, green => 3,
2055 orange => 4, yellow => 5, purple => 6,
2056 black => 7, grey => 8, white => 9);
2057
2058 my @array = qw(red blue green);
2059
2060 say "some array elements in hash keys" if @array ~~ %hash;
2061 say "some array elements in hash keys" if \@array ~~ \%hash;
2062
2063 say "red in array" if "red" ~~ @array;
2064 say "red in array" if "red" ~~ \@array;
2065
2066 say "some keys end in e" if /e$/ ~~ %hash;
2067 say "some keys end in e" if /e$/ ~~ \%hash;
2068
2069=begin original
2070
2071Two arrays smartmatch if each element in the first array smartmatches
2072(that is, is "in") the corresponding element in the second array,
2073recursively.
2074
2075=end original
2076
2077二つの配列は、最初の配列のそれぞれの要素が、二つ目の配列の対応する要素に
2078(つまり "in") 再帰的にスマートマッチングするときに、スマートマッチングします。
2079
2080 use v5.10.1;
2081 my @little = qw(red blue green);
2082 my @bigger = ("red", "blue", [ "orange", "green" ] );
2083 if (@little ~~ @bigger) { # true!
2084 say "little is contained in bigger";
2085 }
2086
2087=begin original
2088
2089Because the smartmatch operator recurses on nested arrays, this
2090will still report that "red" is in the array.
2091
2092=end original
2093
2094スマートマッチング演算子はネストした配列を再帰するので、これは "red" が
2095配列にいると報告するままです。
2096
2097 use v5.10.1;
2098 my @array = qw(red blue green);
2099 my $nested_array = [[[[[[[ @array ]]]]]]];
2100 say "red in array" if "red" ~~ $nested_array;
2101
2102=begin original
2103
2104If two arrays smartmatch each other, then they are deep
2105copies of each others' values, as this example reports:
2106
2107=end original
2108
2109二つの配列が互いにスマートマッチングすると、次の例が報告しているように、
2110互いの値のディープコピーになります:
2111
2112 use v5.12.0;
2113 my @a = (0, 1, 2, [3, [4, 5], 6], 7);
2114 my @b = (0, 1, 2, [3, [4, 5], 6], 7);
2115
2116 if (@a ~~ @b && @b ~~ @a) {
2117 say "a and b are deep copies of each other";
2118 }
2119 elsif (@a ~~ @b) {
2120 say "a smartmatches in b";
2121 }
2122 elsif (@b ~~ @a) {
2123 say "b smartmatches in a";
2124 }
2125 else {
2126 say "a and b don't smartmatch each other at all";
2127 }
2128
2129=begin original
2130
2131If you were to set S<C<$b[3] = 4>>, then instead of reporting that "a and b
2132are deep copies of each other", it now reports that C<"b smartmatches in a">.
2133That's because the corresponding position in C<@a> contains an array that
2134(eventually) has a 4 in it.
2135
2136=end original
2137
2138S<C<$b[3] = 4>> に設定すると、"a and b are deep copies of each other" と
2139報告されるのではなく、C<"b smartmatches in a"> と報告されます。
2140これは、C<@a> の対応する位置には(最終的に)中に 4 がある配列を
2141含んでいるからです。
2142
2143=begin original
2144
2145Smartmatching one hash against another reports whether both contain the
2146same keys, no more and no less. This could be used to see whether two
2147records have the same field names, without caring what values those fields
2148might have. For example:
2149
2150=end original
2151
2152あるハッシュを他のものとスマートマッチングすると、両方に同じキーが
2153含まれているかどうかを報告し、それ以上でもそれ以下でもありません。
2154これは、値を気にせずに、二つのレコードが同じフィールド名を持っているか
2155どうかを見るのに使えるかもしれません。
2156例えば:
2157
2158 use v5.10.1;
2159 sub make_dogtag {
2160 state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 };
2161
2162 my ($class, $init_fields) = @_;
2163
2164 die "Must supply (only) name, rank, and serial number"
2165 unless $init_fields ~~ $REQUIRED_FIELDS;
2166
2167 ...
2168 }
2169
2170=begin original
2171
2172However, this only does what you mean if C<$init_fields> is indeed a hash
2173reference. The condition C<$init_fields ~~ $REQUIRED_FIELDS> also allows the
2174strings C<"name">, C<"rank">, C<"serial_num"> as well as any array reference
2175that contains C<"name"> or C<"rank"> or C<"serial_num"> anywhere to pass
2176through.
2177
2178=end original
2179
2180しかし、これは C<$init_fields> が確かにハッシュリファレンスである場合にのみ
2181考えている通りに動作します。
2182C<$init_fields ~~ $REQUIRED_FIELDS> という条件は、文字列
2183C<"name">, C<"rank">, C<"serial_num"> および、
2184C<"name">, C<"rank">, C<"serial_num"> のいずれかを含むような
2185配列リファレンスでも成立します。
2186
2187=begin original
2188
2189The smartmatch operator is most often used as the implicit operator of a
2190C<when> clause. See the section on "Switch Statements" in L<perlsyn>.
2191
2192=end original
2193
2194スマートマッチング演算子は C<when> 節の暗黙の演算子としてもっともよく
2195使われます。
2196L<perlsyn> の "Switch Statements" の節を参照してください。
2197
2198=head3 Smartmatching of Objects
2199
2200(オブジェクトのスマートマッチング)
2201
2202=begin original
2203
2204To avoid relying on an object's underlying representation, if the
2205smartmatch's right operand is an object that doesn't overload C<~~>,
2206it raises the exception "C<Smartmatching a non-overloaded object
2207breaks encapsulation>". That's because one has no business digging
2208around to see whether something is "in" an object. These are all
2209illegal on objects without a C<~~> overload:
2210
2211=end original
2212
2213オブジェクトの基となる表現に依存することを避けるために、スマートマッチングの
2214右オペランドが C<~~> をオーバーロードしないオブジェクトなら、例外
2215"C<Smartmatching a non-overloaded object breaks encapsulation>" が発生します。
2216これは、何かがオブジェクトに「含まれている」かどうかを知るために
2217調べる筋合いではないからです。
2218次のものは C<~~> のオーバーロードがなければ全て不正です:
2219
2220 %hash ~~ $object
2221 42 ~~ $object
2222 "fred" ~~ $object
2223
2224=begin original
2225
2226However, you can change the way an object is smartmatched by overloading
2227the C<~~> operator. This is allowed to
2228extend the usual smartmatch semantics.
2229For objects that do have an C<~~> overload, see L<overload>.
2230
2231=end original
2232
2233しかし、C<~~> 演算子をオーバーロードすることにオブジェクトが
2234スマートマッチングする方法を変更できます。
2235これにより通常のスマートマッチングの意味論を拡張できます。
2236C<~~> のオーバーロードを持つオブジェクトについては、L<overload> を
2237参照してください。
2238
2239=begin original
2240
2241Using an object as the left operand is allowed, although not very useful.
2242Smartmatching rules take precedence over overloading, so even if the
2243object in the left operand has smartmatch overloading, this will be
2244ignored. A left operand that is a non-overloaded object falls back on a
2245string or numeric comparison of whatever the C<ref> operator returns. That
2246means that
2247
2248=end original
2249
2250左オペランドにオブジェクトを使うことは許されていますが、あまり
2251有用ではありません。
2252スマートマッチングの規則はオーバーロードより優先順位が高いので、例え
2253左オペランドのオブジェクトがスマートマッチングのオーバーロードを
2254持っていても、無視されます。
2255左オペランドがオーバーロードされていないオブジェクトなら、C<ref> 演算子が
2256返したものに従って、文字または数値比較にフォールバックします。
2257これは、
2258
2259 $object ~~ X
2260
2261=begin original
2262
2263does I<not> invoke the overload method with C<I<X>> as an argument.
2264Instead the above table is consulted as normal, and based on the type of
2265C<I<X>>, overloading may or may not be invoked. For simple strings or
2266numbers, "in" becomes equivalent to this:
2267
2268=end original
2269
2270は C<I<X>> を引数としてオーバーロードメソッドを起動 I<しない> ということです。
2271通常通り前述の表を見て C<I<X>> の型に依存するのではなく、オーバーロードは
2272起動されるかもしれませんし、されないかもしれません。
2273単純な文字列や数値については、"in" は以下と等価になります:
2274
2275 $object ~~ $number ref($object) == $number
2276 $object ~~ $string ref($object) eq $string
2277
2278=begin original
2279
2280For example, this reports that the handle smells IOish
2281(but please don't really do this!):
2282
2283=end original
2284
2285例えば、これは "handle smells IOish" と報告します (しかし実際にこれを
2286しないでください!):
2287
2288 use IO::Handle;
2289 my $fh = IO::Handle->new();
2290 if ($fh ~~ /\bIO\b/) {
2291 say "handle smells IOish";
2292 }
2293
2294=begin original
2295
2296That's because it treats C<$fh> as a string like
2297C<"IO::Handle=GLOB(0x8039e0)">, then pattern matches against that.
2298
2299=end original
2300
2301これは、C<$fh> を C<"IO::Handle=GLOB(0x8039e0)"> のような文字列として扱い、
2302それからパターンはこれに対してマッチングするからです。
2303
23041126=head2 Bitwise And
23051127X<operator, bitwise, and> X<bitwise and> X<&>
23061128
23071129(ビットごとの AND)
23081130
23091131=begin original
23101132
2311Binary C<"&"> returns its operands ANDed together bit by bit. Although no
1133Binary "&" returns its operands ANDed together bit by bit.
2312warning is currently raised, the result is not well defined when this operation
1134(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
2313is performed on operands that aren't either numbers (see
2314L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String Operators>).
23151135
23161136=end original
23171137
2318二項演算子の C<"&"> は、両オペランドのビットごとに論理積をとって、
1138二項演算子の "&" は、両オペランドのビットごとに論理積をとって、
23191139その結果を返します。
2320数値 (L</Integer Arithmetic>) 参照) でもビット文字列
1140(L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。)
2321(L</Bitwise String Operators> 参照) でもないオペランドに対してこの演算を
2322実行した場合、現在のところ警告は出ませんが、結果は未定義です。
23231141
23241142=begin original
23251143
2326Note that C<"&"> has lower priority than relational operators, so for example
1144Note that "&" has lower priority than relational operators, so for example
2327the parentheses are essential in a test like
1145the brackets are essential in a test like
23281146
23291147=end original
23301148
2331C<"&"> は関係演算子より優先順位が低いので、例えば以下のようなテストでは、
1149"&" は関係演算子より優先順位が低いので、例えば以下のようなテストでは、
23321150かっこが重要です:
23331151
2334 print "Even\n" if ($x & 1) == 0;
1152 print "Even\n" if ($x & 1) == 0;
23351153
2336=begin original
2337
2338If the "bitwise" feature is enabled via S<C<use feature 'bitwise'>> or
2339C<use v5.28>, then this operator always treats its operands as numbers.
2340Before Perl 5.28 this feature produced a warning in the
2341C<"experimental::bitwise"> category.
2342
2343=end original
2344
2345S<C<use feature 'bitwise'>> か C<use v5.28> によって "bitwise" 機能が
2346有効になっている場合、この演算子はオペランドを常に数値として扱います。
2347Perl 5.28 より前ではこの機能は C<"experimental::bitwise"> カテゴリの
2348警告が出力されていました。
2349
23501154=head2 Bitwise Or and Exclusive Or
23511155X<operator, bitwise, or> X<bitwise or> X<|> X<operator, bitwise, xor>
23521156X<bitwise xor> X<^>
23531157
23541158(ビットごとの OR と XOR)
23551159
23561160=begin original
23571161
2358Binary C<"|"> returns its operands ORed together bit by bit.
1162Binary "|" returns its operands ORed together bit by bit.
1163(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
23591164
23601165=end original
23611166
2362二項演算子の C<"|"> は、両オペランドのビットごとに論理和をとって、
1167二項演算子の "|" は、両オペランドのビットごとに論理和をとって、
23631168その結果を返します。
1169(L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。)
23641170
23651171=begin original
23661172
2367Binary C<"^"> returns its operands XORed together bit by bit.
1173Binary "^" returns its operands XORed together bit by bit.
1174(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
23681175
23691176=end original
23701177
2371二項演算子の C<"^"> は、両オペランドのビットごとに排他論理和をとって、
1178二項演算子の "^" は、両オペランドのビットごとに排他論理和をとって、
23721179その結果を返します。
1180(L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。)
23731181
23741182=begin original
23751183
2376Although no warning is currently raised, the results are not well
1184Note that "|" and "^" have lower priority than relational operators, so
2377defined when these operations are performed on operands that aren't either
1185for example the brackets are essential in a test like
2378numbers (see L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String
2379Operators>).
23801186
23811187=end original
23821188
2383数値 (L</Integer Arithmetic>) 参照) もビット文字列
1189"|" "^" は関係演算子より優先順位が低いの、例えば以下のような
2384(L</Bitwise String Operators> 参照) でもないオペランドに対してこれらの演算を
2385実行した場合、現在のところ警告は出ませんが、結果は未定義です。
2386
2387=begin original
2388
2389Note that C<"|"> and C<"^"> have lower priority than relational operators, so
2390for example the parentheses are essential in a test like
2391
2392=end original
2393
2394C<"|"> と C<"^"> は関係演算子より優先順位が低いので、例えば以下のような
23951190テストでは、かっこが重要です:
23961191
2397 print "false\n" if (8 | 2) != 10;
1192 print "false\n" if (8 | 2) != 10;
23981193
2399=begin original
2400
2401If the "bitwise" feature is enabled via S<C<use feature 'bitwise'>> or
2402C<use v5.28>, then this operator always treats its operands as numbers.
2403Before Perl 5.28. this feature produced a warning in the
2404C<"experimental::bitwise"> category.
2405
2406=end original
2407
2408S<C<use feature 'bitwise'>> か C<use v5.28> によって "bitwise" 機能が
2409有効になっている場合、この演算子はオペランドを常に数値として扱います。
2410Perl 5.28 より前ではこの機能は C<"experimental::bitwise"> カテゴリの
2411警告が出力されていました。
2412
24131194=head2 C-style Logical And
24141195X<&&> X<logical and> X<operator, logical, and>
24151196
24161197(C スタイルの論理積)
24171198
24181199=begin original
24191200
2420Binary C<"&&"> performs a short-circuit logical AND operation. That is,
1201Binary "&&" performs a short-circuit logical AND operation. That is,
24211202if the left operand is false, the right operand is not even evaluated.
24221203Scalar or list context propagates down to the right operand if it
24231204is evaluated.
24241205
24251206=end original
24261207
2427二項演算子の C<"&&"> は、短絡の論理積演算を行ないます。
1208二項演算子の "&&" は、短絡の論理積演算を行ないます。
24281209つまり、左被演算子が偽であれば、右被演算子は評価さえ
24291210行なわれないということです。
24301211評価される場合には、スカラーかリストかというコンテキストは、
24311212右被演算子にも及びます。
24321213
24331214=head2 C-style Logical Or
24341215X<||> X<operator, logical, or>
24351216
24361217(C スタイルの論理和)
24371218
24381219=begin original
24391220
2440Binary C<"||"> performs a short-circuit logical OR operation. That is,
1221Binary "||" performs a short-circuit logical OR operation. That is,
24411222if the left operand is true, the right operand is not even evaluated.
24421223Scalar or list context propagates down to the right operand if it
24431224is evaluated.
24441225
24451226=end original
24461227
2447二項演算子の C<"||"> は、短絡の論理和演算を行ないます。
1228二項演算子の "||" は、短絡の論理和演算を行ないます。
24481229つまり、左被演算子が真であれば、右被演算子は評価さえ
24491230行なわれないということです。
24501231評価される場合には、スカラーかリストかというコンテキストは、
24511232右被演算子にも及びます。
24521233
2453=head2 Logical Defined-Or
1234=head2 C-style Logical Defined-Or
24541235X<//> X<operator, logical, defined-or>
24551236
2456(論理定義性和)
1237(C スタイルの定義性和)
24571238
24581239=begin original
24591240
24601241Although it has no direct equivalent in C, Perl's C<//> operator is related
2461to its C-style "or". In fact, it's exactly the same as C<||>, except that it
1242to its C-style or. In fact, it's exactly the same as C<||>, except that it
2462tests the left hand side's definedness instead of its truth. Thus,
1243tests the left hand side's definedness instead of its truth. Thus, C<$a // $b>
2463S<C<< EXPR1 // EXPR2 >>> returns the value of C<< EXPR1 >> if it's defined,
1244is similar to C<defined($a) || $b> (except that it returns the value of C<$a>
2464otherwise, the value of C<< EXPR2 >> is returned.
1245rather than the value of C<defined($a)>) and is exactly equivalent to
2465(C<< EXPR1 >> is evaluated in scalar context, C<< EXPR2 >>
1246C<defined($a) ? $a : $b>. This is very useful for providing default values
2466in the context of C<< // >> itself). Usually,
1247for variables. If you actually want to test if at least one of C<$a> and
2467this is the same result as S<C<< defined(EXPR1) ? EXPR1 : EXPR2 >>> (except that
1248C<$b> is defined, use C<defined($a // $b)>.
2468the ternary-operator form can be used as a lvalue, while S<C<< EXPR1 // EXPR2 >>>
2469cannot). This is very useful for
2470providing default values for variables. If you actually want to test if
2471at least one of C<$x> and C<$y> is defined, use S<C<defined($x // $y)>>.
24721249
24731250=end original
24741251
24751252C では直接等価なものはありませんが、Perl の C<//> 演算子は C スタイル
24761253論理和に関連しています。
24771254実際、左辺の真偽ではなく定義されているかを判定することを除けば
24781255C<||> と同じです。
2479って S<C<< EXPR1 // EXPR2 >>>C<< EXPR1 >> が定義されていればその値を
1256したがって C<$a // $b> は C<defined($a) || $b> と似ていて (ただし、
2480返し、さもなければ、C<< EXPR2 >> の値を返します
1257C<defined($a)> ではなく C<$a> の値を返します)、C<defined($a) ? $a : $b> と
2481(C<< EXPR1 >> はスカラコンテキスト、C<< EXPR2 >> は C<< // >> 自身の
1258完全に等価す。
2482コンテキストで評価されます。)
2483普通はこれは S<C<< defined(EXPR1) ? EXPR1 : EXPR2 >>> と同じ結果になり
2484完全に等価です (例外は 3 項演算子形式は左辺値として使えますが、
2485S<C<< EXPR1 // EXPR2 >>> は使えません)。
24861259これは、変数に対するデフォルト値を設定するのにとても有用です。
2487実際に、C<$x> と C<$y> の少なくとも片方が定義されているかを判定したいなら、
1260実際に、C<$a> と C<$b> の少なくとも片方が定義されているかを判定したいなら、
2488S<C<defined($x // $y)>> を使ってください。
1261C<defined($a // $b)> を使ってください。
24891262
24901263=begin original
24911264
24921265The C<||>, C<//> and C<&&> operators return the last value evaluated
2493(unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably
1266(unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably
24941267portable way to find out the home directory might be:
24951268
24961269=end original
24971270
24981271C<||>, C<//>, C<&&> 演算子は、(C のように 単に 0 や 1 を返すのではなく)
24991272最後に評価された値を返します。
25001273これにより、かなり一般的に使えるホームディレクトリを探す方法は:
25011274
2502 $home = $ENV{HOME}
1275 $home = $ENV{'HOME'} // $ENV{'LOGDIR'} //
2503 // $ENV{LOGDIR}
1276 (getpwuid($<))[7] // die "You're homeless!\n";
2504 // (getpwuid($<))[7]
2505 // die "You're homeless!\n";
25061277
25071278=begin original
25081279
25091280In particular, this means that you shouldn't use this
25101281for selecting between two aggregates for assignment:
25111282
25121283=end original
25131284
25141285特に、これは代入のために二つの集合を選択するためには
25151286使うべきではないことを意味します。
25161287
2517 @a = @b || @c; # This doesn't do the right thing
1288 @a = @b || @c; # this is wrong
2518 @a = scalar(@b) || @c; # because it really means this.
1289 @a = scalar(@b) || @c; # really meant this
2519 @a = @b ? @b : @c; # This works fine, though.
1290 @a = @b ? @b : @c; # this works fine, though
25201291
25211292=begin original
25221293
2523As alternatives to C<&&> and C<||> when used for
1294As more readable alternatives to C<&&> and C<||> when used for
25241295control flow, Perl provides the C<and> and C<or> operators (see below).
2525The short-circuit behavior is identical. The precedence of C<"and">
1296The short-circuit behavior is identical. The precedence of "and"
2526and C<"or"> is much lower, however, so that you can safely use them after a
1297and "or" is much lower, however, so that you can safely use them after a
25271298list operator without the need for parentheses:
25281299
25291300=end original
25301301
2531Perl では、フロー制御に使う場合の C<&&> と C<||> の同義語として、
1302Perl では、フロー制御に使う場合の多少読みやすい C<&&> と C<||> の
2532C<and> 演算子と C<or> 演算子が用意されています (下記参照)。
1303同義語として、C<and> 演算子と C<or> 演算子が用意されています (下記参照)。
25331304短絡の動作は全く同じです。
2534しかし、C<"and">C<"or"> の優先順位はかなり低くしてあるので、引数に括弧を
1305しかし、"and" と "or" の優先順位はかなり低くしてあるので、引数に括弧を
25351306使っていないリスト演算子のあとに続けて使う場合にも、
25361307安心して使うことができます:
25371308
25381309 unlink "alpha", "beta", "gamma"
25391310 or gripe(), next LINE;
25401311
25411312=begin original
25421313
25431314With the C-style operators that would have been written like this:
25441315
25451316=end original
25461317
25471318C スタイルの演算子では以下のように書く必要があります。
25481319
25491320 unlink("alpha", "beta", "gamma")
25501321 || (gripe(), next LINE);
25511322
25521323=begin original
25531324
2554It would be even more readable to write that this way:
1325Using "or" for assignment is unlikely to do what you want; see below.
25551326
25561327=end original
25571328
2558次のよて書き込みをより読みやすくすることもできます:
1329代入で "or" を使と、たいことと違うことになります
1330以下を参照して下さい。
25591331
2560 unless(unlink("alpha", "beta", "gamma")) {
2561 gripe();
2562 next LINE;
2563 }
2564
2565=begin original
2566
2567Using C<"or"> for assignment is unlikely to do what you want; see below.
2568
2569=end original
2570
2571代入で C<"or"> を使うと、したいことと違うことになります; 以下を
2572参照して下さい。
2573
25741332=head2 Range Operators
25751333X<operator, range> X<range> X<..> X<...>
25761334
25771335(範囲演算子)
25781336
25791337=begin original
25801338
2581Binary C<".."> is the range operator, which is really two different
1339Binary ".." is the range operator, which is really two different
25821340operators depending on the context. In list context, it returns a
25831341list of values counting (up by ones) from the left value to the right
25841342value. If the left value is greater than the right value then it
25851343returns the empty list. The range operator is useful for writing
2586S<C<foreach (1..10)>> loops and for doing slice operations on arrays. In
1344C<foreach (1..10)> loops and for doing slice operations on arrays. In
25871345the current implementation, no temporary array is created when the
25881346range operator is used as the expression in C<foreach> loops, but older
25891347versions of Perl might burn a lot of memory when you write something
25901348like this:
25911349
25921350=end original
25931351
2594二項演算子の C<".."> は範囲演算子で、使われるコンテキストによって
1352二項演算子の ".." は範囲演算子で、使われるコンテキストによって
25951353異なる動作をする 2 つの演算子を合わせたものです。
25961354リストコンテキストでは、左の値から右の値まで (1 づつ昇順で) 数えあげた値から
25971355なるリストを返します。
25981356左側の値が右側の値より大きい場合は、空リストを返します。
2599範囲演算子は、S<C<foreach (1..10)>> のようなループを書くときや、
1357範囲演算子は、C<foreach (1..10)> のようなループを書くときや、
26001358配列のスライス演算を行なうときに便利です。
26011359現状の実装では、C<foreach> ループの式の中で範囲演算子を使っても
26021360一時配列は作りませんが、古い Perl は以下のようなことを書くと、
26031361大量のメモリを消費することになります:
26041362
26051363 for (1 .. 1_000_000) {
26061364 # code
26071365 }
26081366
26091367=begin original
26101368
2611The range operator also works on strings, using the magical
1369The range operator also works on strings, using the magical auto-increment,
2612auto-increment, see below.
1370see below.
26131371
26141372=end original
26151373
2616範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します;
1374範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します
26171375以下を参照してください。
26181376
26191377=begin original
26201378
2621In scalar context, C<".."> returns a boolean value. The operator is
1379In scalar context, ".." returns a boolean value. The operator is
2622bistable, like a flip-flop, and emulates the line-range (comma)
1380bistable, like a flip-flop, and emulates the line-range (comma) operator
2623operator of B<sed>, B<awk>, and various editors. Each C<".."> operator
1381of B<sed>, B<awk>, and various editors. Each ".." operator maintains its
2624maintains its own boolean state, even across calls to a subroutine
1382own boolean state. It is false as long as its left operand is false.
2625that contains it. It is false as long as its left operand is false.
26261383Once the left operand is true, the range operator stays true until the
26271384right operand is true, I<AFTER> which the range operator becomes false
2628again. It doesn't become false till the next time the range operator
1385again. It doesn't become false till the next time the range operator is
2629is evaluated. It can test the right operand and become false on the
1386evaluated. It can test the right operand and become false on the same
2630same evaluation it became true (as in B<awk>), but it still returns
1387evaluation it became true (as in B<awk>), but it still returns true once.
2631true once. If you don't want it to test the right operand until the
1388If you don't want it to test the right operand till the next
2632next evaluation, as in B<sed>, just use three dots (C<"...">) instead of
1389evaluation, as in B<sed>, just use three dots ("...") instead of
2633two. In all other regards, C<"..."> behaves just like C<".."> does.
1390two. In all other regards, "..." behaves just like ".." does.
26341391
26351392=end original
26361393
2637スカラコンテキストで使われたときには、C<".."> は真偽値を返します。
1394スカラコンテキストで使われたときには、".." は真偽値を返します。
26381395この演算子は、フリップフロップのように 2 値安定で、
26391396B<sed> や B<awk> や多くのエディタでの行範囲 (コンマ) 演算子を
26401397エミュレートするものとなります。
2641各々の C<".."> 演算子は、例えそれを含むサブルーチンの呼び出しを
1398各々の ".." 演算子それぞれに独立て自分の真偽状態管理します。
2642またいでも、それぞれに独立して自分の真偽状態を管理します。
26431399はじめは、左被演算子が偽である間、演算全体も偽となっています。
2644いったん左被演算子が真になると、範囲演算子は、右被演算子が真である間、
1400範囲演算子は、いったん左被演算子が真になると、右被演算子が真である間、
2645真を返すようになります; 範囲演算子が再び偽になった I<後> です
1401真を返すようになります。
2646次に範囲演算子が評価されまでは、偽とはなりません
1402右被演算子が偽にな演算子もを返すようになりま
1403(次に範囲演算子が評価されるまでは、偽とはなりません。
26471404(B<awk> でのように) 真となった、その評価の中で右被演算子をテストし、
26481405偽とすることができますが、1 度は真を返すことになります。
26491406B<sed> でのように、次に評価されるまで右被演算子をテストしたくなければ、
26502 個のドットの代わりに 3 つのドット (C<"...">) を使ってください。
14072 個のドットの代わりに 3 つのドット ("...") を使ってください。
2651その他の点では、C<"...">C<".."> と同様に振舞います.
1408その他の点では、"..." は ".." と同様に振舞います.
26521409
26531410=begin original
26541411
26551412The right operand is not evaluated while the operator is in the
26561413"false" state, and the left operand is not evaluated while the
26571414operator is in the "true" state. The precedence is a little lower
26581415than || and &&. The value returned is either the empty string for
2659false, or a sequence number (beginning with 1) for true. The sequence
1416false, or a sequence number (beginning with 1) for true. The
2660number is reset for each range encountered. The final sequence number
1417sequence number is reset for each range encountered. The final
2661in a range has the string C<"E0"> appended to it, which doesn't affect
1418sequence number in a range has the string "E0" appended to it, which
2662its numeric value, but gives you something to search for if you want
1419doesn't affect its numeric value, but gives you something to search
2663to exclude the endpoint. You can exclude the beginning point by
1420for if you want to exclude the endpoint. You can exclude the
2664waiting for the sequence number to be greater than 1.
1421beginning point by waiting for the sequence number to be greater
1422than 1.
26651423
26661424=end original
26671425
26681426右被演算子は、演算子の状態が「偽」である間は評価されることがなく、
26691427左被演算子は、演算子の状態が「真」である間は評価されることがありません。
26701428優先順位は、|| と && の少し下です。
26711429偽としては空文字列が返され、
2672真としては (1 から始まる) 通し番号が返されます。
1430真としては (1 から始まる) 順に並んだ数値が返されます。
26731431この通し番号は、新たに範囲が始まるごとにリセットされます。
2674範囲の最後の通し番号には、文字列 C<"E0"> が末尾につけられます; これは、
1432範囲の最後の数字には、文字列 "E0" が末尾につけられます
2675数値としては何の影響もありませんが、範囲の終わりで何か特別なことをしたい
1433これは、数値としては何の影響もありませんが、範囲の終わりで何か特別なことを
2676場合に、目印として使うことができます。
1434したい場合に、目印として使うことができます。
2677範囲の始まりを除きたい場合には、通し番号が 1 よりも大きくなるのを
1435範囲の始まりで何かしたい場合には、通し番号が 1 よりも大きくなるのを
2678待てばよいでしょう。
1436いればよいでしょう。
26791437
26801438=begin original
26811439
2682If either operand of scalar C<".."> is a constant expression,
1440If either operand of scalar ".." is a constant expression,
26831441that operand is considered true if it is equal (C<==>) to the current
26841442input line number (the C<$.> variable).
26851443
26861444=end original
26871445
2688スカラの C<".."> の被演算子が定数表現であるときは、その被演算子は暗黙に、
1446スカラの ".." の被演算子が定数表現であるときは、その被演算子は暗黙に、
26891447現在の入力行番号(変数 C<$.>) と等しい(C<==>) 場合に真となります。
26901448
26911449=begin original
26921450
2693To be pedantic, the comparison is actually S<C<int(EXPR) == int(EXPR)>>,
1451To be pedantic, the comparison is actually C<int(EXPR) == int(EXPR)>,
26941452but that is only an issue if you use a floating point expression; when
26951453implicitly using C<$.> as described in the previous paragraph, the
2696comparison is S<C<int(EXPR) == int($.)>> which is only an issue when C<$.>
1454comparison is C<int(EXPR) == int($.)> which is only an issue when C<$.>
26971455is set to a floating point value and you are not reading from a file.
2698Furthermore, S<C<"span" .. "spat">> or S<C<2.18 .. 3.14>> will not do what
1456Furthermore, C<"span" .. "spat"> or C<2.18 .. 3.14> will not do what
26991457you want in scalar context because each of the operands are evaluated
27001458using their integer representation.
27011459
27021460=end original
27031461
2704とても細かい話をすると、比較は実際には S<C<int(EXPR) == int(EXPR)>> ですが、
1462とても細かい話をすると、比較は実際には C<int(EXPR) == int(EXPR)> ですが、
27051463これは浮動小数点数を使うときにだけ問題になります; 前の段落で記述したように
2706明示的に C<$.> を使ったとき、比較は S<C<int(EXPR) == int($.)>> となり、
1464明示的に C<$.> を使ったとき、比較は C<int(EXPR) == int($.)> となり、
27071465C<$.> に浮動小数点数がセットされ、ファイルから読み込みを行わない場合にのみ
27081466問題になります。
2709さらに、S<C<"span" .. "spat">>S<C<2.18 .. 3.14>> は、それぞれの
1467さらに、C<"span" .. "spat"> や C<2.18 .. 3.14> は、それぞれのオペランドが
2710オペランドが整数表現を使って評価されるため、スカラコンテキストでは
1468整数表現を使って評価されるため、スカラコンテキストでは望みどおりの結果に
2711望みどおりの結果になりません。
1469なりません。
27121470
27131471=begin original
27141472
27151473Examples:
27161474
27171475=end original
27181476
27191477例:
27201478
27211479=begin original
27221480
27231481As a scalar operator:
27241482
27251483=end original
27261484
27271485スカラ演算子として:
27281486
27291487 if (101 .. 200) { print; } # print 2nd hundred lines, short for
2730 # if ($. == 101 .. $. == 200) { print; }
1488 # if ($. == 101 .. $. == 200) { print; }
27311489
27321490 next LINE if (1 .. /^$/); # skip header lines, short for
27331491 # next LINE if ($. == 1 .. /^$/);
27341492 # (typically in a loop labeled LINE)
27351493
27361494 s/^/> / if (/^$/ .. eof()); # quote body
27371495
27381496 # parse mail messages
27391497 while (<>) {
27401498 $in_header = 1 .. /^$/;
27411499 $in_body = /^$/ .. eof;
27421500 if ($in_header) {
27431501 # do something
27441502 } else { # in body
27451503 # do something else
27461504 }
27471505 } continue {
27481506 close ARGV if eof; # reset $. each file
27491507 }
27501508
27511509=begin original
27521510
27531511Here's a simple example to illustrate the difference between
27541512the two range operators:
27551513
27561514=end original
27571515
27581516以下は二つの範囲演算子の違いを示す単純な例です:
27591517
27601518 @lines = (" - Foo",
27611519 "01 - Bar",
27621520 "1 - Baz",
27631521 " - Quux");
27641522
27651523 foreach (@lines) {
27661524 if (/0/ .. /1/) {
27671525 print "$_\n";
27681526 }
27691527 }
27701528
27711529=begin original
27721530
2773This program will print only the line containing "Bar". If
1531This program will print only the line containing "Bar". If
27741532the range operator is changed to C<...>, it will also print the
27751533"Baz" line.
27761534
27771535=end original
27781536
27791537このプログラムは "Bar" を含む行だけを表示します。
27801538範囲演算子を C<...> に変更すると、"Baz" の行も表示します。
27811539
27821540=begin original
27831541
27841542And now some examples as a list operator:
27851543
27861544=end original
27871545
27881546これはリスト演算子の例です:
27891547
2790=begin original
1548 for (101 .. 200) { print; } # print $_ 100 times
1549 @foo = @foo[0 .. $#foo]; # an expensive no-op
1550 @foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
27911551
2792 for (101 .. 200) { print } # print $_ 100 times
2793 @foo = @foo[0 .. $#foo]; # an expensive no-op
2794 @foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
2795
2796=end original
2797
2798 for (101 .. 200) { print } # $_ を 100 回出力
2799 @foo = @foo[0 .. $#foo]; # 高価な no-op
2800 @foo = @foo[$#foo-4 .. $#foo]; # 末尾の 5 アイテムをスライス
2801
28021552=begin original
28031553
2804Because each operand is evaluated in integer form, S<C<2.18 .. 3.14>> will
1554The range operator (in list context) makes use of the magical
2805return two elements in list context.
1555auto-increment algorithm if the operands are strings. You
1556can say
28061557
28071558=end original
28081559
2809それぞれのオペラドは整数の形評価されるで、S<C<2.18 .. 3.14>>
1560(リストコテキストでの) 範囲演算子、被演算子が文字列であるときには、
2810ストコテキスでは二つ要素返します。
1561マジカルインクントの機能使います。
1562以下のように書くと:
28111563
2812=begin original
1564 @alphabet = ('A' .. 'Z');
28131565
2814 @list = (2.18 .. 3.14); # same as @list = (2 .. 3);
2815
2816=end original
2817
2818 @list = (2.18 .. 3.14); # @list = (2 .. 3); と同じ
2819
28201566=begin original
28211567
2822The range operator in list context can make use of the magical
1568to get all normal letters of the English alphabet, or
2823auto-increment algorithm if both operands are strings, subject to the
2824following rules:
28251569
28261570=end original
28271571
2828リストコンテキストで範囲演算子は、両方のオペランドが
1572英語大文字すべてを得られますし:
2829文字列であるときには、次の規則に従って、マジカルインクリメントの
2830機能が使えます。
28311573
2832=over
1574 $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];
28331575
2834=item *
2835
28361576=begin original
28371577
2838With one exception (below), if both strings look like numbers to Perl,
1578to get a hexadecimal digit, or
2839the magic increment will not be applied, and the strings will be treated
2840as numbers (more specifically, integers) instead.
28411579
28421580=end original
28431581
2844一つ例外(後述)を除いて
1582と書けば、16 進数字が得られますし
2845両側の文字列が Perl にとって数値に見える場合、
2846マジカルインクリメントは適用されず、代わりにその文字列は数値として
2847(より正確には、整数として)扱われます。
28481583
2849=begin original
1584 @z2 = ('01' .. '31'); print $z2[$mday];
28501585
2851For example, C<"-2".."2"> is the same as C<-2..2>, and
2852C<"2.18".."3.14"> produces C<2, 3>.
2853
2854=end original
2855
2856例えば、C<"-2".."2"> は C<-2..2> と同じで、
2857C<"2.18".."3.14"> は C<2, 3> を生成します。
2858
2859=item *
2860
28611586=begin original
28621587
2863The exception to the above rule is when the left-hand string begins with
1588to get dates with leading zeros.
2864C<0> and is longer than one character, in this case the magic increment
2865I<will> be applied, even though strings like C<"01"> would normally look
2866like a number to Perl.
28671589
28681590=end original
28691591
2870前述の規則の例外して
1592すれば0 付きの日付が得られます。
2871左側の文字列が C<0> で始まっていて 2 文字以上の場合、
2872C<"01"> のような文字列は通常 Perl にとって数値に見えるにも関わらず、
2873この場合はマジカルインクリメントは I<適用されます> 。
28741593
28751594=begin original
28761595
2877For example, C<"01".."04"> produces C<"01", "02", "03", "04">, and
2878C<"00".."-1"> produces C<"00"> through C<"99"> - this may seem
2879surprising, but see the following rules for why it works this way.
2880To get dates with leading zeros, you can say:
2881
2882=end original
2883
2884例えば、C<"01".."04"> は C<"01", "02", "03", "04"> を生成し、
2885C<"00".."-1"> は C<"00"> から C<"99"> を生成します - これは驚きがあるかも
2886しれませんが、なぜこれがこのように動作するのかについては
2887次の規則を見てください。
2888先頭に 0 があるものから日付を取り出すために、次のようにできます:
2889
2890 @z2 = ("01" .. "31");
2891 print $z2[$mday];
2892
2893=begin original
2894
2895If you want to force strings to be interpreted as numbers, you could say
2896
2897=end original
2898
2899文字列が数値として解釈することを強制したいなら、次のようにできます:
2900
2901 @numbers = ( 0+$first .. 0+$last );
2902
2903=begin original
2904
2905B<Note:> In Perl versions 5.30 and below, I<any> string on the left-hand
2906side beginning with C<"0">, including the string C<"0"> itself, would
2907cause the magic string increment behavior. This means that on these Perl
2908versions, C<"0".."-1"> would produce C<"0"> through C<"99">, which was
2909inconsistent with C<0..-1>, which produces the empty list. This also means
2910that C<"0".."9"> now produces a list of integers instead of a list of
2911strings.
2912
2913=end original
2914
2915B<注意:> Perl バージョン 5.30 以下では、左側が C<"0"> で始まる
2916I<任意の> 文字列 (文字列 C<"0"> 自体を含む)は、マジカル文字列
2917インクリメントを引き起こします。
2918つまり、これらの Perl バージョンでは、C<"0".."-1"> は C<"0"> から
2919C<"99"> までを生成しますが、これは空のリストを生成する C<0..-1> と
2920矛盾していました。
2921また、C<"0".."9"> では、文字列のリストではなく整数のリストが
2922生成されるということです。
2923
2924=item *
2925
2926=begin original
2927
2928If the initial value specified isn't part of a magical increment
2929sequence (that is, a non-empty string matching C</^[a-zA-Z]*[0-9]*\z/>),
2930only the initial value will be returned.
2931
2932=end original
2933
2934指定された初期値がマジカルインクリメント処理の一部でない場合
2935(つまり、C</^[a-zA-Z]*[0-9]*\z/> にマッチングする、空でない文字列の場合)、
2936初期値のみが返されます。
2937
2938=begin original
2939
2940For example, C<"ax".."az"> produces C<"ax", "ay", "az">, but
2941C<"*x".."az"> produces only C<"*x">.
2942
2943=end original
2944
2945例えば、C<"ax".."az"> は C<"ax", "ay", "az"> を生成しますが、
2946C<"*x".."az"> は C<"*x"> だけを生成します。
2947
2948=item *
2949
2950=begin original
2951
2952For other initial values that are strings that do follow the rules of the
2953magical increment, the corresponding sequence will be returned.
2954
2955=end original
2956
2957マジカルインクリメントの規則に従う文字列がもう片方の初期値の場合、
2958対応する並びが返されます。
2959
2960=begin original
2961
2962For example, you can say
2963
2964=end original
2965
2966例えば以下のように書くと:
2967
2968 @alphabet = ("A" .. "Z");
2969
2970=begin original
2971
2972to get all normal letters of the English alphabet, or
2973
2974=end original
2975
2976英語の大文字すべてを得られますし:
2977
2978 $hexdigit = (0 .. 9, "a" .. "f")[$num & 15];
2979
2980=begin original
2981
2982to get a hexadecimal digit.
2983
2984=end original
2985
2986と書けば、16 進の数字が得られます。
2987
2988=item *
2989
2990=begin original
2991
29921596If the final value specified is not in the sequence that the magical
29931597increment would produce, the sequence goes until the next value would
2994be longer than the final value specified. If the length of the final
1598be longer than the final value specified.
2995string is shorter than the first, the empty list is returned.
29961599
2997
29981600=end original
29991601
30001602マジカルインクリメントによって得られる値の中に指定した最終値に
30011603ちょうど一致するものが見つからないような場合には、
30021604マジカルインクリメントによって得られる次の値の文字列長が、
30031605最終値として指定した値のものより長くなるまでインクリメントが続けられます。
3004最終値の文字列の長さが最初のものより短い場合、
3005空リストが返されます。
30061606
30071607=begin original
30081608
3009For example, C<"a".."--"> is the same as C<"a".."zz">, C<"0".."xx">
1609If the initial value specified isn't part of a magical increment
3010produces C<"0"> through C<"99">, and C<"aaa".."--"> returns the empty
1610sequence (that is, a non-empty string matching "/^[a-zA-Z]*[0-9]*\z/"),
3011list.
1611only the initial value will be returned. So the following will only
1612return an alpha:
30121613
30131614=end original
30141615
3015例えば、C<"a".."--"> は C<"a".."zz"> と同じ
1616指定された初期値がマジカルインクリメント処理の一部ない場合
3016C<"0".."xx"> は C<"0"> から C<"99"> を出力し
1617(つまり、"/^[a-zA-Z]*[0-9]*\z/" にマッチングする、空でない文字列の場合)
3017C<"aaa".."--"> は空リストをます。
1618初期値のみがされます。
1619従って、以下はαのみを返します:
30181620
3019=back
1621 use charnames 'greek';
3020
3021=begin original
3022
3023As of Perl 5.26, the list-context range operator on strings works as expected
3024in the scope of L<< S<C<"use feature 'unicode_strings">>|feature/The
3025'unicode_strings' feature >>. In previous versions, and outside the scope of
3026that feature, it exhibits L<perlunicode/The "Unicode Bug">: its behavior
3027depends on the internal encoding of the range endpoint.
3028
3029=end original
3030
3031Perl 5.26 から、文字列に対するリストコンテキスト範囲演算子は、
3032L<< S<C<"use feature 'unicode_strings">>|feature/The
3033'unicode_strings' feature >> のスコープの中で想定通りに
3034動作するようになりました。
3035以前のバージョン、およびこの機能のスコープの外側では、
3036これは L<perlunicode/The "Unicode Bug"> を起こしていました:
3037その振る舞いは範囲の両端の内部エンコーディングに依存していました。
3038
3039=begin original
3040
3041Because the magical increment only works on non-empty strings matching
3042C</^[a-zA-Z]*[0-9]*\z/>, the following will only return an alpha:
3043
3044=end original
3045
3046マジカルインクリメントは C</^[a-zA-Z]*[0-9]*\z/> にマッチングする
3047空でない文字列でのみ動作するので、
3048以下はαのみを返します:
3049
3050 use charnames "greek";
30511622 my @greek_small = ("\N{alpha}" .. "\N{omega}");
30521623
30531624=begin original
30541625
3055To get the 25 traditional lowercase Greek letters, including both sigmas,
1626To get lower-case greek letters, use this instead:
3056you could use this instead:
30571627
30581628=end original
30591629
3060両方のシグマを含む、25 文字の伝統的な小文字のギリシャ文字を得るためには、
1630小文字のギリシャ文字を得るためには、代わりに以下のようにしてください:
3061代わりに以下のようにしてください:
30621631
3063 use charnames "greek";
1632 my @greek_small = map { chr } ( ord("\N{alpha}") .. ord("\N{omega}") );
3064 my @greek_small = map { chr } ( ord("\N{alpha}")
3065 ..
3066 ord("\N{omega}")
3067 );
30681633
30691634=begin original
30701635
3071However, because there are I<many> other lowercase Greek characters than
1636Because each operand is evaluated in integer form, C<2.18 .. 3.14> will
3072just those, to match lowercase Greek characters in a regular expression,
1637return two elements in list context.
3073you could use the pattern C</(?:(?=\p{Greek})\p{Lower})+/> (or the
3074L<experimental feature|perlrecharclass/Extended Bracketed Character
3075Classes> C<S</(?[ \p{Greek} & \p{Lower} ])+/>>).
30761638
30771639=end original
30781640
3079しかし、小文字ギリシャ文字ここに書いたもよりも I<たくん> あるので、
1641それぞれオペランド整数形で評価るので、C<2.18 .. 3.14> は
3080正規表現で小文字のギシャ文字にマッチグさせるために
1642ストコテキストで二つの要素を返します。
3081C</(?:(?=\p{Greek})\p{Lower})+/> というパターン (または
3082L<実験的機能|perlrecharclass/Extended Bracketed Character Classes> である
3083C<S</(?[ \p{Greek} & \p{Lower} ])+/>>) を使います。
30841643
1644 @list = (2.18 .. 3.14); # same as @list = (2 .. 3);
1645
30851646=head2 Conditional Operator
30861647X<operator, conditional> X<operator, ternary> X<ternary> X<?:>
30871648
30881649(条件演算子)
30891650
30901651=begin original
30911652
3092Ternary C<"?:"> is the conditional operator, just as in C. It works much
1653Ternary "?:" is the conditional operator, just as in C. It works much
3093like an if-then-else. If the argument before the C<?> is true, the
1654like an if-then-else. If the argument before the ? is true, the
3094argument before the C<:> is returned, otherwise the argument after the
1655argument before the : is returned, otherwise the argument after the :
3095C<:> is returned. For example:
1656is returned. For example:
30961657
30971658=end original
30981659
3099三項演算子の C<"?:"> は、C の場合と同じ条件演算子です。
1660三項演算子の "?:" は、C の場合と同じ条件演算子です。
31001661これは、if-then-else のように働きます。
3101C<"?"> の前の引数が真であれば C<":"> の前の引数が返されますが、
1662"?" の前の引数が真であれば ":" の前の引数が返されますが、
3102真でなければ、C<":"> の後の引数が返されます。
1663真でなければ、":" の後の引数が返されます。
31031664例えば:
31041665
31051666 printf "I have %d dog%s.\n", $n,
3106 ($n == 1) ? "" : "s";
1667 ($n == 1) ? '' : "s";
31071668
31081669=begin original
31091670
31101671Scalar or list context propagates downward into the 2nd
31111672or 3rd argument, whichever is selected.
31121673
31131674=end original
31141675
31151676スカラコンテキストかリストコンテキストかという状況は、
31161677選択された 2 番目もしくは 3 番目の引数にまで伝わります。
31171678
3118=begin original
1679 $a = $ok ? $b : $c; # get a scalar
1680 @a = $ok ? @b : @c; # get an array
1681 $a = $ok ? @b : @c; # oops, that's just a count!
31191682
3120 $x = $ok ? $y : $z; # get a scalar
3121 @x = $ok ? @y : @z; # get an array
3122 $x = $ok ? @y : @z; # oops, that's just a count!
3123
3124=end original
3125
3126 $x = $ok ? $y : $z; # スカラを取る
3127 @x = $ok ? @y : @z; # 配列を取る
3128 $x = $ok ? @y : @z; # うわ、これは単なる数です!
3129
31301683=begin original
31311684
31321685The operator may be assigned to if both the 2nd and 3rd arguments are
31331686legal lvalues (meaning that you can assign to them):
31341687
31351688=end original
31361689
313716902 番目と 3 番目の引数双方が左辺値 (代入可能ということ)であれば、
31381691この演算子に代入を行なうこともできます:
31391692
3140 ($x_or_y ? $x : $y) = $z;
1693 ($a_or_b ? $a : $b) = $c;
31411694
31421695=begin original
31431696
31441697Because this operator produces an assignable result, using assignments
31451698without parentheses will get you in trouble. For example, this:
31461699
31471700=end original
31481701
31491702この演算子は代入可能な結果を生み出すので、
31501703括弧なしで代入を行うとおかしくなるかもしれません。
31511704例えばこれは:
31521705
3153 $x % 2 ? $x += 10 : $x += 2
1706 $a % 2 ? $a += 10 : $a += 2
31541707
31551708=begin original
31561709
31571710Really means this:
31581711
31591712=end original
31601713
31611714以下を意味し:
31621715
3163 (($x % 2) ? ($x += 10) : $x) += 2
1716 (($a % 2) ? ($a += 10) : $a) += 2
31641717
31651718=begin original
31661719
31671720Rather than this:
31681721
31691722=end original
31701723
31711724以下のようにはなりません:
31721725
3173 ($x % 2) ? ($x += 10) : ($x += 2)
1726 ($a % 2) ? ($a += 10) : ($a += 2)
31741727
31751728=begin original
31761729
31771730That should probably be written more simply as:
31781731
31791732=end original
31801733
31811734恐らく以下のようにもっと単純に書くべきでしょう:
31821735
3183 $x += ($x % 2) ? 10 : 2;
1736 $a += ($a % 2) ? 10 : 2;
31841737
31851738=head2 Assignment Operators
31861739X<assignment> X<operator, assignment> X<=> X<**=> X<+=> X<*=> X<&=>
31871740X<<< <<= >>> X<&&=> X<-=> X</=> X<|=> X<<< >>= >>> X<||=> X<//=> X<.=>
3188X<%=> X<^=> X<x=> X<&.=> X<|.=> X<^.=>
1741X<%=> X<^=> X<x=>
31891742
31901743(代入演算子)
31911744
31921745=begin original
31931746
3194C<"="> is the ordinary assignment operator.
1747"=" is the ordinary assignment operator.
31951748
31961749=end original
31971750
3198C<"="> は通常の代入演算子です。
1751"=" は通常の代入演算子です。
31991752
32001753=begin original
32011754
32021755Assignment operators work as in C. That is,
32031756
32041757=end original
32051758
32061759代入演算子は C の場合と同様の働きをします。
32071760つまり、
32081761
3209 $x += 2;
1762 $a += 2;
32101763
32111764=begin original
32121765
32131766is equivalent to
32141767
32151768=end original
32161769
32171770は以下と等価です:
32181771
3219 $x = $x + 2;
1772 $a = $a + 2;
32201773
32211774=begin original
32221775
32231776although without duplicating any side effects that dereferencing the lvalue
3224might trigger, such as from C<tie()>. Other assignment operators work similarly.
1777might trigger, such as from tie(). Other assignment operators work similarly.
32251778The following are recognized:
32261779
32271780=end original
32281781
3229しかし、C<tie()> のようなもので起こる左辺値の被参照による
1782しかし、tie() のようなもので起こる左辺値の被参照による
32301783副作用が 2 回起こることはありません。
32311784他の代入演算も同様に働きます。
32321785以下のものが認識されます:
32331786
3234 **= += *= &= &.= <<= &&=
1787 **= += *= &= <<= &&=
3235 -= /= |= |.= >>= ||=
1788 -= /= |= >>= ||=
3236 .= %= ^= ^.= //=
1789 .= %= ^= //=
32371790 x=
32381791
32391792=begin original
32401793
32411794Although these are grouped by family, they all have the precedence
3242of assignment. These combined assignment operators can only operate on
1795of assignment.
3243scalars, whereas the ordinary assignment operator can assign to arrays,
3244hashes, lists and even references. (See L<"Context"|perldata/Context>
3245and L<perldata/List value constructors>, and L<perlref/Assigning to
3246References>.)
32471796
32481797=end original
32491798
32501799グループ分けしてありますが、これらはいずれも代入演算子として
32511800同じ優先順位となっています。
3252これらの複合代入演算子はスカラとしてのみ動作しますが、一方
3253通常の代入演算子は配列、スカラ、リスト、リファレンスに代入できます。
3254(L<"Context"|perldata/Context>, L<perldata/List value constructors>,
3255L<perlref/Assigning to References> を参照してください。)
32561801
32571802=begin original
32581803
32591804Unlike in C, the scalar assignment operator produces a valid lvalue.
32601805Modifying an assignment is equivalent to doing the assignment and
32611806then modifying the variable that was assigned to. This is useful
32621807for modifying a copy of something, like this:
32631808
32641809=end original
32651810
32661811C と違って、スカラ代入演算子は有効な左辺値を作り出します。
32671812代入を修正することは、代入を行なってから、その代入された変数を修正するのと
32681813同じことになります。
32691814これは、以下のように何かのコピーを変更したいときに便利です:
32701815
3271 ($tmp = $global) =~ tr/13579/24680/;
1816 ($tmp = $global) =~ tr [A-Z] [a-z];
32721817
32731818=begin original
32741819
3275Although as of 5.14, that can be also be accomplished this way:
3276
3277=end original
3278
3279しかし 5.14 現在、これは次のようにしてもできるようになりました:
3280
3281 use v5.14;
3282 $tmp = ($global =~ tr/13579/24680/r);
3283
3284=begin original
3285
32861820Likewise,
32871821
32881822=end original
32891823
32901824同様に、
32911825
3292 ($x += 2) *= 3;
1826 ($a += 2) *= 3;
32931827
32941828=begin original
32951829
32961830is equivalent to
32971831
32981832=end original
32991833
33001834は以下と等価です:
33011835
3302 $x += 2;
1836 $a += 2;
3303 $x *= 3;
1837 $a *= 3;
33041838
33051839=begin original
33061840
33071841Similarly, a list assignment in list context produces the list of
33081842lvalues assigned to, and a list assignment in scalar context returns
33091843the number of elements produced by the expression on the right hand
33101844side of the assignment.
33111845
33121846=end original
33131847
33141848同様に、リストコンテキストでのリストへの代入は代入可能な左辺値のリストとなり、
33151849スカラコンテキストでのリストへの代入は代入の右側の式で作成された
33161850要素の数を返します。
33171851
3318=begin original
3319
3320The three dotted bitwise assignment operators (C<&.=> C<|.=> C<^.=>) are new in
3321Perl 5.22. See L</Bitwise String Operators>.
3322
3323=end original
3324
3325三つのドット付きビット単位代入演算子 (C<&.=> C<|.=> C<^.=>) は
3326Perl 5.22 からの新しいものです。
3327L</Bitwise String Operators> を参照してください。
3328
33291852=head2 Comma Operator
33301853X<comma> X<operator, comma> X<,>
33311854
33321855(コンマ演算子)
33331856
33341857=begin original
33351858
3336Binary C<","> is the comma operator. In scalar context it evaluates
1859Binary "," is the comma operator. In scalar context it evaluates
33371860its left argument, throws that value away, then evaluates its right
33381861argument and returns that value. This is just like C's comma operator.
33391862
33401863=end original
33411864
3342二項演算子の C<","> はコンマ演算子です。
1865二項演算子の "," はコンマ演算子です。
33431866スカラコンテキストではその左引数を評価し、その値を捨てて、
33441867それから右引数を評価し、その値を返します。
33451868これはちょうど、C のコンマ演算子と同じです。
33461869
33471870=begin original
33481871
33491872In list context, it's just the list argument separator, and inserts
33501873both its arguments into the list. These arguments are also evaluated
33511874from left to right.
33521875
33531876=end original
33541877
33551878リストコンテキストでは、これは単にリスト引数の区切り文字で、
33561879双方の引数をそのリストに挿入する働きがあります。
33571880これらの引数も左から右に評価されます。
33581881
33591882=begin original
33601883
3361The C<< => >> operator (sometimes pronounced "fat comma") is a synonym
1884The C<< => >> operator is a synonym for the comma except that it causes
3362for the comma except that it causes a
1885its left operand to be interpreted as a string if it begins with a letter
3363word on its left to be interpreted as a string if it begins with a letter
33641886or underscore and is composed only of letters, digits and underscores.
33651887This includes operands that might otherwise be interpreted as operators,
3366constants, single number v-strings or function calls. If in doubt about
1888constants, single number v-strings or function calls. If in doubt about
3367this behavior, the left operand can be quoted explicitly.
1889this behaviour, the left operand can be quoted explicitly.
33681890
33691891=end original
33701892
3371C<< => >> 演算子(時々「ファットコンマ」と発音されます)はコンマ演算子の
1893C<< => >> 演算子はコンマ演算子の同義語ですが、
3372同義語ですが、もし左側の単語が文字か下線で始まっていて、かつ文字、数字、
1894もし左オペランドが文字か下線で始まっていて、かつ文字、数字、下線でのみ
3373下線でのみ構成されている場合、これを文字列として扱うという効果もあります。
1895構成されている場合、これを文字列として扱うという効果もあります。
33741896これには他の場所では演算子、定数、v-文字列、関数呼び出しとして扱われる
33751897オペランドを含みます。
33761898この振る舞いについて迷うことがあるなら、左オペランドを明示的に
33771899クォートすることも出来ます。
33781900
33791901=begin original
33801902
33811903Otherwise, the C<< => >> operator behaves exactly as the comma operator
33821904or list argument separator, according to context.
33831905
33841906=end original
33851907
33861908さもなければ、C<< => >> 演算子はコンテキストによって、
33871909カンマ演算子かリスト引数の区切り文字と全く同様に振る舞います。
33881910
33891911=begin original
33901912
33911913For example:
33921914
33931915=end original
33941916
33951917例えば:
33961918
33971919 use constant FOO => "something";
33981920
33991921 my %h = ( FOO => 23 );
34001922
34011923=begin original
34021924
34031925is equivalent to:
34041926
34051927=end original
34061928
34071929は、以下と等価です:
34081930
34091931 my %h = ("FOO", 23);
34101932
34111933=begin original
34121934
34131935It is I<NOT>:
34141936
34151937=end original
34161938
34171939これは I<違います>:
34181940
34191941 my %h = ("something", 23);
34201942
34211943=begin original
34221944
34231945The C<< => >> operator is helpful in documenting the correspondence
34241946between keys and values in hashes, and other paired elements in lists.
34251947
34261948=end original
34271949
34281950C<< => >> 演算子は、ハッシュのキーと値や、その他のリスト中の組となる
34291951要素の関係を表現するのに便利です。
34301952
3431 %hash = ( $key => $value );
1953 %hash = ( $key => $value );
3432 login( $username => $password );
1954 login( $username => $password );
34331955
3434=begin original
3435
3436The special quoting behavior ignores precedence, and hence may apply to
3437I<part> of the left operand:
3438
3439=end original
3440
3441特殊なクォートの振る舞いは優先順位を無視し、従って左オペランドの I<一部> に
3442適用されることがあります:
3443
3444 print time.shift => "bbb";
3445
3446=begin original
3447
3448That example prints something like C<"1314363215shiftbbb">, because the
3449C<< => >> implicitly quotes the C<shift> immediately on its left, ignoring
3450the fact that C<time.shift> is the entire left operand.
3451
3452=end original
3453
3454この例は C<"1314363215shiftbbb"> のようなものを表示します; なぜなら
3455C<< => >> は暗黙にすぐ左にある C<shift> をクォートし、 C<time.shift> 全体が
3456左オペランドであるという事実を無視するからです。
3457
34581956=head2 List Operators (Rightward)
34591957X<operator, list, rightward> X<list operator>
34601958
34611959(リスト演算子 (右方向))
34621960
34631961=begin original
34641962
3465On the right side of a list operator, the comma has very low precedence,
1963On the right side of a list operator, it has very low precedence,
34661964such that it controls all comma-separated expressions found there.
34671965The only operators with lower precedence are the logical operators
3468C<"and">, C<"or">, and C<"not">, which may be used to evaluate calls to list
1966"and", "or", and "not", which may be used to evaluate calls to list
3469operators without the need for parentheses:
1967operators without the need for extra parentheses:
34701968
34711969=end original
34721970
3473リスト演算子の右側のものにとって、カンマはとても低い優先順位になります;
1971リスト演算子の右側のものにとって、リスト演算子はとても低い優先順位になります
34741972これによってコンマで区切った式をリスト演算子の引数として
34751973置くことができます。
3476これよりも優先順位が低いものは、論理演算子の C<"and">, C<"or">,
1974これよりも優先順位が低いものは、論理演算子の "and", "or", "not" のみで、
3477C<"not"> のみで、括弧を付けないリスト演算子の呼び出しを評価するために使えます:
1975余分な括弧を付けないリスト演算子の呼び出しを評価するために使えます:
34781976
3479 open HANDLE, "< :encoding(UTF-8)", "filename"
1977 open HANDLE, "filename"
3480 or die "Can't open: $!\n";
1978 or die "Can't open: $!\n";
34811979
34821980=begin original
34831981
3484However, some people find that code harder to read than writing
1982See also discussion of list operators in L<Terms and List Operators (Leftward)>.
3485it with parentheses:
34861983
34871984=end original
34881985
3489しかし、かっこ付きで書くよりコードが読みにくという人もいます:
1986L<Terms and List Operators (Leftward)> のリスト演算子の議論参照して下さ
34901987
3491 open(HANDLE, "< :encoding(UTF-8)", "filename")
3492 or die "Can't open: $!\n";
3493
3494=begin original
3495
3496in which case you might as well just use the more customary C<"||"> operator:
3497
3498=end original
3499
3500この場合、より慣習的な C<"||"> 演算子も使えます:
3501
3502 open(HANDLE, "< :encoding(UTF-8)", "filename")
3503 || die "Can't open: $!\n";
3504
3505=begin original
3506
3507See also discussion of list operators in L</Terms and List Operators (Leftward)>.
3508
3509=end original
3510
3511L</Terms and List Operators (Leftward)> のリスト演算子の議論も参照して下さい。
3512
35131988=head2 Logical Not
35141989X<operator, logical, not> X<not>
35151990
35161991(論理否定)
35171992
35181993=begin original
35191994
3520Unary C<"not"> returns the logical negation of the expression to its right.
1995Unary "not" returns the logical negation of the expression to its right.
3521It's the equivalent of C<"!"> except for the very low precedence.
1996It's the equivalent of "!" except for the very low precedence.
35221997
35231998=end original
35241999
3525単項演算子の C<"not"> は右側に来る式の否定を返します。
2000単項演算子の "not" は右側に来る式の否定を返します。
3526これは、優先順位がずっと低いことを除いては C<"!"> と等価です。
2001これは、優先順位がずっと低いことを除いては "!" と等価です。
35272002
35282003=head2 Logical And
35292004X<operator, logical, and> X<and>
35302005
35312006(論理積)
35322007
35332008=begin original
35342009
3535Binary C<"and"> returns the logical conjunction of the two surrounding
2010Binary "and" returns the logical conjunction of the two surrounding
3536expressions. It's equivalent to C<&&> except for the very low
2011expressions. It's equivalent to && except for the very low
3537precedence. This means that it short-circuits: the right
2012precedence. This means that it short-circuits: i.e., the right
35382013expression is evaluated only if the left expression is true.
35392014
35402015=end original
35412016
3542二項演算子の C<"and"> は両側の式の論理積を返します。
2017二項演算子の "and" は両側の式の論理積を返します。
3543これは、優先順位がずっと低いことを除けば C<&&> と等価です。
2018これは、優先順位がずっと低いことを除けば && と等価です。
35442019つまり、これも短絡演算を行ない、右側の式は左側の式が
35452020「真」であった場合にのみ評価されます。
35462021
3547=head2 Logical or and Exclusive Or
2022=head2 Logical or, Defined or, and Exclusive Or
35482023X<operator, logical, or> X<operator, logical, xor>
3549X<operator, logical, exclusive or>
2024X<operator, logical, defined or> X<operator, logical, exclusive or>
35502025X<or> X<xor>
35512026
3552(論理和と排他論理和)
2027(論理和と定義性和と排他論理和)
35532028
35542029=begin original
35552030
3556Binary C<"or"> returns the logical disjunction of the two surrounding
2031Binary "or" returns the logical disjunction of the two surrounding
3557expressions. It's equivalent to C<||> except for the very low precedence.
2032expressions. It's equivalent to || except for the very low precedence.
3558This makes it useful for control flow:
2033This makes it useful for control flow
35592034
35602035=end original
35612036
3562二項演算子の C<"or"> は両側の式の論理和を返します。
2037二項演算子の "or" は両側の式の論理和を返します。
3563これは、優先順位がずっと低いことを除いて C<||> と等価です。
2038これは、優先順位がずっと低いことを除いて || と等価です。
35642039これはフローを制御するのに有用です:
35652040
35662041 print FH $data or die "Can't write to FH: $!";
35672042
35682043=begin original
35692044
3570This means that it short-circuits: the right expression is evaluated
2045This means that it short-circuits: i.e., the right expression is evaluated
3571only if the left expression is false. Due to its precedence, you must
2046only if the left expression is false. Due to its precedence, you should
3572be careful to avoid using it as replacement for the C<||> operator.
2047probably avoid using this for assignment, only for control flow.
3573It usually works out better for flow control than in assignments:
35742048
35752049=end original
35762050
35772051つまり、これも短絡演算を行ない、右側の式は左側の式が
35782052「偽」であった場合にのみ評価されます。
3579優先度の関係で、これを C<||> 演算子の置き換えに使は慎重
2053優先度の関係で、これは代入使わず、フロー制御のみ使うべきです。
3580避けなければなりません。
3581これは普通代入よりも、フローの制御でうまく動作します:
35822054
3583=begin original
2055 $a = $b or $c; # bug: this is wrong
2056 ($a = $b) or $c; # really means this
2057 $a = $b || $c; # better written this way
35842058
3585 $x = $y or $z; # bug: this is wrong
3586 ($x = $y) or $z; # really means this
3587 $x = $y || $z; # better written this way
3588
3589=end original
3590
3591 $x = $y or $z; # バグ: これは間違い
3592 ($x = $y) or $z; # 本当にしたいこと
3593 $x = $y || $z; # こう書いた方がいい
3594
35952059=begin original
35962060
35972061However, when it's a list-context assignment and you're trying to use
3598C<||> for control flow, you probably need C<"or"> so that the assignment
2062"||" for control flow, you probably need "or" so that the assignment
35992063takes higher precedence.
36002064
36012065=end original
36022066
3603しかし、代入がリストコンテキストの時に C<||> をフロー制御に使おうとする場合、
2067しかし、代入がリストコンテキストの時に "||" をフロー制御に使おうとする場合、
3604代入により大きな優先順位を持たせるために C<"or"> が必要かもしれません。
2068代入により大きな優先順位を持たせるために "or" が必要かもしれません。
36052069
3606=begin original
3607
36082070 @info = stat($file) || die; # oops, scalar sense of stat!
36092071 @info = stat($file) or die; # better, now @info gets its due
36102072
3611=end original
3612
3613 @info = stat($file) || die; # うわ、stat がスカラの意味だ!
3614 @info = stat($file) or die; # よりよい; @info はその目的を果たす
3615
36162073=begin original
36172074
36182075Then again, you could always use parentheses.
36192076
36202077=end original
36212078
36222079もちろん、常に括弧をつけてもよいです。
36232080
36242081=begin original
36252082
3626Binary C<"xor"> returns the exclusive-OR of the two surrounding expressions.
2083Binary "xor" returns the exclusive-OR of the two surrounding expressions.
3627It cannot short-circuit (of course).
2084It cannot short circuit, of course.
36282085
36292086=end original
36302087
3631二項演算子の C<"xor"> は両側の式の排他論理和を返します。
2088二項演算子の "xor" は両側の式の排他論理和を返します。
3632これは (もちろん) 短絡でません。
2089これはもちろん短絡ではありません。
36332090
3634=begin original
3635
3636There is no low precedence operator for defined-OR.
3637
3638=end original
3639
3640定義性論理和の低優先順位版はありません。
3641
36422091=head2 C Operators Missing From Perl
36432092X<operator, missing from perl> X<&> X<*>
36442093X<typecasting> X<(TYPE)>
36452094
36462095(Perl にない C の演算子)
36472096
36482097=begin original
36492098
36502099Here is what C has that Perl doesn't:
36512100
36522101=end original
36532102
36542103C にあって Perl に無いものは以下の通りです:
36552104
36562105=over 8
36572106
36582107=item unary &
36592108
36602109=begin original
36612110
3662Address-of operator. (But see the C<"\"> operator for taking a reference.)
2111Address-of operator. (But see the "\" operator for taking a reference.)
36632112
36642113=end original
36652114
36662115アドレス演算子。
3667(しかし C<"\"> 演算子がリファレンスのために用いられます。)
2116("\" 演算子がリファレンスのために用いられます。)
36682117
36692118=item unary *
36702119
36712120=begin original
36722121
3673Dereference-address operator. (Perl's prefix dereferencing
2122Dereference-address operator. (Perl's prefix dereferencing
3674operators are typed: C<$>, C<@>, C<%>, and C<&>.)
2123operators are typed: $, @, %, and &.)
36752124
36762125=end original
36772126
36782127被アドレス参照演算子。
3679(Perl の被参照プリフィクス演算子が型づけを行ないます:
2128(Perl の被参照プリフィクス演算子が型づけを行ないます: $, @, %, &。)
3680C<$>, C<@>, C<%>, C<&>。)
36812129
36822130=item (TYPE)
36832131
36842132=begin original
36852133
36862134Type-casting operator.
36872135
36882136=end original
36892137
36902138型のキャスト演算子。
36912139
36922140=back
36932141
36942142=head2 Quote and Quote-like Operators
36952143X<operator, quote> X<operator, quote-like> X<q> X<qq> X<qx> X<qw> X<m>
36962144X<qr> X<s> X<tr> X<'> X<''> X<"> X<""> X<//> X<`> X<``> X<<< << >>>
36972145X<escape sequence> X<escape>
36982146
36992147(クォートとクォート風の演算子)
37002148
37012149=begin original
37022150
37032151While we usually think of quotes as literal values, in Perl they
37042152function as operators, providing various kinds of interpolating and
37052153pattern matching capabilities. Perl provides customary quote characters
37062154for these behaviors, but also provides a way for you to choose your
37072155quote character for any of them. In the following table, a C<{}> represents
37082156any pair of delimiters you choose.
37092157
37102158=end original
37112159
37122160クォートはリテラル値であると考えるのが普通ですが、Perl において、
37132161クォートは演算子として働き、さまざまな展開やパターンマッチの機能を
37142162持っています。
37152163そのような動作をさせるのに、Perl は慣習的にクォート文字を使っていますが、
37162164どの種類のクォートも、自分でクォート文字を選べるようになっています。
37172165以下の表では、{} がその選んだ区切文字のペアを示しています。
37182166
37192167=begin original
37202168
37212169 Customary Generic Meaning Interpolates
37222170 '' q{} Literal no
37232171 "" qq{} Literal yes
37242172 `` qx{} Command yes*
37252173 qw{} Word list no
37262174 // m{} Pattern match yes*
37272175 qr{} Pattern yes*
37282176 s{}{} Substitution yes*
37292177 tr{}{} Transliteration no (but see below)
3730 y{}{} Transliteration no (but see below)
37312178 <<EOF here-doc yes*
37322179
2180 * unless the delimiter is ''.
2181
37332182=end original
37342183
37352184 通常記法 汎用記法 意味 展開
37362185 =================================================
37372186 '' q{} リテラル 不可
37382187 "" qq{} リテラル 可
37392188 `` qx{} コマンド 可 *
37402189 qw{} 単語リスト 不可
37412190 // m{} パターンマッチ 可 *
37422191 qr{} パターン 可 *
37432192 s{}{} 置換 可 *
37442193 tr{}{} 変換 不可 (但し以下を参照のこと)
3745 y{}{} 変換 不可 (但し以下を参照のこと)
37462194 <<EOF ヒアドキュメント 可 *
37472195
3748=begin original
3749
3750 * unless the delimiter is ''.
3751
3752=end original
3753
37542196 * '' がデリミタでない場合のみ
37552197
37562198=begin original
37572199
37582200Non-bracketing delimiters use the same character fore and aft, but the four
3759sorts of ASCII brackets (round, angle, square, curly) all nest, which means
2201sorts of brackets (round, angle, square, curly) will all nest, which means
37602202that
37612203
37622204=end original
37632205
37642206選んだ区切文字が括弧の類でない場合には、前後の文字として同一のものを
3765使いますが、4 つの ASCII のかっこ ((), <>, [], {}) の場合にはネストできます;
2207使いますが、4 つの括弧 ((), <>, [], {}) の場合にはネストできます
37662208つまり、以下のものは、
37672209
3768 q{foo{bar}baz}
2210 q{foo{bar}baz}
37692211
37702212=begin original
37712213
37722214is the same as
37732215
37742216=end original
37752217
37762218以下と同じです。
37772219
3778 'foo{bar}baz'
2220 'foo{bar}baz'
37792221
37802222=begin original
37812223
37822224Note, however, that this does not always work for quoting Perl code:
37832225
37842226=end original
37852227
37862228しかし、以下のコードはクォートされた Perl コードでは
37872229いつも正しく動くわけではないことに注意してください:
37882230
3789 $s = q{ if($x eq "}") ... }; # WRONG
2231 $s = q{ if($a eq "}") ... }; # WRONG
37902232
37912233=begin original
37922234
3793is a syntax error. The C<L<Text::Balanced>> module (standard as of v5.8,
2235is a syntax error. The C<Text::Balanced> module (from CPAN, and
3794and from CPAN before then) is able to do this properly.
2236starting from Perl 5.8 part of the standard distribution) is able
2237to do this properly.
37952238
37962239=end original
37972240
37982241これは文法エラーとなります。
3799C<L<Text::Balanced>> モジュール(Perl 5.8 からは標準配布、それ以前は CPAN に
2242C<Text::Balanced> モジュール(CPAN から、または Perl 5.8 からは標準配布
3800あります)はこれを適切に行います。
2243一部です)はこれを適切に行います。
38012244
38022245=begin original
38032246
3804There can (and in some cases, must) be whitespace between the operator
2247There can be whitespace between the operator and the quoting
3805and the quoting
38062248characters, except when C<#> is being used as the quoting character.
3807C<q#foo#> is parsed as the string C<foo>, while S<C<q #foo<#del>>> is the
2249C<q#foo#> is parsed as the string C<foo>, while C<q #foo#> is the
38082250operator C<q> followed by a comment. Its argument will be taken
38092251from the next line. This allows you to write:
38102252
38112253=end original
38122254
3813演算子とクォート文字の間に空白を置くことも出来ます (そして場合によっては
2255演算子とクォート文字の間に空白を置くことも出来ます
3814必須です); ただし、C<#> をクォート文字として使う場合は例外です。
2256ただし、C<#> をクォート文字として使う場合は例外です。
38152257C<q#foo#> は文字列 C<foo> としてパースされますが、
3816S<C<q #foo<#del>>> は C<q> 演算子の後にコメントがあるとみなされます。
2258C<q #foo#> は C<q> 演算子の後にコメントがあるとみなされます。
38172259この引数は次の行から取られます。つまり、以下のように書けます:
38182260
3819=begin original
3820
38212261 s {foo} # Replace foo
38222262 {bar} # with bar.
38232263
3824=end original
3825
3826 s {foo} # foo を
3827 {bar} # bar で置き換える
3828
38292264=begin original
38302265
3831The cases where whitespace must be used are when the quoting character
2266The following escape sequences are available in constructs that interpolate
3832is a word character (meaning it matches C</\w/>):
2267and in transliterations.
2268X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N>
38332269
38342270=end original
38352271
3836空白が使われなければならないは、クォト文字単語文字
2272以下エスケプシーケンス展開と文字変換の構文で利用可能です。
3837(つまり C</\w/> にマッチングする)の場合です:
2273X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N>
38382274
3839 q XfooX # Works: means the string 'foo'
3840 qXfooX # WRONG!
3841
38422275=begin original
38432276
3844The following escape sequences are available in constructs that interpolate,
2277 \t tab (HT, TAB)
3845and in transliterations whose delimiters aren't single quotes (C<"'">).
2278 \n newline (NL)
3846In all the ones with braces, any number of blanks and/or tabs adjoining
2279 \r return (CR)
3847and within the braces are allowed (and ignored).
2280 \f form feed (FF)
3848X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}>
2281 \b backspace (BS)
3849X<\o{}>
2282 \a alarm (bell) (BEL)
2283 \e escape (ESC)
2284 \033 octal char (example: ESC)
2285 \x1b hex char (example: ESC)
2286 \x{263a} wide hex char (example: SMILEY)
2287 \c[ control char (example: ESC)
2288 \N{name} named Unicode character
38502289
38512290=end original
38522291
3853以下のエスケープシーケンスが、
2292 \t タブ (HT, TAB)
3854区切り文字がシングルクォート (C<"'">) でない展開と文字変換の構文で
2293 \n 改行 (NL)
3855利用可能です。
2294 \r 復帰 (CR)
3856中かっこつきのもの全ては、中かっこの中で隣接している任意の数の空白や
2295 \f 改ページ (FF)
3857タブが許されます(そして無視されます)
2296 \b バックスペース (BS)
3858X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}>
2297 \a アラーム (BEL)
3859X<\o{}>
2298 \e エスケープ (ESC)
2299 \033 8 進数で表した文字 (例: ESC)
2300 \x1b 16 進数で表した文字 (例: ESC)
2301 \x{263a} 16 進数で表したワイド文字 (例: SMILEY)
2302 \c[ コントロール文字 (例: ESC)
2303 \N{name} 名前つき Unicode 文字
38602304
38612305=begin original
38622306
3863 Sequence Note Description
2307The character following C<\c> is mapped to some other character by
3864 \t tab (HT, TAB)
2308converting letters to upper case and then (on ASCII systems) by inverting
3865 \n newline (NL)
2309the 7th bit (0x40). The most interesting range is from '@' to '_'
3866 \r return (CR)
2310(0x40 through 0x5F), resulting in a control character from 0x00
3867 \f form feed (FF)
2311through 0x1F. A '?' maps to the DEL character. On EBCDIC systems only
3868 \b backspace (BS)
2312'@', the letters, '[', '\', ']', '^', '_' and '?' will work, resulting
3869 \a alarm (bell) (BEL)
2313in 0x00 through 0x1F and 0x7F.
3870 \e escape (ESC)
3871 \x{263A} [1,8] hex char (example shown: SMILEY)
3872 \x{ 263A } Same, but shows optional blanks inside and
3873 adjoining the braces
3874 \x1b [2,8] restricted range hex char (example: ESC)
3875 \N{name} [3] named Unicode character or character sequence
3876 \N{U+263D} [4,8] Unicode character (example: FIRST QUARTER MOON)
3877 \c[ [5] control char (example: chr(27))
3878 \o{23072} [6,8] octal char (example: SMILEY)
3879 \033 [7,8] restricted range octal char (example: ESC)
38802314
38812315=end original
38822316
3883 ーケン 注意 説明
2317C<\c> に引き続く文字は、英字は大文字に変換された後、(ASCII システムでは)
3884 \t タブ (HT, TAB)
23187 番目のビット (0x40) を反転させることで別の文字にマッピングされます。
3885 \n 改行 (NL)
2319最も興味深い範囲は '@' から '_' (0x40 から 0x5F) で、結果として
3886 \r 復帰 (CR)
2320コントロール文字 0x00 から 0x1F になります。
3887 \f 改ページ (FF)
2321'?' DEL 文字にマッピングされます。
3888 \b バックスペース (BS)
2322EBCDIC システムでは '@', 英字, '[', '\', ']', '^', '_', '?' のみが
3889 \a アラーム (BEL)
2323動作し、結果として 0x00 から 0x1F 0x7F になります。
3890 \e エスケープ (ESC)
3891 \x{263a} [1,8] 16 進文字 (例: SMILEY)
3892 \x{ 263A } 同様、しかし中かっこの内側に隣接している
3893 オプションの空白を示している
3894 \x1b [2,8] 範囲制限された 16 進数で表した文字 (例: ESC)
3895 \N{name} [3] 名前つき Unicode 文字または文字シーケンス
3896 \N{U+263D} [4,8] Unicode 文字 (例: FIRST QUARTER MOON)
3897 \c[ [5] 制御文字 (例: chr(27))
3898 \o{23072} [6,8] 8 進文字 (例: SMILEY)
3899 \033 [7,8] 範囲制限された 8 進文字 (例: ESC)
39002324
39012325=begin original
39022326
3903Note that any escape sequence using braces inside interpolated
2327B<NOTE>: Unlike C and other languages, Perl has no \v escape sequence for
3904constructs may have optional blanks (tab or space characters) adjoining
2328the vertical tab (VT - ASCII 11), but you may use C<\ck> or C<\x0b>.
3905with and inside of the braces, as illustrated above by the second
3906S<C<\x{ }>> example.
39072329
39082330=end original
39092331
3910変数置換構文中かこを使ったエスケープシーケンスは
2332B<注意>: C やそ言語と違Perl は垂直タブ (VT - ASCII 11) のための
3911前述の 2 番目の S<C<\x{ }>> の例で図示したように、中かっこの内側隣接の
2333\v エスケープシーケンスはありませんが、C<\ck> または C<\x0b> が使えます。
3912位置にオプションの空白(タブまたはスペース文字)を置くことができます。
39132334
3914=over 4
3915
3916=item [1]
3917
39182335=begin original
39192336
3920The result is the character specified by the hexadecimal number between
2337The following escape sequences are available in constructs that interpolate
3921the braces. See L</[8]> below for details on which character.
3922
3923=end original
3924
3925結果は中かっこで囲まれた 16 進数で指定された文字です。
3926その文字に関する詳細については以下の L</[8]> を参照してください。
3927
3928=begin original
3929
3930Blanks (tab or space characters) may separate the number from either or
3931both of the braces.
3932
3933=end original
3934
3935空白 (タブまたはスペース文字) は、片方または両方の中かっこから数字を
3936分離します。
3937
3938=begin original
3939
3940Otherwise, only hexadecimal digits are valid between the braces. If an
3941invalid character is encountered, a warning will be issued and the
3942invalid character and all subsequent characters (valid or invalid)
3943within the braces will be discarded.
3944
3945=end original
3946
3947さもなければ、中かっこの中には 16 進数字のみが妥当です。
3948不正な文字に遭遇すると、警告が発生し、中かっこの内側の不正な文字と
3949それ以降の文字(妥当でも不正でも)は捨てられます。
3950
3951=begin original
3952
3953If there are no valid digits between the braces, the generated character is
3954the NULL character (C<\x{00}>). However, an explicit empty brace (C<\x{}>)
3955will not cause a warning (currently).
3956
3957=end original
3958
3959中かっこの中に妥当な文字がなければ、生成される文字は NULL 文字
3960(C<\x{00}>) です。
3961しかし、明示的な空の中かっこ (C<\x{}>) は(今のところ)警告を出しません。
3962
3963=item [2]
3964
3965=begin original
3966
3967The result is the character specified by the hexadecimal number in the range
39680x00 to 0xFF. See L</[8]> below for details on which character.
3969
3970=end original
3971
3972結果は 0x00 から 0xFF の範囲の 16 進数で指定された文字です。
3973その文字に関する詳細については以下の L</[8]> を参照してください。
3974
3975=begin original
3976
3977Only hexadecimal digits are valid following C<\x>. When C<\x> is followed
3978by fewer than two valid digits, any valid digits will be zero-padded. This
3979means that C<\x7> will be interpreted as C<\x07>, and a lone C<"\x"> will be
3980interpreted as C<\x00>. Except at the end of a string, having fewer than
3981two valid digits will result in a warning. Note that although the warning
3982says the illegal character is ignored, it is only ignored as part of the
3983escape and will still be used as the subsequent character in the string.
3984For example:
3985
3986=end original
3987
3988C<\x> に引き続くのは 16 進数字のみが妥当です。
3989C<\x> に引き続く妥当な数字が 2 桁ない場合、妥当な数字はゼロで
3990パッディングされます。
3991これは、C<\x7> は C<\x07> と解釈され、単独の C<"\x"> は C<\x00> と
3992解釈されるということです。
3993文字列の末尾を例外として、妥当な数字が 2 桁ない場合は警告が発生します。
3994警告は不正な文字が無視されると言うにも関わらず、エスケープの一部のみが
3995無視され、文字列中の引き続く文字は使われるままであることに注意してください。
3996例えば:
3997
3998 Original Result Warns?
3999 "\x7" "\x07" no
4000 "\x" "\x00" no
4001 "\x7q" "\x07q" yes
4002 "\xq" "\x00q" yes
4003
4004=item [3]
4005
4006=begin original
4007
4008The result is the Unicode character or character sequence given by I<name>.
4009See L<charnames>.
4010
4011=end original
4012
4013結果は I<name> で指定される Unicode 文字または文字の並びです。
4014L<charnames> を参照してください。
4015
4016=item [4]
4017
4018=begin original
4019
4020S<C<\N{U+I<hexadecimal number>}>> means the Unicode character whose Unicode code
4021point is I<hexadecimal number>.
4022
4023=end original
4024
4025S<C<\N{U+I<hexadecimal number>}>> は、Unicode 符号位置が
4026I<hexadecimal number> の Unicode 文字を意味します。
4027
4028=item [5]
4029
4030=begin original
4031
4032The character following C<\c> is mapped to some other character as shown in the
4033table:
4034
4035=end original
4036
4037C<\c> に引き続く文字は以下の表に示すように他の文字にマッピングされます:
4038
4039 Sequence Value
4040 \c@ chr(0)
4041 \cA chr(1)
4042 \ca chr(1)
4043 \cB chr(2)
4044 \cb chr(2)
4045 ...
4046 \cZ chr(26)
4047 \cz chr(26)
4048 \c[ chr(27)
4049 # See below for chr(28)
4050 \c] chr(29)
4051 \c^ chr(30)
4052 \c_ chr(31)
4053 \c? chr(127) # (on ASCII platforms; see below for link to
4054 # EBCDIC discussion)
4055
4056=begin original
4057
4058In other words, it's the character whose code point has had 64 xor'd with
4059its uppercase. C<\c?> is DELETE on ASCII platforms because
4060S<C<ord("?") ^ 64>> is 127, and
4061C<\c@> is NULL because the ord of C<"@"> is 64, so xor'ing 64 itself produces 0.
4062
4063=end original
4064
4065言い換えると、符号位置を 64 で xor して大文字にした文字です。
4066S<C<ord("?") ^ 64>> は 127 なので C<\c?> は ASCII プラットフォームでは
4067DELETE で、C<"@"> は 64 のために
406864 で xor すると 0 になるので C<\c@> は NUL です。
4069
4070=begin original
4071
4072Also, C<\c\I<X>> yields S<C< chr(28) . "I<X>">> for any I<X>, but cannot come at the
4073end of a string, because the backslash would be parsed as escaping the end
4074quote.
4075
4076=end original
4077
4078また、C<\c\I<X>> は任意の I<X> について S<C< chr(28) . "I<X>">> となりますが、
4079文字列の末尾には来ません; 逆スラッシュは末尾のクォートをエスケープするように
4080パースされるからです。
4081
4082=begin original
4083
4084On ASCII platforms, the resulting characters from the list above are the
4085complete set of ASCII controls. This isn't the case on EBCDIC platforms; see
4086L<perlebcdic/OPERATOR DIFFERENCES> for a full discussion of the
4087differences between these for ASCII versus EBCDIC platforms.
4088
4089=end original
4090
4091ASCII プラットフォームでは、上述の一覧からの結果の文字は ASCII 制御文字の
4092完全な集合です。
4093これは EBCDIC プラットフォームには当てはまりません; これに関する
4094ASCII プラットフォームと EBCDIC プラットフォームとの違いの完全な議論については
4095L<perlebcdic/OPERATOR DIFFERENCES> を参照してください。
4096
4097=begin original
4098
4099Use of any other character following the C<"c"> besides those listed above is
4100discouraged, and as of Perl v5.20, the only characters actually allowed
4101are the printable ASCII ones, minus the left brace C<"{">. What happens
4102for any of the allowed other characters is that the value is derived by
4103xor'ing with the seventh bit, which is 64, and a warning raised if
4104enabled. Using the non-allowed characters generates a fatal error.
4105
4106=end original
4107
4108C<"c"> に引き続いて上述した以外の文字を使うことは非推奨であり、
4109as of Perl v5.20, the only characters actually allowed
4110are the printable ASCII ones, minus the left brace C<"{">.
4111許されている他の文字を置いたときに起こることは、値が第 7 ビット;
4112つまり 64 で xor を取ったものになり、
4113有効の場合は警告が発生します。
4114許されていない文字を使うと致命的エラーが発生します。
4115
4116=begin original
4117
4118To get platform independent controls, you can use C<\N{...}>.
4119
4120=end original
4121
4122プラットフォーム非依存の制御文字を得るには、C<\N{...}> を使ってください。
4123
4124=item [6]
4125
4126=begin original
4127
4128The result is the character specified by the octal number between the braces.
4129See L</[8]> below for details on which character.
4130
4131=end original
4132
4133結果は中かっこで囲まれた 8 進数で指定された文字です。
4134その文字に関する詳細については以下の L</[8]> を参照してください。
4135
4136=begin original
4137
4138Blanks (tab or space characters) may separate the number from either or
4139both of the braces.
4140
4141=end original
4142
4143空白 (タブまたはスペース文字) は、片方または両方の中かっこから数字を
4144分離します。
4145
4146=begin original
4147
4148Otherwise, if a character that isn't an octal digit is encountered, a
4149warning is raised, and the value is based on the octal digits before it,
4150discarding it and all following characters up to the closing brace. It
4151is a fatal error if there are no octal digits at all.
4152
4153=end original
4154
4155さもなければ、8 進数でない文字に遭遇すると、警告が発生し、値はそこまでの
41568 進数字を基として、それ以降閉じ中かっこまでの全ての文字を捨てます。
41578 進数字がまったくないと致命的エラーになります。
4158
4159=item [7]
4160
4161=begin original
4162
4163The result is the character specified by the three-digit octal number in the
4164range 000 to 777 (but best to not use above 077, see next paragraph). See
4165L</[8]> below for details on which character.
4166
4167=end original
4168
4169結果は範囲 000 から 777 までの 3 桁の 8 進数で指定される文字です
4170(しかし 077 より上は使わないのが最良です; 次の段落を参照してください)。
4171その文字に関する詳細については以下の L</[8]> を参照してください。
4172
4173=begin original
4174
4175Some contexts allow 2 or even 1 digit, but any usage without exactly
4176three digits, the first being a zero, may give unintended results. (For
4177example, in a regular expression it may be confused with a backreference;
4178see L<perlrebackslash/Octal escapes>.) Starting in Perl 5.14, you may
4179use C<\o{}> instead, which avoids all these problems. Otherwise, it is best to
4180use this construct only for ordinals C<\077> and below, remembering to pad to
4181the left with zeros to make three digits. For larger ordinals, either use
4182C<\o{}>, or convert to something else, such as to hex and use C<\N{U+}>
4183(which is portable between platforms with different character sets) or
4184C<\x{}> instead.
4185
4186=end original
4187
4188一部のコンテキストでは 2 桁や、1 桁ですら許されますが、正確に 3 桁かつ
4189先頭が 0、以外の使い方は意図していない結果をもたらすかもしれません。
4190(例えば、正規表現中では後方参照で混乱するかもしれません;
4191L<perlrebackslash/Octal escapes> を参照してください。)
4192Perl 5.14 から、代わりに C<\o{}> を使えます; これはこれらすべての問題を
4193避けられます。
4194さもなければ、この構文を値 C<\077> 以下でのみ使用するのが最良です;
41953 桁にするために左側にゼロをパッディングするのを忘れないでください。
4196より大きな値では、C<\o{}> を使うか、代わりに 16 進数にして
4197C<\N{U+}>
4198(これは異なった文字集合のプラットフォーム間で移植性があります) を使うか、
4199または C<\x{}> を
4200使うような、他のものに変換してください。
4201
4202=item [8]
4203
4204=begin original
4205
4206Several constructs above specify a character by a number. That number
4207gives the character's position in the character set encoding (indexed from 0).
4208This is called synonymously its ordinal, code position, or code point. Perl
4209works on platforms that have a native encoding currently of either ASCII/Latin1
4210or EBCDIC, each of which allow specification of 256 characters. In general, if
4211the number is 255 (0xFF, 0377) or below, Perl interprets this in the platform's
4212native encoding. If the number is 256 (0x100, 0400) or above, Perl interprets
4213it as a Unicode code point and the result is the corresponding Unicode
4214character. For example C<\x{50}> and C<\o{120}> both are the number 80 in
4215decimal, which is less than 256, so the number is interpreted in the native
4216character set encoding. In ASCII the character in the 80th position (indexed
4217from 0) is the letter C<"P">, and in EBCDIC it is the ampersand symbol C<"&">.
4218C<\x{100}> and C<\o{400}> are both 256 in decimal, so the number is interpreted
4219as a Unicode code point no matter what the native encoding is. The name of the
4220character in the 256th position (indexed by 0) in Unicode is
4221C<LATIN CAPITAL LETTER A WITH MACRON>.
4222
4223=end original
4224
4225上述のいくつかの構造は文字を数値で指定しています。
4226この値は文字集合エンコーディングで (0 から始めた) 文字の位置を指定します。
4227これは同意語として序数(ordinal)、コード位置(code position)、
4228符号位置(code point)と呼ばれます。
4229Perl は現在のところ、それぞれ 256 文字を定義している ASCII/Latin1 または
4230EBCDIC のどちらかのネイティブエンコーディングを持つプラットフォームで
4231動作します。
4232一般的に、数値が 255 (0xFF, 0377) 以下なら、Perl はこれをプラットフォームの
4233ネイティブエンコーディングと解釈します。
4234数値が 256 (0x100, 0400) 以上なら、Perl はこれを Unicode 符号位置と解釈し、
4235結果は対応する Unicode 文字となります。
4236例えば C<\x{50}> と C<\o{120}> はどちらも 10 進数では 80 で、これは 256 より
4237小さいので、数値はネイティブ文字集合エンコーディングとして解釈されます。
4238ASCII では (0 から始めて) 80 番目の位置の文字は C<"P"> で、EBCDIC では
4239アンパサンド記号 C<"&"> です。
4240C<\x{100}> と C<\o{400}> はどちらも 10 進数では 256 なので、数値は
4241ネイティブエンコーディングが何かに関わらず Unicode 符号位置として
4242解釈されます。
4243Unicode での (0 から始めて) 256 番目の位置の文字の名前は
4244C<LATIN CAPITAL LETTER A WITH MACRON> です。
4245
4246=begin original
4247
4248An exception to the above rule is that S<C<\N{U+I<hex number>}>> is
4249always interpreted as a Unicode code point, so that C<\N{U+0050}> is C<"P"> even
4250on EBCDIC platforms.
4251
4252=end original
4253
4254上述の規則の例外として、S<C<\N{U+I<16 進数>}>> は常に Unicode 符号位置として
4255解釈されるので、C<\N{U+0050}> は EBCDIC プラットフォームでも C<"P"> です。
4256
4257=back
4258
4259=begin original
4260
4261B<NOTE>: Unlike C and other languages, Perl has no C<\v> escape sequence for
4262the vertical tab (VT, which is 11 in both ASCII and EBCDIC), but you may
4263use C<\N{VT}>, C<\ck>, C<\N{U+0b}>, or C<\x0b>. (C<\v>
4264does have meaning in regular expression patterns in Perl, see L<perlre>.)
4265
4266=end original
4267
4268B<注意>: C やその他の言語と違って、Perl は垂直タブ (VT - ASCII と EBCDIC の
4269両方で 11) のための \v エスケープシーケンスはありませんが、C<\N{VT}>, C<\ck>,
4270C<\N{U+0b}>, C<\x0b> が使えます。
4271(C<\v> は Perl の正規表現パターンでは意味があります; L<perlre> を
4272参照してください。)
4273
4274=begin original
4275
4276The following escape sequences are available in constructs that interpolate,
42772338but not in transliterations.
4278X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> X<\F>
2339X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q>
42792340
42802341=end original
42812342
42822343以下のエスケープシーケンスが展開と文字変換の構文で利用可能です。
42832344X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q>
4284X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> X<\F>
42852345
42862346=begin original
42872347
4288 \l lowercase next character only
2348 \l lowercase next char
4289 \u titlecase (not uppercase!) next character only
2349 \u uppercase next char
4290 \L lowercase all characters till \E or end of string
2350 \L lowercase till \E
4291 \U uppercase all characters till \E or end of string
2351 \U uppercase till \E
4292 \F foldcase all characters till \E or end of string
2352 \E end case modification
4293 \Q quote (disable) pattern metacharacters till \E or
2353 \Q quote non-word characters till \E
4294 end of string
4295 \E end either case modification or quoted section
4296 (whichever was last seen)
42972354
42982355=end original
42992356
4300 \l 次の文字だけを小文字にする
2357 \l 次の文字を小文字にする
4301 \u 次の文字だけタイトル文字(大文字ではありません!)にする
2358 \u 次の文字を大文字にする
4302 \L \E か文字列の末尾まで小文字にする
2359 \L \E まで小文字にする
4303 \U \E か文字列の末尾まで大文字にする
2360 \U \E まで大文字にする
4304 \F \E か文字列末尾まで畳み込み文字にする
2361 \E 変更終わり
4305 \Q \E か文字列の末尾までパターンメタ文字をクォート(無効化)する
2362 \Q \E まで非単語文字をクォートする
4306 \E 大文字小文字変換かクォート部分(どちらか最後に現れたもの)を
4307 終了させる
43082363
43092364=begin original
43102365
4311See L<perlfunc/quotemeta> for the exact definition of characters that
2366If C<use locale> is in effect, the case map used by C<\l>, C<\L>,
4312are quoted by C<\Q>.
2367C<\u> and C<\U> is taken from the current locale. See L<perllocale>.
2368If Unicode (for example, C<\N{}> or wide hex characters of 0x100 or
2369beyond) is being used, the case map used by C<\l>, C<\L>, C<\u> and
2370C<\U> is as defined by Unicode. For documentation of C<\N{name}>,
2371see L<charnames>.
43132372
43142373=end original
43152374
4316C<\Q> でクォートされる文字正確な定義については L<perlfunc/quotemeta>
2375C<use locale> が有効場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる
4317参照してください。
4318
4319=begin original
4320
4321C<\L>, C<\U>, C<\F>, and C<\Q> can stack, in which case you need one
4322C<\E> for each. For example:
4323
4324=end original
4325
4326C<\L>, C<\U>, C<\F>, C<\Q> はスタックできます; この場合それぞれに対して
4327C<\E> が必要です。
4328例えば:
4329
4330 say "This \Qquoting \ubusiness \Uhere isn't quite\E done yet,\E is it?";
4331 This quoting\ Business\ HERE\ ISN\'T\ QUITE\ done\ yet\, is it?
4332
4333=begin original
4334
4335If a S<C<use locale>> form that includes C<LC_CTYPE> is in effect (see
4336L<perllocale>), the case map used by C<\l>, C<\L>, C<\u>, and C<\U> is
4337taken from the current locale. If Unicode (for example, C<\N{}> or code
4338points of 0x100 or beyond) is being used, the case map used by C<\l>,
4339C<\L>, C<\u>, and C<\U> is as defined by Unicode. That means that
4340case-mapping a single character can sometimes produce a sequence of
4341several characters.
4342Under S<C<use locale>>, C<\F> produces the same results as C<\L>
4343for all locales but a UTF-8 one, where it instead uses the Unicode
4344definition.
4345
4346=end original
4347
4348C<LC_CTYPE> を含む S<C<use locale>> 型式が有効の場合(L<perllocale> を
4349参照して下さい)、C<\l>, C<\L>, C<\u>, C<\U> で使われる
43502376大文字小文字テーブルは現在のロケールのものが使われます。
4351(例えば、C<\N{}> や、0x100 以上の符号位置の) Unicode
2377L<perllocale> を参照して下さい。
2378(例えば、C<\N{}> や、0x100 以上のワイド 16 進文字を使った) Unicode が
43522379使われている場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる大文字小文字
43532380テーブルは Unicode で定義されているものになります。
4354これは、単一文字の大文字小文字マッピ複数の文字の並び生成することが
2381C<\N{name}> ドキュメトに関して、L<charnames> 参照して下さい。
4355あるということです。
4356S<C<use locale>> の基では、C<\F> は、Unicode の定義が使われる UTF-8 以外の
4357全てのロケールにおいて、C<\L> と同じ結果を生成します。
43582382
43592383=begin original
43602384
43612385All systems use the virtual C<"\n"> to represent a line terminator,
43622386called a "newline". There is no such thing as an unvarying, physical
43632387newline character. It is only an illusion that the operating system,
43642388device drivers, C libraries, and Perl all conspire to preserve. Not all
43652389systems read C<"\r"> as ASCII CR and C<"\n"> as ASCII LF. For example,
4366on the ancient Macs (pre-MacOS X) of yesteryear, these used to be reversed,
2390on a Mac, these are reversed, and on systems without line terminator,
4367and on systems without a line terminator,
2391printing C<"\n"> may emit no actual data. In general, use C<"\n"> when
4368printing C<"\n"> might emit no actual data. In general, use C<"\n"> when
43692392you mean a "newline" for your system, but use the literal ASCII when you
43702393need an exact character. For example, most networking protocols expect
43712394and prefer a CR+LF (C<"\015\012"> or C<"\cM\cJ">) for line terminators,
43722395and although they often accept just C<"\012">, they seldom tolerate just
43732396C<"\015">. If you get in the habit of using C<"\n"> for networking,
43742397you may be burned some day.
43752398X<newline> X<line terminator> X<eol> X<end of line>
43762399X<\n> X<\r> X<\r\n>
43772400
43782401=end original
43792402
43802403全てのシステムでは "newline" と呼ばれる行端末子を表現するために
43812404仮想的な C<"\n"> が用いられます。
43822405普遍の、物理的な "newline" 文字と言うものはありません。
43832406オペレーティングシステム、デバイスドライバ、C ライブラリ、Perl が全て
43842407協力して保存しようとすると言うのは単なる幻想です。
43852408全てのシステムで C<"\r"> を ASCII CR として、また C<"\n"> を
43862409ASCII LF として読み込むわけではありません。
4387例えば昔の Mac (MacOS X 以前)ではこれらは保存され、行端末子のない
2410例えば Mac ではこれらは保存され、行端末子のないシステムでは、
4388システムでは、C<"\n"> を print しても実際のデータは何も出力しません。
2411C<"\n"> を print しても実際のデータは何も出力しません。
43892412一般に、システムで "newline" を意味したいときには C<"\n"> を使いますが、
43902413正確な文字が必要な場合はリテラルな ASCII を使います。
43912414例えば、ほとんどのネットワークプロトコルでは行端末子として
43922415CR+LF (C<"\015\012"> または C<"\cM\cJ">) を予想し、また好みますが、
43932416しばしば C<"\012"> だけでも許容し、さらに時々は C<"\015"> だけでも認めます。
43942417もしネットワーク関係で C<"\n"> を使う習慣がついていると、
43952418いつか痛い目を見ることになるでしょう。
4396X<newline> X<line terminator> X<eol> X<end of line>
4397X<\n> X<\r> X<\r\n>
43982419
43992420=begin original
44002421
44012422For constructs that do interpolate, variables beginning with "C<$>"
44022423or "C<@>" are interpolated. Subscripted variables such as C<$a[3]> or
44032424C<< $href->{key}[0] >> are also interpolated, as are array and hash slices.
44042425But method calls such as C<< $obj->meth >> are not.
44052426
44062427=end original
44072428
44082429展開が行なわれる構文では、"C<$>" や "C<@>" で始まる変数が展開されます。
4409C<$a[3]> C<< $href->{key}[0] >> のような添え字付き変数もまた
2430Subscripted variables such as C<$a[3]> or
4410配列やハッシュのスライスのように展開されます。
2431C<< $href->{key}[0] >> もまた配列やハッシュのスライスのように展開されます。
44112432しかし、C<< $obj->meth >> のようなメソッド呼び出しは展開されません。
44122433
44132434=begin original
44142435
44152436Interpolating an array or slice interpolates the elements in order,
44162437separated by the value of C<$">, so is equivalent to interpolating
4417S<C<join $", @array>>. "Punctuation" arrays such as C<@*> are usually
2438C<join $", @array>. "Punctuation" arrays such as C<@*> are only
4418interpolated only if the name is enclosed in braces C<@{*}>, but the
2439interpolated if the name is enclosed in braces C<@{*}>, but special
4419arrays C<@_>, C<@+>, and C<@-> are interpolated even without braces.
2440arrays C<@_>, C<@+>, and C<@-> are interpolated, even without braces.
44202441
44212442=end original
44222443
44232444配列やスライスの展開は、要素を順番に、C<$"> の値で分割して展開されるので、
4424S<C<join $", @array>> の展開と等価です。
2445C<join $", @array> の展開と等価です。
4425C<@*> のような「句読点」配列は普通は名前が C<@{*}> のように中かっこで
2446C<@*> のような「句読点」配列は名前が C<@{*}> のように中かっこで囲われている
4426囲われている場合にのみ展開されますが、配列 C<@_>, C<@+>, C<@-> は
2447場合にのみ展開されますが、特殊配列 C<@_>, C<@+>, C<@-> は中かっこなしでも
4427中かっこなしでも展開されます。
2448展開されます。
44282449
44292450=begin original
44302451
4431For double-quoted strings, the quoting from C<\Q> is applied after
2452You cannot include a literal C<$> or C<@> within a C<\Q> sequence.
4432interpolation and escapes are processed.
2453An unescaped C<$> or C<@> interpolates the corresponding variable,
2454while escaping will cause the literal string C<\$> to be inserted.
2455You'll need to write something like C<m/\Quser\E\@\Qhost/>.
44332456
44342457=end original
44352458
4436ダブルクォートされた文字列では、C<\Q> からのクォトは文字変換
2459C<\Q> ケンスの中にリテラルな C<$> や C<@> を入れるこはできません。
4437エスケープが処理された後適用されます。
2460エスケープされない C<$> や C<@> は対応する変数変換されます。
2461一方、エスケープすると、リテラルな文字列 C<\$> が挿入されます。
4439 "abc\Qfoo\tbar$s\Exyz"
4440
4441=begin original
4442
4443is equivalent to
4444
4445=end original
4446
4447は以下と等価です:
4448
4449 "abc" . quotemeta("foo\tbar$s") . "xyz"
4450
4451=begin original
4452
4453For the pattern of regex operators (C<qr//>, C<m//> and C<s///>),
4454the quoting from C<\Q> is applied after interpolation is processed,
4455but before escapes are processed. This allows the pattern to match
4456literally (except for C<$> and C<@>). For example, the following matches:
4457
4458=end original
4459
4460正規表現演算子 (C<qr//>, C<m//>, C<s///>) のパターンでは、C<\Q> による
4461クォートは変数展開が行われた後、エスケープが処理される前に適用されます。
4462これによりパターンを (C<$> と C<@> 以外は) リテラルにマッチングできます。
4463例えば、以下はマッチングします:
4464
4465 '\s\t' =~ /\Q\s\t/
4466
4467=begin original
4468
4469Because C<$> or C<@> trigger interpolation, you'll need to use something
4470like C</\Quser\E\@\Qhost/> to match them literally.
4471
4472=end original
4473
4474C<$> や C<@> は変換を引き起こすので、リテラルにマッチングさせるためには
44752462C<m/\Quser\E\@\Qhost/> などという風に書く必要があります。
44762463
44772464=begin original
44782465
44792466Patterns are subject to an additional level of interpretation as a
44802467regular expression. This is done as a second pass, after variables are
44812468interpolated, so that regular expressions may be incorporated into the
44822469pattern from the variables. If this is not what you want, use C<\Q> to
44832470interpolate a variable literally.
44842471
44852472=end original
44862473
44872474パターンはさらに、正規表現として展開が行なわれます。
4488これは、変数が展開された後の 2 回目のパスで行なわれるので、変数に正規表現を
2475これは、変数が展開された後の 2 回目のパスで行なわれるので、変数に
4489含めておき、パターンの中へ展開することができます。
2476正規表現を含めておき、パターンの中へ展開することができます。
44902477もし、そうしたくないのであれば、C<\Q> を使うと変数の内容を文字通りに
44912478展開することができます。
44922479
44932480=begin original
44942481
44952482Apart from the behavior described above, Perl does not expand
44962483multiple levels of interpolation. In particular, contrary to the
44972484expectations of shell programmers, back-quotes do I<NOT> interpolate
44982485within double quotes, nor do single quotes impede evaluation of
44992486variables when used within double quotes.
45002487
45012488=end original
45022489
45032490上記の振る舞いを除けば、Perl は 複数の段階を踏んで展開を行ないません。
4504特に、シェルのプログラマの期待とは裏腹に、バッククォートはダブルクォートの
2491特に、シェルのプログラマの期待とは裏腹に、
4505中では展開されませんし、シングルクォートがダブルクォートの中で使われても、
2492バッククォートはダブルクォートの中では展開されませんし、シングルクォートが
4506変数の展開を妨げることは I<ありません>。
2493ダブルクォートの中で使われても、変数の展開を妨げることは I<ありません>。
45072494
45082495=head2 Regexp Quote-Like Operators
45092496X<operator, regexp>
45102497
45112498(正規表現のクォート風の演算子)
45122499
45132500=begin original
45142501
45152502Here are the quote-like operators that apply to pattern
45162503matching and related activities.
45172504
45182505=end original
45192506
45202507以下はパターンマッチングと関連する行動に関するクォート風の演算子です。
45212508
45222509=over 8
45232510
4524=item C<qr/I<STRING>/msixpodualn>
2511=item qr/STRING/msixpo
45252512X<qr> X</i> X</m> X</o> X</s> X</x> X</p>
45262513
45272514=begin original
45282515
45292516This operator quotes (and possibly compiles) its I<STRING> as a regular
45302517expression. I<STRING> is interpolated the same way as I<PATTERN>
4531in C<m/I<PATTERN>/>. If C<"'"> is used as the delimiter, no variable
2518in C<m/PATTERN/>. If "'" is used as the delimiter, no interpolation
4532interpolation is done. Returns a Perl value which may be used instead of the
2519is done. Returns a Perl value which may be used instead of the
4533corresponding C</I<STRING>/msixpodualn> expression. The returned value is a
2520corresponding C</STRING/msixpo> expression. The returned value is a
4534normalized version of the original pattern. It magically differs from
2521normalized version of the original pattern. It magically differs from
4535a string containing the same characters: C<ref(qr/x/)> returns "Regexp";
2522a string containing the same characters: C<ref(qr/x/)> returns "Regexp",
4536however, dereferencing it is not well defined (you currently get the
2523even though dereferencing the result returns undef.
4537normalized version of the original pattern, but this may change).
45382524
45392525=end original
45402526
45412527この演算子は I<STRING> を正規表現としてクォートします
45422528(そして可能ならコンパイルします)。
4543I<STRING> は C<m/I<PATTERN>/> 内の I<PATTERN> と同様に文字変換されます。
2529I<STRING> は C<m/PATTERN/> 内の I<PATTERN> と同様に文字変換されます。
4544C<"'"> がデリミタとして使用された場合、変数展開は行われません。
2530"'" がデリミタとして使用された場合、文字は行われません。
4545対応する C</I<STRING>/msixpodualn> 表現の代わりに使われた Perl の値を返します。
2531対応する C</STRING/msixpo> 表現の代わりに使われた Perl の値を返します。
45462532返り値は元のパターンを正規化したものです。
45472533これは不思議なことに、同じ文字を含む文字列とは異なります:
4548C<ref(qr/x/)> は "Regexp" を返します; しかしこれのデリファレンス
2534C<ref(qr/x/)> は、結果をデリファレンスすると未定義値を返すにも関わらず、
4549定義されていません (現在の所元のパターンの正規化版を返しますが、これは
2535"Regexp" を返します
4550変更されるかもしれません)。
45512536
45522537=begin original
45532538
45542539For example,
45552540
45562541=end original
45572542
45582543例えば:
45592544
45602545 $rex = qr/my.STRING/is;
45612546 print $rex; # prints (?si-xm:my.STRING)
45622547 s/$rex/foo/;
45632548
45642549=begin original
45652550
45662551is equivalent to
45672552
45682553=end original
45692554
45702555は以下と等価です:
45712556
45722557 s/my.STRING/foo/is;
45732558
45742559=begin original
45752560
45762561The result may be used as a subpattern in a match:
45772562
45782563=end original
45792564
45802565結果はマッチのサブパターンとして使えます:
45812566
4582=begin original
4583
45842567 $re = qr/$pattern/;
4585 $string =~ /foo${re}bar/; # can be interpolated in other
2568 $string =~ /foo${re}bar/; # can be interpolated in other patterns
4586 # patterns
45872569 $string =~ $re; # or used standalone
45882570 $string =~ /$re/; # or this way
45892571
4590=end original
4591
4592 $re = qr/$pattern/;
4593 $string =~ /foo${re}bar/; # 他のパターンに展開できる
4594 $string =~ $re; # または単独で使う
4595 $string =~ /$re/; # またはこのようにする
4596
45972572=begin original
45982573
4599Since Perl may compile the pattern at the moment of execution of the C<qr()>
2574Since Perl may compile the pattern at the moment of execution of qr()
4600operator, using C<qr()> may have speed advantages in some situations,
2575operator, using qr() may have speed advantages in some situations,
4601notably if the result of C<qr()> is used standalone:
2576notably if the result of qr() is used standalone:
46022577
46032578=end original
46042579
4605Perl は C<qr()> 演算子を実行する瞬間にパターンをコンパイルするので、
2580Perl は qr() 演算子を実行する瞬間にパターンをコンパイルするので、
4606C<qr()> を使うことでいくつかの場面で速度的に有利になります;
2581qr() を使うことでいくつかの場面で速度的に有利になります
4607特に C<qr()> の結果が独立して使われる場合に有利になります。
2582特に qr() の結果が独立して使われる場合に有利になります。
46082583
46092584 sub match {
46102585 my $patterns = shift;
46112586 my @compiled = map qr/$_/i, @$patterns;
46122587 grep {
46132588 my $success = 0;
46142589 foreach my $pat (@compiled) {
46152590 $success = 1, last if /$pat/;
46162591 }
46172592 $success;
46182593 } @_;
46192594 }
46202595
46212596=begin original
46222597
46232598Precompilation of the pattern into an internal representation at
4624the moment of C<qr()> avoids the need to recompile the pattern every
2599the moment of qr() avoids a need to recompile the pattern every
46252600time a match C</$pat/> is attempted. (Perl has many other internal
46262601optimizations, but none would be triggered in the above example if
4627we did not use C<qr()> operator.)
2602we did not use qr() operator.)
46282603
46292604=end original
46302605
4631C<qr()> の時点でパターンを内部表現にプリコンパイルすることにより、
2606qr() の時点でパターンを内部表現にプリコンパイルすることにより、C</$pat/> を
4632C</$pat/> を試みる毎に毎回パターンを再コンパイルするのを
2607試みる毎に毎回パターンを再コンパイルするのを避けることができます
4633避けることができます
2608(Perl はその他にも多くの内部最適化を行いますが、上の例で qr() 演算子を
4634(Perl 他にも多くの内部最適化すが、上の例で C<qr()> 演算子を
2609使わなかった場合の最適化われせん)
4635使わなかった場合はどの最適化も行われません。)
46362610
46372611=begin original
46382612
4639Options (specified by the following modifiers) are:
2613Options are:
46402614
46412615=end original
46422616
4643(以下の修飾子で指定される)オプションは以下の通りです:
2617オプションは以下の通りです:
46442618
46452619=begin original
46462620
46472621 m Treat string as multiple lines.
46482622 s Treat string as single line. (Make . match a newline)
46492623 i Do case-insensitive pattern matching.
4650 x Use extended regular expressions; specifying two
2624 x Use extended regular expressions.
4651 x's means \t and the SPACE character are ignored within
4652 square-bracketed character classes
46532625 p When matching preserve a copy of the matched string so
4654 that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be
2626 that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be defined.
4655 defined (ignored starting in v5.20 as these are always
4656 defined starting in that release)
46572627 o Compile pattern only once.
4658 a ASCII-restrict: Use ASCII for \d, \s, \w and [[:posix:]]
4659 character classes; specifying two a's adds the further
4660 restriction that no ASCII character will match a
4661 non-ASCII one under /i.
4662 l Use the current run-time locale's rules.
4663 u Use Unicode rules.
4664 d Use Unicode or native charset, as in 5.12 and earlier.
4665 n Non-capture mode. Don't let () fill in $1, $2, etc...
46662628
46672629=end original
46682630
4669 m 文字列を複数行として扱う
2631 m 文字列を複数行として扱う
4670 s 文字列を一行として扱う (. が 改行にマッチングするようにする)
2632 s 文字列を一行として扱う (. が 改行にマッチングするようにする)
4671 i パターンマッチにおいて大文字小文字を区別しない
2633 i パターンマッチにおいて大文字小文字を区別しない
4672 x 拡張正規表現を使う; x を二つ指定すると、
2634 x 拡張正規表現を使う
4673 \t と SPACE 文字は大かっこ文字クラスの中では無視されます
46742635 p マッチング時にマッチングした文字列を保存するので、
46752636 ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} が定義される
4676 (このリリースから常に定義されので、v5.20 から無視される)
2637 o 一度だけコンパイルす
4677 o 一度だけコンパイルする。
4678 a ASCII 制限: \d, \s, \w, [[:posix:]] 文字クラスに ASCII を使う;
4679 a を二つ指定すると、/i で ASCII 文字が非 ASCII 文字に
4680 マッチングしないようなさらなる制限を追加する。
4681 l 現在の実行時ロケールの規則を使う。
4682 u Unicode の規則を使う。
4683 d 5.12 以降かどうかで、Unicode かネイティブな文字集合を使う。
4684 n 非捕捉モード。() で $1, $2 などを埋めない
46852638
46862639=begin original
46872640
46882641If a precompiled pattern is embedded in a larger pattern then the effect
4689of C<"msixpluadn"> will be propagated appropriately. The effect that the
2642of 'msixp' will be propagated appropriately. The effect of the 'o'
4690C</o> modifier has is not propagated, being restricted to those patterns
2643modifier has is not propagated, being restricted to those patterns
46912644explicitly using it.
46922645
46932646=end original
46942647
46952648プリコンパイルされたパターンがより大きいパターンに組み込まれている場合、
4696C<"msixpluadn"> の効果は適切に伝播します。
2649'msixp' の効果は適切に伝播します。
4697C</o> 修飾子の効果は伝播せず、明示的に使われたパターンに制限されます。
2650'o' 修飾子の効果は伝播せず、明示的に使われたパターンに制限されます。
46982651
46992652=begin original
47002653
4701The C</a>, C</d>, C</l>, and C</u> modifiers (added in Perl 5.14)
2654See L<perlre> for additional information on valid syntax for STRING, and
4702control the character set rules, but C</a> is the only one you are likely
2655for a detailed look at the semantics of regular expressions.
4703to want to specify explicitly; the other three are selected
4704automatically by various pragmas.
47052656
47062657=end original
47072658
4708(Perl 5.14 で追加された) C</a>, C</d>, C</l>, C</u> 修飾子は
2659STRING として有効な文法に関する追加の情報と正規表現の意味論に関する
4709文字集合の規則を制御しますが、明示的に指定したいと思いそうなものは
4710C</a> だけでしょう;
4711その他の三つはさまざまなプラグマによって自動的に選択されます。
4712
4713=begin original
4714
4715See L<perlre> for additional information on valid syntax for I<STRING>, and
4716for a detailed look at the semantics of regular expressions. In
4717particular, all modifiers except the largely obsolete C</o> are further
4718explained in L<perlre/Modifiers>. C</o> is described in the next section.
4719
4720=end original
4721
4722I<STRING> として有効な文法に関する追加の情報と、正規表現の意味論に関する
47232660詳細については、L<perlre> を参照してください。
4724特に、かなり古いものである C</o> 以外の全ての修飾子については
4725L<perlre/Modifiers> でさらに説明されています。
4726C</o> は次の節に記述されています。
47272661
4728=item C<m/I<PATTERN>/msixpodualngc>
2662=item m/PATTERN/msixpogc
47292663X<m> X<operator, match>
47302664X<regexp, options> X<regexp> X<regex, options> X<regex>
47312665X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c>
47322666
4733=item C</I<PATTERN>/msixpodualngc>
2667=item /PATTERN/msixpogc
47342668
47352669=begin original
47362670
47372671Searches a string for a pattern match, and in scalar context returns
47382672true if it succeeds, false if it fails. If no string is specified
4739via the C<=~> or C<!~> operator, the C<$_> string is searched. (The
2673via the C<=~> or C<!~> operator, the $_ string is searched. (The
47402674string specified with C<=~> need not be an lvalue--it may be the
47412675result of an expression evaluation, but remember the C<=~> binds
4742rather tightly.) See also L<perlre>.
2676rather tightly.) See also L<perlre>. See L<perllocale> for
2677discussion of additional considerations that apply when C<use locale>
2678is in effect.
47432679
47442680=end original
47452681
47462682パターンマッチで文字列検索を行ない、スカラコンテキストでは成功したときは真、
47472683失敗したときは偽を返します。
47482684C<=~> 演算子か C<!~> 演算子で検索対象の文字列を示さなかったときには、
47492685C<$_> の文字列が検索対象となります。
4750(C<=~> で指定される文字列は、左辺値である必要はありません--
2686(C<=~> で指定される文字列は、左辺値である必要はありません
47512687式を評価した結果でもかまいませんが、C<=~> の優先順位がいくぶん高いことに
47522688注意してください。)
47532689L<perlre> も参照してください。
2690C<use locale> が有効の場合の議論については L<perllocale> を参照して下さい。
47542691
47552692=begin original
47562693
4757Options are as described in C<qr//> above; in addition, the following match
2694Options are as described in C<qr//>; in addition, the following match
47582695process modifiers are available:
47592696
47602697=end original
47612698
4762オプションは上述した C<qr//> に記述されています; さらに、以下の
2699オプションは C<qr//> に記述されています; さらに、以下のマッチング処理
4763マッチング処理修飾子が利用可能です:
2700修飾子が利用可能です:
47642701
47652702=begin original
47662703
4767 g Match globally, i.e., find all occurrences.
2704 g Match globally, i.e., find all occurrences.
4768 c Do not reset search position on a failed match when /g is
2705 c Do not reset search position on a failed match when /g is in effect.
4769 in effect.
47702706
47712707=end original
47722708
4773 g グローバルにマッチング、つまり、すべてを探し出す
2709 g グローバルにマッチ、つまり、すべてを探し出す
4774 c /g が有効なとき、マッチングに失敗しても検索位置をリセットしない
2710 c /g が有効なとき、マッチングに失敗しても検索位置をリセットしない
47752711
47762712=begin original
47772713
4778If C<"/"> is the delimiter then the initial C<m> is optional. With the C<m>
2714If "/" is the delimiter then the initial C<m> is optional. With the C<m>
4779you can use any pair of non-whitespace (ASCII) characters
2715you can use any pair of non-alphanumeric, non-whitespace characters
47802716as delimiters. This is particularly useful for matching path names
4781that contain C<"/">, to avoid LTS (leaning toothpick syndrome). If C<"?"> is
2717that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is
4782the delimiter, then a match-only-once rule applies,
2718the delimiter, then the match-only-once rule of C<?PATTERN?> applies.
4783described in C<m?I<PATTERN>?> below. If C<"'"> (single quote) is the delimiter,
2719If "'" is the delimiter, no interpolation is performed on the PATTERN.
4784no variable interpolation is performed on the I<PATTERN>.
4785When using a delimiter character valid in an identifier, whitespace is required
4786after the C<m>.
47872720
47882721=end original
47892722
4790区切文字が C<"/"> のときには、最初の C<m> は付けても付けなくてもかまいません。
2723区切文字が "/" のときには、最初の C<m> は付けても付けなくてもかまいません。
4791C<m> を付けるときには、(ASCII の)空白でもない、任意の文字のペアを
2724C<m> を付けるときには、英数字でも空白でもない、任意の文字のペアを
47922725区切文字として使うことができます。
4793これは特に、C<"/"> を含むパス名にパターンマッチングを行なうときに、
2726これは特に、"/" を含むパス名にパターンマッチングを行なうときに、
47942727LTS (傾斜楊枝症候群) を避けるために便利でしょう。
4795"?" がデリミタなら後述する C<m?I<PATTERN>?>「一度だけマッチング」
2728"'" がデリミタの場合、PATTERN に対す展開は行われません。
4796ルールが適用されます。
4797C<"'"> (シングルクォート) がデリミタの場合、I<PATTERN> に対する変数展開は
4798行われません。
4799識別子として有効な区切り文字を使う場合、C<m> の後に空白が必要です。
48002729
48012730=begin original
48022731
4803I<PATTERN> may contain variables, which will be interpolated
2732PATTERN may contain variables, which will be interpolated (and the
4804every time the pattern search is evaluated, except
2733pattern recompiled) every time the pattern search is evaluated, except
48052734for when the delimiter is a single quote. (Note that C<$(>, C<$)>, and
48062735C<$|> are not interpolated because they look like end-of-string tests.)
4807Perl will not recompile the pattern unless an interpolated
2736If you want such a pattern to be compiled only once, add a C</o> after
4808variable that it contains changes. You can force Perl to skip the
2737the trailing delimiter. This avoids expensive run-time recompilations,
4809test and never recompile by adding a C</o> (which stands for "once")
2738and is useful when the value you are interpolating won't change over
4810after the trailing delimiter.
2739the life of the script. However, mentioning C</o> constitutes a promise
4811Once upon a time, Perl would recompile regular expressions
2740that you won't change the variables in the pattern. If you change them,
4812unnecessarily, and this modifier was useful to tell it not to do so, in the
2741Perl won't even notice. See also L<"qr/STRING/msixpo">.
4813interests of speed. But now, the only reasons to use C</o> are one of:
48142742
48152743=end original
48162744
4817I<PATTERN> には、変数が含まれていてもよく、パターンが評価されるごとに、
2745PATTERN には、変数が含まれていてもよく、パターンが評価されるごとに、
48182746(デリミタがシングルクォートでない限り)変数は展開され
48192747(パターンが再コンパイルされ) ます。
48202748(変数 C<$(>, C<$)>, C<$|> は文字列の終わりを調べるパターンであると
48212749解釈されるので、展開されません。)
4822Perl は展開された変数の値が変更されない限りパターンを再コンパイルしません。
2750パターンコンパイルされるのを 1 度だけにたい場合には、
4823デリミタ引き続いて C</o> ("once" 意味します) を追加することで、
2751終わりの区切文字の後に C</o> 修飾子付けます
4824テストを飛ばして再コンパイルしないようることができま
2752これにより、実行時に再コンパイルが頻繁起こることが避けられ、展開
4825昔々、Perl は不必要正規表現を再コンパイルたので、速度関心が
2753値がスクリプトの実行中変化場合有効なものとなります。
4826場合再コンイルしないようにするためにの修飾子は有用でした。
2754しかし、C</o> を付けことターンの中の変数を変更しないことを
4827しかし今では、C</o> を使う理由は以下いずれかだけです:
2755約束するものです
2756変更したとしても、Perl がそれに気付くことはありません。
2757L<"qr/STRING/msixpo"> も参照して下さい。
48282758
4829=over
2759=item The empty pattern //
48302760
4831=item 1
2761(空パターン //)
48322762
48332763=begin original
48342764
4835The variables are thousands of characters long and you know that they
2765If the PATTERN evaluates to the empty string, the last
4836don't change, and you need to wring out the last little bit of speed by
4837having Perl skip testing for that. (There is a maintenance penalty for
4838doing this, as mentioning C</o> constitutes a promise that you won't
4839change the variables in the pattern. If you do change them, Perl won't
4840even notice.)
4841
4842=end original
4843
4844変数が数千文字の長さで、これが変更されないことが分かっており、これに対する
4845テストを飛ばすことであともう少しだけ速度を稼ぐ必要がある。
4846(こうすることには保守上のペナルティがあります; なぜなら C</o> と
4847言及することでパターン内の変数を変更しないことを約束したことになるからです。
4848変更しても、Perl は気づきもしません。)
4849
4850=item 2
4851
4852=begin original
4853
4854you want the pattern to use the initial values of the variables
4855regardless of whether they change or not. (But there are saner ways
4856of accomplishing this than using C</o>.)
4857
4858=end original
4859
4860変数が変更されようが変更されまいが、変数の初期値を使ったパターンがほしい。
4861(しかしこれを達成するための、C</o> を使うよりもまともな方法があります。)
4862
4863=item 3
4864
4865=begin original
4866
4867If the pattern contains embedded code, such as
4868
4869=end original
4870
4871以下のようにパターンに組み込みコードが含まれている場合
4872
4873 use re 'eval';
4874 $code = 'foo(?{ $x })';
4875 /$code/
4876
4877=begin original
4878
4879then perl will recompile each time, even though the pattern string hasn't
4880changed, to ensure that the current value of C<$x> is seen each time.
4881Use C</o> if you want to avoid this.
4882
4883=end original
4884
4885C<$x> の現在の値を毎回確認するために、例えパターン文字列が
4886変更されていなくても、毎回再コンパイルされます。
4887これを避けたい場合は C</o> を使ってください。
4888
4889=back
4890
4891=begin original
4892
4893The bottom line is that using C</o> is almost never a good idea.
4894
4895=end original
4896
4897結論としては、C</o> を使うことがいい考えであることはほとんどありません。
4898
4899=item The empty pattern C<//>
4900
4901(空パターン C<//>)
4902
4903=begin original
4904
4905If the I<PATTERN> evaluates to the empty string, the last
49062766I<successfully> matched regular expression is used instead. In this
4907case, only the C<g> and C<c> flags on the empty pattern are honored; the
2767case, only the C<g> and C<c> flags on the empty pattern is honoured -
4908other flags are taken from the original pattern. If no match has
2768the other flags are taken from the original pattern. If no match has
49092769previously succeeded, this will (silently) act instead as a genuine
4910empty pattern (which will always match). Using a user supplied string as
2770empty pattern (which will always match).
4911a pattern has the risk that if the string is empty that it triggers the
4912"last successful match" behavior, which can be very confusing. In such
4913cases you are recommended to replace C<m/$pattern/> with
4914C<m/(?:$pattern)/> to avoid this behavior.
49152771
49162772=end original
49172773
4918I<PATTERN> を評価した結果が空文字列となった場合には、最後にマッチに
2774PATTERN を評価した結果が空文字列となった場合には、最後にマッチに
49192775I<成功した> 正規表現が、代わりに使われます。
4920この場合、空パターンに対して C<g> の C<c> フラグだけが有効です;
2776この場合、空パターンに対して C<g> の C<c> フラグだけが有効です -
49212777その他のフラグは元のパターンから取られます。
49222778以前に成功したマッチングがない場合、これは(暗黙に)真の空パターンとして
49232779動作します(つまり常にマッチングします)。
4924ユーザーが提供した文字列をパターンとして使うことは、
4925文字列が空だった場合に「最後に成功したマッチング」の振る舞いを引き起こし、
4926とても混乱するというリスクがあります。
4927このような場合では、この振る舞いを避けるために、
4928C<m/$pattern/> を C<m/(?:$pattern)/> に置き換えることを勧めます。
49292780
49302781=begin original
49312782
4932The last successful pattern may be accessed as a variable via
4933C<${^LAST_SUCCESSFUL_PATTERN}>. Matching against it, or the empty
4934pattern should have the same effect, with the exception that when there
4935is no last successful pattern the empty pattern will silently match,
4936whereas using the C<${^LAST_SUCCESSFUL_PATTERN}> variable will produce
4937undefined warnings (if warnings are enabled). You can check
4938C<defined(${^LAST_SUCCESSFUL_PATTERN})> to test if there is a "last
4939successful match" in the current scope.
4940
4941=end original
4942
4943最後に成功したパターンは、 C<${^LAST_SUCCESSFUL_PATTERN}> 変数経由で
4944アクセスされます。
4945これと、空文字列に対するマッチングは同じ効果を持ちますが、
4946空文字列が暗黙にマッチングする最後に成功したパターンはないけれども、
4947C<${^LAST_SUCCESSFUL_PATTERN}> 変数を使うと(警告がが有効なら)
4948未定義警告発生するという例外があります。
4949現在のスコープで「最後に成功したマッチング」が
4950あるかどうかをテストにするには、
4951C<defined(${^LAST_SUCCESSFUL_PATTERN})> とします。
4952
4953=begin original
4954
49552783Note that it's possible to confuse Perl into thinking C<//> (the empty
49562784regex) is really C<//> (the defined-or operator). Perl is usually pretty
49572785good about this, but some pathological cases might trigger this, such as
4958C<$x///> (is that S<C<($x) / (//)>> or S<C<$x // />>?) and S<C<print $fh //>>
2786C<$a///> (is that C<($a) / (//)> or C<$a // />?) and C<print $fh //>
4959(S<C<print $fh(//>> or S<C<print($fh //>>?). In all of these examples, Perl
2787(C<print $fh(//> or C<print($fh //>?). In all of these examples, Perl
49602788will assume you meant defined-or. If you meant the empty regex, just
49612789use parentheses or spaces to disambiguate, or even prefix the empty
49622790regex with an C<m> (so C<//> becomes C<m//>).
49632791
49642792=end original
49652793
49662794Perl が C<//> (空正規表現) と C<//> (定義性和演算子) を混同する
49672795可能性があることに注意してください。
4968Perl は普通これをかなりうまく処理しますが、C<$x///> (S<C<($x) / (//)>>
2796Perl は普通これをかなりうまく処理しますが、C<$a///> (C<($a) / (//)>
4969それとも S<C<$x // />>?) や S<C<print $fh //>> (S<C<print $fh(//>>
2797それとも C<$a // />?) や C<print $fh //> (C<print $fh(//> それとも
4970それとも S<C<print($fh //>>?) のような病的な状況ではこれが起こりえます。
2798C<print($fh //>?) のような病的な状況ではこれが起こりえます。
49712799これらの例の全てでは、Perl は定義性和を意味していると仮定します。
49722800もし空正規表現を意味したいなら、あいまいさをなくすために単に
49732801かっこや空白を使うか、空正規表現に接頭辞 C<m> を付けてください
49742802(つまり C<//> を C<m//> にします)。
49752803
49762804=item Matching in list context
49772805
49782806(リストコンテキストでのマッチング)
49792807
49802808=begin original
49812809
49822810If the C</g> option is not used, C<m//> in list context returns a
49832811list consisting of the subexpressions matched by the parentheses in the
4984pattern, that is, (C<$1>, C<$2>, C<$3>...) (Note that here C<$1> etc. are
2812pattern, i.e., (C<$1>, C<$2>, C<$3>...). (Note that here C<$1> etc. are
4985also set). When there are no parentheses in the pattern, the return
2813also set, and that this differs from Perl 4's behavior.) When there are
4986value is the list C<(1)> for success.
2814no parentheses in the pattern, the return value is the list C<(1)> for
4987With or without parentheses, an empty list is returned upon failure.
2815success. With or without parentheses, an empty list is returned upon
2816failure.
49882817
49892818=end original
49902819
4991C</g> オプションが使われなかった場合、リストコンテキストでのC<m//>は
2820C</g>オプションが使われなかった場合、リストコンテキストでのC<m//>は
49922821パターンの中の括弧で括られた部分列にマッチしたもので構成されるリストを
4993返します; これは、(C<$1>, C<$2>, C<$3>, ...) ということです
2822返します
4994(の場合、C<$1> なども設定されます)。
2823れは(C<$1>, C<$2>, C<$3>, ...) ということです
2824(この場合、C<$1> なども設定されます。
2825この点で Perl 4 の動作と違っています。)
49952826パターンに括弧がない場合は、返り値は成功時はリスト C<(1)> です。
49962827括弧のあるなしに関わらず、失敗時は空リストを返します。
49972828
49982829=begin original
49992830
50002831Examples:
50012832
50022833=end original
50032834
50042835例:
50052836
5006 open(TTY, "+</dev/tty")
2837 open(TTY, '/dev/tty');
5007 || die "can't access /dev/tty: $!";
2838 <TTY> =~ /^y/i && foo(); # do foo if desired
50082839
5009 <TTY> =~ /^y/i && foo(); # do foo if desired
2840 if (/Version: *([0-9.]*)/) { $version = $1; }
50102841
5011 if (/Version: *([0-9.]*)/) { $version = $1; }
2842 next if m#^/usr/spool/uucp#;
50122843
5013 next if m<#del>^/usr/spool/uucp#;
2844 <#ins> poor man's grep
2845 $arg = shift;
2846 while (<>) {
2847 print if /$arg/o; # compile only once
2848 }
50142849
5015 # poor man's grep
2850 if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
5016 $arg = shift;
5017 while (<>) {
5018 print if /$arg/o; # compile only once (no longer needed!)
5019 }
50202851
5021 if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
5022
50232852=begin original
50242853
5025This last example splits C<$foo> into the first two words and the
2854This last example splits $foo into the first two words and the
5026remainder of the line, and assigns those three fields to C<$F1>, C<$F2>, and
2855remainder of the line, and assigns those three fields to $F1, $F2, and
5027C<$Etc>. The conditional is true if any variables were assigned; that is,
2856$Etc. The conditional is true if any variables were assigned, i.e., if
5028if the pattern matched.
2857the pattern matched.
50292858
50302859=end original
50312860
5032最後の例は、C<$foo> を最初の 2 つの単語と行の残りに分解し、
2861最後の例は、$foo を最初の 2 つの単語と行の残りに分解し、
5033C<$F1>C<$F2>C<$Etc> に代入しています。
2862$F1 と $F2 と $Etc に代入しています。
50342863変数に代入されれば、すなわちパターンがマッチすれば、
50352864if の条件が真となります。
50362865
50372866=begin original
50382867
50392868The C</g> modifier specifies global pattern matching--that is,
50402869matching as many times as possible within the string. How it behaves
50412870depends on the context. In list context, it returns a list of the
50422871substrings matched by any capturing parentheses in the regular
50432872expression. If there are no parentheses, it returns a list of all
50442873the matched strings, as if there were parentheses around the whole
50452874pattern.
50462875
50472876=end original
50482877
50492878C</g> 修飾子は、グローバルなパターンマッチを指定するもので、
50502879文字列の中で可能な限りたくさんマッチを行ないます。
50512880この動作は、コンテキストに依存します。
50522881リストコンテキストでは、正規表現内の括弧付けされたものにマッチした
50532882部分文字列のリストが返されます。
50542883括弧がなければ、パターン全体を括弧で括っていたかのように、
50552884すべてのマッチした文字列のリストが返されます。
50562885
50572886=begin original
50582887
50592888In scalar context, each execution of C<m//g> finds the next match,
50602889returning true if it matches, and false if there is no further match.
5061The position after the last match can be read or set using the C<pos()>
2890The position after the last match can be read or set using the pos()
5062function; see L<perlfunc/pos>. A failed match normally resets the
2891function; see L<perlfunc/pos>. A failed match normally resets the
50632892search position to the beginning of the string, but you can avoid that
5064by adding the C</c> modifier (for example, C<m//gc>). Modifying the target
2893by adding the C</c> modifier (e.g. C<m//gc>). Modifying the target
50652894string also resets the search position.
50662895
50672896=end original
50682897
5069スカラコンテキストでは、C<m//g> を実行する毎に次のマッチを探します;
2898スカラコンテキストでは、C<m//g> を実行する毎に次のマッチを探します
50702899マッチした場合は真を返し、もうマッチしなくなったら偽を返します。
5071最後のマッチの位置は C<pos()> 関数で読み出しや設定ができます;
2900最後のマッチの位置は pos() 関数で読み出しや設定ができます
50722901L<perlfunc/pos> を参照して下さい。
50732902マッチに失敗すると通常は検索位置を文字列の先頭にリセットしますが、
5074C</c> 修飾子をつける(例えば C<m//gc>)ことでこれを防ぐことができます。
2903C</c> 修飾子をつける(つまり C<m//gc>)ことでこれを防ぐことができます。
50752904ターゲットとなる文字列が変更された場合も検索位置はリセットされます。
50762905
5077=item C<\G I<assertion>>
2906=item \G assertion
50782907
5079(C<\G アサート>)
2908(\G アサート)
50802909
50812910=begin original
50822911
50832912You can intermix C<m//g> matches with C<m/\G.../g>, where C<\G> is a
5084zero-width assertion that matches the exact position where the
2913zero-width assertion that matches the exact position where the previous
5085previous C<m//g>, if any, left off. Without the C</g> modifier, the
2914C<m//g>, if any, left off. Without the C</g> modifier, the C<\G> assertion
5086C<\G> assertion still anchors at C<pos()> as it was at the start of
2915still anchors at pos(), but the match is of course only attempted once.
5087the operation (see L<perlfunc/pos>), but the match is of course only
2916Using C<\G> without C</g> on a target string that has not previously had a
5088attempted once. Using C<\G> without C</g> on a target string that has
2917C</g> match applied to it is the same as using the C<\A> assertion to match
5089not previously had a C</g> match applied to it is the same as using
2918the beginning of the string. Note also that, currently, C<\G> is only
5090the C<\A> assertion to match the beginning of the string. Note also
2919properly supported when anchored at the very beginning of the pattern.
5091that, currently, C<\G> is only properly supported when anchored at the
5092very beginning of the pattern.
50932920
50942921=end original
50952922
5096C<m//g> マッチを C<m/\G.../g> と混ぜることもできます; C<\G> は前回の
2923C<m//g> マッチを C<m/\G.../g> と混ぜることもできます
5097C<m//g> があればその同じ位置でマッチするゼロ文字幅のアサートです。
2924C<\G> は前回の C<m//g> があればその同じ位置でマッチする
5098C</g> 修飾子なし場合、C<\G> アサートは操作の最初としてC<pos()> に
2925ゼロ文字幅のアサートです。
5099固定ますが(L<perlfunc/pos> 参照) マッチはもちろん一度だけ試されます
2926C</g> 修飾子なの場合C<\G> アサートは pos() に固定しますが、
2927マッチはもちろん一度だけ試されます。
51002928以前に C</g> マッチを適用していないターゲット文字列に対して C</g> なしで
51012929C<\G> を使うと、文字列の先頭にマッチする C<\A> アサートを使うのと
51022930同じことになります。
51032931C<\G> は現在のところ、パターンのまさに先頭を示す場合にのみ正しく
51042932対応することにも注意してください。
51052933
51062934=begin original
51072935
51082936Examples:
51092937
51102938=end original
51112939
51122940例:
51132941
51142942 # list context
51152943 ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
51162944
51172945 # scalar context
5118 local $/ = "";
2946 $/ = "";
5119 while ($paragraph = <>) {
2947 while (defined($paragraph = <>)) {
5120 while ($paragraph =~ /\p{Ll}['")]*[.!?]+['")]*\s/g) {
2948 while ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) {
51212949 $sentences++;
51222950 }
51232951 }
5124 say $sentences;
2952 print "$sentences\n";
51252953
5126=begin original
2954 # using m//gc with \G
5127
5128Here's another way to check for sentences in a paragraph:
5129
5130=end original
5131
5132以下は段落内の文をチェックするためのもう一つの方法です:
5133
5134=begin original
5135
5136 my $sentence_rx = qr{
5137 (?: (?<= ^ ) | (?<= \s ) ) # after start-of-string or
5138 # whitespace
5139 \p{Lu} # capital letter
5140 .*? # a bunch of anything
5141 (?<= \S ) # that ends in non-
5142 # whitespace
5143 (?<! \b [DMS]r ) # but isn't a common abbr.
5144 (?<! \b Mrs )
5145 (?<! \b Sra )
5146 (?<! \b St )
5147 [.?!] # followed by a sentence
5148 # ender
5149 (?= $ | \s ) # in front of end-of-string
5150 # or whitespace
5151 }sx;
5152 local $/ = "";
5153 while (my $paragraph = <>) {
5154 say "NEW PARAGRAPH";
5155 my $count = 0;
5156 while ($paragraph =~ /($sentence_rx)/g) {
5157 printf "\tgot sentence %d: <%s>\n", ++$count, $1;
5158 }
5159 }
5160
5161=end original
5162
5163 my $sentence_rx = qr{
5164 (?: (?<= ^ ) | (?<= \s ) ) # 文字列の先頭か空白の後
5165 \p{Lu} # 大文字
5166 .*? # なんでも
5167 (?<= \S ) # 空白以外で終わる
5168 (?<! \b [DMS]r ) # しかし一般的な省略形ではない
5169 (?<! \b Mrs )
5170 (?<! \b Sra )
5171 (?<! \b St )
5172 [.?!] # 引き続いて文を終わらせるものが
5173 (?= $ | \s ) # 文字列の末尾か空白の前に
5174 }sx;
5175 local $/ = "";
5176 while (my $paragraph = <>) {
5177 say "NEW PARAGRAPH";
5178 my $count = 0;
5179 while ($paragraph =~ /($sentence_rx)/g) {
5180 printf "\tgot sentence %d: <%s>\n", ++$count, $1;
5181 }
5182 }
5183
5184=begin original
5185
5186Here's how to use C<m//gc> with C<\G>:
5187
5188=end original
5189
5190以下は C<m//gc> を C<\G> で使う方法です:
5191
51922955 $_ = "ppooqppqq";
51932956 while ($i++ < 2) {
51942957 print "1: '";
51952958 print $1 while /(o)/gc; print "', pos=", pos, "\n";
51962959 print "2: '";
51972960 print $1 if /\G(q)/gc; print "', pos=", pos, "\n";
51982961 print "3: '";
51992962 print $1 while /(p)/gc; print "', pos=", pos, "\n";
52002963 }
52012964 print "Final: '$1', pos=",pos,"\n" if /\G(.)/;
52022965
52032966=begin original
52042967
52052968The last example should print:
52062969
52072970=end original
52082971
52092972最後のものは以下のものを表示するはずです:
52102973
52112974 1: 'oo', pos=4
52122975 2: 'q', pos=5
52132976 3: 'pp', pos=7
52142977 1: '', pos=7
52152978 2: 'q', pos=8
52162979 3: '', pos=8
52172980 Final: 'q', pos=8
52182981
52192982=begin original
52202983
52212984Notice that the final match matched C<q> instead of C<p>, which a match
5222without the C<\G> anchor would have done. Also note that the final match
2985without the C<\G> anchor would have done. Also note that the final match
5223did not update C<pos>. C<pos> is only updated on a C</g> match. If the
2986did not update C<pos> -- C<pos> is only updated on a C</g> match. If the
52242987final match did indeed match C<p>, it's a good bet that you're running an
5225ancient (pre-5.6.0) version of Perl.
2988older (pre-5.6.0) Perl.
52262989
52272990=end original
52282991
52292992C<\G> なしでのマッチが行われたため、最後のマッチでは C<p> ではなく
52302993C<q> がマッチすることに注意してください。
52312994また、最後のマッチは C<pos> を更新しないことに注意してください。
52322995C<pos> は C</g> マッチでのみ更新されます。
52332996もし最後のマッチで C<p> にマッチした場合、かなりの確率で
5234とても古い (5.6.0 以前の) Perl で実行しているはずです。
2997古い (5.6.0 以前の) Perl で実行しているはずです。
52352998
52362999=begin original
52373000
52383001A useful idiom for C<lex>-like scanners is C</\G.../gc>. You can
52393002combine several regexps like this to process a string part-by-part,
52403003doing different actions depending on which regexp matched. Each
52413004regexp tries to match where the previous one leaves off.
52423005
52433006=end original
52443007
52453008C<lex> 風にスキャンするために便利な指定は C</\G.../gc> です。
5246文字列を部分ごとに処理するためにいくつかの正規表現をつなげて、どの正規表現に
3009文字列を部分ごとに処理するためにいくつかの正規表現をつなげて、どの
5247マッチングしたかによって異なる処理をすることができます。
3010正規表現にマッチしたかによって異なる処理をすることができます。
5248それぞれの正規表現は前の正規表現が飛ばした部分に対してマッチングを試みます。
3011それぞれの正規表現は前の正規表現が飛ばした部分に対して
3012マッチを試みます。
52493013
52503014 $_ = <<'EOL';
5251 $url = URI::URL->new( "http://example.com/" );
3015 $url = URI::URL->new( "http://www/" ); die if $url eq "xXx";
5252 die if $url eq "xXx";
52533016 EOL
3017 LOOP:
3018 {
3019 print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
3020 print(" lowercase"), redo LOOP if /\G[a-z]+\b[,.;]?\s*/gc;
3021 print(" UPPERCASE"), redo LOOP if /\G[A-Z]+\b[,.;]?\s*/gc;
3022 print(" Capitalized"), redo LOOP if /\G[A-Z][a-z]+\b[,.;]?\s*/gc;
3023 print(" MiXeD"), redo LOOP if /\G[A-Za-z]+\b[,.;]?\s*/gc;
3024 print(" alphanumeric"), redo LOOP if /\G[A-Za-z0-9]+\b[,.;]?\s*/gc;
3025 print(" line-noise"), redo LOOP if /\G[^A-Za-z0-9]+/gc;
3026 print ". That's all!\n";
3027 }
52543028
5255 LOOP: {
5256 print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
5257 print(" lowercase"), redo LOOP
5258 if /\G\p{Ll}+\b[,.;]?\s*/gc;
5259 print(" UPPERCASE"), redo LOOP
5260 if /\G\p{Lu}+\b[,.;]?\s*/gc;
5261 print(" Capitalized"), redo LOOP
5262 if /\G\p{Lu}\p{Ll}+\b[,.;]?\s*/gc;
5263 print(" MiXeD"), redo LOOP if /\G\pL+\b[,.;]?\s*/gc;
5264 print(" alphanumeric"), redo LOOP
5265 if /\G[\p{Alpha}\pN]+\b[,.;]?\s*/gc;
5266 print(" line-noise"), redo LOOP if /\G\W+/gc;
5267 print ". That's all!\n";
5268 }
5269
52703029=begin original
52713030
52723031Here is the output (split into several lines):
52733032
52743033=end original
52753034
52763035出力は以下のようになります(何行かに分割しています):
52773036
5278 line-noise lowercase line-noise UPPERCASE line-noise UPPERCASE
3037 line-noise lowercase line-noise lowercase UPPERCASE line-noise
5279 line-noise lowercase line-noise lowercase line-noise lowercase
3038 UPPERCASE line-noise lowercase line-noise lowercase line-noise
5280 lowercase line-noise lowercase lowercase line-noise lowercase
3039 lowercase lowercase line-noise lowercase lowercase line-noise
5281 lowercase line-noise MiXeD line-noise. That's all!
3040 MiXeD line-noise. That's all!
52823041
5283=item C<m?I<PATTERN>?msixpodualngc>
3042=item ?PATTERN?
5284X<?> X<operator, match-once>
3043X<?>
52853044
52863045=begin original
52873046
5288This is just like the C<m/I<PATTERN>/> search, except that it matches
3047This is just like the C</pattern/> search, except that it matches only
5289only once between calls to the C<reset()> operator. This is a useful
3048once between calls to the reset() operator. This is a useful
52903049optimization when you want to see only the first occurrence of
5291something in each file of a set of files, for instance. Only C<m??>
3050something in each file of a set of files, for instance. Only C<??>
52923051patterns local to the current package are reset.
52933052
52943053=end original
52953054
5296これは、C<reset()> 演算子を呼び出すごとに 1 度だけしかマッチングしないことを
3055これは、reset() 演算子を呼び出すごとに 1 度だけしか
5297除いては C<m/I<PATTERN>/> による検索と全く同じです。
3056マッチしないことを除いては C</pattern/> による検索と全く同じです。
52983057たとえば、ファイルの集まりの中で個々のファイルについて、あるものを
52993058探すとき、最初の 1 つだけの存在がわかれば良いのであれば、この機能を
53003059使って最適化をはかることができます。
5301現在のパッケージにローカルとなっている C<m??> のパターンだけが
3060現在のパッケージにローカルとなっている C<??> のパターンだけが
53023061リセットされます。
53033062
53043063 while (<>) {
5305 if (m?^$?) {
3064 if (?^$?) {
53063065 # blank line between header and body
53073066 }
53083067 } continue {
5309 reset if eof; # clear m?? status for next file
3068 reset if eof; # clear ?? status for next file
53103069 }
53113070
53123071=begin original
53133072
5314Another example switched the first "latin1" encoding it finds
3073This usage is vaguely deprecated, which means it just might possibly
5315to "utf8" in a pod file:
3074be removed in some distant future version of Perl, perhaps somewhere
3075around the year 2168.
53163076
53173077=end original
53183078
5319は、pod ファイル中の最初の "latin1" エンコーディングを "utf8" に
3079方法は、あまりお勧めしません。
5320切り替えす:
3080Perl の遠い将来のバージョン(おそらく 2168 年頃)では削除されるかもしれせん。
53213081
5322 s//utf8/ if m? ^ =encoding \h+ \K latin1 ?x;
3082=item s/PATTERN/REPLACEMENT/msixpogce
3083X<substitute> X<substitution> X<replace> X<regexp, replace>
3084X<regexp, substitute> X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> X</e>
53233085
53243086=begin original
53253087
5326The match-once behavior is controlled by the match delimiter being
5327C<?>; with any other delimiter this is the normal C<m//> operator.
5328
5329=end original
5330
5331一度だけマッチングという振る舞いはマッチングデリミタが C<?> かどうかで
5332制御されます: その他のデリミタの場合はこれは通常の C<m//> 演算子です。
5333
5334=begin original
5335
5336In the past, the leading C<m> in C<m?I<PATTERN>?> was optional, but omitting it
5337would produce a deprecation warning. As of v5.22.0, omitting it produces a
5338syntax error. If you encounter this construct in older code, you can just add
5339C<m>.
5340
5341=end original
5342
5343過去には、C<m?I<PATTERN>?> の先頭の C<m> は省略可能でしたが、これを省略すると
5344廃止予定警告が出ていました。
5345v5.22.0 から、これを省略すると文法エラーが発生します。
5346もし古いコードでこの構文に遭遇した場合は、単に C<m> を追加してください。
5347
5348=item C<s/I<PATTERN>/I<REPLACEMENT>/msixpodualngcer>
5349X<s> X<substitute> X<substitution> X<replace> X<regexp, replace>
5350X<regexp, substitute> X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> X</e> X</r>
5351
5352=begin original
5353
53543088Searches a string for a pattern, and if found, replaces that pattern
53553089with the replacement text and returns the number of substitutions
5356made. Otherwise it returns false (a value that is both an empty string (C<"">)
3090made. Otherwise it returns false (specifically, the empty string).
5357and numeric zero (C<0>) as described in L</Relational Operators>).
53583091
53593092=end original
53603093
53613094文字列中でパターンを検索し、もし見つかれば、置換テキストで置き換え、
53623095置換した数を返します。
5363見つからなければ、偽 (空文字列 (C<"">) と数値のゼロ (C<0> の両方)
3096見つからなければ、偽 (具体的には、空文字列) を返します。
5364返します。
53653097
53663098=begin original
53673099
5368If the C</r> (non-destructive) option is used then it runs the
5369substitution on a copy of the string and instead of returning the
5370number of substitutions, it returns the copy whether or not a
5371substitution occurred. The original string is never changed when
5372C</r> is used. The copy will always be a plain string, even if the
5373input is an object or a tied variable.
5374
5375=end original
5376
5377C</r> (非破壊) オプションが使われると、文字列のコピーに対して置換が行われ、
5378置換された数ではなく、置換が行われたかどうかにかかわらずこのコピーが
5379返されます。
5380C</r> が使われた場合、元の文字列は決して変更されません。
5381コピーは、たとえ入力がオブジェクトや tie された変数でも、常に
5382プレーンな文字列です。
5383
5384=begin original
5385
53863100If no string is specified via the C<=~> or C<!~> operator, the C<$_>
5387variable is searched and modified. Unless the C</r> option is used,
3101variable is searched and modified. (The string specified with C<=~> must
5388the string specified must be a scalar variable, an array element, a
3102be scalar variable, an array element, a hash element, or an assignment
5389hash element, or an assignment to one of those; that is, some sort of
3103to one of those, i.e., an lvalue.)
5390scalar lvalue.
53913104
53923105=end original
53933106
53943107C<=~> 演算子や C<!~> 演算子によって文字列が指定されていなければ、
53953108変数 C<$_> が検索され、修正されます。
5396C</r> 指定されていない限り
3109(C<=~> 指定される文字列は、スカラ変数、配列要素、ハッシュ要素
5397C<=~> で指定され文字列は、スカラ変数、配列要素、ハッシュ要素、
3110は、これらへの代入式といった左辺値でなければなりません。)
5398あるいは、これらへの代入式といったある種の
5399スカラ左辺値でなければなりません。
54003111
54013112=begin original
54023113
5403If the delimiter chosen is a single quote, no variable interpolation is
3114If the delimiter chosen is a single quote, no interpolation is
5404done on either the I<PATTERN> or the I<REPLACEMENT>. Otherwise, if the
3115done on either the PATTERN or the REPLACEMENT. Otherwise, if the
5405I<PATTERN> contains a C<$> that looks like a variable rather than an
3116PATTERN contains a $ that looks like a variable rather than an
54063117end-of-string test, the variable will be interpolated into the pattern
54073118at run-time. If you want the pattern compiled only once the first time
54083119the variable is interpolated, use the C</o> option. If the pattern
54093120evaluates to the empty string, the last successfully executed regular
54103121expression is used instead. See L<perlre> for further explanation on these.
3122See L<perllocale> for discussion of additional considerations that apply
3123when C<use locale> is in effect.
54113124
54123125=end original
54133126
3127あとで述べますが、区切り文字はスラッシュとは限りません。
54143128シングルクォートを区切り文字として使った場合には、
5415I<PATTERN> にも I<REPLACEMENT> にも変数展開を行ないません。
3129PATTERN にも REPLACEMENT にも変数展開を行ないません。
5416それ以外の場合、文字列の最後を表わすものには見えない C<$>
3130それ以外の場合、文字列の最後を表わすものには見えない $ が
5417I<PATTERN> に含まれると、実行時に変数がパターン内に展開されます。
3131PATTERN に含まれると、実行時に変数がパターン内に展開されます。
54183132最初に変数が展開されるときにだけパターンのコンパイルを行ないたいときには、
54193133C</o> オプションを使ってください。
54203134パターンの評価結果が空文字列になった場合には、最後に成功した正規表現が
54213135代わりに使われます。
54223136これについてさらに詳しくは、L<perlre> を参照してください。
3137C<use locale> が有効の場合の議論については L<perllocale> を参照して下さい。
54233138
54243139=begin original
54253140
5426Options are as with C<m//> with the addition of the following replacement
3141Options are as with m// with the addition of the following replacement
54273142specific options:
54283143
54293144=end original
54303145
5431オプションは、C<m//> のものに加えて、以下の置換固有のものがあります:
3146オプションは、m// のものに加えて、以下の置換固有のものがあります:
54323147
54333148=begin original
54343149
54353150 e Evaluate the right side as an expression.
5436 ee Evaluate the right side as a string then eval the
3151 ee Evaluate the right side as a string then eval the result
5437 result.
5438 r Return substitution and leave the original string
5439 untouched.
54403152
54413153=end original
54423154
5443 e 式の右側の評価を行なう
3155 e 式の右側の評価を行なう
5444 ee 右側を文字列として評価して、その結果を評価する
3156 ee 右側を文字列として評価して、その結果を評価する
5445 r 置換した結果を返し、もとの文字列はそのままにする。
54463157
54473158=begin original
54483159
5449Any non-whitespace delimiter may replace the slashes. Add space after
3160Any non-alphanumeric, non-whitespace delimiter may replace the
5450the C<s> when using a character allowed in identifiers. If single quotes
3161slashes. If single quotes are used, no interpretation is done on the
5451are used, no interpretation is done on the replacement string (the C</e>
3162replacement string (the C</e> modifier overrides this, however). Unlike
5452modifier overrides this, however). Note that Perl treats backticks
3163Perl 4, Perl 5 treats backticks as normal delimiters; the replacement
5453as normal delimiters; the replacement text is not evaluated as a command.
3164text is not evaluated as a command. If the
5454If the I<PATTERN> is delimited by bracketing quotes, the I<REPLACEMENT> has
3165PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own
5455its own pair of quotes, which may or may not be bracketing quotes, for example,
3166pair of quotes, which may or may not be bracketing quotes, e.g.,
54563167C<s(foo)(bar)> or C<< s<foo>/bar/ >>. A C</e> will cause the
54573168replacement portion to be treated as a full-fledged Perl expression
54583169and evaluated right then and there. It is, however, syntax checked at
5459compile-time. A second C<e> modifier will cause the replacement portion
3170compile-time. A second C<e> modifier will cause the replacement portion
54603171to be C<eval>ed before being run as a Perl expression.
54613172
54623173=end original
54633174
5464空白ではない任意の区切り文字で、スラッシュを置き換えられます。
3175英数字、空白ではない任意の区切り文字で、スラッシュを置き換えられます。
5465識別子として許されている文字を使うときには C<s> の後に空白を
5466追加してください。
54673176先に述べたように、シングルクォートを使うと置換文字列での展開は
54683177されません (C</e>修飾子を使えば可能です)。
5469バッククォートを通常のデリミタとして扱うことに注意してくださ;
3178Perl 4 と違って、 Perl 5 はバッククォートを通常のデリミタとして扱います。
54703179置換テキストはコマンドとして評価されません。
5471I<PATTERN> を括弧類で括った場合には、I<REPLACEMENT> 用にもう一組の区切り文字を
3180PATTERN を括弧類で括った場合には、REPLACEMENT 用にもう一組の区切り文字を
5472用意します; これは、括弧類であっても、なくてもかまいません;
3181用意します
5473C<s(foo)(bar)> や C<< s<foo>/bar/ >>。
3182これは、括弧類であっても、なくてもかまいません; C<s(foo)(bar)> や
3183C<< s<foo>/bar/ >>。
54743184C</e> は置換文字列を完全な Perl の式として扱い、その場所で直ちに解釈します。
54753185しかし、これはコンパイル時に構文チェックされます。
54763186二番目の C<e> 修飾子を指定すると、置換部分がまず Perl の式として
54773187C<eval> されます。
54783188
54793189=begin original
54803190
54813191Examples:
54823192
54833193=end original
54843194
54853195例:
54863196
5487 s/\bgreen\b/mauve/g; # don't change wintergreen
3197 s/\bgreen\b/mauve/g; # don't change wintergreen
54883198
54893199 $path =~ s|/usr/bin|/usr/local/bin|;
54903200
54913201 s/Login: $foo/Login: $bar/; # run-time pattern
54923202
5493 ($foo = $bar) =~ s/this/that/; # copy first, then
3203 ($foo = $bar) =~ s/this/that/; # copy first, then change
5494 # change
5495 ($foo = "$bar") =~ s/this/that/; # convert to string,
5496 # copy, then change
5497 $foo = $bar =~ s/this/that/r; # Same as above using /r
5498 $foo = $bar =~ s/this/that/r
5499 =~ s/that/the other/r; # Chained substitutes
5500 # using /r
5501 @foo = map { s/this/that/r } @bar # /r is very useful in
5502 # maps
55033204
5504 $count = ($paragraph =~ s/Mister\b/Mr./g); # get change-cnt
3205 $count = ($paragraph =~ s/Mister\b/Mr./g); # get change-count
55053206
55063207 $_ = 'abc123xyz';
55073208 s/\d+/$&*2/e; # yields 'abc246xyz'
55083209 s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz'
55093210 s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz'
55103211
55113212 s/%(.)/$percent{$1}/g; # change percent escapes; no /e
55123213 s/%(.)/$percent{$1} || $&/ge; # expr now, so /e
55133214 s/^=(\w+)/pod($1)/ge; # use function call
55143215
5515 $_ = 'abc123xyz';
5516 $x = s/abc/def/r; # $x is 'def123xyz' and
5517 # $_ remains 'abc123xyz'.
5518
55193216 # expand variables in $_, but dynamics only, using
55203217 # symbolic dereferencing
55213218 s/\$(\w+)/${$1}/g;
55223219
55233220 # Add one to the value of any numbers in the string
55243221 s/(\d+)/1 + $1/eg;
55253222
5526 # Titlecase words in the last 30 characters only (presuming
5527 # that the substring doesn't start in the middle of a word)
5528 substr($str, -30) =~ s/\b(\p{Alpha})(\p{Alpha}*)\b/\u$1\L$2/g;
5529
55303223 # This will expand any embedded scalar variable
55313224 # (including lexicals) in $_ : First $1 is interpolated
55323225 # to the variable name, and then evaluated
55333226 s/(\$\w+)/$1/eeg;
55343227
55353228 # Delete (most) C comments.
55363229 $program =~ s {
55373230 /\* # Match the opening delimiter.
55383231 .*? # Match a minimal number of characters.
55393232 \*/ # Match the closing delimiter.
55403233 } []gsx;
55413234
5542 s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_,
3235 s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_, expensively
5543 # expensively
55443236
5545 for ($variable) { # trim whitespace in $variable,
3237 for ($variable) { # trim whitespace in $variable, cheap
5546 # cheap
55473238 s/^\s+//;
55483239 s/\s+$//;
55493240 }
55503241
55513242 s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields
55523243
5553 $foo !~ s/A/a/g; # Lowercase all A's in $foo; return
5554 # 0 if any were found and changed;
5555 # otherwise return 1
5556
55573244=begin original
55583245
5559Note the use of C<$> instead of C<\> in the last example. Unlike
3246Note the use of $ instead of \ in the last example. Unlike
5560B<sed>, we use the \<I<digit>> form only in the left hand side.
3247B<sed>, we use the \<I<digit>> form in only the left hand side.
55613248Anywhere else it's $<I<digit>>.
55623249
55633250=end original
55643251
5565最後の例で C<\> の代わりに C<$> を使っているのに注意してください。
3252最後の例で \ の代わりに $ を使っているのに注意してください。
55663253B<sed> と違って、\<I<数字>> の形式はパターンの方でのみ使用できます。
55673254その他の場所では、$<I<数字>> を使います。
55683255
55693256=begin original
55703257
55713258Occasionally, you can't use just a C</g> to get all the changes
55723259to occur that you might want. Here are two common cases:
55733260
55743261=end original
55753262
55763263ときには、C</g> を付けるだけでは、あなたが望んでいるような形で
55773264すべてを変更することができないことがあります。
55783265良くある例を 2 つ示します:
55793266
55803267 # put commas in the right places in an integer
55813268 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;
55823269
55833270 # expand tabs to 8-column spacing
55843271 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
55853272
5586=begin original
5587
5588X</c>While C<s///> accepts the C</c> flag, it has no effect beyond
5589producing a warning if warnings are enabled.
5590
5591=end original
5592
5593X</c>
5594C<s///> は C</c> フラグを受け付けますが、
5595警告が有効の場合に警告を出す以外の効果はありません。
5596
55973273=back
55983274
55993275=head2 Quote-Like Operators
56003276X<operator, quote-like>
56013277
56023278(クォート風演算子)
56033279
56043280=over 4
56053281
5606=item C<q/I<STRING>/>
3282=item q/STRING/
56073283X<q> X<quote, single> X<'> X<''>
56083284
5609=item C<'I<STRING>'>
3285=item 'STRING'
56103286
56113287=begin original
56123288
56133289A single-quoted, literal string. A backslash represents a backslash
56143290unless followed by the delimiter or another backslash, in which case
56153291the delimiter or backslash is interpolated.
56163292
56173293=end original
56183294
56193295シングルクォートされた、リテラル文字列です。
56203296バックスラッシュは、後ろに続くものが区切文字か、別のバックスラッシュで
5621ある場合を除いて単なるバックスラッシュです;
3297ある場合を除いて単なるバックスラッシュです
56223298区切文字やバックスラッシュが続く場合には、その区切文字自身もしくは
56233299バックスラッシュそのものが展開されます。
56243300
56253301 $foo = q!I said, "You said, 'She said it.'"!;
56263302 $bar = q('This is it.');
56273303 $baz = '\n'; # a two-character string
56283304
5629=item C<qq/I<STRING>/>
3305=item qq/STRING/
56303306X<qq> X<quote, double> X<"> X<"">
56313307
5632=item C<"I<STRING>">
3308=item "STRING"
56333309
56343310=begin original
56353311
56363312A double-quoted, interpolated string.
56373313
56383314=end original
56393315
56403316ダブルクォートされた、リテラル文字列です。
56413317
56423318 $_ .= qq
56433319 (*** The previous line contains the naughty word "$1".\n)
56443320 if /\b(tcl|java|python)\b/i; # :-)
56453321 $baz = "\n"; # a one-character string
56463322
5647=item C<qx/I<STRING>/>
3323=item qx/STRING/
56483324X<qx> X<`> X<``> X<backtick>
56493325
5650=item C<`I<STRING>`>
3326=item `STRING`
56513327
56523328=begin original
56533329
56543330A string which is (possibly) interpolated and then executed as a
5655system command, via F</bin/sh> or its equivalent if required. Shell
3331system command with C</bin/sh> or its equivalent. Shell wildcards,
5656wildcards, pipes, and redirections will be honored. Similarly to
3332pipes, and redirections will be honored. The collected standard
5657C<system>, if the string contains no shell metacharacters then it will
3333output of the command is returned; standard error is unaffected. In
5658executed directly. The collected standard output of the command is
3334scalar context, it comes back as a single (potentially multi-line)
5659returned; standard error is unaffected. In scalar context, it comes
3335string, or undef if the command failed. In list context, returns a
5660back as a single (potentially multi-line) string, or C<undef> if the
3336list of lines (however you've defined lines with $/ or
5661shell (or command) could not be started. In list context, returns a
3337$INPUT_RECORD_SEPARATOR), or an empty list if the command failed.
5662list of lines (however you've defined lines with C<$/> or
5663C<$INPUT_RECORD_SEPARATOR>), or an empty list if the shell (or command)
5664could not be started.
56653338
56663339=end original
56673340
5668展開され、必要なら F</bin/sh> またはそれと等価なものでシステムの
3341展開され、C</bin/sh> またはそれと等価なものでシステムのコマンドとして
5669コマンドとして実行される(であろう)文字列です。
3342実行される(であろう)文字列です。
56703343シェルのワイルドカード、パイプ、リダイレクトが有効です。
5671C<system> と同様文字列にシェルメタ文字含まていないときは、
3344そのコマンドの標準出力を集めたもの返さます。
5672直接実行され
3345標準エラーは影響を与えせん
5673そのコマンドの、標準出力を集めたものが返されます; 標準エラーは影響を
5674与えません。
56753346スカラコンテキストでは、(複数行を含むかもしれない)
56761 つの文字列が戻ってきます;
33471 つの文字列が戻ってきます
5677シェル (またはコマンド) 開始できなかったときは C<undef> を返します。
3348コマンドが失敗したときは未定義値を返します。
5678リストコンテキストでは、(C<$/> もしくは C<$INPUT_RECORD_SEPARATOR>
3349リストコンテキストでは、($/ もしくは $INPUT_RECORD_SEPARATOR を
5679どのように設定していても) 行のリストを返します;
3350どのように設定していても) 行のリストを返します
5680シェル (またはコマンド) 開始できなかったときは
3351コマンドが失敗したときは空リストを返します。
5681空リストを返します。
56823352
56833353=begin original
56843354
56853355Because backticks do not affect standard error, use shell file descriptor
56863356syntax (assuming the shell supports this) if you care to address this.
56873357To capture a command's STDERR and STDOUT together:
56883358
56893359=end original
56903360
56913361バッククォートは標準エラーには影響を与えないので、標準エラーを
56923362使いたい場合は(シェルが対応しているものとして)シェルのファイル記述子の
56933363文法を使ってください。
56943364コマンドの STDERR と STDOUT を共に取得したい場合は:
56953365
56963366 $output = `cmd 2>&1`;
56973367
56983368=begin original
56993369
57003370To capture a command's STDOUT but discard its STDERR:
57013371
57023372=end original
57033373
57043374コマンドの STDOUT は取得するが STDERR は捨てる場合は:
57053375
57063376 $output = `cmd 2>/dev/null`;
57073377
57083378=begin original
57093379
57103380To capture a command's STDERR but discard its STDOUT (ordering is
57113381important here):
57123382
57133383=end original
57143384
57153385コマンドの STDERR は取得するが STDOUT は捨てる場合は
57163386(ここでは順序が重要です):
57173387
57183388 $output = `cmd 2>&1 1>/dev/null`;
57193389
57203390=begin original
57213391
57223392To exchange a command's STDOUT and STDERR in order to capture the STDERR
57233393but leave its STDOUT to come out the old STDERR:
57243394
57253395=end original
57263396
57273397STDERR を取得するが、STDOUT は古い STDERR のために残しておくために
57283398STDOUT と STDERR を交換するには:
57293399
57303400 $output = `cmd 3>&1 1>&2 2>&3 3>&-`;
57313401
57323402=begin original
57333403
57343404To read both a command's STDOUT and its STDERR separately, it's easiest
57353405to redirect them separately to files, and then read from those files
57363406when the program is done:
57373407
57383408=end original
57393409
57403410コマンドの STDOUT と STDERR の両方を別々に読み込みたい場合、
57413411一番簡単な方法は別々のファイルにリダイレクトし、
57423412プログラムが終了してからそのファイルを読むことです:
57433413
57443414 system("program args 1>program.stdout 2>program.stderr");
57453415
57463416=begin original
57473417
57483418The STDIN filehandle used by the command is inherited from Perl's STDIN.
57493419For example:
57503420
57513421=end original
57523422
57533423コマンドによって使われる STDIN ファイルハンドルは Perl の STDIN を
57543424継承します。
57553425例えば:
57563426
5757 open(SPLAT, "stuff") || die "can't open stuff: $!";
3427 open BLAM, "blam" || die "Can't open: $!";
5758 open(STDIN, "<&SPLAT") || die "can't dupe SPLAT: $!";
3428 open STDIN, "<&BLAM";
5759 print STDOUT `sort`;
3429 print `sort`;
57603430
57613431=begin original
57623432
5763will print the sorted contents of the file named F<"stuff">.
3433will print the sorted contents of the file "blam".
57643434
57653435=end original
57663436
5767 F<"stuff"> という名前のファイルの内容をソートして表示します。
3437はファイル "blam" の内容をソートして表示します。
57683438
57693439=begin original
57703440
57713441Using single-quote as a delimiter protects the command from Perl's
57723442double-quote interpolation, passing it on to the shell instead:
57733443
57743444=end original
57753445
57763446シングルクォートをデリミタとして使うと Perl のダブルクォート展開から
57773447保護され、そのままシェルに渡されます:
57783448
57793449 $perl_info = qx(ps $$); # that's Perl's $$
57803450 $shell_info = qx'ps $$'; # that's the new shell's $$
57813451
57823452=begin original
57833453
57843454How that string gets evaluated is entirely subject to the command
57853455interpreter on your system. On most platforms, you will have to protect
57863456shell metacharacters if you want them treated literally. This is in
57873457practice difficult to do, as it's unclear how to escape which characters.
5788See L<perlsec> for a clean and safe example of a manual C<fork()> and C<exec()>
3458See L<perlsec> for a clean and safe example of a manual fork() and exec()
57893459to emulate backticks safely.
57903460
57913461=end original
57923462
57933463この文字列がどのように評価されるかは完全にシステムのコマンドインタプリタに
57943464依存します。
57953465ほとんどのプラットフォームでは、シェルのメタ文字をリテラルに
57963466扱ってほしい場合はそれを守る必要があります。
57973467文字をエスケープする方法が明確ではないので、これは理論的には難しいことです。
5798逆クォートを安全にエミュレートするために手動で C<fork()>C<exec()>
3468逆クォートを安全にエミュレートするために手動で fork() と exec() を
57993469行うためのきれいで安全な例については L<perlsec> を参照してください。
58003470
58013471=begin original
58023472
58033473On some platforms (notably DOS-like ones), the shell may not be
58043474capable of dealing with multiline commands, so putting newlines in
58053475the string may not get you what you want. You may be able to evaluate
58063476multiple commands in a single line by separating them with the command
5807separator character, if your shell supports that (for example, C<;> on
3477separator character, if your shell supports that (e.g. C<;> on many Unix
5808many Unix shells and C<&> on the Windows NT C<cmd> shell).
3478shells; C<&> on the Windows NT C<cmd> shell).
58093479
58103480=end original
58113481
58123482(特に DOS 風の)プラットフォームには、シェルが複数行のコマンドを扱うことが
58133483できないものがあるので、文字列に改行を入れるとあなたの望まない結果に
58143484なる場合があります。
58153485シェルが対応していれば、コマンド分割文字で分割することで
581634861 行に複数のコマンドを入れて解釈させることができます
58173487(この文字は、多くの Unix シェルでは C<;>、Windows NT C<cmd> シェルでは
58183488C<&> です)。
58193489
58203490=begin original
58213491
5822Perl will attempt to flush all files opened for
3492Beginning with v5.6.0, Perl will attempt to flush all files opened for
58233493output before starting the child process, but this may not be supported
58243494on some platforms (see L<perlport>). To be safe, you may need to set
5825C<$|> (C<$AUTOFLUSH> in C<L<English>>) or call the C<autoflush()> method of
3495C<$|> ($AUTOFLUSH in English) or call the C<autoflush()> method of
5826C<L<IO::Handle>> on any open handles.
3496C<IO::Handle> on any open handles.
58273497
58283498=end original
58293499
5830Perl は子プロセスの実行前に書き込み用に開いている全ての
3500v5.6.0 以降、Perl は子プロセスの実行前に書き込み用に開いている全ての
58313501ファイルをフラッシュしようとしますが、これに対応していない
58323502プラットフォームもあります(L<perlport> を参照してください)。
5833安全のためには、C<$|> (C<L<English>> モジュールでは C<$AUTOFLUSH>)を
3503安全のためには、C<$|> (English モジュールでは $AUTOFLUSH)をセットするか、
5834セットするか、開いている全てのハンドルに対して C<L<IO::Handle>>
3504開いている全てのハンドルに対して C<IO::Handle> の C<autoflush()> メソッドを
5835C<autoflush()> メソッドを呼び出す必要があります。
3505呼び出す必要があります。
58363506
58373507=begin original
58383508
58393509Beware that some command shells may place restrictions on the length
58403510of the command line. You must ensure your strings don't exceed this
58413511limit after any necessary interpolations. See the platform-specific
58423512release notes for more details about your particular environment.
58433513
58443514=end original
58453515
58463516コマンド行の長さに制限があるコマンドシェルがあることに注意してください。
58473517全ての必要な変換が行われた後、コマンド文字列がこの制限を越えないことを
58483518保障する必要があります。
58493519特定の環境に関するさらなる詳細についてはプラットフォーム固有の
58503520リリースノートを参照してください。
58513521
58523522=begin original
58533523
58543524Using this operator can lead to programs that are difficult to port,
58553525because the shell commands called vary between systems, and may in
58563526fact not be present at all. As one example, the C<type> command under
58573527the POSIX shell is very different from the C<type> command under DOS.
58583528That doesn't mean you should go out of your way to avoid backticks
58593529when they're the right way to get something done. Perl was made to be
58603530a glue language, and one of the things it glues together is commands.
58613531Just understand what you're getting yourself into.
58623532
58633533=end original
58643534
5865この演算子を使うと、プログラムの移殖が困難になります; 呼び出されるシェル
3535この演算子を使うと、プログラムの移殖が困難になります
5866コマンドはシステムによって異なり、実際全く存在しないこともあるからです。
3536呼び出されるシェルコマンドはシステムによって異なり、実際全く
3537存在しないこともあるからです。
58673538一つの例としては、POSIX シェルの C<type> コマンドは DOS の C<type> コマンドと
58683539大きく異なっています。
58693540これは、何かを為すために正しい方法として逆クォートを使うことを
58703541避けるべきであることを意味しません。
58713542Perl は接着剤のような言語として作られ、接着されるべきものの一つは
58723543コマンドです。
58733544単にあなたが何をしようとしているかを理解しておいてください。
58743545
58753546=begin original
58763547
5877Like C<system>, backticks put the child process exit code in C<$?>.
5878If you'd like to manually inspect failure, you can check all possible
5879failure modes by inspecting C<$?> like this:
5880
5881=end original
5882
5883C<system> シフトと同様、逆クォートは子プロセスの終了コードを C<$?> に
5884設定します。
5885手動で失敗を調査したい場合、次のように C<$?> を調べることによって
5886全てのあり得る失敗モードをチェックできます:
5887
5888 if ($? == -1) {
5889 print "failed to execute: $!\n";
5890 }
5891 elsif ($? & 127) {
5892 printf "child died with signal %d, %s coredump\n",
5893 ($? & 127), ($? & 128) ? 'with' : 'without';
5894 }
5895 else {
5896 printf "child exited with value %d\n", $? >> 8;
5897 }
5898
5899=begin original
5900
5901Use the L<open> pragma to control the I/O layers used when reading the
5902output of the command, for example:
5903
5904=end original
5905
5906コマンドの出力を読み込むのに使われる I/O 層を制御するには L<open> を使います;
5907例えば:
5908
5909 use open IN => ":encoding(UTF-8)";
5910 my $x = `cmd-producing-utf-8`;
5911
5912=begin original
5913
5914C<qx//> can also be called like a function with L<perlfunc/readpipe>.
5915
5916=end original
5917
5918C<qx//> はまた、L<perlfunc/readpipe> のような関数から呼び出されます。
5919
5920=begin original
5921
59223548See L</"I/O Operators"> for more discussion.
59233549
59243550=end original
59253551
59263552さらなる議論については L</"I/O Operators"> を参照して下さい。
59273553
5928=item C<qw/I<STRING>/>
3554=item qw/STRING/
59293555X<qw> X<quote, list> X<quote, words>
59303556
59313557=begin original
59323558
5933Evaluates to a list of the words extracted out of I<STRING>, using embedded
3559Evaluates to a list of the words extracted out of STRING, using embedded
59343560whitespace as the word delimiters. It can be understood as being roughly
59353561equivalent to:
59363562
59373563=end original
59383564
5939埋め込まれた空白を区切文字として、I<STRING> から抜き出した単語のリストを
3565埋め込まれた空白を区切文字として、STRING から抜き出した単語のリストを
59403566評価します。
59413567これは、以下の式と大体同じと考えられます:
59423568
5943 split(" ", q/STRING/);
3569 split(' ', q/STRING/);
59443570
59453571=begin original
59463572
5947the differences being that it only splits on ASCII whitespace,
3573the differences being that it generates a real list at compile time, and
5948generates a real list at compile time, and
59493574in scalar context it returns the last element in the list. So
59503575this expression:
59513576
59523577=end original
59533578
5954違いは、ASCII の空白でのみ分割し、実際のリストをコンパイル時に生成し、
3579違いは、実際のリストをコンパイル時に生成し、スカラコンテキストではリストの
5955スカラコンテキストではリストの最後の要素を返すことです。
3580最後の要素を返すことです。
59563581従って、以下の表現は:
59573582
59583583 qw(foo bar baz)
59593584
59603585=begin original
59613586
59623587is semantically equivalent to the list:
59633588
59643589=end original
59653590
59663591以下のリストと文法的に等価です。
59673592
5968 "foo", "bar", "baz"
3593 'foo', 'bar', 'baz'
59693594
59703595=begin original
59713596
59723597Some frequently seen examples:
59733598
59743599=end original
59753600
59763601よく行なわれる例としては以下のものです:
59773602
59783603 use POSIX qw( setlocale localeconv )
59793604 @EXPORT = qw( foo bar baz );
59803605
59813606=begin original
59823607
5983A common mistake is to try to separate the words with commas or to
3608A common mistake is to try to separate the words with comma or to
59843609put comments into a multi-line C<qw>-string. For this reason, the
5985S<C<use warnings>> pragma and the B<-w> switch (that is, the C<$^W> variable)
3610C<use warnings> pragma and the B<-w> switch (that is, the C<$^W> variable)
5986produces warnings if the I<STRING> contains the C<","> or the C<"#"> character.
3611produces warnings if the STRING contains the "," or the "#" character.
59873612
59883613=end original
59893614
59903615よくある間違いは、単語をカンマで区切ったり、複数行の C<qw> 文字列の中に
59913616コメントを書いたりすることです。
5992このために、S<C<usr warnings>> プラグマと B<-w> スイッチ
3617このために、C<usr warnings> プラグマと B<-w> スイッチ
5993(つまり、C<$^W> 変数) は I<STRING>C<",">C<"#"> の文字が入っていると
3618(つまり、C<$^W> 変数) は STRING に "," や "#" の文字が入っていると
59943619警告を出します。
59953620
5996=item C<tr/I<SEARCHLIST>/I<REPLACEMENTLIST>/cdsr>
3622=item tr/SEARCHLIST/REPLACEMENTLIST/cds
59973623X<tr> X<y> X<transliterate> X</c> X</d> X</s>
59983624
5999=item C<y/I<SEARCHLIST>/I<REPLACEMENTLIST>/cdsr>
3625=item y/SEARCHLIST/REPLACEMENTLIST/cds
60003626
60013627=begin original
60023628
6003Transliterates all occurrences of the characters found (or not found
3629Transliterates all occurrences of the characters found in the search list
6004if the C</c> modifier is specified) in the search list with the
3630with the corresponding character in the replacement list. It returns
6005positionally corresponding character in the replacement list, possibly
3631the number of characters replaced or deleted. If no string is
6006deleting some, depending on the modifiers specified. It returns the
3632specified via the =~ or !~ operator, the $_ string is transliterated. (The
6007number of characters replaced or deleted. If no string is specified via
3633string specified with =~ must be a scalar variable, an array element, a
6008the C<=~> or C<!~> operator, the C<$_> string is transliterated.
3634hash element, or an assignment to one of those, i.e., an lvalue.)
60093635
60103636=end original
60113637
6012検索リスト (SEARCHLIST) に含まれる
3638検索リスト (SEARCHLIST) に含まれる文字を、対応する置換リスト
6013(あるいは C</c> 修飾子が指定されている場合は含まれない)文字を、
3639(REPLACEMENTLIST)文字に変換します。
6014置換リスト (REPLACEMENTLIST) の位置的に対応する文字に変換します
6015(指定された修飾子によっては一部が削除されることもあります)。
60163640置換または削除が行なわれた、文字数を返します。
6017C<=~> 演算子や C<!~> 演算子で文字列が指定されていなければ、C<$_> の文字列が
3641=~ 演算子や =! 演算子で文字列が指定されていなければ、$_ の文字列が
60183642変換されます。
3643(=~ で指定される文字列は、スカラ変数、配列要素、ハッシュ要素、
3644あるいはこれらへの代入式といった左辺値でなければなりません。)
60193645
60203646=begin original
60213647
6022For B<sed> devotees, C<y> is provided as a synonym for C<tr>.
3648A character range may be specified with a hyphen, so C<tr/A-J/0-9/>
3649does the same replacement as C<tr/ACEGIBDFHJ/0246813579/>.
3650For B<sed> devotees, C<y> is provided as a synonym for C<tr>. If the
3651SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST has
3652its own pair of quotes, which may or may not be bracketing quotes,
3653e.g., C<tr[A-Z][a-z]> or C<tr(+\-*/)/ABCD/>.
60233654
60243655=end original
60253656
6026B<sed> 愛好者ために、C<y> が C<tr> の同義語とし提供されています。
3657文字範囲はハイフンを使っ指定できます。
3658C<tr/A-J/0-9/> は C<tr/ACEGIBDFHJ/0246813579/> と同じ置換を行います。
3659B<sed> の信仰者のために C<y> が C<tr> の同義語として提供されています。
3660SEARCHLIST を括弧類で括った場合には、
3661REPLACEMENTLIST 用に、もう一組の区切り文字を用意します。
3662これは、括弧類であっても、なくてもかまいません。
3663例: C<tr[A-Z][a-z]> や C<tr(+\-*/)/ABCD/>。
60273664
60283665=begin original
60293666
6030If the C</r> (non-destructive) option is present, a new copy of the string
3667Note that C<tr> does B<not> do regular expression character classes
6031is made and its characters transliterated, and this copy is returned no
3668such as C<\d> or C<[:lower:]>. The C<tr> operator is not equivalent to
6032matter whether it was modified or not: the original string is always
3669the tr(1) utility. If you want to map strings between lower/upper
6033left unchanged. The new copy is always a plain string, even if the input
3670cases, see L<perlfunc/lc> and L<perlfunc/uc>, and in general consider
6034string is an object or a tied variable.
3671using the C<s> operator if you need regular expressions.
60353672
60363673=end original
60373674
6038C</r> (非破壊) オプションがある文字列の新しいコピーが作られてその
3675C<tr> C<\d> や C<[:lower:]> いった正規表現文字クラスを
6039文字が変換され、変更されたかどうかに関らずのコピーが返れます:
3676B<使ない> とに注意してくだい。
6040元の文字列常に無変更残され
3677C<tr> 演算子 tr(1) ユーティリティと等価はありせん
6041しいコピーは、たとえ入力がオブジェクトや tie された変数でも、常に
3678文字列の大文字小文字をマップ場合は、
6042プレーンな文字列です
3679L<perlfunc/lc> と L<perlfunc/uc> を参照して下さい
3680また正規表現が必要な場合には一般的に C<s> 演算子を使うことを
3681考慮してみてください。
60433682
60443683=begin original
60453684
6046Unless the C</r> option is used, the string specified with C<=~> must be a
3685Note also that the whole range idea is rather unportable between
6047scalar variable, an array element, a hash element, or an assignment to one
3686character sets--and even within character sets they may cause results
6048of those; in other words, an lvalue.
3687you probably didn't expect. A sound principle is to use only ranges
3688that begin from and end at either alphabets of equal case (a-e, A-E),
3689or digits (0-4). Anything else is unsafe. If in doubt, spell out the
3690character sets in full.
60493691
60503692=end original
60513693
6052C</r> オプションが使われない限り、C<=~> で指定される文字、スカラ変数、
3694範囲指定という考え方は文字セットが異なる場合やや移植性に欠けることにも
6053配列要素、ハッシュ要素、あるはこれ代入式いった左辺値で
3695注意してくださ -- そして同じ文字セットでも恐く期待しているのとは違う
6054なければなりません
3696結果を引き起こすこともありま
3697健全な原則としては、範囲の最初と最後をどちらもアルファベット
6056=begin original
3698(大文字小文字も同じ)(a-e, A-E)にするか、どちらも数字にする(0-4)ことです。
3699それ以外は全て安全ではありません。
6058The characters delimitting I<SEARCHLIST> and I<REPLACEMENTLIST>
6059can be any printable character, not just forward slashes. If they
6060are single quotes (C<tr'I<SEARCHLIST>'I<REPLACEMENTLIST>'>), the only
6061interpolation is removal of C<\> from pairs of C<\\>; so hyphens are
6062interpreted literally rather than specifying a character range.
6063
6064=end original
6065
6066I<SEARCHLIST> と I<REPLACEMENTLIST> を区切る文字は、
6067スラッシュだけでなく、任意の表示文字が可能です。
6068それがシングルクォート (C<tr'I<SEARCHLIST>'I<REPLACEMENTLIST>'>) の場合、
6069C<\\> の組から C<\> を削除する変換のみが行われます; したがって、
6070ハイフンは文字の範囲ではなく、リテラルに解釈されます。
6071
6072=begin original
6073
6074Otherwise, a character range may be specified with a hyphen, so
6075C<tr/A-J/0-9/> does the same replacement as
6076C<tr/ACEGIBDFHJ/0246813579/>.
6077
6078=end original
6079
6080さもなければ、文字の範囲はハイフンを使って指定できます; C<tr/A-J/0-9/> は
6081C<tr/ACEGIBDFHJ/0246813579/> と同じ置換を行います。
6082
6083=begin original
6084
6085If the I<SEARCHLIST> is delimited by bracketing quotes, the
6086I<REPLACEMENTLIST> must have its own pair of quotes, which may or may
6087not be bracketing quotes; for example, C<tr(aeiouy)(yuoiea)> or
6088C<tr[+\-*/]"ABCD">. This final example shows a way to visually clarify
6089what is going on for people who are more familiar with regular
6090expression patterns than with C<tr>, and who may think forward slash
6091delimiters imply that C<tr> is more like a regular expression pattern
6092than it actually is. (Another option might be to use C<tr[...][...]>.)
6093
6094=end original
6095
6096I<SEARCHLIST> をかっこ類で括った場合には、I<REPLACEMENTLIST> 用に、
6097もう一組の区切り文字を用意しなければなりません;
6098これは、括弧類であっても、なくてもかまいません;
6099例えば、C<tr(aeiouy)(yuoiea)> や C<tr[+\-*/]"ABCD"> です。
6100最後の例は、C<tr> よりも正規表現パターンに親しんでいる人や、
6101前のスラッシュ区切り文字は、C<tr> が実際以上に正規表現パターンに
6102似ていることを暗示していると考える人に、何が起きているかを
6103視覚的に明確化しています。
6104(もう一つの選択肢は C<tr[...][...]> を使うことかもしれません。)
6105
6106=begin original
6107
6108C<tr> isn't fully like bracketed character classes, just
6109(significantly) more like them than it is to full patterns. For
6110example, characters appearing more than once in either list behave
6111differently here than in patterns, and C<tr> lists do not allow
6112backslashed character classes such as C<\d> or C<\pL>, nor variable
6113interpolation, so C<"$"> and C<"@"> are always treated as literals.
6114
6115=end original
6116
6117C<tr> は完全に大かっこ文字クラスと同様ではなく、
6118単に完全なパターンよりは(かなり)そちらにより似ていると言うだけです。
6119例えば、どちらかのリストに複数回文字が現れた場合、パターンと
6120異なる振る舞いをします; そして
6121C<tr> の一覧は C<\d> や C<\pL> といった逆スラッシュ文字クラスや
6122変数展開を許さないので、C<"$"> と C<"@"> は
6123常にリテラルとして扱われます。
6124
6125=begin original
6126
6127The allowed elements are literals plus C<\'> (meaning a single quote).
6128If the delimiters aren't single quotes, also allowed are any of the
6129escape sequences accepted in double-quoted strings. Escape sequence
6130details are in L<the table near the beginning of this section|/Quote and
6131Quote-like Operators>.
6132
6133=end original
6134
6135許される要素はリテラルと (シングルクォートを意味する) C<\'> です。
6136区切り文字がシングルクォートでない場合
6137ダブルクォート文字列の中で受け入れられるエスケープシーケンスも
6138許されます。
6139エスケープシーケンスの詳細は
6140L<この節の先頭付近の表|/Quote and
6141Quote-like Operators> にあります。
6142
6143=begin original
6144
6145A hyphen at the beginning or end, or preceded by a backslash is also
6146always considered a literal. Precede a delimiter character with a
6147backslash to allow it.
6148
6149=end original
6150
6151先頭、末尾、および逆スラッシュが前置されたハイフンもまた常に
6152リテラルとして扱われます。
6153それをするために逆スラッシュと共に区切り文字を前に置きます。
6154
6155=begin original
6156
6157The C<tr> operator is not equivalent to the C<L<tr(1)>> utility.
6158C<tr[a-z][A-Z]> will uppercase the 26 letters "a" through "z", but for
6159case changing not confined to ASCII, use L<C<lc>|perlfunc/lc>,
6160L<C<uc>|perlfunc/uc>, L<C<lcfirst>|perlfunc/lcfirst>,
6161L<C<ucfirst>|perlfunc/ucfirst> (all documented in L<perlfunc>), or the
6162L<substitution operator
6163C<sE<sol>I<PATTERN>E<sol>I<REPLACEMENT>E<sol>>|/sE<sol>PATTERNE<sol>REPLACEMENTE<sol>msixpodualngcer>
6164(with C<\U>, C<\u>, C<\L>, and C<\l> string-interpolation escapes in the
6165I<REPLACEMENT> portion).
6166
6167=end original
6168
6169C<tr> 演算子は C<L<tr(1)>> ユーティリティと等価ではありません。
6170C<tr[a-z][A-Z]> は "a" から "z" までの 26 文字を大文字にしますが、
6171ASCII の範囲外の大文字小文字を変更する場合は、
6172L<C<lc>|perlfunc/lc>, L<C<uc>|perlfunc/uc>,
6173L<C<lcfirst>|perlfunc/lcfirst>, L<C<ucfirst>|perlfunc/ucfirst>
6174(全て L<perlfunc> に文書化されています), あるいは
6175L<置換演算子 C<sE<sol>I<PATTERN>E<sol>I<REPLACEMENT>E<sol>>|/sE<sol>PATTERNE<sol>REPLACEMENTE<sol>msixpodualngcer>
6176(I<REPLACEMENT> 部での C<\U>, C<\u>, C<\L>, C<\l> 文字列変換エスケープ) を
6177使ってください。
6178
6179=begin original
6180
6181Most ranges are unportable between character sets, but certain ones
6182signal Perl to do special handling to make them portable. There are two
6183classes of portable ranges. The first are any subsets of the ranges
6184C<A-Z>, C<a-z>, and C<0-9>, when expressed as literal characters.
6185
6186=end original
6187
6188ほとんどの範囲は文字集合管で互換性がありませんが、一部のものは互換性を
6189持たせるために Perl が特別に扱います。
6190移植性のある範囲には二つのクラスがあります。
6191一つ目はリテラル文字として記述された C<A-Z>, C<a-z>, C<0-9> の部分集合です。
6192
6193 tr/h-k/H-K/
6194
6195=begin original
6196
6197capitalizes the letters C<"h">, C<"i">, C<"j">, and C<"k"> and nothing
6198else, no matter what the platform's character set is. In contrast, all
6199of
6200
6201=end original
6202
6203とすると、プラットフォームの文字集合が何であるかに関わらず、
6204文字 C<"h">, C<"i">, C<"j">, C<"k"> を大文字にして、それ以外は何もしません。
6205一方、次のようなもの全ては
6206
6207 tr/\x68-\x6B/\x48-\x4B/
6208 tr/h-\x6B/H-\x4B/
6209 tr/\x68-k/\x48-K/
6210
6211=begin original
6212
6213do the same capitalizations as the previous example when run on ASCII
6214platforms, but something completely different on EBCDIC ones.
6215
6216=end original
6217
6218ASCII プラットフォームで実行した場合は前述の例と同じ大文字かを行いますが、
6219EBCDIC プラットフォームでは全く違うことをします。
6220
6221=begin original
6222
6223The second class of portable ranges is invoked when one or both of the
6224range's end points are expressed as C<\N{...}>
6225
6226=end original
6227
6228移植性のある範囲の二つ目のクラスは、範囲の片方または両方の端が
6229C<\N{...}> として記述された場合です
6230
6231 $string =~ tr/\N{U+20}-\N{U+7E}//d;
6232
6233=begin original
6234
6235removes from C<$string> all the platform's characters which are
6236equivalent to any of Unicode U+0020, U+0021, ... U+007D, U+007E. This
6237is a portable range, and has the same effect on every platform it is
6238run on. In this example, these are the ASCII
6239printable characters. So after this is run, C<$string> has only
6240controls and characters which have no ASCII equivalents.
6241
6242=end original
6243
6244これは、Unicode での U+0020, U+0021, ... U+007D, U+007E と等価な、
6245今のプラットフォームでの全ての文字を C<$string> から削除します。
6246これは移植性のある範囲で、実行される全てのプラットフォームで同じ効果が
6247あります。
6248この例では、これらは ASCII の表示文字です。
6249従ってこれを実行した後、C<$string> は制御文字および ASCII で等価なものがない
6250文字のみとなります。
6251
6252=begin original
6253
6254But, even for portable ranges, it is not generally obvious what is
6255included without having to look things up in the manual. A sound
6256principle is to use only ranges that both begin from, and end at, either
6257ASCII alphabetics of equal case (C<b-e>, C<B-E>), or digits (C<1-4>).
6258Anything else is unclear (and unportable unless C<\N{...}> is used). If
6259in doubt, spell out the character sets in full.
6260
6261=end original
6262
6263しかし、移植性のある範囲であっても、
6264マニュアルで調べる必要なしには、何が含まれるのかは
6265一般的に明らかではありません。
6266健全な原則としては、範囲の最初と最後をどちらも ASCII 英字
6267(大文字小文字も同じ)(C<b-e>, C<B-E>)にするか、どちらも数字にする(C<1-4>)
6268ことです。
6269それ以外は不明確です(そして C<\N{...}> が使われていない限り
6270移植性はありません)。
62713700疑わしいときは、文字セットを完全に書き出してください。
62723701
62733702=begin original
62743703
62753704Options:
62763705
62773706=end original
62783707
62793708オプションは以下の通りです:
62803709
62813710=begin original
62823711
62833712 c Complement the SEARCHLIST.
62843713 d Delete found but unreplaced characters.
6285 r Return the modified string and leave the original string
6286 untouched.
62873714 s Squash duplicate replaced characters.
62883715
62893716=end original
62903717
6291 c SEARCHLIST を補集合にする
3718 c SEARCHLIST を補集合にする
6292 d 見つかったが置換されなかった文字を削除する
3719 d 見つかったが置換されなかった文字を削除する
6293 r 修正し結果を返し、もとの文字列はそのままにする
3720 s 置換された文字が重なったとき圧縮する
6294 s 置換された文字が重なったときに圧縮する。
62953721
6296
62973722=begin original
62983723
6299If the C</d> modifier is specified, any characters specified by
3724If the C</c> modifier is specified, the SEARCHLIST character set
6300I<SEARCHLIST> not found in I<REPLACEMENTLIST> are deleted. (Note that
3725is complemented. If the C</d> modifier is specified, any characters
6301this is slightly more flexible than the behavior of some B<tr> programs,
3726specified by SEARCHLIST not found in REPLACEMENTLIST are deleted.
6302which delete anything they find in the I<SEARCHLIST>, period.)
3727(Note that this is slightly more flexible than the behavior of some
3728B<tr> programs, which delete anything they find in the SEARCHLIST,
3729period.) If the C</s> modifier is specified, sequences of characters
3730that were transliterated to the same character are squashed down
3731to a single instance of the character.
63033732
63043733=end original
63053734
6306C</d> 修飾子が指定されると、I<SEARCHLIST> に指定されて、
3735C</c> 修飾子が指定されると、SEARCHLIST には補集合が指定されたものと
6307I<REPLACEMENTLIST> に対応するものがない文字が削除されます。
3736解釈されます。
6308(こI<SEARCHLIST> で見つかったものを削除する、ただそだけのある種の
3737C</d> 修飾子が指定さると、SEARCHLIST に指定さ
3738REPLACEMENTLIST に対応するものがない文字が削除されます。
3739(これは、SEARCHLIST で見つかったものを削除する、ただそれだけの、ある種の
63093740B<tr> プログラムの動作よりと比べれば、いく分柔軟なものになっています。)
6310
6311=begin original
6312
6313If the C</s> modifier is specified, sequences of characters, all in a
6314row, that were transliterated to the same character are squashed down to
6315a single instance of that character.
6316
6317=end original
6318
63193741C</s> 修飾子が指定されると、同じ文字に文字変換された文字の並びを、
6320その文字 1 文字だけに圧縮します。
3742その文字 1 文字だけに圧縮します。 
63213743
6322 my $a = "aaabbbca";
6323 $a =~ tr/ab/dd/s; # $a now is "dcd"
6324
63253744=begin original
63263745
6327If the C</d> modifier is used, the I<REPLACEMENTLIST> is always interpreted
3746If the C</d> modifier is used, the REPLACEMENTLIST is always interpreted
6328exactly as specified. Otherwise, if the I<REPLACEMENTLIST> is shorter
3747exactly as specified. Otherwise, if the REPLACEMENTLIST is shorter
6329than the I<SEARCHLIST>, the final character, if any, is replicated until
3748than the SEARCHLIST, the final character is replicated till it is long
6330it is long enough. There won't be a final character if and only if the
3749enough. If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated.
6331I<REPLACEMENTLIST> is empty, in which case I<REPLACEMENTLIST> is
3750This latter is useful for counting characters in a class or for
6332copied from I<SEARCHLIST>. An empty I<REPLACEMENTLIST> is useful
3751squashing character sequences in a class.
6333for counting characters in a class, or for squashing character sequences
6334in a class.
63353752
63363753=end original
63373754
6338C</d> 修飾子が使われると、I<REPLACEMENTLIST> は、常に指定された通りに
3755C</d> 修飾子が使われると、REPLACEMENTLIST は、常に指定された通りに
63393756解釈されます。
6340もなけI<REPLACEMENTLIST>I<SEARCHLIST> よりも短い場合
3757C</d> が指定されない場合で、REPLACEMENTLIST が SEARCHLIST よりも短い
6341同じ長さになるまで、最後の文字が繰り返されているものとして扱われます。
3758同じ長さになるまで、REPLACEMENTLIST の最後の文字が
6342I<REPLACEMENTLIST> が空文字列のときには最後の文字とものはなく、
3759繰り返されてものとして扱われます。
6343その場合は I<REPLACEMENTLIST> I<SEARCHLIST> からコピーされます。
3760REPLACEMENTLIST が空文字列でのときにSEARCHLIST と同じになります。
6344空の I<REPLACEMENTLIST> は、ある文字クラスに含まれる文字数を数えるときや、
3761後者は、ある文字クラスに含まれる文字数を数えるときや、
63453762ある文字クラスの文字の並びを圧縮するようなときに便利です。
63463763
6347 tr/abcd// tr/abcd/abcd/
6348 tr/abcd/AB/ tr/abcd/ABBB/
6349 tr/abcd//d s/[abcd]//g
6350 tr/abcd/AB/d (tr/ab/AB/ + s/[cd]//g) - but run together
6351
63523764=begin original
63533765
6354If the C</c> modifier is specified, the characters to be transliterated
3766Examples:
6355are the ones NOT in I<SEARCHLIST>, that is, it is complemented. If
6356C</d> and/or C</s> are also specified, they apply to the complemented
6357I<SEARCHLIST>. Recall, that if I<REPLACEMENTLIST> is empty (except
6358under C</d>) a copy of I<SEARCHLIST> is used instead. That copy is made
6359after complementing under C</c>. I<SEARCHLIST> is sorted by code point
6360order after complementing, and any I<REPLACEMENTLIST> is applied to
6361that sorted result. This means that under C</c>, the order of the
6362characters specified in I<SEARCHLIST> is irrelevant. This can
6363lead to different results on EBCDIC systems if I<REPLACEMENTLIST>
6364contains more than one character, hence it is generally non-portable to
6365use C</c> with such a I<REPLACEMENTLIST>.
63663767
63673768=end original
63683769
6369C</c> 修飾子が指定されると、I<SEARCHLIST> に「ない」ものが文字変換されます;
3770例:
6370つまり、補集合です。
6371C</d> や C</s> も指定されている場合、これらは I<SEARCHLIST> の補集合に
6372適用されます。
6373I<REPLACEMENTLIST> が空の場合、(C</d> の下でなければ)
6374代わりに I<SEARCHLIST> のコピーが使われることを思い出してください。
6375このコピーは、C</c> の下の補集合化の後で行われます。
6376I<SEARCHLIST> は補集合化の後で符号位置の順序でソートされ、
6377I<REPLACEMENTLIST> はソートされた結果に対して適用されます。
6378つまり、C</c> の下では、I<SEARCHLIST> で指定された文字の順序は
6379無関係です。
6380I<REPLACEMENTLIST> に複数の文字が含まれている場合、
6381EBCDIC システムでは異なった結果を引き起こすことがあるので、
6382そのような I<REPLACEMENTLIST> に対して
6383C</c> を使うのは一般的に移植性がありません。
63843771
6385=begin original
3772 $ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case
63863773
6387Another way of describing the operation is this:
3774 $cnt = tr/*/*/; # count the stars in $_
6388If C</c> is specified, the I<SEARCHLIST> is sorted by code point order,
6389then complemented. If I<REPLACEMENTLIST> is empty and C</d> is not
6390specified, I<REPLACEMENTLIST> is replaced by a copy of I<SEARCHLIST> (as
6391modified under C</c>), and these potentially modified lists are used as
6392the basis for what follows. Any character in the target string that
6393isn't in I<SEARCHLIST> is passed through unchanged. Every other
6394character in the target string is replaced by the character in
6395I<REPLACEMENTLIST> that positionally corresponds to its mate in
6396I<SEARCHLIST>, except that under C</s>, the 2nd and following characters
6397are squeezed out in a sequence of characters in a row that all translate
6398to the same character. If I<SEARCHLIST> is longer than
6399I<REPLACEMENTLIST>, characters in the target string that match a
6400character in I<SEARCHLIST> that doesn't have a correspondence in
6401I<REPLACEMENTLIST> are either deleted from the target string if C</d> is
6402specified; or replaced by the final character in I<REPLACEMENTLIST> if
6403C</d> isn't specified.
64043775
6405=end original
3776 $cnt = $sky =~ tr/*/*/; # count the stars in $sky
64063777
6407この操作を説明するもう一つの方法は次のものです:
3778 $cnt = tr/0-9//; # count the digits in $_
6408C</c> が指定されると、I<SEARCHLIST> は符号位置順にソートされ、
6409それから補集合化されます。
6410I<REPLACEMENTLIST> が空で C</d> が指定されていないと、
6411I<REPLACEMENTLIST> は (C</c> で修正された) I<SEARCHLIST> のコピーに
6412置き換えられ、この変更されているかも知れないリストは引き続くものに対しての
6413基礎として使われます。
6414ターゲット文字列のうち、I<SEARCHLIST> ににない文字は変更されずに
6415そのままになります。
6416ターゲット文字列のその他のそれぞれの文字は、
6417I<SEARCHLIST> の中の文字に位置的に対応する、
6418I<REPLACEMENTLIST> の中の文字に置き換えられます;
6419C</s> の下では例外で、
6420連続して同じ文字に変換された文字並びの 2 文字目以降の文字は削除されます。
6421I<SEARCHLIST> が I<REPLACEMENTLIST> より長い場合、
6422I<SEARCHLIST> の文字のうち I<REPLACEMENTLIST> に対応するものが
6423ない文字にマッチングしたターゲット文字列の中の文字は、
6424C</d> が指定されていればターゲット文字列から削除されます;
6425C</d> が指定されていなければI<REPLACEMENTLIST> の最後の文字に
6426置き換えられます。
64273779
6428=begin original
3780 tr/a-zA-Z//s; # bookkeeper -> bokeper
64293781
6430Some examples:
3782 ($HOST = $host) =~ tr/a-z/A-Z/;
64313783
6432=end original
3784 tr/a-zA-Z/ /cs; # change non-alphas to single space
64333785
6434いくつかの例:
3786 tr [\200-\377]
3787 [\000-\177]; # delete 8th bit
64353788
6436 $ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case ASCII
6437
6438 $cnt = tr/*/*/; # count the stars in $_
6439 $cnt = tr/*//; # same thing
6440
6441 $cnt = $sky =~ tr/*/*/; # count the stars in $sky
6442 $cnt = $sky =~ tr/*//; # same thing
6443
6444 $cnt = $sky =~ tr/*//c; # count all the non-stars in $sky
6445 $cnt = $sky =~ tr/*/*/c; # same, but transliterate each non-star
6446 # into a star, leaving the already-stars
6447 # alone. Afterwards, everything in $sky
6448 # is a star.
6449
6450 $cnt = tr/0-9//; # count the ASCII digits in $_
6451
6452 tr/a-zA-Z//s; # bookkeeper -> bokeper
6453 tr/o/o/s; # bookkeeper -> bokkeeper
6454 tr/oe/oe/s; # bookkeeper -> bokkeper
6455 tr/oe//s; # bookkeeper -> bokkeper
6456 tr/oe/o/s; # bookkeeper -> bokkopor
6457
6458 ($HOST = $host) =~ tr/a-z/A-Z/;
6459 $HOST = $host =~ tr/a-z/A-Z/r; # same thing
6460
6461 $HOST = $host =~ tr/a-z/A-Z/r # chained with s///r
6462 =~ s/:/ -p/r;
6463
6464 tr/a-zA-Z/ /cs; # change non-alphas to single space
6465
6466 @stripped = map tr/a-zA-Z/ /csr, @original;
6467 # /r with map
6468
6469 tr [\200-\377]
6470 [\000-\177]; # wickedly delete 8th bit
6471
6472 $foo !~ tr/A/a/ # transliterate all the A's in $foo to 'a',
6473 # return 0 if any were found and changed.
6474 # Otherwise return 1
6475
64763789=begin original
64773790
64783791If multiple transliterations are given for a character, only the
64793792first one is used:
64803793
64813794=end original
64823795
64833796複数の文字変換が一つの文字について指定されると、最初のものだけが使われます。
64843797
6485 tr/AAA/XYZ/
3798 tr/AAA/XYZ/
64863799
64873800=begin original
64883801
64893802will transliterate any A to X.
64903803
64913804=end original
64923805
64933806は A を X に変換します。
64943807
64953808=begin original
64963809
64973810Because the transliteration table is built at compile time, neither
6498the I<SEARCHLIST> nor the I<REPLACEMENTLIST> are subjected to double quote
3811the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote
64993812interpolation. That means that if you want to use variables, you
6500must use an C<eval()>:
3813must use an eval():
65013814
65023815=end original
65033816
6504変換テーブルはコンパイル時に作られるので、I<SEARCHLIST>
3817変換テーブルはコンパイル時に作られるので、SEARCHLIST も
6505I<REPLACEMENTLIST> もダブルクォート展開の対象とはなりません。
3818REPLACEMENTLIST もダブルクォート展開の対象とはなりません。
6506変数を使いたい場合には、C<eval()> を使わなければならないということです:
3819変数を使いたい場合には、eval() を使わなければならないということです:
65073820
6508 eval "tr/$oldlist/$newlist/";
3821 eval "tr/$oldlist/$newlist/";
6509 die $@ if $@;
3822 die $@ if $@;
65103823
6511 eval "tr/$oldlist/$newlist/, 1" or die $@;
3824 eval "tr/$oldlist/$newlist/, 1" or die $@;
65123825
6513=item C<< <<I<EOF> >>
3826=item <<EOF
65143827X<here-doc> X<heredoc> X<here-document> X<<< << >>>
65153828
65163829=begin original
65173830
65183831A line-oriented form of quoting is based on the shell "here-document"
65193832syntax. Following a C<< << >> you specify a string to terminate
65203833the quoted material, and all lines following the current line down to
65213834the terminating string are the value of the item.
65223835
65233836=end original
65243837
65253838クォートの行指向形式は、シェルの「ヒアドキュメント」構文を基にしています。
65263839C<< << >> に引き続いて、クォートされるテキストを終了させる文字列を指定でき、
65273840現在の行の次の行から終端文字列までの全ての行がその項目の値となります。
65283841
65293842=begin original
65303843
6531Prefixing the terminating string with a C<~> specifies that you
6532want to use L</Indented Here-docs> (see below).
6533
6534=end original
6535
6536終端文字列に C<~> を前置することで、L</Indented Here-docs> (後述)を
6537使いたいことを指定します。
6538
6539=begin original
6540
65413844The terminating string may be either an identifier (a word), or some
65423845quoted text. An unquoted identifier works like double quotes.
65433846There may not be a space between the C<< << >> and the identifier,
6544unless the identifier is explicitly quoted. The terminating string
3847unless the identifier is explicitly quoted. (If you put a space it
6545must appear by itself (unquoted and with no surrounding whitespace)
3848will be treated as a null identifier, which is valid, and matches the
6546on the terminating line.
3849first empty line.) The terminating string must appear by itself
3850(unquoted and with no surrounding whitespace) on the terminating line.
65473851
65483852=end original
65493853
65503854終端文字列には、識別子(単語) かクォートされたテキストが使えます。
65513855クォートされていない識別子は、ダブルクォートのように扱われます。
65523856識別子がクォートされていない場合は、C<< << >> と識別子の間に
65533857空白を入れてはいけません。
3858(もし空白を入れると、空識別子として扱われます; これは有効で、
3859最初の空行にマッチするようになります。)
65543860終端文字列は終端行に単体で(クォートされず、前後にも空白なしで)
65553861現れなければなりません。
65563862
65573863=begin original
65583864
65593865If the terminating string is quoted, the type of quotes used determine
65603866the treatment of the text.
65613867
65623868=end original
65633869
65643870終端文字列がクォートされている場合には、そのクォートの種類によって、
65653871クォートされるテキストの扱い決められます。
65663872
65673873=over 4
65683874
65693875=item Double Quotes
65703876
6571(ダブルクォート)
6572
65733877=begin original
65743878
65753879Double quotes indicate that the text will be interpolated using exactly
65763880the same rules as normal double quoted strings.
65773881
65783882=end original
65793883
65803884ダブルクォートは、通常のダブルクォートされた文字列と全く同じ規則を使って
65813885変数展開されることを示します。
65823886
65833887 print <<EOF;
65843888 The price is $Price.
65853889 EOF
65863890
65873891 print << "EOF"; # same as above
65883892 The price is $Price.
65893893 EOF
65903894
65913895=item Single Quotes
65923896
6593(シングルクォート)
6594
65953897=begin original
65963898
65973899Single quotes indicate the text is to be treated literally with no
6598interpolation of its content. This is similar to single quoted
3900interpolation of its content. This is similar to single quoted
65993901strings except that backslashes have no special meaning, with C<\\>
66003902being treated as two backslashes and not one as they would in every
66013903other quoting construct.
66023904
66033905=end original
66043906
6605シングルクォートは、内容が展開されずにリテラルに扱われることを意味します。
3907シングルクォートは、内容が展開されずにリテラルに扱われることを
3908意味します。
66063909これはシングルクォート文字列と似ていますが、バックスラッシュは
66073910特別な意味を持たず、他の全てのクォート構造違って、C<\\> は
66083911二つのバックスラッシュとして扱われることが違います。
66093912
66103913=begin original
66113914
6612Just as in the shell, a backslashed bareword following the C<<< << >>>
6613means the same thing as a single-quoted string does:
6614
6615=end original
6616
6617シェルでの場合と同様、C<<< << >>> に引き続いてバックスラッシュ付きの
6618裸の単語があると、シングルクォートされた文字列と同じこととなります:
6619
6620 $cost = <<'VISTA'; # hasta la ...
6621 That'll be $10 please, ma'am.
6622 VISTA
6623
6624 $cost = <<\VISTA; # Same thing!
6625 That'll be $10 please, ma'am.
6626 VISTA
6627
6628=begin original
6629
66303915This is the only form of quoting in perl where there is no need
66313916to worry about escaping content, something that code generators
66323917can and do make good use of.
66333918
66343919=end original
66353920
66363921これは、perl において、内容のエスケープについて心配する必要のない
66373922唯一の形で、コードジェネレータがうまく使えるものです。
66383923
66393924=item Backticks
66403925
6641(逆スラッシュ)
6642
66433926=begin original
66443927
66453928The content of the here doc is treated just as it would be if the
6646string were embedded in backticks. Thus the content is interpolated
3929string were embedded in backticks. Thus the content is interpolated
66473930as though it were double quoted and then executed via the shell, with
66483931the results of the execution returned.
66493932
66503933=end original
66513934
6652ドキュメントのの内容は、文字列がバッククォートで
3935ドキュメントのの内容は、文字列がバッククォートで
66533936埋め込まれているかのように扱われます。
66543937したがって、その内容、はダブルクォートされているかのように変数展開され、
66553938その後シェル経由で実行され、実行された結果になります。
66563939
66573940 print << `EOC`; # execute command and get results
66583941 echo hi there
66593942 EOC
66603943
66613944=back
66623945
6663=over 4
6664
6665=item Indented Here-docs
6666
66673946=begin original
66683947
6669The here-doc modifier C<~> allows you to indent your here-docs to make
6670the code more readable:
6671
6672=end original
6673
6674ヒヤドキュメント修飾子 C<~> は、コードをより読みやすくするために
6675ヒヤドキュメントをインデントできるようにします:
6676
6677 if ($some_var) {
6678 print <<~EOF;
6679 This is a here-doc
6680 EOF
6681 }
6682
6683=begin original
6684
6685This will print...
6686
6687=end original
6688
6689これは次のものを...
6690
6691 This is a here-doc
6692
6693=begin original
6694
6695...with no leading whitespace.
6696
6697=end original
6698
6699...先頭の空白なしで表示します。
6700
6701=begin original
6702
6703The line containing the delimiter that marks the end of the here-doc
6704determines the indentation template for the whole thing. Compilation
6705croaks if any non-empty line inside the here-doc does not begin with the
6706precise indentation of the terminating line. (An empty line consists of
6707the single character "\n".) For example, suppose the terminating line
6708begins with a tab character followed by 4 space characters. Every
6709non-empty line in the here-doc must begin with a tab followed by 4
6710spaces. They are stripped from each line, and any leading white space
6711remaining on a line serves as the indentation for that line. Currently,
6712only the TAB and SPACE characters are treated as whitespace for this
6713purpose. Tabs and spaces may be mixed, but are matched exactly; tabs
6714remain tabs and are not expanded.
6715
6716=end original
6717
6718ヒヤドキュメントの末尾を記す区切り文字を含む行は、
6719全体のためのインデントテンプレートを決定します。
6720ヒヤドキュメントの非空行が末尾行と正確に同じインデントで始まっていない場合、
6721コンパイルは croak します。
6722(空行は単一の文字 "\n" からなります。)
6723例えば、終了行がタブ文字と、それに引き続いて四つのスペース文字で
6724始まっていたとします。
6725ヒヤドキュメントの全ての非空行はタブとそれに引き続く四つのスペースで
6726始まっていなければなりません。
6727それらは各行から取り除かれ、行に残った戦闘の空白は
6728その行のインデントとして動作します。
6729現在の所、この目的には TAB と SPACE の文字だけが空白として扱われます。
6730タブとスペースは混ぜることが出来ますが、正確に
6731マッチングしなければなりません;
6732タブはタブのままで、展開はされません。
6733
6734=begin original
6735
6736Additional beginning whitespace (beyond what preceded the
6737delimiter) will be preserved:
6738
6739=end original
6740
6741(区切り文字で指定された以上の)追加の先頭の空白は保存されます:
6742
6743 print <<~EOF;
6744 This text is not indented
6745 This text is indented with two spaces
6746 This text is indented with two tabs
6747 EOF
6748
6749=begin original
6750
6751Finally, the modifier may be used with all of the forms
6752mentioned above:
6753
6754=end original
6755
6756最後に、この修飾子は前述した全ての型式で使うことが出来ます:
6757
6758 <<~\EOF;
6759 <<~'EOF'
6760 <<~"EOF"
6761 <<~`EOF`
6762
6763=begin original
6764
6765And whitespace may be used between the C<~> and quoted delimiters:
6766
6767=end original
6768
6769そして空白は C<~> とクォートされた区切り文字の間にも使えます:
6770
6771 <<~ 'EOF'; # ... "EOF", `EOF`
6772
6773=back
6774
6775=begin original
6776
67773948It is possible to stack multiple here-docs in a row:
67783949
67793950=end original
67803951
6781複数のヒドキュメントを連続してスタックすることも可能です:
3952複数のヒドキュメントを連続してスタックすることも可能です:
67823953
67833954 print <<"foo", <<"bar"; # you can stack them
67843955 I said foo.
67853956 foo
67863957 I said bar.
67873958 bar
67883959
67893960 myfunc(<< "THIS", 23, <<'THAT');
67903961 Here's a line
67913962 or two.
67923963 THIS
67933964 and here's another.
67943965 THAT
67953966
67963967=begin original
67973968
67983969Just don't forget that you have to put a semicolon on the end
67993970to finish the statement, as Perl doesn't know you're not going to
68003971try to do this:
68013972
68023973=end original
68033974
68043975以下のようなことをしたいのではないということが Perl にはわからないので、
68053976文を終わらせるためには末尾にセミコロンをつけなければならないことを
68063977忘れないで下さい:
68073978
68083979 print <<ABC
68093980 179231
68103981 ABC
68113982 + 20;
68123983
68133984=begin original
68143985
68153986If you want to remove the line terminator from your here-docs,
68163987use C<chomp()>.
68173988
68183989=end original
68193990
6820ドキュメントから行終端子を除去したい場合は、C<chomp()> を使ってください。
3991ドキュメントから行終端子を除去したい場合は、C<chomp()> を使ってください。
68213992
68223993 chomp($string = <<'END');
68233994 This is a string.
68243995 END
68253996
68263997=begin original
68273998
68283999If you want your here-docs to be indented with the rest of the code,
6829use the C<<< <<~FOO >>> construct described under L</Indented Here-docs>:
4000you'll need to remove leading whitespace from each line manually:
68304001
68314002=end original
68324003
68334004ヒアドキュメントをソースのほかの部分からインデントしたい場合、
6834L</Indented Here-docs> に記述されている C<<< <<~FOO >>> 構文を使ってださい:
4005各行の先頭の空白は手動で取り除必要があります:
68354006
6836 $quote = <<~'FINIS';
4007 ($quote = <<'FINIS') =~ s/^\s+//gm;
68374008 The Road goes ever on and on,
68384009 down from the door where it began.
6839 FINIS
4010 FINIS
68404011
68414012=begin original
68424013
68434014If you use a here-doc within a delimited construct, such as in C<s///eg>,
6844the quoted material must still come on the line following the
4015the quoted material must come on the lines following the final delimiter.
6845C<<< <<FOO >>> marker, which means it may be inside the delimited
4016So instead of
6846construct:
68474017
68484018=end original
68494019
68504020C<s///eg> のようなデリミタ構造の中でヒアドキュメントを使う場合、
6851クォートされたものは、やはり区切られた構造内側を意味するかもしれな
4021クォートされたものは最後デリミタに引き続くのとて来なけりません。
6852C<<< <<FOO >>> マーカーに引き続くものとしければなりません:
4022従っ、以下のようでは:
68534023
68544024 s/this/<<E . 'that'
68554025 the other
68564026 E
68574027 . 'more '/eg;
68584028
68594029=begin original
68604030
6861It works this way as of Perl 5.18. Historically, it was inconsistent, and
4031you have to write
6862you would have to write
68634032
68644033=end original
68654034
6866方法は Perl 5.18 ら動作しす。
4035以下ように書なければなりせん:
6867歴史的には、これは一貫性がなく、以下のように
68684036
68694037 s/this/<<E . 'that'
68704038 . 'more '/eg;
68714039 the other
68724040 E
68734041
68744042=begin original
68754043
6876outside of string evals.
4044If the terminating identifier is on the last line of the program, you
4045must be sure there is a newline after it; otherwise, Perl will give the
4046warning B<Can't find string terminator "END" anywhere before EOF...>.
68774047
68784048=end original
68794049
6880文字列 eval 外側で書く必要があります。
4050プログラム最後の行に終端識別子がある場合、その後には
4051改行がなければなりません; さもなければ、Perl は
4052B<Can't find string terminator "END" anywhere before EOF...> という
4053警告を出します。
68814054
68824055=begin original
68834056
6884Additionally, quoting rules for the end-of-string identifier are
4057Additionally, the quoting rules for the end of string identifier are not
6885unrelated to Perl's quoting rules. C<q()>, C<qq()>, and the like are not
4058related to Perl's quoting rules -- C<q()>, C<qq()>, and the like are not
68864059supported in place of C<''> and C<"">, and the only interpolation is for
68874060backslashing the quoting character:
68884061
68894062=end original
68904063
68914064さらに、文字列終端識別子に対するクォートルールは Perl のクォートルールとは
6892関係がありません
4065関係がありません -- C<q()>, C<qq()> および同種のものは C<''> や C<""> の
6893C<q()>, C<qq()> および同種のものは C<''> や C<""> の
68944066代わりのなるものには対応しておらず、文字をクォートするための
68954067バックスラッシュだけが展開されます:
68964068
68974069 print << "abc\"def";
68984070 testing...
68994071 abc"def
69004072
69014073=begin original
69024074
69034075Finally, quoted strings cannot span multiple lines. The general rule is
69044076that the identifier must be a string literal. Stick with that, and you
69054077should be safe.
69064078
69074079=end original
69084080
69094081最後に、クォートされた文字列は複数行にかかることはありません。
69104082識別子に関する一般的なルールは、文字列リテラルでなければならいことです。
69114083これに従っていれば、安全のはずです。
69124084
69134085=back
69144086
69154087=head2 Gory details of parsing quoted constructs
69164088X<quote, gory details>
69174089
69184090(クォートされた構造のパースに関する詳細)
69194091
69204092=begin original
69214093
69224094When presented with something that might have several different
69234095interpretations, Perl uses the B<DWIM> (that's "Do What I Mean")
69244096principle to pick the most probable interpretation. This strategy
69254097is so successful that Perl programmers often do not suspect the
69264098ambivalence of what they write. But from time to time, Perl's
69274099notions differ substantially from what the author honestly meant.
69284100
69294101=end original
69304102
69314103何か複数の解釈が可能な表現があった場合、Perl は最も確からしい解釈を
69324104選択するために B<DWIM> ("Do What I Mean")原則を使います。
69334105この戦略は非常に成功したので、Perl プログラマはしばしば
69344106自分が書いたものの矛盾を疑いません。
69354107しかし時間がたつにつれて、Perl の概念は作者が本当に意味していたものから
69364108かなり変わりました。
69374109
69384110=begin original
69394111
69404112This section hopes to clarify how Perl handles quoted constructs.
69414113Although the most common reason to learn this is to unravel labyrinthine
69424114regular expressions, because the initial steps of parsing are the
69434115same for all quoting operators, they are all discussed together.
69444116
69454117=end original
69464118
69474119この章では Perl がどのようにクォートされた構造を扱うかを
69484120明確にしようと思います。
69494121これを学ぼうとする最もよくある理由は正規表現の迷宮をほぐすためですが、
69504122パースの初期ステップは全てのクォート演算子で同じなので、全て同時に扱います。
69514123
69524124=begin original
69534125
69544126The most important Perl parsing rule is the first one discussed
69554127below: when processing a quoted construct, Perl first finds the end
69564128of that construct, then interprets its contents. If you understand
69574129this rule, you may skip the rest of this section on the first
69584130reading. The other rules are likely to contradict the user's
69594131expectations much less frequently than this first one.
69604132
69614133=end original
69624134
69634135Perl のパースに関するルールで最も重要なものは以下で述べているうち
6964最初のものです: つまり、クォートされた構造を処理するときは、Perl はまずその
4136最初のものです
6965構造の最後探してそれから中身を解釈しす。
4137つまり、クォートされた構造を処理するときはPerl はずその構造の
4138最後を探して、それから中身を解釈します。
69664139このルールがわかれば、とりあえずはこの章の残りは読み飛ばしてもかまいません。
69674140その他のルールは最初のルールに比べてユーザーの予想に反する頻度は
69684141はるかに少ないです。
69694142
69704143=begin original
69714144
69724145Some passes discussed below are performed concurrently, but because
69734146their results are the same, we consider them individually. For different
69744147quoting constructs, Perl performs different numbers of passes, from
69754148one to four, but these passes are always performed in the same order.
69764149
69774150=end original
69784151
69794152以下で議論するパスには同時に実行されるものもありますが、
69804153結果は同じことなので、別々に考えることにします。
69814154クォート構造の種類によって、Perl が実行するパスの数は
698241551 から 4 まで異なりますが、これらのパスは常に同じ順番で実行されます。
69834156
69844157=over 4
69854158
69864159=item Finding the end
69874160
69884161(最後を探す)
69894162
69904163=begin original
69914164
6992The first pass is finding the end of the quoted construct. This results
4165The first pass is finding the end of the quoted construct, where
6993in saving to a safe location a copy of the text (between the starting
4166the information about the delimiters is used in parsing.
6994and ending delimiters), normalized as necessary to avoid needing to know
4167During this search, text between the starting and ending delimiters
6995what the original delimiters were.
4168is copied to a safe location. The text copied gets delimiter-independent.
69964169
69974170=end original
69984171
6999最初のパスは、クォート構造の末尾を探すことです。
4172最初のパスは、クォート構造の末尾を探して、パース中に使うデリミタの情報が
7000れにより、(開始デリミタと終了デリミタ間の)テキストは安全な場所に
4173かを探すもです。
7001コピーされ元のデリミタが何だったかを知る必要がいよう正規化されます。
4174検索中に開始デリミタと終了デリミタの間のテキストは安全場所
4175コピーされます。
4176コピーされたテキストはデリミタに依存しません。
70024177
70034178=begin original
70044179
70054180If the construct is a here-doc, the ending delimiter is a line
7006that has a terminating string as the content. Therefore C<<<EOF> is
4181that has a terminating string as the content. Therefore C<<<EOF> is
70074182terminated by C<EOF> immediately followed by C<"\n"> and starting
70084183from the first column of the terminating line.
70094184When searching for the terminating line of a here-doc, nothing
7010is skipped. In other words, lines after the here-doc syntax
4185is skipped. In other words, lines after the here-doc syntax
70114186are compared with the terminating string line by line.
70124187
70134188=end original
70144189
7015構造がヒドキュメントの場合、終了デリミタは内容として終端文字列を持つ
4190構造がヒドキュメントの場合、終了デリミタは内容として終端文字列を持つ
70164191行です。
70174192従って、C<<<EOF> は、C<"\n"> の直後の、終端行の最初の列から始まる C<EOF> で
70184193終端します。
7019ドキュメントの終端行を探すとき、読み飛ばされるものはありません。
4194ドキュメントの終端行を探すとき、読み飛ばされるものはありません。
7020言い換えると、ヒドキュメント文法以降の行は、1 行毎に終端文字列と
4195言い換えると、ヒドキュメント文法以降の行は、1 行毎に終端文字列と
70214196比較されます。
70224197
70234198=begin original
70244199
70254200For the constructs except here-docs, single characters are used as starting
7026and ending delimiters. If the starting delimiter is an opening punctuation
4201and ending delimiters. If the starting delimiter is an opening punctuation
70274202(that is C<(>, C<[>, C<{>, or C<< < >>), the ending delimiter is the
70284203corresponding closing punctuation (that is C<)>, C<]>, C<}>, or C<< > >>).
70294204If the starting delimiter is an unpaired character like C</> or a closing
7030punctuation, the ending delimiter is the same as the starting delimiter.
4205punctuation, the ending delimiter is same as the starting delimiter.
70314206Therefore a C</> terminates a C<qq//> construct, while a C<]> terminates
7032both C<qq[]> and C<qq]]> constructs.
4207C<qq[]> and C<qq]]> constructs.
70334208
70344209=end original
70354210
7036ドキュメント以外の構造では、1 文字が開始デリミタと終了デリミタとして
4211ドキュメント以外の構造では、1 文字が開始デリミタと終了デリミタとして
70374212使われます。
70384213開始デリミタが組の開き文字(C<(>, C<[>, C<{>, C<< < >> のいずれか)の場合、
70394214終了デリミタは対応する組の閉じ文字(つまり C<)>, C<]>, C<}>, C<< > >>) です。
70404215開始デリミタが C</> 組になる文字や、組の閉じ文字の場合、終了デリミタは
70414216開始デリミタと同じです。
7042従って C</> は C<qq//> 構造を終端し、一方 C<]> は C<qq[]> と C<qq]]>
4217従って C</> は C<qq//> 構造を終端し、一方 C<]> は C<qq[]> と C<qq]]> 構造を
7043両方の構文を終端します。
4218終端します。
70444219
70454220=begin original
70464221
70474222When searching for single-character delimiters, escaped delimiters
7048and C<\\> are skipped. For example, while searching for terminating C</>,
4223and C<\\> are skipped. For example, while searching for terminating C</>,
70494224combinations of C<\\> and C<\/> are skipped. If the delimiters are
70504225bracketing, nested pairs are also skipped. For example, while searching
7051for a closing C<]> paired with the opening C<[>, combinations of C<\\>, C<\]>,
4226for closing C<]> paired with the opening C<[>, combinations of C<\\>, C<\]>,
70524227and C<\[> are all skipped, and nested C<[> and C<]> are skipped as well.
70534228However, when backslashes are used as the delimiters (like C<qq\\> and
70544229C<tr\\\>), nothing is skipped.
7055During the search for the end, backslashes that escape delimiters or
4230During the search for the end, backslashes that escape delimiters
7056other backslashes are removed (exactly speaking, they are not copied to the
4231are removed (exactly speaking, they are not copied to the safe location).
7057safe location).
70584232
70594233=end original
70604234
706142351 文字のデリミタを探す場合、エスケープされたデリミタと C<\\> は
70624236読み飛ばします。
70634237例えば、C</> を探しているときには、C<\\> と C<\/> の組み合わせを
70644238読み飛ばします。
70654239デリミタがかっこでくくられている場合は、ネストした組も読み飛ばされます。
70664240例えば、開きの C<[> と組になる閉じの C<]> を探しているときには、
70674241C<\\>, C<\]>, C<\[> の組み合わせをは全て読み飛ばし、
70684242さらにネストした C<[>, C<]> も読み飛ばします。
70694243しかし、(C<qq\\> や C<tr\\\> のように)バックスラッシュがデリミタとして
70704244使われた場合、何も読み飛ばしません。
7071末尾の検索中、デリミタやその他のバックスラッシュをエスケープ
4245末尾の検索中、デリミタをエスケープするバックスラッシュは除去されま
7072バックスラッシュは除去されます(述べたとおりに正確に、安全な場所に
4246(述べたとおりに正確に、安全な場所にコピーはされません)。
7073コピーはされません)。
70744247
70754248=begin original
70764249
70774250For constructs with three-part delimiters (C<s///>, C<y///>, and
70784251C<tr///>), the search is repeated once more.
7079If the first delimiter is not an opening punctuation, the three delimiters must
4252If the first delimiter is not an opening punctuation, three delimiters must
7080be the same, such as C<s!!!> and C<tr)))>,
4253be same such as C<s!!!> and C<tr)))>, in which case the second delimiter
7081in which case the second delimiter
70824254terminates the left part and starts the right part at once.
7083If the left part is delimited by bracketing punctuation (that is C<()>,
4255If the left part is delimited by bracketing punctuations (that is C<()>,
70844256C<[]>, C<{}>, or C<< <> >>), the right part needs another pair of
7085delimiters such as C<s(){}> and C<tr[]//>. In these cases, whitespace
4257delimiters such as C<s(){}> and C<tr[]//>. In these cases, whitespaces
7086and comments are allowed between the two parts, although the comment must follow
4258and comments are allowed between both parts, though the comment must follow
7087at least one whitespace character; otherwise a character expected as the
4259at least one whitespace; otherwise a character expected as the start of
7088start of the comment may be regarded as the starting delimiter of the right part.
4260the comment may be regarded as the starting delimiter of the right part.
70894261
70904262=end original
70914263
709242643 つのデリミタからなる構造 (C<s///>, C<y///>, C<tr///>) の場合、
70934265検索はもう一度繰り返されます。
70944266最初のデリミタが開きかっこでない場合、C<s!!!> and C<tr)))> のように
709542673 つのデリミタは同じでなければなりません;
70964268この場合、2 番目のデリミタが左側の終端と右側の開始を同時に行います。
70974269左側のデリミタがかっこを構成するもの (これは C<()>,
70984270C<[]>, C<{}>, C<< <> >> のいずれか) の場合、右側も
70994271C<s(){}> や C<tr[]//> のようなデリミタである必要があります。
71004272これらの場合、右側と左側の間には空白やコメントを置けますが、
7101コメントは少なくとも一つの空白文字の後である必要があります;
4273コメントは少なくとも一つの空白の後である必要があります;
71024274さもなければコメントの開始文字が右側の開始デリミタとして扱われてしまいます。
71034275
71044276=begin original
71054277
71064278During this search no attention is paid to the semantics of the construct.
71074279Thus:
71084280
71094281=end original
71104282
71114283検索する間、構造の文脈は考慮しません。
71124284従って:
71134285
71144286 "$hash{"$foo/$bar"}"
71154287
71164288=begin original
71174289
71184290or:
71194291
71204292=end original
71214293
71224294または:
71234295
71244296 m/
71254297 bar # NOT a comment, this slash / terminated m//!
71264298 /x
71274299
71284300=begin original
71294301
71304302do not form legal quoted expressions. The quoted part ends on the
71314303first C<"> and C</>, and the rest happens to be a syntax error.
71324304Because the slash that terminated C<m//> was followed by a C<SPACE>,
71334305the example above is not C<m//x>, but rather C<m//> with no C</x>
71344306modifier. So the embedded C<#> is interpreted as a literal C<#>.
71354307
71364308=end original
71374309
71384310は正しいクォート表現ではありません。
71394311クォートは最初の C<"> や C</> で終わりとなり、残りの部分は文法エラーと
71404312なります。
71414313C<m//> を終わらせているスラッシュの次に来ているのが C<空白> なので、
71424314上の例では C<m//x> ではなく、C</x> なしの C<m//> となります。
71434315従って、中にある C<#> はリテラルな C<#> として扱われます。
71444316
71454317=begin original
71464318
71474319Also no attention is paid to C<\c\> (multichar control char syntax) during
7148this search. Thus the second C<\> in C<qq/\c\/> is interpreted as a part
4320this search. Thus the second C<\> in C<qq/\c\/> is interpreted as a part
71494321of C<\/>, and the following C</> is not recognized as a delimiter.
71504322Instead, use C<\034> or C<\x1c> at the end of quoted constructs.
71514323
71524324=end original
71534325
71544326この検索の間、C<\c\> (マルチバイト文字制御文法)に注意は払われません。
71554327従って、C<qq/\c\/> の 2 番目の C<\> は C<\/> の一部として扱われ、
71564328引き続く C</> はデリミタとして認識されません。
71574329代わりに、クォート構造の末尾に C<\034> か C<\x1c> を使ってください。
71584330
71594331=item Interpolation
71604332X<interpolation>
71614333
71624334(展開)
71634335
71644336=begin original
71654337
71664338The next step is interpolation in the text obtained, which is now
71674339delimiter-independent. There are multiple cases.
71684340
71694341=end original
71704342
71714343次のステップは、得られた(デリミタに依存しない)テキストに対する展開です。
71724344複数のケースがあります。
71734345
71744346=over 4
71754347
71764348=item C<<<'EOF'>
71774349
71784350=begin original
71794351
71804352No interpolation is performed.
71814353Note that the combination C<\\> is left intact, since escaped delimiters
71824354are not available for here-docs.
71834355
71844356=end original
71854357
71864358展開は行われません。
71874359C<\\> の組み合わせはそのままであることに注意してください;
7188ドキュメントではデリミタのエスケープはできないからです。
4360ドキュメントではデリミタのエスケープはできないからです。
71894361
71904362=item C<m''>, the pattern of C<s'''>
71914363
71924364=begin original
71934365
71944366No interpolation is performed at this stage.
71954367Any backslashed sequences including C<\\> are treated at the stage
7196of L</"Parsing regular expressions">.
4368to L</"parsing regular expressions">.
71974369
71984370=end original
71994371
72004372このステージでは展開は行われません。
72014373C<\\> を含む、バックスラッシュ付きのシーケンスは
7202L</"Parsing regular expressions"> で扱われます。
4374L</"parsing regular expressions"> で扱われます。
72034375
72044376=item C<''>, C<q//>, C<tr'''>, C<y'''>, the replacement of C<s'''>
72054377
72064378=begin original
72074379
72084380The only interpolation is removal of C<\> from pairs of C<\\>.
7209Therefore C<"-"> in C<tr'''> and C<y'''> is treated literally
4381Therefore C<-> in C<tr'''> and C<y'''> is treated literally
72104382as a hyphen and no character range is available.
72114383C<\1> in the replacement of C<s'''> does not work as C<$1>.
72124384
72134385=end original
72144386
72154387C<\\> の組における C<\> の削除のみが行われます。
7216従って、C<tr'''> や C<y'''> の中にある C<"-"> は文字通りハイフンとして扱われ、
4388従って、C<tr'''> や C<y'''> の中にある C<-> は文字通りハイフンとして扱われ、
72174389文字範囲は使えません。
72184390C<s'''> の置換文字列での C<\1> は C<$1> としては動作しません。
72194391
72204392=item C<tr///>, C<y///>
72214393
72224394=begin original
72234395
72244396No variable interpolation occurs. String modifying combinations for
72254397case and quoting such as C<\Q>, C<\U>, and C<\E> are not recognized.
72264398The other escape sequences such as C<\200> and C<\t> and backslashed
72274399characters such as C<\\> and C<\-> are converted to appropriate literals.
7228The character C<"-"> is treated specially and therefore C<\-> is treated
4400The character C<-> is treated specially and therefore C<\-> is treated
7229as a literal C<"-">.
4401as a literal C<->.
72304402
72314403=end original
72324404
72334405変数展開は行われません。
72344406C<\Q>, C<\U>, C<\E> のような、大文字小文字やクォートに関する、文字列を
72354407変更するような組み合わせは認識されません。
72364408C<\200> や C<\t> のようなその他のエスケープシーケンスや、
72374409C<\\> や C<\-> のようなバックスラッシュ付きの文字は、適切なリテラルに
72384410変換されます。
7239文字 C<"-"> は特別扱いされるので、C<\-> はリテラルな C<"-"> として扱われます。
4411文字 C<-> は特別扱いされるので、C<\-> はリテラルな C<-> として扱われます。
72404412
72414413=item C<"">, C<``>, C<qq//>, C<qx//>, C<< <file*glob> >>, C<<<"EOF">
72424414
72434415=begin original
72444416
7245C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\F> (possibly paired with C<\E>) are
4417C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> (possibly paired with C<\E>) are
72464418converted to corresponding Perl constructs. Thus, C<"$foo\Qbaz$bar">
7247is converted to S<C<$foo . (quotemeta("baz" . $bar))>> internally.
4419is converted to C<$foo . (quotemeta("baz" . $bar))> internally.
72484420The other escape sequences such as C<\200> and C<\t> and backslashed
72494421characters such as C<\\> and C<\-> are replaced with appropriate
72504422expansions.
72514423
72524424=end original
72534425
7254C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\F> (おそらくは C<\E> との組)は
4426C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> (おそらくは C<\E> との組)は
72554427対応する Perl 構造に変換されます。
72564428従って、C<"$foo\Qbaz$bar"> は内部的に
7257S<C<$foo . (quotemeta("baz" . $bar))>> に変換されます。
4429C<$foo . (quotemeta("baz" . $bar))> に変換されます。
72584430C<\200> や C<\t> のような、その他のエスケープシーケンスや、C<\\> や
72594431C<\-> のような、バックスラッシュがつけられた文字は適切な拡張に
72604432置換されます。
72614433
72624434=begin original
72634435
72644436Let it be stressed that I<whatever falls between C<\Q> and C<\E>>
72654437is interpolated in the usual way. Something like C<"\Q\\E"> has
7266no C<\E> inside. Instead, it has C<\Q>, C<\\>, and C<E>, so the
4438no C<\E> inside. instead, it has C<\Q>, C<\\>, and C<E>, so the
72674439result is the same as for C<"\\\\E">. As a general rule, backslashes
72684440between C<\Q> and C<\E> may lead to counterintuitive results. So,
72694441C<"\Q\t\E"> is converted to C<quotemeta("\t")>, which is the same
72704442as C<"\\\t"> (since TAB is not alphanumeric). Note also that:
72714443
72724444=end original
72734445
72744446I<C<\Q> と C<\E> の間にある全てのもの> が通常の方法で展開されます。
7275C<"\Q\\E"> のようなものは内部にあるのは C<\E> ではありません。
4447C<"\Q\\E"> のようなものは内部にあるのは C<\E> ではなく、
72764448C<\Q>, C<\\>, C<E> であるので、結果は C<"\\\\E"> と同じになります。
72774449一般的なルールとして、C<\Q> と C<\E> の間にあるバックスラッシュは
72784450直感に反した結果になります。
72794451それで、C<"\Q\t\E"> は C<quotemeta("\t")> に変換され、これは(TAB は
72804452英数字ではないので C<"\\\t"> と同じです。
72814453以下のようなことにも注意してください:
72824454
72834455 $str = '\t';
72844456 return "\Q$str";
72854457
72864458=begin original
72874459
72884460may be closer to the conjectural I<intention> of the writer of C<"\Q\t\E">.
72894461
72904462=end original
72914463
72924464これは C<"\Q\t\E"> を書いた人の憶測上の I<意図> により近いです。
72934465
72944466=begin original
72954467
72964468Interpolated scalars and arrays are converted internally to the C<join> and
7297C<"."> catenation operations. Thus, S<C<"$foo XXX '@arr'">> becomes:
4469C<.> catenation operations. Thus, C<"$foo XXX '@arr'"> becomes:
72984470
72994471=end original
73004472
7301展開されたスカラと配列は内部で C<join> と C<"."> の結合操作に変換されます。
4473展開されたスカラと配列は内部で C<join> と C<.> の結合操作に変換されます。
7302従って、S<C<"$foo XXX '@arr'">> は以下のようになります:
4474従って、C<"$foo XXX '@arr'"> は以下のようになります:
73034475
73044476 $foo . " XXX '" . (join $", @arr) . "'";
73054477
73064478=begin original
73074479
73084480All operations above are performed simultaneously, left to right.
73094481
73104482=end original
73114483
73124484上記の全ての操作は、左から右に同時に行われます。
73134485
73144486=begin original
73154487
7316Because the result of S<C<"\Q I<STRING> \E">> has all metacharacters
4488Because the result of C<"\Q STRING \E"> has all metacharacters
73174489quoted, there is no way to insert a literal C<$> or C<@> inside a
7318C<\Q\E> pair. If protected by C<\>, C<$> will be quoted to become
4490C<\Q\E> pair. If protected by C<\>, C<$> will be quoted to became
73194491C<"\\\$">; if not, it is interpreted as the start of an interpolated
73204492scalar.
73214493
73224494=end original
73234495
7324S<C<"\Q I<STRING> \E">> の結果は全てのメタ文字がクォートされているので、
4496C<"\Q STRING \E"> の結果は全てのメタ文字がクォートされているので、
73254497C<\Q\E> の組の内側にリテラルの C<$> や C<@> を挿入する方法はありません。
73264498C<\> によって守られている場合、C<$> はクォートされて C<"\\\$"> と
7327なります; そうでない場合、これは展開されるスカラ変数の開始として
4499なります
7328解釈されます。
4500そうでない場合、これは展開されるスカラ変数の開始として解釈されます。
73294501
73304502=begin original
73314503
73324504Note also that the interpolation code needs to make a decision on
73334505where the interpolated scalar ends. For instance, whether
7334S<C<< "a $x -> {c}" >>> really means:
4506C<< "a $b -> {c}" >> really means:
73354507
73364508=end original
73374509
73384510展開コードは、展開するスカラ変数がどこで終わるかを決定する必要が
73394511あることにも注意してください。
7340例えば、S<C<< "a $x -> {c}" >>> が実際には以下のどちらかになります:
4512例えば、C<< "a $b -> {c}" >> が実際には以下のどちらかになります:
73414513
7342 "a " . $x . " -> {c}";
4514 "a " . $b . " -> {c}";
73434515
73444516=begin original
73454517
73464518or:
73474519
73484520=end original
73494521
73504522または:
73514523
7352 "a " . $x -> {c};
4524 "a " . $b -> {c};
73534525
73544526=begin original
73554527
73564528Most of the time, the longest possible text that does not include
73574529spaces between components and which contains matching braces or
73584530brackets. because the outcome may be determined by voting based
73594531on heuristic estimators, the result is not strictly predictable.
73604532Fortunately, it's usually correct for ambiguous cases.
73614533
73624534=end original
73634535
73644536ほとんどの場合、要素と、マッチする中かっこや大かっこの間に空白を含まない、
73654537最も長いテキストになります。
73664538出力は発見的な推定器をよる投票によって決定されるので、結果は厳密には
73674539予測できません。
73684540幸い、紛らわしい場合でも普通は正しいです。
73694541
7370=item The replacement of C<s///>
4542=item the replacement of C<s///>
73714543
73724544=begin original
73734545
7374Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\F> and interpolation
4546Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, and interpolation
73754547happens as with C<qq//> constructs.
73764548
73774549=end original
73784550
7379C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\F> の処理と展開が C<qq//> 構造と
4551C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> の処理と展開が C<qq//> 構造と
73804552同じように起こります。
73814553
73824554=begin original
73834555
73844556It is at this step that C<\1> is begrudgingly converted to C<$1> in
73854557the replacement text of C<s///>, in order to correct the incorrigible
73864558I<sed> hackers who haven't picked up the saner idiom yet. A warning
7387is emitted if the S<C<use warnings>> pragma or the B<-w> command-line flag
4559is emitted if the C<use warnings> pragma or the B<-w> command-line flag
73884560(that is, the C<$^W> variable) was set.
73894561
73904562=end original
73914563
73924564このステップでは、より健全な文法をまだ導入していない、手に負えない I<sed>
73934565ハッカーのために、C<s///> の置換テキストの中にある C<\1> を、しぶしぶながら
73944566C<$1> に変換します。
7395S<C<use warnings>> プラグマやコマンドラインオプション B<-w> (これは C<$^W>
4567C<use warnings> プラグマやコマンドラインオプション B<-w> (これは C<$^W>
73964568変数です) がセットされていると警告が生成されます。
73974569
7398=item C<RE> in C<m?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>,
4570=item C<RE> in C<?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>,
73994571
74004572=begin original
74014573
7402Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\F>, C<\E>,
4574Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\E>,
74034575and interpolation happens (almost) as with C<qq//> constructs.
74044576
74054577=end original
74064578
7407C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\F>, C<\E> の処理と展開が C<qq//> 構
4579C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> C<\E> の処理と展開が C<qq//> 構
74084580(ほとんど)同じように起こります。
74094581
74104582=begin original
74114583
7412Processing of C<\N{...}> is also done here, and compiled into an intermediate
7413form for the regex compiler. (This is because, as mentioned below, the regex
7414compilation may be done at execution time, and C<\N{...}> is a compile-time
7415construct.)
7416
7417=end original
7418
7419C<\N{...}> の処理もここで行われ、正規表現コンパイラのための中間形式に
7420コンパイルされます。
7421(これは、後述するように、正規表現のコンパイルは実行時に行われ、
7422C<\N{...}> はコンパイル時の構文だからです。)
7423
7424=begin original
7425
74264584However any other combinations of C<\> followed by a character
74274585are not substituted but only skipped, in order to parse them
74284586as regular expressions at the following step.
74294587As C<\c> is skipped at this step, C<@> of C<\c@> in RE is possibly
74304588treated as an array symbol (for example C<@foo>),
74314589even though the same text in C<qq//> gives interpolation of C<\c@>.
74324590
74334591=end original
74344592
74354593しかし、その他の、C<\> の後に文字が続く組み合わせは置換されず、単に
74364594読み飛ばされます; これは以下のステップで正規表現としてパースするためです。
74374595ここでは C<\c> は読み飛ばされるので、正規表現中の C<\c@> の C<@> は
74384596配列のシンボル(例えば C<@foo>) と扱われる可能性があります;
74394597一方 C<qq//> 内の同じテキストは C<\c@> と展開されます。
74404598
74414599=begin original
74424600
7443Code blocks such as C<(?{BLOCK})> are handled by temporarily passing control
4601Moreover, inside C<(?{BLOCK})>, C<(?# comment )>, and
7444back to the perl parser, in a similar way that an interpolated array
4602a C<#>-comment in a C<//x>-regular expression, no processing is
7445subscript expression such as C<"foo$array[1+f("[xyz")]bar"> would be.
7446
7447=end original
7448
7449C<(?{BLOCK})> のようなコードブロックは、一時的に制御を perl パーサに
7450渡すことで扱われます; これは C<"foo$array[1+f("[xyz")]bar"> のような、
7451変数展開される配列添え字表現と同様の方法です。
7452
7453=begin original
7454
7455Moreover, inside C<(?{BLOCK})>, S<C<(?# comment )>>, and
7456a C<#>-comment in a C</x>-regular expression, no processing is
74574603performed whatsoever. This is the first step at which the presence
7458of the C</x> modifier is relevant.
4604of the C<//x> modifier is relevant.
74594605
74604606=end original
74614607
7462さらに、C<(?{BLOCK})>, S<C<(?# comment )>>, C</x> 正規表現での C<#> の
4608さらに、C<(?{BLOCK})>, C<(?# comment )>, C<//x> 正規表現での C<#> の
74634609コメントの中では、どのような処理も行われません。
7464これは C</x> 修飾子が影響を与える最初のステップです。
4610これは C<//x> 修飾子が影響を与える最初のステップです。
74654611
74664612=begin original
74674613
74684614Interpolation in patterns has several quirks: C<$|>, C<$(>, C<$)>, C<@+>
74694615and C<@-> are not interpolated, and constructs C<$var[SOMETHING]> are
74704616voted (by several different estimators) to be either an array element
74714617or C<$var> followed by an RE alternative. This is where the notation
74724618C<${arr[$bar]}> comes handy: C</${arr[0-9]}/> is interpreted as
74734619array element C<-9>, not as a regular expression from the variable
74744620C<$arr> followed by a digit, which would be the interpretation of
74754621C</$arr[0-9]/>. Since voting among different estimators may occur,
74764622the result is not predictable.
74774623
74784624=end original
74794625
74804626パターン内の展開ではいくつか特殊な動作をします:
74814627C<$|>, C<$(>, C<$)>, C<@+>, C<@-> は展開されず、
74824628C<$var[SOMETHING]> は(いくつかの異なる推定器によって)配列の要素か
74834629C<$var> の後に正規表現が続いているのかが投票されます。
74844630これは C<${arr[$bar]}> が便利になるところです: C</${arr[0-9]}/> は
74854631配列要素 C<-9> として解釈され、C</$arr[0-9]/> の場合のように C<$arr> の後に
74864632数値が続いているような正規表現としては解釈されません。
74874633異なった推定器によって投票されることがあるので、結果は予測できません。
74884634
74894635=begin original
74904636
74914637The lack of processing of C<\\> creates specific restrictions on
74924638the post-processed text. If the delimiter is C</>, one cannot get
74934639the combination C<\/> into the result of this step. C</> will
74944640finish the regular expression, C<\/> will be stripped to C</> on
74954641the previous step, and C<\\/> will be left as is. Because C</> is
74964642equivalent to C<\/> inside a regular expression, this does not
74974643matter unless the delimiter happens to be character special to the
7498RE engine, such as in C<s*foo*bar*>, C<m[foo]>, or C<m?foo?>; or an
4644RE engine, such as in C<s*foo*bar*>, C<m[foo]>, or C<?foo?>; or an
74994645alphanumeric char, as in:
75004646
75014647=end original
75024648
75034649C<\\> を処理しないことにより、後処理したテキストに特定の制限があります。
75044650デリミタが C</> の場合、このステップの結果として C<\/> を得ることは
75054651できません。
75064652C</> は正規表現を終わらせ、C<\/> は前のステップで C</> に展開され、
75074653C<\\/> はそのまま残されます。
75084654C</> は正規表現の中では C<\/> と等価なので、これはたまたまデリミタが
75094655正規検索エンジンにとって特別な文字の場合、つまり C<s*foo*bar*>,
7510C<m[foo]>, C<m?foo?> のような場合、あるいは以下のように英数字でなければ、
4656C<m[foo]>, C<?foo?> のような場合、あるいは以下のように英数字でなければ、
75114657問題にはなりません:
75124658
75134659 m m ^ a \s* b mmx;
75144660
75154661=begin original
75164662
75174663In the RE above, which is intentionally obfuscated for illustration, the
75184664delimiter is C<m>, the modifier is C<mx>, and after delimiter-removal the
7519RE is the same as for S<C<m/ ^ a \s* b /mx>>. There's more than one
4665RE is the same as for C<m/ ^ a \s* b /mx>. There's more than one
75204666reason you're encouraged to restrict your delimiters to non-alphanumeric,
75214667non-whitespace choices.
75224668
75234669=end original
75244670
75254671上記の正規表現では、説明のために意図的にわかりにくくしていますが、
75264672デリミタは C<m> で、修飾子は C<mx> で、デリミタを取り除いた後の
7527正規表現は S<C<m/ ^ a \s* b /mx>> と同じです。
4673正規表現は C<m/ ^ a \s* b /mx> と同じです。
75284674デリミタを英数字や空白でないものに制限するべきである理由は複数あります。
75294675
75304676=back
75314677
75324678=begin original
75334679
75344680This step is the last one for all constructs except regular expressions,
75354681which are processed further.
75364682
75374683=end original
75384684
7539これは正規表現以外の全ての構造にとって最後のステップです; 正規表現は
4685これは正規表現以外の全ての構造にとって最後のステップです
7540さらに処理が続きます。
4686正規表現はさらに処理が続きます。
75414687
7542=item Parsing regular expressions
4688=item parsing regular expressions
75434689X<regexp, parse>
75444690
75454691(正規表現のパース)
75464692
75474693=begin original
75484694
75494695Previous steps were performed during the compilation of Perl code,
7550but this one happens at run time, although it may be optimized to
4696but this one happens at run time--although it may be optimized to
75514697be calculated at compile time if appropriate. After preprocessing
75524698described above, and possibly after evaluation if concatenation,
75534699joining, casing translation, or metaquoting are involved, the
75544700resulting I<string> is passed to the RE engine for compilation.
75554701
75564702=end original
75574703
75584704以前のステップは Perl コードのコンパイル中に実行されますが、
7559これは実行時に起こります、もし適切ならコンパイル時に
4705これは実行時に起こります -- しかし、もし適切ならコンパイル時に
75604706計算できるように最適化されることもあります。
75614707上記の前処理の後、そして必要なら連結、結合、大文字小文字変換、
75624708メタクォート化が行われた後、結果の I<文字列> がコンパイルのために
75634709正規表現エンジンに渡されます。
75644710
75654711=begin original
75664712
75674713Whatever happens in the RE engine might be better discussed in L<perlre>,
75684714but for the sake of continuity, we shall do so here.
75694715
75704716=end original
75714717
75724718正規表現エンジンで起こることについては L<perlre> で議論した方が
75734719よいでしょうが、継続性のために、ここでそれを行います。
75744720
75754721=begin original
75764722
7577This is another step where the presence of the C</x> modifier is
4723This is another step where the presence of the C<//x> modifier is
75784724relevant. The RE engine scans the string from left to right and
7579converts it into a finite automaton.
4725converts it to a finite automaton.
75804726
75814727=end original
75824728
7583これも C</x> 修飾子の存在が関連するステップの一つです。
4729これも C<//x> 修飾子の存在が関連するステップの一つです。
75844730正規表現エンジンは文字列を左から右にスキャンして、有限状態オートマトンに
75854731変換します。
75864732
75874733=begin original
75884734
75894735Backslashed characters are either replaced with corresponding
75904736literal strings (as with C<\{>), or else they generate special nodes
75914737in the finite automaton (as with C<\b>). Characters special to the
75924738RE engine (such as C<|>) generate corresponding nodes or groups of
75934739nodes. C<(?#...)> comments are ignored. All the rest is either
75944740converted to literal strings to match, or else is ignored (as is
7595whitespace and C<#>-style comments if C</x> is present).
4741whitespace and C<#>-style comments if C<//x> is present).
75964742
75974743=end original
75984744
75994745バックスラッシュ付きの文字は(C<\{> のように)対応するリテラル文字列に
76004746置換されるか、あるいは(C<\b> のように)有限状態オートマトンの特別な
76014747ノードを生成します。
76024748(C<|> のような)正規表現エンジンにとって特別な文字は対応するノードか
76034749ノードのグループを生成します。
7604C<(?#...)> コメントは無視されます。
76054750残りの全てはマッチするリテラル文字列に変換されるか、そうでなければ
7606(C</x> が指定された時の空白と C<#> スタイルのコメントと同様に)
4751(C<//x> が指定された時の空白と C<#> スタイルのコメントと同様に)
76074752無視されます。
76084753
76094754=begin original
76104755
76114756Parsing of the bracketed character class construct, C<[...]>, is
76124757rather different than the rule used for the rest of the pattern.
76134758The terminator of this construct is found using the same rules as
76144759for finding the terminator of a C<{}>-delimited construct, the only
76154760exception being that C<]> immediately following C<[> is treated as
7616though preceded by a backslash.
4761though preceded by a backslash. Similarly, the terminator of
4762C<(?{...})> is found using the same rules as for finding the
4763terminator of a C<{}>-delimited construct.
76174764
76184765=end original
76194766
76204767文字クラス構造 C<[...]> のパースは他のパターンとはルールが異なります。
76214768この構造の終端は C<{}> でデリミタされた構造の終端を検索するのと同じルールで
76224769検索されます; 唯一の例外は、C<[> の直後の C<]> はバックスラッシュが
76234770先行しているものとして扱われます。
4771同様に、C<(?{...})> の終端は C<{}> でデリミタされた構造の終端を
4772検索されるのと同じルールで検索されます。
76244773
76254774=begin original
76264775
7627The terminator of runtime C<(?{...})> is found by temporarily switching
7628control to the perl parser, which should stop at the point where the
7629logically balancing terminating C<}> is found.
7630
7631=end original
7632
7633実行時 C<(?{...})> の終端は、一時的に perl パーサに制御を切り替えることで
7634見つけられ、論理的にバランスした終端 C<}> が見つかって場所で停止します。
7635
7636=begin original
7637
76384776It is possible to inspect both the string given to RE engine and the
76394777resulting finite automaton. See the arguments C<debug>/C<debugcolor>
7640in the S<C<use L<re>>> pragma, as well as Perl's B<-Dr> command-line
4778in the C<use L<re>> pragma, as well as Perl's B<-Dr> command-line
76414779switch documented in L<perlrun/"Command Switches">.
76424780
76434781=end original
76444782
76454783正規表現に与えられる文字列と、結果としての有限状態オートマトンの両方を
76464784検査できます。
7647S<C<use L<re>>> プラグマの C<debug>/C<debugcolor> 引数と、
4785C<use L<re>> プラグマの C<debug>/C<debugcolor> 引数と、
76484786L<perlrun/"Command Switches"> に記述されている B<-Dr> コマンドライン
76494787オプションを参照してください。
76504788
76514789=item Optimization of regular expressions
76524790X<regexp, optimization>
76534791
7654(正規表現の最適化)
7655
76564792=begin original
76574793
76584794This step is listed for completeness only. Since it does not change
76594795semantics, details of this step are not documented and are subject
76604796to change without notice. This step is performed over the finite
76614797automaton that was generated during the previous pass.
76624798
76634799=end original
76644800
76654801このステップは完全性のためだけにリストされています。
76664802これは意味論的には変化がないので、このステップの詳細は文書化されておらず、
76674803将来予告なしに変更されることがあります。
76684804このステップはここまでの処理で生成された有限オートマトンに対して
76694805適用されます。
76704806
76714807=begin original
76724808
76734809It is at this stage that C<split()> silently optimizes C</^/> to
76744810mean C</^/m>.
76754811
76764812=end original
76774813
76784814C<split()> で C</^/> を暗黙に C</^/m> に最適化するのもこのステップです。
76794815
76804816=back
76814817
76824818=head2 I/O Operators
76834819X<operator, i/o> X<operator, io> X<io> X<while> X<filehandle>
7684X<< <> >> X<< <<>> >> X<@ARGV>
4820X<< <> >> X<@ARGV>
76854821
76864822(I/O 演算子)
76874823
76884824=begin original
76894825
76904826There are several I/O operators you should know about.
76914827
76924828=end original
76934829
76944830知っておいた方がよい I/O 演算子もいくつかあります。
76954831
76964832=begin original
76974833
76984834A string enclosed by backticks (grave accents) first undergoes
76994835double-quote interpolation. It is then interpreted as an external
77004836command, and the output of that command is the value of the
77014837backtick string, like in a shell. In scalar context, a single string
77024838consisting of all output is returned. In list context, a list of
77034839values is returned, one per line of output. (You can set C<$/> to use
77044840a different line terminator.) The command is executed each time the
77054841pseudo-literal is evaluated. The status value of the command is
77064842returned in C<$?> (see L<perlvar> for the interpretation of C<$?>).
77074843Unlike in B<csh>, no translation is done on the return data--newlines
77084844remain newlines. Unlike in any of the shells, single quotes do not
77094845hide variable names in the command from interpretation. To pass a
77104846literal dollar-sign through to the shell you need to hide it with a
7711backslash. The generalized form of backticks is C<qx//>, or you can
4847backslash. The generalized form of backticks is C<qx//>. (Because
7712call the L<perlfunc/readpipe> function. (Because
77134848backticks always undergo shell expansion as well, see L<perlsec> for
77144849security concerns.)
77154850X<qx> X<`> X<``> X<backtick> X<glob>
77164851
77174852=end original
77184853
77194854バッククォートで括られた文字列は、まず、ダブルクォート補完のように
77204855変数の展開が行なわれます。
77214856その後、シェルでの場合と同じように、外部コマンドとして解釈され、
77224857そのコマンドの出力がこのバッククォート文字列の値となります。
77234858スカラーコンテキストでは、出力すべてを含む一個の文字列が返されます。
77244859リストコンテキストでは、出力の 1 行 1 行が個々の要素となるリストが返されます。
77254860(C<$/> を設定すれば、行の終わりを示す文字を変えることができます。)
77264861コマンドは、この擬似リテラルが評価されるごとに実行されます。
77274862コマンドのステータス値は C<$?> に返されます (C<$?> の解釈については、
77284863L<perlvar> を参照してください)。
77294864B<csh> での場合とは違って、結果のデータに対する変換は行なわれず、
77304865改行は改行のままです。
77314866どのシェルとも違って、シングルクォートがコマンド中の変数名を
77324867解釈させないようにすることはありません。
77334868シェルにリテラルなドル記号を渡すには、バックスラッシュで
77344869エスケープしなければなりません。
7735バッククォートの一般形は、C<qx//> か、
4870バッククォートの一般形は、C<qx//> です。
7736L<perlfunc/readpipe> 関数を呼び出せます。
4871(バッククォートは常にシェル展開されます。
7737(バッククォートは常シェル展開れます;
4872セキュリティ関しては L<perlsec> を参照して下い)
7738セキュリティに関しては L<perlsec> を参照して下さい。)
77394873
77404874=begin original
77414875
77424876In scalar context, evaluating a filehandle in angle brackets yields
77434877the next line from that file (the newline, if any, included), or
77444878C<undef> at end-of-file or on error. When C<$/> is set to C<undef>
77454879(sometimes known as file-slurp mode) and the file is empty, it
77464880returns C<''> the first time, followed by C<undef> subsequently.
77474881
77484882=end original
77494883
77504884スカラーコンテキストで山括弧の中のファイルハンドルを評価すると、
77514885そのファイルから、次の行を読み込むことになります
7752(改行があればそれも含まれます); ファイルの最後またはエラーの場合は
4886(改行があればそれも含まれます)
7753C<undef> を返します。
4887ファイルの最後またはエラーの場合は C<undef> を返します。
77544888C<$/> が C<undef> に設定されている場合(ファイル吸い込みモードと呼ばれます)
77554889でファイルが空の場合、
77564890最初は C<''> を返し、次は C<undef> を返します。
77574891
77584892=begin original
77594893
77604894Ordinarily you must assign the returned value to a variable, but
77614895there is one situation where an automatic assignment happens. If
77624896and only if the input symbol is the only thing inside the conditional
77634897of a C<while> statement (even if disguised as a C<for(;;)> loop),
7764the value is automatically assigned to the global variable C<$_>,
4898the value is automatically assigned to the global variable $_,
77654899destroying whatever was there previously. (This may seem like an
77664900odd thing to you, but you'll use the construct in almost every Perl
7767script you write.) The C<$_> variable is not implicitly localized.
4901script you write.) The $_ variable is not implicitly localized.
7768You'll have to put a S<C<local $_;>> before the loop if you want that
4902You'll have to put a C<local $_;> before the loop if you want that
7769to happen. Furthermore, if the input symbol or an explicit assignment
4903to happen.
7770of the input symbol to a scalar is used as a C<while>/C<for> condition,
7771then the condition actually tests for definedness of the expression's
7772value, not for its regular truth value.
77734904
77744905=end original
77754906
77764907通常は、返された値を変数に代入しなければなりませんが、自動的に
77774908代入される場合が 1 つだけあります。
77784909この入力シンボルが、while 文(C<for(;;)> の形になっていたとしても)の条件式中に
7779単独で現れた場合だけは、その値が自動的にグローバル変数 C<$_> に代入されます;
4910単独で現れた場合だけは、その値が自動的にグローバル変数 $_ に代入されます
77804911以前の値は破壊されます。
77814912(これは、奇妙に思えるかもしれませんが、ほとんどすべての Perl スクリプトで
77824913これが必要になることでしょう。)
7783C<$_> 変数は暗黙にはローカル化されません。
4914$_ 変数は暗黙にはローカル化されません。
7784そうしたい場合はループの前に S<C<local $_;>> と書く必要があります。
4915そうしたい場合はループの前に C<local $_;> と書く必要があります。
7785さらに、入力シンボルまたは入力シンボルからスカラへの明示的な代入が
7786C<while>/C<for> の条件部として使われた場合、
7787条件は通常の真の値かどうかではなく、式の値が定義されているかどうかを
7788テストします。
77894916
77904917=begin original
77914918
7792Thus the following lines are equivalent:
4919The following lines are equivalent:
77934920
77944921=end original
77954922
7796従って、以下のものは互いに同値なものです:
4923以下のものは、お互いに同値なものです:
77974924
77984925 while (defined($_ = <STDIN>)) { print; }
77994926 while ($_ = <STDIN>) { print; }
78004927 while (<STDIN>) { print; }
78014928 for (;<STDIN>;) { print; }
78024929 print while defined($_ = <STDIN>);
78034930 print while ($_ = <STDIN>);
78044931 print while <STDIN>;
78054932
78064933=begin original
78074934
7808This also behaves similarly, but assigns to a lexical variable
4935This also behaves similarly, but avoids $_ :
7809instead of to C<$_>:
78104936
78114937=end original
78124938
7813以下は同様の振る舞いをしますが、C<$_> おn代わりにレキシカル変数に
4939以下は同様の振る舞いをしますが、$_ を使いません:
7814代入します:
78154940
78164941 while (my $line = <STDIN>) { print $line }
78174942
78184943=begin original
78194944
78204945In these loop constructs, the assigned value (whether assignment
78214946is automatic or explicit) is then tested to see whether it is
7822defined. The defined test avoids problems where the line has a string
4947defined. The defined test avoids problems where line has a string
7823value that would be treated as false by Perl; for example a "" or
4948value that would be treated as false by Perl, for example a "" or
7824a C<"0"> with no trailing newline. If you really mean for such values
4949a "0" with no trailing newline. If you really mean for such values
78254950to terminate the loop, they should be tested for explicitly:
78264951
78274952=end original
78284953
78294954これらのループ構造の中で、代入された値は (代入が自動か明示的かに関わりなく)
78304955定義されているかどうかを見るためにテストされます。
78314956定義テストは、行が Perl にとって偽となる文字列値を持っているかどうかの
7832問題を避けます; 例えば newline のついていない "" や C<"0"> です。
4957問題を避けます例えば newline のついていない "" や "0" です。
78334958もし本当にこのような値でループを終了させたいときは、
78344959以下のように明示的にテストするべきです:
78354960
78364961 while (($_ = <STDIN>) ne '0') { ... }
78374962 while (<STDIN>) { last unless $_; ... }
78384963
78394964=begin original
78404965
7841In other boolean contexts, C<< <I<FILEHANDLE>> >> without an
4966In other boolean contexts, C<< <I<filehandle>> >> without an
7842explicit C<defined> test or comparison elicits a warning if the
4967explicit C<defined> test or comparison elicit a warning if the
7843S<C<use warnings>> pragma or the B<-w>
4968C<use warnings> pragma or the B<-w>
78444969command-line switch (the C<$^W> variable) is in effect.
78454970
78464971=end original
78474972
78484973その他の真偽値コンテキストでは、明示的な C<defined> や比較なしに
7849C<< <I<FILEHANDLE>> >> を使うと、S<C<use warnings>> プラグマや
4974C<< <I<filehandle>> >> を使うと、C<use warnings> プラグマや
78504975B<-w> コマンドラインスイッチ (C<$^W> 変数) が有効なときには、
78514976警告を発生させます。
78524977
78534978=begin original
78544979
78554980The filehandles STDIN, STDOUT, and STDERR are predefined. (The
78564981filehandles C<stdin>, C<stdout>, and C<stderr> will also work except
78574982in packages, where they would be interpreted as local identifiers
78584983rather than global.) Additional filehandles may be created with
7859the C<open()> function, amongst others. See L<perlopentut> and
4984the open() function, amongst others. See L<perlopentut> and
78604985L<perlfunc/open> for details on this.
78614986X<stdin> X<stdout> X<sterr>
78624987
78634988=end original
78644989
78654990STDIN、STDOUT、STDERR というファイルハンドルは、あらかじめ定義されています。
78664991(C<stdin>、C<stdout>、C<stderr> というファイルハンドルも、
78674992ローカルな名前でこれらのグローバルな名前が見えなくなっている
78684993パッケージを除けば、使用することができます。)
7869その他のファイルハンドルは、C<open()> 関数などで作ることができます。
4994その他のファイルハンドルは、open() 関数などで作ることができます。
78704995これに関する詳細については L<perlopentut> と L<perlfunc/open> を
78714996参照して下さい。
78724997X<stdin> X<stdout> X<sterr>
78734998
78744999=begin original
78755000
7876If a C<< <I<FILEHANDLE>> >> is used in a context that is looking for
5001If a <FILEHANDLE> is used in a context that is looking for
78775002a list, a list comprising all input lines is returned, one line per
78785003list element. It's easy to grow to a rather large data space this
78795004way, so use with care.
78805005
78815006=end original
78825007
7883C<< <I<FILEHANDLE>> >> がリストを必要とするコンテキストで用いられると、
5008<FILEHANDLE> がリストを必要とするコンテキストで用いられると、
788450091 要素に 1 行の入力行すべてからなるリストが返されます。
78855010これを使うと簡単にかなり大きなデータになってしまいますので、
78865011注意を要します。
78875012
78885013=begin original
78895014
7890C<< <I<FILEHANDLE>> >> may also be spelled C<readline(*I<FILEHANDLE>)>.
5015<FILEHANDLE> may also be spelled C<readline(*FILEHANDLE)>.
78915016See L<perlfunc/readline>.
78925017
78935018=end original
78945019
7895C<< <I<FILEHANDLE>> >> は C<readline(*I<FILEHANDLE>)> とも書けます。
5020<FILEHANDLE> は C<readline(*FILEHANDLE)> とも書けます。
78965021L<perlfunc/readline> を参照して下さい。
78975022
78985023=begin original
78995024
7900The null filehandle C<< <> >> (sometimes called the diamond operator) is
5025The null filehandle <> is special: it can be used to emulate the
7901special: it can be used to emulate the
5026behavior of B<sed> and B<awk>. Input from <> comes either from
7902behavior of B<sed> and B<awk>, and any other Unix filter program
7903that takes a list of filenames, doing the same to each line
7904of input from all of them. Input from C<< <> >> comes either from
79055027standard input, or from each file listed on the command line. Here's
7906how it works: the first time C<< <> >> is evaluated, the C<@ARGV> array is
5028how it works: the first time <> is evaluated, the @ARGV array is
7907checked, and if it is empty, C<$ARGV[0]> is set to C<"-">, which when opened
5029checked, and if it is empty, C<$ARGV[0]> is set to "-", which when opened
7908gives you standard input. The C<@ARGV> array is then processed as a list
5030gives you standard input. The @ARGV array is then processed as a list
79095031of filenames. The loop
79105032
79115033=end original
79125034
7913ヌルファイルハンドル C<< <> >> (時々ダイヤモンド演算子と呼ばれます) は特別で、
5035ヌルファイルハンドル <> は特別で、B<sed> や B<awk> の動作を
7914B<sed> や B<awk> および、ファイル名のリスを取って、
5036エミュレーするために使われます。
7915それら全てからの入力の各行同じことをするような
5037<> からの入力は、標準入力からか、コマンドライン上並べられた個々の
7916その他の Unix フィルタプログラムをエミュレートするために使われます。
7917C<< <> >> からの入力は、標準入力からか、コマンドライン上に並べられた個々の
79185038ファイルから行なわれます。
7919動作の概要は、以下のようになります: 最初に C<< <> >> が評価されると、配列
5039動作の概要は、以下のようになります
7920C<@ARGV> が調べられ、空であれば、C<$ARGV[0]> に C<"-"> を設定します。
5040最初に <> が評価されると、配列 @ARGV が調べられ、空であれば、
5041C<$ARGV[0]> に "-"を設定します。
79215042これは、open されるとき標準入力となります。
7922その後、配列 C<@ARGV> がファイル名のリストとして処理されます。
5043その後、配列 @ARGV がファイル名のリストとして処理されます。
79235044
79245045 while (<>) {
79255046 ... # code for each line
79265047 }
79275048
79285049=begin original
79295050
79305051is equivalent to the following Perl-like pseudo code:
79315052
79325053=end original
79335054
79345055は以下ののような Perl の擬似コードと等価です:
79355056
79365057 unshift(@ARGV, '-') unless @ARGV;
79375058 while ($ARGV = shift) {
79385059 open(ARGV, $ARGV);
79395060 while (<ARGV>) {
79405061 ... # code for each line
79415062 }
79425063 }
79435064
79445065=begin original
79455066
79465067except that it isn't so cumbersome to say, and will actually work.
7947It really does shift the C<@ARGV> array and put the current filename
5068It really does shift the @ARGV array and put the current filename
7948into the C<$ARGV> variable. It also uses filehandle I<ARGV>
5069into the $ARGV variable. It also uses filehandle I<ARGV>
7949internally. C<< <> >> is just a synonym for C<< <ARGV> >>, which
5070internally--<> is just a synonym for <ARGV>, which
79505071is magical. (The pseudo code above doesn't work because it treats
7951C<< <ARGV> >> as non-magical.)
5072<ARGV> as non-magical.)
79525073
79535074=end original
79545075
79555076但し、わずらわしく書かなくても、動作します。
7956実際に C<@ARGV> を shift しますし、その時点のファイル名を変数 C<$ARGV>
5077実際に @ARGV を shift しますし、その時点のファイル名を変数 $ARGV に
79575078入れています。
7958また、内部的にファイルハンドル I<ARGV> を使っています。
5079また、内部的にファイルハンドル ARGV を使っていて、<> はマジカルな
7959C<< <> >> は C<< <ARGV> >> の同義語で、マジカルです。
5080<ARGV> の同義語となっています。
7960(上記の擬似コードは、C<< <ARGV> >> マジカルではないものとして
5081(上記の擬似コードは、<ARGV> を通常のものとして扱っているので、
7961扱っているので、うまく動作しません。)
5082うまく動作しません。)
79625083
79635084=begin original
79645085
79655086Since the null filehandle uses the two argument form of L<perlfunc/open>
79665087it interprets special characters, so if you have a script like this:
79675088
79685089=end original
79695090
79705091空ファイルハンドルは 2 引数の L<perlfunc/open> を使った特別な文字列なので、
79715092もし以下のようなスクリプトを書いて:
79725093
79735094 while (<>) {
79745095 print;
79755096 }
79765097
79775098=begin original
79785099
7979and call it with S<C<perl dangerous.pl 'rm -rfv *|'>>, it actually opens a
5100and call it with C<perl dangerous.pl 'rm -rfv *|'>, it actually opens a
79805101pipe, executes the C<rm> command and reads C<rm>'s output from that pipe.
79815102If you want all items in C<@ARGV> to be interpreted as file names, you
7982can use the module C<ARGV::readonly> from CPAN, or use the double
5103can use the module C<ARGV::readonly> from CPAN.
7983diamond bracket:
79845104
79855105=end original
79865106
7987これを S<C<perl dangerous.pl 'rm -rfv *|'>> として呼び出すと、実際には
5107これを C<perl dangerous.pl 'rm -rfv *|'> として呼び出すと、実際には
79885108パイプを開き、C<rm> コマンドを実行して、 C<rm> の出力をパイプから読みます。
79895109もし C<@ARGV> の全ての要素をファイル名として解釈させたいなら、
7990CPAN にある C<ARGV::readonly> モジュールか、二重ダイヤモンド山かっこ
5110CPAN にある C<ARGV::readonly> モジュールが使えます。
7991使えます。
79925111
7993 while (<<>>) {
7994 print;
7995 }
7996
79975112=begin original
79985113
7999Using double angle brackets inside of a while causes the open to use the
5114You can modify @ARGV before the first <> as long as the array ends up
8000three argument form (with the second argument being C<< < >>), so all
8001arguments in C<ARGV> are treated as literal filenames (including C<"-">).
8002(Note that for convenience, if you use C<< <<>> >> and if C<@ARGV> is
8003empty, it will still read from the standard input.)
8004
8005=end original
8006
8007while の中で二重角かっこを使うと、
80083 引数型式 (かつ 2 番目の引数が C<< < >> の) open を引き起こすので、
8009C<ARGV> の全ての引数は (C<"-"> を含めて) リテラル名ファイル名として
8010扱われます。
8011(便宜のため、C<< <<>> >> を使って C<@ARGV> が空の場合、標準入力から
8012読み込みます。)
8013
8014=begin original
8015
8016You can modify C<@ARGV> before the first C<< <> >> as long as the array ends up
80175115containing the list of filenames you really want. Line numbers (C<$.>)
80185116continue as though the input were one big happy file. See the example
80195117in L<perlfunc/eof> for how to reset line numbers on each file.
80205118
80215119=end original
80225120
8023最終的に、C<@ARGV> に扱いたいと思っているファイル名が含まれるのであれば、
5121最終的に、@ARGV に扱いたいと思っているファイル名が含まれるのであれば、
8024最初に C<< <> >> を評価する前に C<@ARGV> を変更することも可能です。
5122最初に <> を評価する前に @ARGV を変更することも可能です。
80255123行番号 (C<$.>) は、入力ファイルがあたかも 1 つの大きなファイルで
80265124あるかのように、続けてカウントされます。
80275125個々のファイルごとにリセットする方法は、L<perlfunc/eof> の例を
80285126参照してください。
80295127
80305128=begin original
80315129
8032If you want to set C<@ARGV> to your own list of files, go right ahead.
5130If you want to set @ARGV to your own list of files, go right ahead.
8033This sets C<@ARGV> to all plain text files if no C<@ARGV> was given:
5131This sets @ARGV to all plain text files if no @ARGV was given:
80345132
80355133=end original
80365134
8037最初から C<@ARGV> に自分でファイルのリストを設定してもかまいません。
5135最初から @ARGV に自分でファイルのリストを設定してもかまいません。
8038以下は C<@ARGV> が与えられなかったときに全てのテキストファイルを
5136以下は @ARGV が与えられなかったときに全てのテキストファイルを
8039C<@ARGV> に設定します。
5137@ARGV に設定します。
80405138
80415139 @ARGV = grep { -f && -T } glob('*') unless @ARGV;
80425140
80435141=begin original
80445142
80455143You can even set them to pipe commands. For example, this automatically
80465144filters compressed arguments through B<gzip>:
80475145
80485146=end original
80495147
80505148ここにパイプコマンドを置くことも出来ます。
80515149例えば、以下は圧縮された引数を自動的に B<gzip> のフィルタに通します:
80525150
80535151 @ARGV = map { /\.(gz|Z)$/ ? "gzip -dc < $_ |" : $_ } @ARGV;
80545152
80555153=begin original
80565154
80575155If you want to pass switches into your script, you can use one of the
8058C<Getopts> modules or put a loop on the front like this:
5156Getopts modules or put a loop on the front like this:
80595157
80605158=end original
80615159
8062スクリプトにスイッチを渡したいのであれば、C<Getopts> モジュールを
5160スクリプトにスイッチを渡したいのであれば、Getopts モジュールを
80635161使うこともできますし、実際の処理の前にのようなループを置くこともできます。
80645162
80655163 while ($_ = $ARGV[0], /^-/) {
80665164 shift;
80675165 last if /^--$/;
80685166 if (/^-D(.*)/) { $debug = $1 }
80695167 if (/^-v/) { $verbose++ }
80705168 # ... # other switches
80715169 }
80725170
80735171 while (<>) {
80745172 # ... # code for each line
80755173 }
80765174
80775175=begin original
80785176
8079The C<< <> >> symbol will return C<undef> for end-of-file only once.
5177The <> symbol will return C<undef> for end-of-file only once.
80805178If you call it again after this, it will assume you are processing another
8081C<@ARGV> list, and if you haven't set C<@ARGV>, will read input from STDIN.
5179@ARGV list, and if you haven't set @ARGV, will read input from STDIN.
80825180
80835181=end original
80845182
8085シンボル C<< <> >> がファイルの最後で C<undef> を返すのは一度きりです。
5183シンボル <> がファイルの最後で C<undef> を返すのは一度きりです。
8086そのあとでもう一度呼び出すと、新たに別の C<@ARGV> を処理するものとみなされ、
5184そのあとでもう一度呼び出すと、新たに別の @ARGV を処理するものとみなされ、
8087その時に C<@ARGV> を設定しなおしていないと、STDIN からの入力を
5185その時に @ARGV を設定しなおしていないと、STDIN からの入力を
80885186読み込むことになります。
80895187
80905188=begin original
80915189
8092If what the angle brackets contain is a simple scalar variable (for example,
5190If what the angle brackets contain is a simple scalar variable (e.g.,
8093C<$foo>), then that variable contains the name of the
5191<$foo>), then that variable contains the name of the
80945192filehandle to input from, or its typeglob, or a reference to the
80955193same. For example:
80965194
80975195=end original
80985196
8099山括弧の中の文字列が (C<$foo> のような) 単純スカラ変数であれば、
5197山括弧の中の文字列が (<$foo> のような) 単純スカラ変数であれば、
81005198その変数が入力を行なうファイルハンドルの名前そのもの、名前への型グロブ、
81015199名前へのリファレンスのいずれかを示しているとみなされます。
8102例えば:
81035200
81045201 $fh = \*STDIN;
81055202 $line = <$fh>;
81065203
81075204=begin original
81085205
81095206If what's within the angle brackets is neither a filehandle nor a simple
81105207scalar variable containing a filehandle name, typeglob, or typeglob
81115208reference, it is interpreted as a filename pattern to be globbed, and
81125209either a list of filenames or the next filename in the list is returned,
81135210depending on context. This distinction is determined on syntactic
8114grounds alone. That means C<< <$x> >> is always a C<readline()> from
5211grounds alone. That means C<< <$x> >> is always a readline() from
8115an indirect handle, but C<< <$hash{key}> >> is always a C<glob()>.
5212an indirect handle, but C<< <$hash{key}> >> is always a glob().
8116That's because C<$x> is a simple scalar variable, but C<$hash{key}> is
5213That's because $x is a simple scalar variable, but C<$hash{key}> is
81175214not--it's a hash element. Even C<< <$x > >> (note the extra space)
81185215is treated as C<glob("$x ")>, not C<readline($x)>.
81195216
81205217=end original
81215218
81225219山括弧の中の文字列がファイルハンドルでもファイルハンドル名、型グロブ、
81235220型グロブリファレンスのいずれかが入った単純スカラ変数でもなければ、
81245221グロブを行なうファイル名のパターンと解釈され、コンテキストによって
81255222ファイル名のリストか、そのリストの次のファイル名が返されます。
81265223この区別は単に構文的に行われます。
8127C<< <$x> >> は常に間接ハンドルから C<readline()> しますが、
5224C<< <$x> >> は常に間接ハンドルから readline() しますが、
8128C<< <$hash{key}> >> は常に C<glob()> します。
5225C<< <$hash{key}> >> は常に glob() します。
8129C<$x> は単純スカラー変数ですが、C<$hash{key}> は違う(ハッシュ要素)からです。
5226$x は単純スカラー変数ですが、C<$hash{key}> は違う(ハッシュ要素)からです。
81305227C<< <$x > >> (余分な空白に注意) ですら C<readline($x)> ではなく
81315228C<glob("$x ")> として扱われます。
81325229
81335230=begin original
81345231
81355232One level of double-quote interpretation is done first, but you can't
81365233say C<< <$foo> >> because that's an indirect filehandle as explained
81375234in the previous paragraph. (In older versions of Perl, programmers
81385235would insert curly brackets to force interpretation as a filename glob:
81395236C<< <${foo}> >>. These days, it's considered cleaner to call the
81405237internal function directly as C<glob($foo)>, which is probably the right
81415238way to have done it in the first place.) For example:
81425239
81435240=end original
81445241
81455242まず、1 段階だけダブルクォート展開が行なわれますが、前の段落に書いた
81465243間接ファイルハンドルと同じになる、C<< <$foo> >> のようには書けません。
81475244(Perl の古いバージョンでは、ファイル名グロブと解釈させるために
81485245C<< <${foo}> >> のように中括弧を入れていました。
8149最近ではより明確にするために、C<glob($foo)> と内部関数を呼ぶことも
5246最近ではより明確にするために、C<glob($foo)> と内部関数を
8150できます; おそらく、まず、こちらの方で試すのが正解でしょう)
5247呼ぶこともできます。
8151例えば:
5248おそらく、まず、こちらの方で試すのが正解でしょう。)
5249例:
81525250
81535251 while (<*.c>) {
81545252 chmod 0644, $_;
81555253 }
81565254
81575255=begin original
81585256
81595257is roughly equivalent to:
81605258
81615259=end original
81625260
81635261はだいたい以下と等価です:
81645262
81655263 open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
81665264 while (<FOO>) {
81675265 chomp;
81685266 chmod 0644, $_;
81695267 }
81705268
81715269=begin original
81725270
81735271except that the globbing is actually done internally using the standard
8174C<L<File::Glob>> extension. Of course, the shortest way to do the above is:
5272C<File::Glob> extension. Of course, the shortest way to do the above is:
81755273
81765274=end original
81775275
8178但し実際のグロブは内部的に標準の C<L<File::Glob>> モジュールを使います。
5276但し実際のグロブは内部的に標準の C<File::Glob> モジュールを使います。
81795277もちろん、もっと簡単に以下のように書けます:
81805278
81815279 chmod 0644, <*.c>;
81825280
81835281=begin original
81845282
81855283A (file)glob evaluates its (embedded) argument only when it is
81865284starting a new list. All values must be read before it will start
81875285over. In list context, this isn't important because you automatically
81885286get them all anyway. However, in scalar context the operator returns
81895287the next value each time it's called, or C<undef> when the list has
81905288run out. As with filehandle reads, an automatic C<defined> is
81915289generated when the glob occurs in the test part of a C<while>,
8192because legal glob returns (for example,
5290because legal glob returns (e.g. a file called F<0>) would otherwise
8193a file called F<0>) would otherwise
81945291terminate the loop. Again, C<undef> is returned only once. So if
81955292you're expecting a single value from a glob, it is much better to
81965293say
81975294
81985295=end original
81995296
82005297(ファイル)グロブは新しいリストを開始するときにだけ(組み込みの)引数を
82015298評価します。
82025299全ての値は開始する前に読み込んでいなければなりません。
82035300これはリストコンテキストでは、とにかく自動的に全てを取り込むので
82045301重要ではありません。
8205しかし、スカラコンテキストではこの演算子は呼び出された時の
5302しかし、スカラコンテキストではこの演算子は呼び出された時の
82065303次の値か、リストがなくなったときには C<undef> を返します。
82075304ファイルハンドルを読み込む場合は、グロブが C<while> の条件部にある場合は
8208自動的な C<defined> が生成されます;
5305自動的な C<defined> が生成されます
8209なぜならそうしないと、本来の glob の返り値 (例えば、F<0> というファイル) が
5306なぜならそうしないと、本来の glob の返り値 (F<0> というファイル) が
82105307ループを終了させるからです。
82115308ここでも、C<undef> は一度だけ返されます。
82125309従って、もしグロブから一つの値だけを想定している場合、
82135310以下のように書くことが:
82145311
82155312 ($file) = <blurch*>;
82165313
82175314=begin original
82185315
82195316than
82205317
82215318=end original
82225319
82235320以下のように書くよりはるかに良いです:
82245321
82255322 $file = <blurch*>;
82265323
82275324=begin original
82285325
82295326because the latter will alternate between returning a filename and
82305327returning false.
82315328
82325329=end original
82335330
82345331なぜなら後者はファイル名を返す場合と偽を返す場合があるからです。
82355332
82365333=begin original
82375334
82385335If you're trying to do variable interpolation, it's definitely better
8239to use the C<glob()> function, because the older notation can cause people
5336to use the glob() function, because the older notation can cause people
82405337to become confused with the indirect filehandle notation.
82415338
82425339=end original
82435340
8244変数変換に挑戦する場合、明らかに C<glob()> 関数を使う方が良いです;
5341変数変換に挑戦する場合、明らかに glob() 関数を使う方が良いです
82455342なぜなら古い表記は間接ファイルハンドル表記と混乱するかも知れないからです。
82465343
82475344 @files = glob("$dir/*.[ch]");
82485345 @files = glob($files[$i]);
82495346
8250=begin original
8251
8252If an angle-bracket-based globbing expression is used as the condition of
8253a C<while> or C<for> loop, then it will be implicitly assigned to C<$_>.
8254If either a globbing expression or an explicit assignment of a globbing
8255expression to a scalar is used as a C<while>/C<for> condition, then
8256the condition actually tests for definedness of the expression's value,
8257not for its regular truth value.
8258
8259=end original
8260
8261角かっこグロブ式が C<while> や C<for> ループの条件として使われた場合、
8262これは暗黙に C<$_> に代入されます。
8263さらに、グロブ式またはグロブ式からスカラへの明示的な代入が
8264C<while>/C<for> の条件部として使われた場合、
8265条件は通常の真の値かどうかではなく、式の値が定義されているかどうかを
8266テストします。
8267
82685347=head2 Constant Folding
82695348X<constant folding> X<folding>
82705349
82715350(定数の畳み込み)
82725351
82735352=begin original
82745353
82755354Like C, Perl does a certain amount of expression evaluation at
82765355compile time whenever it determines that all arguments to an
82775356operator are static and have no side effects. In particular, string
82785357concatenation happens at compile time between literals that don't do
82795358variable substitution. Backslash interpolation also happens at
82805359compile time. You can say
82815360
82825361=end original
82835362
82845363C と同じように Perl でも、演算子に対するすべての引数がスタティックで、
82855364副作用がないと判断できれば、コンパイル時に式の評価を行なってしまいます。
82865365特に、変数置換の無いリテラルどうしの文字列連結はコンパイル時に行なわれます。
82875366バックスラッシュの解釈もコンパイル時に行なわれます。
8288以下のように書けて、
82895367
8290 'Now is the time for all'
5368 'Now is the time for all' . "\n" .
8291 . "\n"
5369 'good men to come to.'
8292 . 'good men to come to.'
82935370
82945371=begin original
82955372
82965373and this all reduces to one string internally. Likewise, if
82975374you say
82985375
82995376=end original
83005377
8301内部的に 1 つの文字列になります。同様に
5378と書いても、内部的に 1 つの文字列になります。同様に
83025379
83035380 foreach $file (@filenames) {
83045381 if (-s $file > 5 + 100 * 2**16) { }
83055382 }
83065383
83075384=begin original
83085385
8309the compiler precomputes the number which that expression
5386the compiler will precompute the number which that expression
83105387represents so that the interpreter won't have to.
83115388
83125389=end original
83135390
83145391と書くとコンパイラは、式が表わす数値をあらかじめ計算しますので、
83155392インタプリタで計算する必要がなくなっています。
83165393
83175394=head2 No-ops
83185395X<no-op> X<nop>
83195396
83205397(無実行)
83215398
83225399=begin original
83235400
83245401Perl doesn't officially have a no-op operator, but the bare constants
8325C<0> and C<1> are special-cased not to produce a warning in void
5402C<0> and C<1> are special-cased to not produce a warning in a void
83265403context, so you can for example safely do
83275404
83285405=end original
83295406
83305407Perl は公式には無実行演算子はありませんが、裸の定数 C<0> と C<1> は
83315408特別に無効コンテキストでも警告を出さないことになっているので、
83325409例えば安全に以下のように書けます:
83335410
83345411 1 while foo();
83355412
83365413=head2 Bitwise String Operators
8337X<operator, bitwise, string> X<&.> X<|.> X<^.> X<~.>
5414X<operator, bitwise, string>
83385415
83395416(ビット列演算子)
83405417
83415418=begin original
83425419
83435420Bitstrings of any size may be manipulated by the bitwise operators
83445421(C<~ | & ^>).
83455422
83465423=end original
83475424
83485425任意のサイズのビット列はビット単位演算子(C<~ | & ^>)で操作できます。
83495426
83505427=begin original
83515428
83525429If the operands to a binary bitwise op are strings of different
83535430sizes, B<|> and B<^> ops act as though the shorter operand had
83545431additional zero bits on the right, while the B<&> op acts as though
83555432the longer operand were truncated to the length of the shorter.
83565433The granularity for such extension or truncation is one or more
83575434bytes.
83585435
83595436=end original
83605437
83615438二項ビット単位演算子のオペランドが異なった長さの文字列だった場合、
83625439B<|> と B<^> の演算子は短い側のオペランドの右側に追加のゼロが
8363ついているとみなします; 一方 B<&> 演算子は長い方のオペランドが短い方に
5440ついているとみなします
8364切り詰められます。
5441一方 B<&> 演算子は長い方のオペランドが短い方に切り詰められます。
83655442この拡張や短縮の粒度はバイト単位です。
83665443
83675444 # ASCII-based examples
83685445 print "j p \n" ^ " a h"; # prints "JAPH\n"
83695446 print "JA" | " ph\n"; # prints "japh\n"
83705447 print "japh\nJunk" & '_____'; # prints "JAPH\n";
83715448 print 'p N$' ^ " E<H\n"; # prints "Perl\n";
83725449
83735450=begin original
83745451
83755452If you are intending to manipulate bitstrings, be certain that
83765453you're supplying bitstrings: If an operand is a number, that will imply
83775454a B<numeric> bitwise operation. You may explicitly show which type of
83785455operation you intend by using C<""> or C<0+>, as in the examples below.
83795456
83805457=end original
83815458
83825459ビット列を操作したい場合は、確実にビット列が渡されるようにしてください:
83835460オペランドが数字の場合、B<数値> ビット単位演算を仮定します。
83845461明示的に演算の型を指定するときには、以下の例のように
83855462C<""> か C<0+> を使ってください。
83865463
8387=begin original
8388
83895464 $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF)
83905465 $foo = '150' | 105; # yields 255
83915466 $foo = 150 | '105'; # yields 255
83925467 $foo = '150' | '105'; # yields string '155' (under ASCII)
83935468
8394=end original
8395
8396 $foo = 150 | 105; # 255 になる (0x96 | 0x69 は 0xFF)
8397 $foo = '150' | 105; # 255 になる
8398 $foo = 150 | '105'; # 255 になる
8399 $foo = '150' | '105'; # (ASCII では) 文字列 '155' になる
8400
8401=begin original
8402
84035469 $baz = 0+$foo & 0+$bar; # both ops explicitly numeric
84045470 $biz = "$foo" ^ "$bar"; # both ops explicitly stringy
84055471
8406=end original
8407
8408 $baz = 0+$foo & 0+$bar; # どちらのオペランドも明示的に数値
8409 $biz = "$foo" ^ "$bar"; # どちらのオペランドも明示的に文字列
8410
84115472=begin original
84125473
8413This somewhat unpredictable behavior can be avoided with the "bitwise"
8414feature, new in Perl 5.22. You can enable it via S<C<use feature
8415'bitwise'>> or C<use v5.28>. Before Perl 5.28, it used to emit a warning
8416in the C<"experimental::bitwise"> category. Under this feature, the four
8417standard bitwise operators (C<~ | & ^>) are always numeric. Adding a dot
8418after each operator (C<~. |. &. ^.>) forces it to treat its operands as
8419strings:
8420
8421=end original
8422
8423この、いくらか予測の難しい振る舞いは、Perl 5.22 で導入された
8424"bitwise" 機能で避けることができます。
8425これは S<C<use feature 'bitwise'>> か C<use v5.28> で有効にできます。
8426Perl 5.28 より前では、これは C<"experimental::bitwise"> カテゴリの警告を
8427出力していました。
8428この機能の基では、四つのビット単位演算子 (C<~ | & ^>) は常に数値です。
8429それぞれの演算子の後にピリオドを付ける (C<~. |. &. ^.>) ことで、
8430そのオペランドを文字列として扱うことを強制します:
8431
8432 use feature "bitwise";
8433 $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF)
8434 $foo = '150' | 105; # yields 255
8435 $foo = 150 | '105'; # yields 255
8436 $foo = '150' | '105'; # yields 255
8437 $foo = 150 |. 105; # yields string '155'
8438 $foo = '150' |. 105; # yields string '155'
8439 $foo = 150 |.'105'; # yields string '155'
8440 $foo = '150' |.'105'; # yields string '155'
8441
8442 $baz = $foo & $bar; # both operands numeric
8443 $biz = $foo ^. $bar; # both operands stringy
8444
8445=begin original
8446
8447The assignment variants of these operators (C<&= |= ^= &.= |.= ^.=>)
8448behave likewise under the feature.
8449
8450=end original
8451
8452この機能の基では、これらの演算子の亜種 (C<&= |= ^= &.= |.= ^.=>) も
8453同様に振る舞います。
8454
8455=begin original
8456
8457It is a fatal error if an operand contains a character whose ordinal
8458value is above 0xFF, and hence not expressible except in UTF-8. The
8459operation is performed on a non-UTF-8 copy for other operands encoded in
8460UTF-8. See L<perlunicode/Byte and Character Semantics>.
8461
8462=end original
8463
8464値が 0xFF 以上で、UTF-8 以外で表現できない文字がオペランドに含まれている
8465場合、これは致命的エラーになります。
8466他の UTF-8 でエンコードされたオペランドに対して
8467非 UTF-8 のコピーに対して操作が行われます。
8468L<perlunicode/Byte and Character Semantics> を参照してください。
8469
8470=begin original
8471
84725474See L<perlfunc/vec> for information on how to manipulate individual bits
84735475in a bit vector.
84745476
84755477=end original
84765478
84775479ビットベクタの個々のビットをどのように操作するかの情報については
84785480L<perlfunc/vec> を参照して下さい。
84795481
84805482=head2 Integer Arithmetic
84815483X<integer>
84825484
84835485(整数演算)
84845486
84855487=begin original
84865488
84875489By default, Perl assumes that it must do most of its arithmetic in
84885490floating point. But by saying
84895491
84905492=end original
84915493
84925494デフォルトでは、Perl は演算を浮動小数で行なわなければならないものと
84935495しています。
84945496しかし、(もしそうしたいなら)
84955497
84965498 use integer;
84975499
84985500=begin original
84995501
8500you may tell the compiler to use integer operations
5502you may tell the compiler that it's okay to use integer operations
8501(see L<integer> for a detailed explanation) from here to the end of
5503(if it feels like it) from here to the end of the enclosing BLOCK.
8502the enclosing BLOCK. An inner BLOCK may countermand this by saying
5504An inner BLOCK may countermand this by saying
85035505
85045506=end original
85055507
8506と書けば、その場所から現在の BLOCK の終わりまでは、整数演算
5508と書けば、その場所から現在の BLOCK の終わりまでは、整数演算
8507(詳しい説明は L<integer> を参照してください) を
85085509行なってよいと、コンパイラに指示することができます。
85095510内部の BLOCK で、
85105511
85115512 no integer;
85125513
85135514=begin original
85145515
85155516which lasts until the end of that BLOCK. Note that this doesn't
8516mean everything is an integer, merely that Perl will use integer
5517mean everything is only an integer, merely that Perl may use integer
8517operations for arithmetic, comparison, and bitwise operators. For
5518operations if it is so inclined. For example, even under C<use
8518example, even under S<C<use integer>>, if you take the C<sqrt(2)>, you'll
5519integer>, if you take the C<sqrt(2)>, you'll still get C<1.4142135623731>
8519still get C<1.4142135623731> or so.
5520or so.
85205521
85215522=end original
85225523
85235524と書けば、その BLOCK の終わりまでは、指示を取り消すことになります。
8524これは全てを整数だけを使って処理することを意味するわけではなく、
5525これは全てを整数だけを使って処理することを意味するわけではないことに
8525単に Perl が算術、比較、ビット単位演算子で整数演算を
5526注意してください。
8526するというだけであることに注意てくださ
5527れは単に Perl が整数を使いたい思ったとき使うかもれな
8527例えば、S<C<use integer>> の指定があっても、C<sqrt(2)> とすると、
5528いうだけで
5529例えば、C<use integer> の指定があっても、C<sqrt(2)> とすると、
85285530C<1.4142135623731> といった結果が返ってきます。
85295531
85305532=begin original
85315533
8532Used on numbers, the bitwise operators (C<&> C<|> C<^> C<~> C<< << >>
5534Used on numbers, the bitwise operators ("&", "|", "^", "~", "<<",
8533C<< >> >>) always produce integral results. (But see also
5535and ">>") always produce integral results. (But see also
8534L</Bitwise String Operators>.) However, S<C<use integer>> still has meaning for
5536L<Bitwise String Operators>.) However, C<use integer> still has meaning for
85355537them. By default, their results are interpreted as unsigned integers, but
8536if S<C<use integer>> is in effect, their results are interpreted
5538if C<use integer> is in effect, their results are interpreted
85375539as signed integers. For example, C<~0> usually evaluates to a large
8538integral value. However, S<C<use integer; ~0>> is C<-1> on two's-complement
5540integral value. However, C<use integer; ~0> is C<-1> on two's-complement
85395541machines.
85405542
85415543=end original
85425544
8543数値を使う場合、ビット単位演算子 (C<&> C<|> C<^> C<~> C<< << >> C<< >> >>) は
5545数値を使う場合、ビット単位演算子 ("&", "|", "^", "~", "<<", ">>") は
8544常に整数の結果を生成します
5546常に整数の結果を生成します(但し L<Bitwise String Operators> も
8545(但し L</Bitwise String Operators> も参照して下さい)
5547参照して下さい)
8546しかし、それでも S<C<use integer>> は意味があります。
5548しかし、それでも C<use integer> は意味があります。
85475549デフォルトでは、これらの結果は符号なし整数として解釈されますが、
8548S<C<use integer>> が有効の場合は、符号付き整数として解釈されます。
5550C<use integer> が有効の場合は、符号付き整数として解釈されます。
85495551例えば、C<~0> は通常大きな整数の値として評価されます。
8550しかし、S<C<use integer; ~0>> は 2 の補数のマシンでは C<-1> になります。
5552しかし、C<use integer; ~0> は 2 の補数のマシンでは C<-1> になります。
85515553
85525554=head2 Floating-point Arithmetic
8553
8554(浮動小数点数演算)
8555
85565555X<floating-point> X<floating point> X<float> X<real>
85575556
5557(浮動小数点演算)
5558
85585559=begin original
85595560
8560While S<C<use integer>> provides integer-only arithmetic, there is no
5561While C<use integer> provides integer-only arithmetic, there is no
85615562analogous mechanism to provide automatic rounding or truncation to a
85625563certain number of decimal places. For rounding to a certain number
8563of digits, C<sprintf()> or C<printf()> is usually the easiest route.
5564of digits, sprintf() or printf() is usually the easiest route.
85645565See L<perlfaq4>.
85655566
85665567=end original
85675568
8568S<C<use integer>> が整数演算を提供する一方、数を特定の桁で自動的に丸めたり
5569C<use integer> が整数演算を提供する一方、数を特定の桁で自動的に丸めたり
85695570切り捨てたりする機構はありません。
8570数を丸めるには、C<sprintf()>C<printf()> を使うのが一番簡単な方法です。
5571数を丸めるには、sprintf() や printf() を使うのが一番簡単な方法です。
85715572L<perlfaq4> を参照して下さい。
85725573
85735574=begin original
85745575
85755576Floating-point numbers are only approximations to what a mathematician
85765577would call real numbers. There are infinitely more reals than floats,
85775578so some corners must be cut. For example:
85785579
85795580=end original
85805581
85815582浮動小数点数は数学者が実数と呼ぶものの近似でしかありません。
85825583実数は浮動小数点より無限に続くので、多少角が丸められます。
8583えば:
5584例:
85845585
85855586 printf "%.20g\n", 123456789123456789;
85865587 # produces 123456789123456784
85875588
85885589=begin original
85895590
8590Testing for exact floating-point equality or inequality is not a
5591Testing for exact equality of floating-point equality or inequality is
8591good idea. Here's a (relatively expensive) work-around to compare
5592not a good idea. Here's a (relatively expensive) work-around to compare
85925593whether two floating-point numbers are equal to a particular number of
85935594decimal places. See Knuth, volume II, for a more robust treatment of
85945595this topic.
85955596
85965597=end original
85975598
8598浮動小数点数が等しいかどうかをちょうど同じかどうかで比較するのはよい
5599浮動小数点数が等しいかどうかをちょうど同じかどうかで比較するのは
8599考えではありません。
5600いいアイデアではありません。
86005601以下に、二つの浮動小数点数が指定された桁まで等しいかどうかを
86015602比較する(比較的重い)次善の策を示します。
86025603この問題に関するより厳密な扱いについては Knuth, volume II を参照して下さい。
86035604
86045605 sub fp_equal {
86055606 my ($X, $Y, $POINTS) = @_;
86065607 my ($tX, $tY);
86075608 $tX = sprintf("%.${POINTS}g", $X);
86085609 $tY = sprintf("%.${POINTS}g", $Y);
86095610 return $tX eq $tY;
86105611 }
86115612
86125613=begin original
86135614
86145615The POSIX module (part of the standard perl distribution) implements
8615C<ceil()>, C<floor()>, and other mathematical and trigonometric functions.
5616ceil(), floor(), and other mathematical and trigonometric functions.
8616The C<L<Math::Complex>> module (part of the standard perl distribution)
5617The Math::Complex module (part of the standard perl distribution)
86175618defines mathematical functions that work on both the reals and the
8618imaginary numbers. C<Math::Complex> is not as efficient as POSIX, but
5619imaginary numbers. Math::Complex not as efficient as POSIX, but
86195620POSIX can't work with complex numbers.
86205621
86215622=end original
86225623
8623POSIX モジュール(Perl 標準配布パッケージの一部) は C<ceil()>, C<floor()> 及び
5624POSIX モジュール(Perl 標準配布パッケージの一部) は ceil(), floor() 及び
86245625その他の数学関数や三角関数を実装しています。
8625C<L<Math::Complex>> モジュール(Perl 標準配布パッケージの一部)は実数と虚数の
5626Math::Complex モジュール(Perl 標準配布パッケージの一部)は
8626両方で動作する数学関数を定義しています。
5627実数と虚数の両方で動作する数学関数を定義しています。
8627C<Math::Complex> は POSIX ほど効率的ではありませんが、POSIX は複素数は
5628Math::Complex は POSIX ほど効率的ではありませんが、
8628扱えません。
5629POSIX は複素数は扱えません。
86295630
86305631=begin original
86315632
86325633Rounding in financial applications can have serious implications, and
86335634the rounding method used should be specified precisely. In these
86345635cases, it probably pays not to trust whichever system rounding is
86355636being used by Perl, but to instead implement the rounding function you
86365637need yourself.
86375638
86385639=end original
86395640
86405641金融アプリケーションにおける丸めは深刻な影響を与える可能性があり、
86415642使用する丸めメソッドは指定された精度で行われるべきです。
86425643このような場合、Perl が使用するシステム丸めを信用せず、
86435644代わりに自分自身で丸め関数を実装するべきです。
86445645
86455646=head2 Bigger Numbers
86465647X<number, arbitrary precision>
86475648
86485649(より大きな数)
86495650
86505651=begin original
86515652
8652The standard C<L<Math::BigInt>>, C<L<Math::BigRat>>, and
5653The standard Math::BigInt and Math::BigFloat modules provide
8653C<L<Math::BigFloat>> modules,
8654along with the C<bignum>, C<bigint>, and C<bigrat> pragmas, provide
86555654variable-precision arithmetic and overloaded operators, although
8656they're currently pretty slow. At the cost of some space and
5655they're currently pretty slow. At the cost of some space and
86575656considerable speed, they avoid the normal pitfalls associated with
86585657limited-precision representations.
86595658
86605659=end original
86615660
8662標準の C<L<Math::BigInt>>, C<L<Math::BigRat>>,
5661標準の Math::BigInt Math::BigFloat モジュールは多倍長演算を提供し、
8663C<L<Math::BigFloat>> モジュールと
8664C<bignum>, C<bigint>, C<bigrat> プラグマは多倍長演算を提供し、
86655662演算子をオーバーロードしますが、これらは現在のところかなり遅いです。
86665663多少の領域とかなりの速度を犠牲にして、桁数が制限されていることによる
86675664ありがちな落とし穴を避けることができます。
86685665
8669 use 5.010;
5666 use Math::BigInt;
8670 use bigint; # easy interface to Math::BigInt
5667 $x = Math::BigInt->new('123456789123456789');
8671 $x = 123456789123456789;
5668 print $x * $x;
8672 say $x * $x;
8673 +15241578780673678515622620750190521
86745669
8675=begin original
5670 # prints +15241578780673678515622620750190521
86765671
8677Or with rationals:
8678
8679=end original
8680
8681あるいは分数を使って:
8682
8683 use 5.010;
8684 use bigrat;
8685 $x = 3/22;
8686 $y = 4/6;
8687 say "x/y is ", $x/$y;
8688 say "x*y is ", $x*$y;
8689 x/y is 9/44
8690 x*y is 1/11
8691
86925672=begin original
86935673
8694Several modules let you calculate with unlimited or fixed precision
5674There are several modules that let you calculate with (bound only by
8695(bound only by memory and CPU time). There
5675memory and cpu-time) unlimited or fixed precision. There are also
8696are also some non-standard modules that
5676some non-standard modules that provide faster implementations via
8697provide faster implementations via external C libraries.
5677external C libraries.
86985678
86995679=end original
87005680
87015681(メモリと CPU 時間のみに依存する)無制限か固定の精度での計算ができる
87025682モジュールがいくつかあります。
87035683さらに外部 C ライブラリを使ってより速い実装を提供する
87045684非標準のモジュールもあります。
87055685
87065686=begin original
87075687
87085688Here is a short, but incomplete summary:
87095689
87105690=end original
87115691
87125692以下は短いですが不完全なリストです。
87135693
87145694=begin original
87155695
8716 Math::String treat string sequences like numbers
5696 Math::Fraction big, unlimited fractions like 9973 / 12967
8717 Math::FixedPrecision calculate with a fixed precision
5697 Math::String treat string sequences like numbers
8718 Math::Currency for currency calculations
5698 Math::FixedPrecision calculate with a fixed precision
8719 Bit::Vector manipulate bit vectors fast (uses C)
5699 Math::Currency for currency calculations
8720 Math::BigIntFast Bit::Vector wrapper for big numbers
5700 Bit::Vector manipulate bit vectors fast (uses C)
8721 Math::Pari provides access to the Pari C library
5701 Math::BigIntFast Bit::Vector wrapper for big numbers
8722 Math::Cephes uses the external Cephes C library (no
5702 Math::Pari provides access to the Pari C library
8723 big numbers)
5703 Math::BigInteger uses an external C library
8724 Math::Cephes::Fraction fractions via the Cephes library
5704 Math::Cephes uses external Cephes C library (no big numbers)
8725 Math::GMP another one using an external C library
5705 Math::Cephes::Fraction fractions via the Cephes library
8726 Math::GMPz an alternative interface to libgmp's big ints
5706 Math::GMP another one using an external C library
8727 Math::GMPq an interface to libgmp's fraction numbers
8728 Math::GMPf an interface to libgmp's floating point numbers
87295707
87305708=end original
87315709
8732 Math::String 文字列を数値のように扱う
5710 Math::Fraction 9973 / 12967 のような、大きくて無制限の分数
8733 Math::FixedPrecision 固定精度で計算する
5711 Math::String 文字列を数値のように扱う
8734 Math::Currency 通貨の計算
5712 Math::FixedPrecision 固定精度で計算する
8735 Bit::Vector (C を使って)ビットベクタを速く操作する
5713 Math::Currency 通貨の計算用
8736 Math::BigIntFast 大きな数のための Bit::Vector のラパー
5714 Bit::Vector (C を使って)ビトベクタを速く操作する
8737 Math::Pari Pari C ライブラリへアクセスを提供する
5715 Math::BigIntFast 大きな数のための Bit::Vectorラッパー
8738 Math::Cephes 外部の Cephes C 使う(大きな数はなし)
5716 Math::Pari Pari C ライブラリへのアクセス提供する
8739 Math::Cephes::Fraction Cephes ライブラリを使った分数
5717 Math::BigInteger 外部 C ライブラリを使
8740 Math::GMP これも外部 C ライブラリを使う
5718 Math::Cephes 外部 Cephes C を使う(大きな数はなし)
8741 Math::GMPz libgmp の大きな整数へのもう一つのンターフェース
5719 Math::Cephes::Fraction Cephes ブラリを使った分数
8742 Math::GMPq libgmp の分数へのンターフェース
5720 Math::GMP これも外部 C ブラリを使う
8743 Math::GMPf libgmp の浮動小数点のインターフェース
87445721
87455722=begin original
87465723
87475724Choose wisely.
87485725
87495726=end original
87505727
87515728うまく選んでください。
87525729
87535730=cut
87545731
87555732=begin meta
87565733
87575734Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp> (5.000)
87585735Update: Kentaro Shirakata <argrath@ub32.org> (5.6.1-)
87595736Status: completed
87605737
87615738=end meta