perlop >
5.38.0
との差分
perlop 5.38.0 と 5.10.1 の差分
1 | 1 | |
2 | =encoding u | |
2 | =encoding euc-jp | |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | X<operator> |
6 | 6 | |
7 | 7 | =begin original |
8 | 8 | |
9 | 9 | perlop - Perl operators and precedence |
10 | 10 | |
11 | 11 | =end original |
12 | 12 | |
13 | 13 | perlop - Perl の演算子と優先順位 |
14 | 14 | |
15 | 15 | =head1 DESCRIPTION |
16 | 16 | |
17 | =begin original | |
18 | ||
19 | In Perl, the operator determines what operation is performed, | |
20 | independent of the type of the operands. For example S<C<$x + $y>> | |
21 | is always a numeric addition, and if C<$x> or C<$y> do not contain | |
22 | numbers, an attempt is made to convert them to numbers first. | |
23 | ||
24 | =end original | |
25 | ||
26 | Perl では、演算子はどんな演算を行うかをオペランドの型と独立して決定します。 | |
27 | 例えば S<C<$x + $y>> は常に数値加算で、C<$x> や C<$y> に数値でないものが | |
28 | 含まれている場合、まずそれらを数値に変換しようとします。 | |
29 | ||
30 | =begin original | |
31 | ||
32 | This is in contrast to many other dynamic languages, where the | |
33 | operation is determined by the type of the first argument. It also | |
34 | means that Perl has two versions of some operators, one for numeric | |
35 | and one for string comparison. For example S<C<$x == $y>> compares | |
36 | two numbers for equality, and S<C<$x eq $y>> compares two strings. | |
37 | ||
38 | =end original | |
39 | ||
40 | これは、最初の引数の型によって演算が決定されるその他の多くの動的言語と | |
41 | 対照的です。 | |
42 | これはまた、数値比較用と文字列比較用の 2 種類の演算子があるということです。 | |
43 | 例えば S<C<$x == $y>> は二つの数値の等価性を比較し、S<C<$x eq $y>> は二つの | |
44 | 文字列を比較します。 | |
45 | ||
46 | =begin original | |
47 | ||
48 | There are a few exceptions though: C<x> can be either string | |
49 | repetition or list repetition, depending on the type of the left | |
50 | operand, and C<&>, C<|>, C<^> and C<~> can be either string or numeric bit | |
51 | operations. | |
52 | ||
53 | =end original | |
54 | ||
55 | しかし、いくつかの例外があります: C<x> は左オペランドの型によって、 | |
56 | 文字列の繰り返しの場合とリストの繰り返しの場合があります; また | |
57 | C<&>, C<|>, C<^>, C<~> は文字列としてのビット演算と数値としてのビット演算の | |
58 | 場合があります。 | |
59 | ||
60 | 17 | =head2 Operator Precedence and Associativity |
61 | 18 | X<operator, precedence> X<precedence> X<associativity> |
62 | 19 | |
63 | 20 | (演算子の優先順位と結合性) |
64 | 21 | |
65 | 22 | =begin original |
66 | 23 | |
67 | 24 | Operator precedence and associativity work in Perl more or less like |
68 | 25 | they do in mathematics. |
69 | 26 | |
70 | 27 | =end original |
71 | 28 | |
72 | 29 | Perl での演算子の優先順位と結合性は多かれ少なかれ数学のものと似ています。 |
73 | 30 | |
74 | 31 | =begin original |
75 | 32 | |
76 | I<Operator precedence> means some operators | |
33 | I<Operator precedence> means some operators are evaluated before | |
77 | For example, in C<2 + 4 * 5>, the multiplication has higher | |
34 | others. For example, in C<2 + 4 * 5>, the multiplication has higher | |
78 | * 5> is | |
35 | precedence so C<4 * 5> is evaluated first yielding C<2 + 20 == | |
79 | ||
36 | 22> and not C<6 * 5 == 30>. | |
80 | multiplication. It is as if the expression were written C<2 + (4 * 5)>, not | |
81 | C<(2 + 4) * 5>. So the expression yields C<2 + 20 == 22>, rather than | |
82 | C<6 * 5 == 30>. | |
83 | 37 | |
84 | 38 | =end original |
85 | 39 | |
86 | I<演算子の優先順位> とは、他の演算子 | |
40 | I<演算子の優先順位> とは、他の演算子より先に評価される演算子が | |
87 | ||
41 | あるということです。 | |
88 | 42 | 例えば、C<2 + 4 * 5> の場合、乗算が高い優先順位を持っているので、 |
89 | C< | |
43 | C<4 * 5> が先に評価され、結果は C<6 * 5 == 30> ではなく、 | |
90 | C< | |
44 | C<2 + 20 == 22> となります。 | |
91 | これは、C<(2 + 4) * 5> ではなく、C<2 + (4 * 5)> と | |
92 | 書かれたかのようなものです。 | |
93 | 従って、この式は C<6 * 5 == 30> ではなく C<2 + 20 == 22> になります。 | |
94 | 45 | |
95 | 46 | =begin original |
96 | 47 | |
97 | I<Operator associativity> defines what happens if a sequence of the | |
48 | I<Operator associativity> defines what happens if a sequence of the | |
98 | operators is used one after another: | |
49 | same operators is used one after another: whether the evaluator will | |
99 | ||
50 | evaluate the left operations first or the right. For example, in C<8 | |
100 | ||
51 | - 4 - 2>, subtraction is left associative so Perl evaluates the | |
101 | so C< | |
52 | expression left to right. C<8 - 4> is evaluated first making the | |
102 | ||
53 | expression C<4 - 2 == 2> and not C<8 - 2 == 6>. | |
103 | operand of the first subtraction. It is as if the expression were written | |
104 | C<(9 - 3) - 2>, not C<9 - (3 - 2)>. So the expression yields C<6 - 2 == 4>, | |
105 | rather than C<9 - 1 == 8>. | |
106 | 54 | |
107 | 55 | =end original |
108 | 56 | |
109 | 57 | I<演算子の結合性> は、同じ演算子が連続して現れた場合に何が起こるかを |
110 | 定義します: | |
58 | 定義します: 評価器が左側を先に評価するか右側を先に評価するかです。 | |
111 | 例えば | |
59 | 例えば、C<8 - 4 - 2> の場合、減算は左結合なので、Perl は式を左から | |
112 | ||
60 | 右に評価します。 | |
113 | C< | |
61 | C<8 - 4> が先に評価されるので、C<8 - 2 == 6> ではなく | |
114 | ||
62 | C<4 - 2 == 2> となります。 | |
115 | 従って、この式は C<9 - 1 == 8> ではなく C<6 - 2 == 4> になります。 | |
116 | 63 | |
117 | 64 | =begin original |
118 | 65 | |
119 | For simple operators that evaluate all their operands and then combine the | |
120 | values in some way, precedence and associativity (and parentheses) imply some | |
121 | ordering requirements on those combining operations. For example, in C<2 + 4 * | |
122 | 5>, the grouping implied by precedence means that the multiplication of 4 and | |
123 | 5 must be performed before the addition of 2 and 20, simply because the result | |
124 | of that multiplication is required as one of the operands of the addition. But | |
125 | the order of operations is not fully determined by this: in C<2 * 2 + 4 * 5> | |
126 | both multiplications must be performed before the addition, but the grouping | |
127 | does not say anything about the order in which the two multiplications are | |
128 | performed. In fact Perl has a general rule that the operands of an operator | |
129 | are evaluated in left-to-right order. A few operators such as C<&&=> have | |
130 | special evaluation rules that can result in an operand not being evaluated at | |
131 | all; in general, the top-level operator in an expression has control of | |
132 | operand evaluation. | |
133 | ||
134 | =end original | |
135 | ||
136 | 全てのオペランドを評価してから何らかの形で値を結合する | |
137 | 単純な演算子の場合、優先順位と結合性(とかっこ)はそれらの結合操作で | |
138 | ある種の順序要求を暗示します。 | |
139 | 例えば C<2 + 4 * 5> では、 | |
140 | 優先順位が暗示するグループ化によって、 | |
141 | 4 と 5 の乗算は | |
142 | 2 と 20 の加算の前に行われる必要があります; | |
143 | 単にこの乗法の結果が加法のオペランドの一つとして必要だからです。 | |
144 | しかし、演算の順序はこれによって完全に決定されるわけではありません: | |
145 | C<2 * 2 + 4 * 5> では、両方の乗算は加算の前に行われなければなりませんが、 | |
146 | グループ化は二つの乗算が行われる順序については何も言っていません。 | |
147 | 実際の所、Perl には、演算子のオペランドは左から右の順で | |
148 | 評価されるという一般的な規則があります。 | |
149 | C<&&=> のようないくつかの演算子は、 | |
150 | 全く評価されないオペランドとなる特別な評価規則を持ちます; | |
151 | 一般的に、式の中の最上位の演算子がオペランド評価を制御します。 | |
152 | ||
153 | =begin original | |
154 | ||
155 | Some comparison operators, as their associativity, I<chain> with some | |
156 | operators of the same precedence (but never with operators of different | |
157 | precedence). This chaining means that each comparison is performed | |
158 | on the two arguments surrounding it, with each interior argument taking | |
159 | part in two comparisons, and the comparison results are implicitly ANDed. | |
160 | Thus S<C<"$x E<lt> $y E<lt>= $z">> behaves exactly like S<C<"$x E<lt> | |
161 | $y && $y E<lt>= $z">>, assuming that C<"$y"> is as simple a scalar as | |
162 | it looks. The ANDing short-circuits just like C<"&&"> does, stopping | |
163 | the sequence of comparisons as soon as one yields false. | |
164 | ||
165 | =end original | |
166 | ||
167 | 一部の比較演算子は、それらの優先順位と、 | |
168 | 同じ優先順位を持つ一部の演算子と I<連鎖> させることができます | |
169 | (異なる優先順位を持つ演算子とはできません)。 | |
170 | 連鎖というのは、それぞれの比較はそれらの周りの二つの引数に対して | |
171 | 行われ、それぞれの内部引数は二つの比較の一部となり、比較結果は | |
172 | 暗黙に AND されるということです。 | |
173 | 従って S<C<"$x E<lt> $y E<lt>= $z">> は、C<"$y"> が見た目単純な | |
174 | スカラだと仮定すると、 | |
175 | S<C<"$x E<lt> $y && $y E<lt>= $z">> と正確に同じように振る舞います。 | |
176 | AND の短絡は C<"&&"> と同様に行われ、一つが偽となった時点で一連の連鎖は | |
177 | 停止します。 | |
178 | ||
179 | =begin original | |
180 | ||
181 | In a chained comparison, each argument expression is evaluated at most | |
182 | once, even if it takes part in two comparisons, but the result of the | |
183 | evaluation is fetched for each comparison. (It is not evaluated | |
184 | at all if the short-circuiting means that it's not required for any | |
185 | comparisons.) This matters if the computation of an interior argument | |
186 | is expensive or non-deterministic. For example, | |
187 | ||
188 | =end original | |
189 | ||
190 | 連鎖比較において、それぞれの引数式は、例え二つの比較の一部となったとしても、 | |
191 | 評価されるのは最大 1 回ですが、評価の結果はそれぞれの比較毎に取得されます。 | |
192 | (短絡によって比較が必要でない場合は、まったく評価されません。) | |
193 | これは、内側の引数の計算が高価だったり非決定的だったりする場合に | |
194 | 問題になります。 | |
195 | 例えば: | |
196 | ||
197 | if($x < expensive_sub() <= $z) { ... | |
198 | ||
199 | =begin original | |
200 | ||
201 | is not entirely like | |
202 | ||
203 | =end original | |
204 | ||
205 | これは全体的に次のようなものではなく: | |
206 | ||
207 | if($x < expensive_sub() && expensive_sub() <= $z) { ... | |
208 | ||
209 | =begin original | |
210 | ||
211 | but instead closer to | |
212 | ||
213 | =end original | |
214 | ||
215 | しかし次のものに近いです: | |
216 | ||
217 | my $tmp = expensive_sub(); | |
218 | if($x < $tmp && $tmp <= $z) { ... | |
219 | ||
220 | =begin original | |
221 | ||
222 | in that the subroutine is only called once. However, it's not exactly | |
223 | like this latter code either, because the chained comparison doesn't | |
224 | actually involve any temporary variable (named or otherwise): there is | |
225 | no assignment. This doesn't make much difference where the expression | |
226 | is a call to an ordinary subroutine, but matters more with an lvalue | |
227 | subroutine, or if the argument expression yields some unusual kind of | |
228 | scalar by other means. For example, if the argument expression yields | |
229 | a tied scalar, then the expression is evaluated to produce that scalar | |
230 | at most once, but the value of that scalar may be fetched up to twice, | |
231 | once for each comparison in which it is actually used. | |
232 | ||
233 | =end original | |
234 | ||
235 | ここでサブルーチンは 1 回だけ呼び出されます。 | |
236 | しかし、正確に後者のコードのようなものでもありません; なぜなら | |
237 | 連鎖比較は実際には(名前付きかどうかにかかわらず)一時変数を使わないからです; | |
238 | 代入はありません。 | |
239 | これは、式が通常のサブルーチンとして呼び出されるときにはあまり違いは | |
240 | ありませんが、左辺値サブルーチンの呼び出しの場合や、引数式が | |
241 | 他の手段によって何らかの普通でない種類のスカラになった場合には | |
242 | より問題になります。 | |
243 | 例えば、引数式が tie されたスカラになった場合、式はスカラを作るために | |
244 | 最大 1 回評価されますが、スカラの値は、実際に使われる比較毎に 1 回、 | |
245 | 合計 2 回読み込まれます。 | |
246 | ||
247 | =begin original | |
248 | ||
249 | In this example, the expression is evaluated only once, and the tied | |
250 | scalar (the result of the expression) is fetched for each comparison that | |
251 | uses it. | |
252 | ||
253 | =end original | |
254 | ||
255 | この例で、式は 1 回だけ評価され、tie されたスカラ (式の結果) は、 | |
256 | これら使われる比較毎に取得されます。 | |
257 | ||
258 | if ($x < $tied_scalar < $z) { ... | |
259 | ||
260 | =begin original | |
261 | ||
262 | In the next example, the expression is evaluated only once, and the tied | |
263 | scalar is fetched once as part of the operation within the expression. | |
264 | The result of that operation is fetched for each comparison, which | |
265 | normally doesn't matter unless that expression result is also magical due | |
266 | to operator overloading. | |
267 | ||
268 | =end original | |
269 | ||
270 | 次の例では、式は 1 回だけ評価され、 tie されたスカラは式の中の演算の | |
271 | 一部として 1 回だけ取得されます。 | |
272 | この演算の結果は比較毎に取得されます; | |
273 | 通常これは、式の結果も演算子オーバーロードによってマジカルでない限り、 | |
274 | 関係ありません。 | |
275 | ||
276 | if ($x < $tied_scalar + 42 < $z) { ... | |
277 | ||
278 | =begin original | |
279 | ||
280 | Some operators are instead non-associative, meaning that it is a syntax | |
281 | error to use a sequence of those operators of the same precedence. | |
282 | For example, S<C<"$x .. $y .. $z">> is an error. | |
283 | ||
284 | =end original | |
285 | ||
286 | 一部の演算子は非結合です; 同じ優先順位の演算子の並びを使うと | |
287 | 文法エラーになります。 | |
288 | 例えば、S<C<"$x .. $y .. $z">> はエラーです。 | |
289 | ||
290 | =begin original | |
291 | ||
292 | 66 | Perl operators have the following associativity and precedence, |
293 | 67 | listed from highest precedence to lowest. Operators borrowed from |
294 | 68 | C keep the same precedence relationship with each other, even where |
295 | 69 | C's precedence is slightly screwy. (This makes learning Perl easier |
296 | 70 | for C folks.) With very few exceptions, these all operate on scalar |
297 | 71 | values only, not array values. |
298 | 72 | |
299 | 73 | =end original |
300 | 74 | |
301 | 75 | Perl の演算子には、以下のような結合性と優先順位 (高い優先順位から |
302 | 76 | 低いものへ並べている) があります。 |
303 | 77 | C から持ってきた演算子の優先順位は、C での優先順位が多少おかしくても、 |
304 | 78 | そのままにしてあります。 |
305 | 79 | (これによって、C を使っている方が Perl に移りやすくなっています。) |
306 | 80 | ごく僅かな例外を別として、全ての演算子はスカラ値のみを持ち、 |
307 | 81 | 配列値を持ちません。 |
308 | 82 | |
309 | 83 | =begin original |
310 | 84 | |
311 | 85 | left terms and list operators (leftward) |
312 | 86 | left -> |
313 | 87 | nonassoc ++ -- |
314 | 88 | right ** |
315 | right ! ~ | |
89 | right ! ~ \ and unary + and - | |
316 | 90 | left =~ !~ |
317 | 91 | left * / % x |
318 | 92 | left + - . |
319 | 93 | left << >> |
320 | 94 | nonassoc named unary operators |
321 | nonassoc | |
95 | nonassoc < > <= >= lt gt le ge | |
322 | | |
96 | nonassoc == != <=> eq ne cmp ~~ | |
323 | | |
97 | left & | |
324 | left | |
98 | left | ^ | |
325 | left | |. ^ ^. | |
326 | 99 | left && |
327 | 100 | left || // |
328 | 101 | nonassoc .. ... |
329 | 102 | right ?: |
330 | right = += -= *= etc. | |
103 | right = += -= *= etc. | |
331 | 104 | left , => |
332 | 105 | nonassoc list operators (rightward) |
333 | 106 | right not |
334 | 107 | left and |
335 | 108 | left or xor |
336 | 109 | |
337 | 110 | =end original |
338 | 111 | |
339 | 112 | 左結合 項 リスト演算子 (左方向に対して) |
340 | 113 | 左結合 -> |
341 | 114 | 非結合 ++ -- |
342 | 115 | 右結合 ** |
343 | 右結合 ! ~ | |
116 | 右結合 ! ~ \ 単項の+ 単項の- | |
344 | 117 | 左結合 =~ !~ |
345 | 118 | 左結合 * / % x |
346 | 119 | 左結合 + - . |
347 | 120 | 左結合 << >> |
348 | 121 | 非結合 名前付き単項演算子 |
349 | | |
122 | 非結合 < > <= >= lt gt le ge | |
350 | | |
123 | 非結合 == != <=> eq ne cmp ~~ | |
351 | | |
124 | 左結合 & | |
352 | 左結合 | |
125 | 左結合 | ^ | |
353 | 左結合 | |. ^ ^. | |
354 | 126 | 左結合 && |
355 | 127 | 左結合 || // |
356 | 128 | 非結合 .. ... |
357 | 129 | 右結合 ?: |
358 | 右結合 = += -= *= など | |
130 | 右結合 = += -= *= などの代入演算子 | |
359 | 131 | 左結合 , => |
360 | 132 | 非結合 リスト演算子 (右方向に対して) |
361 | 133 | 右結合 not |
362 | 134 | 左結合 and |
363 | 135 | 左結合 or xor |
364 | 136 | |
365 | 137 | =begin original |
366 | 138 | |
367 | In the following sections, these operators are covered in de | |
139 | In the following sections, these operators are covered in precedence order. | |
368 | same order in which they appear in the table above. | |
369 | 140 | |
370 | 141 | =end original |
371 | 142 | |
372 | 以下の章では、 | |
143 | 以下の章では、演算子は優先順位の順に記述されています。 | |
373 | 144 | |
374 | 145 | =begin original |
375 | 146 | |
376 | 147 | Many operators can be overloaded for objects. See L<overload>. |
377 | 148 | |
378 | 149 | =end original |
379 | 150 | |
380 | 151 | 多くの演算子はオブジェクトでオーバーロードできます。 |
381 | 152 | L<overload> を参照して下さい。 |
382 | 153 | |
383 | 154 | =head2 Terms and List Operators (Leftward) |
384 | 155 | X<list operator> X<operator, list> X<term> |
385 | 156 | |
386 | 157 | (項とリスト演算子 (左方向)) |
387 | 158 | |
388 | 159 | =begin original |
389 | 160 | |
390 | 161 | A TERM has the highest precedence in Perl. They include variables, |
391 | 162 | quote and quote-like operators, any expression in parentheses, |
392 | 163 | and any function whose arguments are parenthesized. Actually, there |
393 | 164 | aren't really functions in this sense, just list operators and unary |
394 | 165 | operators behaving as functions because you put parentheses around |
395 | 166 | the arguments. These are all documented in L<perlfunc>. |
396 | 167 | |
397 | 168 | =end original |
398 | 169 | |
399 | 170 | 「項」は Perl でもっとも優先順位が高いものです。 |
400 | 171 | これには、変数、クォートとクォート的な演算子、括弧で括った任意の式、 |
401 | 172 | 引数を括弧で括った任意の関数が含まれます。 |
402 | 173 | 実際には、この意味では本当の関数はなく、リスト演算子と関数のように働く |
403 | 174 | 単項演算子が、引数を括弧で括るためそのように見えます。 |
404 | 175 | これらはすべて L<perlfunc> に記述しています。 |
405 | 176 | |
406 | 177 | =begin original |
407 | 178 | |
408 | If any list operator ( | |
179 | If any list operator (print(), etc.) or any unary operator (chdir(), etc.) | |
409 | 180 | is followed by a left parenthesis as the next token, the operator and |
410 | 181 | arguments within parentheses are taken to be of highest precedence, |
411 | 182 | just like a normal function call. |
412 | 183 | |
413 | 184 | =end original |
414 | 185 | |
415 | リスト演算子 ( | |
186 | もし、リスト演算子 (print() など) や単項演算子 (chdir() など)の | |
416 | ||
187 | 名前の後に開き括弧が続く場合には、その演算子と括弧内の引数は、 | |
417 | 通常の関数呼び出しのようにもっとも高い優先順位 | |
188 | 通常の関数呼び出しのように、もっとも高い優先順位で処理されます。 | |
418 | 189 | |
419 | 190 | =begin original |
420 | 191 | |
421 | 192 | In the absence of parentheses, the precedence of list operators such as |
422 | 193 | C<print>, C<sort>, or C<chmod> is either very high or very low depending on |
423 | 194 | whether you are looking at the left side or the right side of the operator. |
424 | 195 | For example, in |
425 | 196 | |
426 | 197 | =end original |
427 | 198 | |
428 | 199 | 括弧が無い場合には、C<print>、C<sort>、C<chmod> のようなリスト演算子の |
429 | 200 | 優先順位は、演算子の左側をからすると非常に高く、右側からすると |
430 | 201 | 非常に低く見えます。たとえば、 |
431 | 202 | |
432 | 203 | @ary = (1, 3, sort 4, 2); |
433 | 204 | print @ary; # prints 1324 |
434 | 205 | |
435 | 206 | =begin original |
436 | 207 | |
437 | the commas on the right of the | |
208 | the commas on the right of the sort are evaluated before the sort, | |
438 | 209 | but the commas on the left are evaluated after. In other words, |
439 | 210 | list operators tend to gobble up all arguments that follow, and |
440 | 211 | then act like a simple TERM with regard to the preceding expression. |
441 | 212 | Be careful with parentheses: |
442 | 213 | |
443 | 214 | =end original |
444 | 215 | |
445 | では、 | |
216 | では、sort の右のコンマは sort よりも前に評価されます (右側から見ると | |
446 | ||
217 | sort の優先順位が低い) が、左側のコンマは sort のあとに評価されます | |
218 | (左側から見ると sort の方が優先順位が高くなっている)。 | |
447 | 219 | 言い方を変えると、リスト演算子は自分の後にある引数をすべて使って処理を行ない、 |
448 | 220 | その結果を自分の前の式に対する「項」であるかのように見せるということです。 |
449 | 221 | ただし、括弧には気を付けないといけません: |
450 | 222 | |
451 | 223 | =begin original |
452 | 224 | |
453 | 225 | # These evaluate exit before doing the print: |
454 | 226 | print($foo, exit); # Obviously not what you want. |
455 | 227 | print $foo, exit; # Nor is this. |
456 | 228 | |
457 | 229 | =end original |
458 | 230 | |
459 | 231 | # 以下は print を行なう前に exit を評価します: |
460 | 232 | print($foo, exit); # 明らかにやりたいことではないでしょう。 |
461 | 233 | print $foo, exit; # これでもない。 |
462 | 234 | |
463 | 235 | =begin original |
464 | 236 | |
465 | 237 | # These do the print before evaluating exit: |
466 | 238 | (print $foo), exit; # This is what you want. |
467 | 239 | print($foo), exit; # Or this. |
468 | 240 | print ($foo), exit; # Or even this. |
469 | 241 | |
470 | 242 | =end original |
471 | 243 | |
472 | 244 | # 以下は exit を評価する前に print を行ないます: |
473 | 245 | (print $foo), exit; # これがしたかった。 |
474 | 246 | print($foo), exit; # これでもいい。 |
475 | 247 | print ($foo), exit; # これも OK。 |
476 | 248 | |
477 | 249 | =begin original |
478 | 250 | |
479 | 251 | Also note that |
480 | 252 | |
481 | 253 | =end original |
482 | 254 | |
483 | 255 | また、 |
484 | 256 | |
485 | 257 | print ($foo & 255) + 1, "\n"; |
486 | 258 | |
487 | 259 | =begin original |
488 | 260 | |
489 | 261 | probably doesn't do what you expect at first glance. The parentheses |
490 | 262 | enclose the argument list for C<print> which is evaluated (printing |
491 | the result of | |
263 | the result of C<$foo & 255>). Then one is added to the return value | |
492 | 264 | of C<print> (usually 1). The result is something like this: |
493 | 265 | |
494 | 266 | =end original |
495 | 267 | |
496 | 268 | の動作を一目見ただけで判断するのは、難しいでしょう。 |
497 | 269 | かっこは C<print> のために評価される引数リストを囲っています |
498 | ( | |
270 | (C<$foo & 255> の結果が表示されます)。 | |
499 | 271 | それから C<print> の返り値 (通常は 1) に 1 が加えられます。 |
500 | 272 | 結果は以下のようになります: |
501 | 273 | |
502 | =begin original | |
503 | ||
504 | 274 | 1 + 1, "\n"; # Obviously not what you meant. |
505 | 275 | |
506 | =end original | |
507 | ||
508 | 1 + 1, "\n"; # 明らかにやりたいことではないでしょう。 | |
509 | ||
510 | 276 | =begin original |
511 | 277 | |
512 | 278 | To do what you meant properly, you must write: |
513 | 279 | |
514 | 280 | =end original |
515 | 281 | |
516 | 282 | 意味したいことを適切に行うには、以下のように書く必要があります: |
517 | 283 | |
518 | 284 | print(($foo & 255) + 1, "\n"); |
519 | 285 | |
520 | 286 | =begin original |
521 | 287 | |
522 | See L< | |
288 | See L<Named Unary Operators> for more discussion of this. | |
523 | 289 | |
524 | 290 | =end original |
525 | 291 | |
526 | 詳しくは、L< | |
292 | 詳しくは、L<Named Unary Operators> を参照してください。 | |
527 | 293 | |
528 | 294 | =begin original |
529 | 295 | |
530 | Also parsed as terms are the | |
296 | Also parsed as terms are the C<do {}> and C<eval {}> constructs, as | |
531 | 297 | well as subroutine and method calls, and the anonymous |
532 | 298 | constructors C<[]> and C<{}>. |
533 | 299 | |
534 | 300 | =end original |
535 | 301 | |
536 | この他に「項」として解析されるものには、 | |
302 | この他に「項」として解析されるものには、C<do {}> や C<eval {}> の | |
537 | 303 | 構成、サブルーティンやメソッドの呼び出し、無名のコンストラクタ |
538 | 304 | C<[]> と C<{}> があります。 |
539 | 305 | |
540 | 306 | =begin original |
541 | 307 | |
542 | See also L< | |
308 | See also L<Quote and Quote-like Operators> toward the end of this section, | |
543 | 309 | as well as L</"I/O Operators">. |
544 | 310 | |
545 | 311 | =end original |
546 | 312 | |
547 | 後の方の | |
313 | 後の方のL<Quote and Quote-like Operators>や | |
548 | L</"I/O Operators"> | |
314 | L</"I/O Operators">も参照してください。 | |
549 | 315 | |
550 | 316 | =head2 The Arrow Operator |
551 | 317 | X<arrow> X<dereference> X<< -> >> |
552 | 318 | |
553 | 319 | (矢印演算子) |
554 | 320 | |
555 | 321 | =begin original |
556 | 322 | |
557 | 323 | "C<< -> >>" is an infix dereference operator, just as it is in C |
558 | 324 | and C++. If the right side is either a C<[...]>, C<{...}>, or a |
559 | 325 | C<(...)> subscript, then the left side must be either a hard or |
560 | 326 | symbolic reference to an array, a hash, or a subroutine respectively. |
561 | 327 | (Or technically speaking, a location capable of holding a hard |
562 | 328 | reference, if it's an array or hash reference being used for |
563 | 329 | assignment.) See L<perlreftut> and L<perlref>. |
564 | 330 | |
565 | 331 | =end original |
566 | 332 | |
567 | 333 | C や C++ と同じように "C<< -> >>" は中置の被参照演算子です。 |
568 | 右側が C<[...]>, C<{...}>, | |
334 | 右側が C<[...]>, C<{...}>, | |
569 | ハッシュ、 | |
335 | C<(...)> のいずれかの形の添字であれば、左側は配列、ハッシュ、 | |
570 | シンボリックリファレンス | |
336 | サブルーチンへのハードリファレンスかシンボリックリファレンス (あるいは | |
571 | ||
337 | 技術的には、配列またはハードリファレンスが代入可能であれば | |
572 | ハードリファレンスを保持できる場所 | |
338 | ハードリファレンスを保持できる場所) | |
573 | L<perlreftut> と L<perlref> を参照してください。 | |
339 | でなければなりません。L<perlreftut> と L<perlref> を参照してください。 | |
574 | 340 | |
575 | 341 | =begin original |
576 | 342 | |
577 | 343 | Otherwise, the right side is a method name or a simple scalar |
578 | 344 | variable containing either the method name or a subroutine reference, |
579 | and | |
345 | and the left side must be either an object (a blessed reference) | |
580 | ||
346 | or a class name (that is, a package name). See L<perlobj>. | |
581 | L<perlobj>. | |
582 | 347 | |
583 | 348 | =end original |
584 | 349 | |
585 | 350 | そうでなければ、右側はメソッド名かサブルーチンのリファレンスを持った |
586 | 単純スカラ変数で、 | |
351 | 単純スカラ変数で、左側はオブジェクト (bless されたリファレンス) か | |
587 | (メソッド名なら) 左側はオブジェクト (bless されたリファレンス) か | |
588 | 352 | クラス名でなければなりません。 |
589 | 353 | L<perlobj> を参照してください。 |
590 | 354 | |
591 | =begin original | |
592 | ||
593 | The dereferencing cases (as opposed to method-calling cases) are | |
594 | somewhat extended by the C<postderef> feature. For the | |
595 | details of that feature, consult L<perlref/Postfix Dereference Syntax>. | |
596 | ||
597 | =end original | |
598 | ||
599 | (メソッド呼び出しの場合ではなく) デリファレンスの場合、 | |
600 | C<後置デリファレンス> (postderef) 機能によっていくらか拡張されます。 | |
601 | この機能の詳細については、L<perlref/Postfix Dereference Syntax> を | |
602 | 参照してください。 | |
603 | ||
604 | 355 | =head2 Auto-increment and Auto-decrement |
605 | 356 | X<increment> X<auto-increment> X<++> X<decrement> X<auto-decrement> X<--> |
606 | 357 | |
607 | 358 | (インクリメントとデクリメント) |
608 | 359 | |
609 | 360 | =begin original |
610 | 361 | |
611 | ||
362 | "++" and "--" work as in C. That is, if placed before a variable, | |
612 | 363 | they increment or decrement the variable by one before returning the |
613 | 364 | value, and if placed after, increment or decrement after returning the |
614 | 365 | value. |
615 | 366 | |
616 | 367 | =end original |
617 | 368 | |
618 | ||
369 | "++" と "--" は、C の場合と同じように動作します。 | |
619 | 370 | つまり、変数の前に置かれれば、値を返す前に変数を 1 インクリメントまたは |
620 | 371 | デクリメントし、後に置かれれば、値を返した後で変数を |
621 | 372 | インクリメントまたはデクリメントします。 |
622 | 373 | |
623 | 374 | $i = 0; $j = 0; |
624 | 375 | print $i++; # prints 0 |
625 | 376 | print ++$j; # prints 1 |
626 | 377 | |
627 | 378 | =begin original |
628 | 379 | |
629 | 380 | Note that just as in C, Perl doesn't define B<when> the variable is |
630 | incremented or decremented. | |
381 | incremented or decremented. You just know it will be done sometime | |
631 | before or after the value is returned. | |
382 | before or after the value is returned. This also means that modifying | |
632 | a variable twice in the same statement will lead to undefined behavior. | |
383 | a variable twice in the same statement will lead to undefined behaviour. | |
633 | 384 | Avoid statements like: |
634 | 385 | |
635 | 386 | =end original |
636 | 387 | |
637 | 388 | C と同様、Perl は B<いつ> 変数がインクリメントまたはデクリメントされるかは |
638 | 389 | 定義されません。 |
639 | 390 | 値が返される前か後のどこかで行われる、ということだけがわかります。 |
640 | 391 | これは、同じ文である変数を 2 回修正すると、振る舞いが未定義になることを |
641 | 392 | 意味します。 |
642 | 393 | 以下のような文は避けてください: |
643 | 394 | |
644 | 395 | $i = $i ++; |
645 | 396 | print ++ $i + $i ++; |
646 | 397 | |
647 | 398 | =begin original |
648 | 399 | |
649 | 400 | Perl will not guarantee what the result of the above statements is. |
650 | 401 | |
651 | 402 | =end original |
652 | 403 | |
653 | 404 | Perl は上記の文の結果について保障しません。 |
654 | 405 | |
655 | 406 | =begin original |
656 | 407 | |
657 | 408 | The auto-increment operator has a little extra builtin magic to it. If |
658 | 409 | you increment a variable that is numeric, or that has ever been used in |
659 | 410 | a numeric context, you get a normal increment. If, however, the |
660 | 411 | variable has been used in only string contexts since it was set, and |
661 | 412 | has a value that is not the empty string and matches the pattern |
662 | 413 | C</^[a-zA-Z]*[0-9]*\z/>, the increment is done as a string, preserving each |
663 | 414 | character within its range, with carry: |
664 | 415 | |
665 | 416 | =end original |
666 | 417 | |
667 | 418 | インクリメント演算子には、ちょっと風変わりな機能が組み込まれています。 |
668 | 419 | 数値が入った変数や、数値の文脈で使われてきた変数を |
669 | 420 | インクリメントする場合には、通常のインクリメントとして動作します。 |
670 | しかし、その変数が設定されてからずっと文字列の文脈で | |
421 | しかし、その変数が設定されてからずっと文字列の文脈で | |
671 | 空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする | |
422 | しか使われていなくて、空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする | |
672 | 個々の文字の範囲を保ちながら桁あげを行なって、 | |
423 | 値を持っているときには、個々の文字の範囲を保ちながら桁あげを行なって、 | |
673 | 行なわれます (マジカルインクリメントと呼ばれます): | |
424 | 文字列としてインクリメントが行なわれます (マジカルインクリメントと呼ばれます): | |
674 | 425 | |
675 | print ++($foo = | |
426 | print ++($foo = '99'); # prints '100' | |
676 | print ++($foo = | |
427 | print ++($foo = 'a0'); # prints 'a1' | |
677 | print ++($foo = | |
428 | print ++($foo = 'Az'); # prints 'Ba' | |
678 | print ++($foo = | |
429 | print ++($foo = 'zz'); # prints 'aaa' | |
679 | 430 | |
680 | 431 | =begin original |
681 | 432 | |
682 | 433 | C<undef> is always treated as numeric, and in particular is changed |
683 | 434 | to C<0> before incrementing (so that a post-increment of an undef value |
684 | 435 | will return C<0> rather than C<undef>). |
685 | 436 | |
686 | 437 | =end original |
687 | 438 | |
688 | 439 | C<undef> は常に数値として扱われ、特にインクリメントされる前には C<0> に |
689 | 440 | 変換されます(従って、undef のポストインクリメント値は C<undef> ではなく |
690 | 441 | C<0> になります)。 |
691 | 442 | |
692 | 443 | =begin original |
693 | 444 | |
694 | 445 | The auto-decrement operator is not magical. |
695 | 446 | |
696 | 447 | =end original |
697 | 448 | |
698 | 449 | デクリメント演算子には、マジカルなものはありません。 |
699 | 450 | |
700 | 451 | =head2 Exponentiation |
701 | 452 | X<**> X<exponentiation> X<power> |
702 | 453 | |
703 | 454 | (指数演算子) |
704 | 455 | |
705 | 456 | =begin original |
706 | 457 | |
707 | Binary | |
458 | Binary "**" is the exponentiation operator. It binds even more | |
708 | tightly than unary minus, so | |
459 | tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is | |
709 | ( | |
460 | implemented using C's pow(3) function, which actually works on doubles | |
710 | implemented using C's C<pow(3)> function, which actually works on doubles | |
711 | 461 | internally.) |
712 | 462 | |
713 | 463 | =end original |
714 | 464 | |
715 | 二項演算子の | |
465 | 二項演算子の "**" は指数演算子です。 | |
716 | 466 | この演算子は、単項のマイナスよりも結合が強い演算子で、 |
717 | ||
467 | -2**4 は (-2)**4 ではなく、-(2**4) と解釈されます。 | |
718 | (これは C の | |
468 | (これは C の pow(3) を使って実装されていますので、 | |
719 | 469 | 内部的には double で動作します。) |
720 | 470 | |
721 | =begin original | |
722 | ||
723 | Note that certain exponentiation expressions are ill-defined: | |
724 | these include C<0**0>, C<1**Inf>, and C<Inf**0>. Do not expect | |
725 | any particular results from these special cases, the results | |
726 | are platform-dependent. | |
727 | ||
728 | =end original | |
729 | ||
730 | 一部の指数表現は明確に定義されていません: | |
731 | これは C<0**0>, C<1**Inf>, C<Inf**0> などです。 | |
732 | これらの特殊な場合に関して特定の結果を想定しないでください; | |
733 | 結果はプラットフォーム依存です。 | |
734 | ||
735 | 471 | =head2 Symbolic Unary Operators |
736 | 472 | X<unary operator> X<operator, unary> |
737 | 473 | |
738 | 474 | (単項演算子) |
739 | 475 | |
740 | 476 | =begin original |
741 | 477 | |
742 | Unary | |
478 | Unary "!" performs logical negation, i.e., "not". See also C<not> for a lower | |
743 | ||
479 | precedence version of this. | |
744 | 480 | X<!> |
745 | 481 | |
746 | 482 | =end original |
747 | 483 | |
748 | 単項演算子の | |
484 | 単項演算子の "!" は論理否定を行ないます。 | |
749 | こ | |
485 | つまり 「not」 ということです。 | |
750 | ||
486 | この演算子の優先順位を低くしたものとして、C<not> が用意されています。 | |
751 | 487 | X<!> |
752 | 488 | |
753 | 489 | =begin original |
754 | 490 | |
755 | Unary | |
491 | Unary "-" performs arithmetic negation if the operand is numeric. If | |
756 | ||
492 | the operand is an identifier, a string consisting of a minus sign | |
757 | an identifier | |
493 | concatenated with the identifier is returned. Otherwise, if the string | |
758 | with | |
494 | starts with a plus or minus, a string starting with the opposite sign | |
759 | ||
495 | is returned. One effect of these rules is that -bareword is equivalent | |
760 | ||
496 | to the string "-bareword". If, however, the string begins with a | |
761 | ||
497 | non-alphabetic character (excluding "+" or "-"), Perl will attempt to convert | |
762 | no | |
498 | the string to a numeric and the arithmetic negation is performed. If the | |
763 | to convert | |
764 | the string to a numeric, and the arithmetic negation is performed. If the | |
765 | 499 | string cannot be cleanly converted to a numeric, Perl will give the warning |
766 | 500 | B<Argument "the string" isn't numeric in negation (-) at ...>. |
767 | 501 | X<-> X<negation, arithmetic> |
768 | 502 | |
769 | 503 | =end original |
770 | 504 | |
771 | 単項演算子の | |
505 | 単項演算子の "-" は被演算子が数値であれば、算術否定を行ないます。 | |
772 | 算 | |
506 | 被演算子が識別子ならば、マイナス記号にその識別子をつなげた | |
773 | ||
507 | 文字列が返されます。 | |
774 | 508 | これ以外で被演算子の最初の文字がプラスかマイナスのときには、 |
775 | 509 | その記号を逆のものに置き換えた文字列を返します。 |
776 | この規則の結果、 | |
510 | この規則の結果、-bareword が文字列 "-bareword" に等価となります。 | |
777 | しかし、文字列が英字以外( | |
511 | しかし、文字列が英字以外("+" と "-" を除く)で始まっていると、 | |
778 | 512 | Perl は文字列を数値に変換しようとし、それから算術否定が実行されます。 |
779 | 513 | もし文字列が明確に数値に変換できない場合、Perl は |
780 | 514 | B<Argument "the string" isn't numeric in negation (-) at ...> という |
781 | 515 | 警告を出します。 |
782 | 516 | X<-> X<negation, arithmetic> |
783 | 517 | |
784 | 518 | =begin original |
785 | 519 | |
786 | Unary | |
520 | Unary "~" performs bitwise negation, i.e., 1's complement. For | |
787 | example, | |
521 | example, C<0666 & ~027> is 0640. (See also L<Integer Arithmetic> and | |
788 | L< | |
522 | L<Bitwise String Operators>.) Note that the width of the result is | |
789 | platform-dependent: | |
523 | platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64 | |
790 | 524 | bits wide on a 64-bit platform, so if you are expecting a certain bit |
791 | width, remember to use the | |
525 | width, remember to use the & operator to mask off the excess bits. | |
792 | 526 | X<~> X<negation, binary> |
793 | 527 | |
794 | 528 | =end original |
795 | 529 | |
796 | 単項演算子の | |
530 | 単項演算子の "~" はビットごとの否定を行ないます。 | |
797 | ||
531 | つまり、1 の補数を返します。 | |
798 | ||
532 | 例えば、C<0666 & ~027> は 0640 です。 | |
799 | ||
533 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) | |
800 | ||
534 | 結果の幅はプラットホーム依存であることに注意してください。 | |
801 | ||
535 | ~0 は 32-bit プラットホームでは 32 ビット幅ですが、 | |
802 | ||
536 | 64-bit プラットホームでは 64 ビット幅ですので、 | |
537 | 特定のビット幅を仮定する場合は、 | |
538 | 余分なビットをマスクするために & 演算子を使うことを忘れないでください。 | |
803 | 539 | X<~> X<negation, binary> |
804 | 540 | |
805 | 541 | =begin original |
806 | 542 | |
807 | ||
543 | Unary "+" has no effect whatsoever, even on strings. It is useful | |
808 | containing a character with an ordinal value above 255. | |
809 | ||
810 | =end original | |
811 | ||
812 | Perl 5.28 から、値が 255 を超える文字を含む文字列の | |
813 | 補数を求めようとすると致命的エラーになります。 | |
814 | ||
815 | =begin original | |
816 | ||
817 | If the "bitwise" feature is enabled via S<C<use | |
818 | feature 'bitwise'>> or C<use v5.28>, then unary | |
819 | C<"~"> always treats its argument as a number, and an | |
820 | alternate form of the operator, C<"~.">, always treats its argument as a | |
821 | string. So C<~0> and C<~"0"> will both give 2**32-1 on 32-bit platforms, | |
822 | whereas C<~.0> and C<~."0"> will both yield C<"\xff">. Until Perl 5.28, | |
823 | this 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<"~"> は常にその引数を数値として扱い、その代替形式である | |
829 | C<"~."> はその引数を常に文字列として扱います。 | |
830 | 従って 32 ビットプラットフォームでは C<~0> と C<~"0"> はどちらも | |
831 | 2**32-1 を意味しますが、C<~.0> と C<~."0"> はどちらも C<"\xff"> になります。 | |
832 | Perl 5.28 まで、この機能は C<"experimental::bitwise"> カテゴリの警告を | |
833 | 発生させていました。 | |
834 | ||
835 | =begin original | |
836 | ||
837 | Unary C<"+"> has no effect whatsoever, even on strings. It is useful | |
838 | 544 | syntactically for separating a function name from a parenthesized expression |
839 | 545 | that would otherwise be interpreted as the complete list of function |
840 | arguments. (See examples above under L< | |
546 | arguments. (See examples above under L<Terms and List Operators (Leftward)>.) | |
841 | 547 | X<+> |
842 | 548 | |
843 | 549 | =end original |
844 | 550 | |
845 | 単項演算子の | |
551 | 単項演算子の "+" は、たとえ文字列に対して用いられた場合にも、何もしません。 | |
846 | 552 | 関数名に続けて括弧付きの式を書く場合に、関数の引数リストと |
847 | 553 | 解釈されないようにするために用いることができます。 |
848 | (下記 L< | |
554 | (下記 L<Terms and List Operators (Leftward)> の例を参照してください。) | |
849 | 555 | X<+> |
850 | 556 | |
851 | 557 | =begin original |
852 | 558 | |
853 | Unary | |
559 | Unary "\" creates a reference to whatever follows it. See L<perlreftut> | |
854 | thing, it creates a reference to that object. If its operand is a | |
855 | parenthesised list, then it creates references to the things mentioned | |
856 | in the list. Otherwise it puts its operand in list context, and creates | |
857 | a list of references to the scalars in the list provided by the operand. | |
858 | See L<perlreftut> | |
859 | 560 | and L<perlref>. Do not confuse this behavior with the behavior of |
860 | 561 | backslash within a string, although both forms do convey the notion |
861 | 562 | of protecting the next thing from interpolation. |
862 | 563 | X<\> X<reference> X<backslash> |
863 | 564 | |
864 | 565 | =end original |
865 | 566 | |
866 | 単項演算子の | |
567 | 単項演算子の "\" はその後に続くものへのリファレンスを生成します。 | |
867 | オペランドが一つの印付きのものの場合、それへのリファレンスを作ります。 | |
868 | オペランドがかっこでくくられたリストの場合、 | |
869 | リストで言及されているものへのリファレンスを作ります。 | |
870 | さもなければオペランドをリストコンテキストとし、 | |
871 | オペランドによって提供されたリストのスカラへのリファレンスのリストを作ります。 | |
872 | 568 | L<perlreftut> と L<perlref> を参照してください。 |
873 | 569 | この用法も文字列中のバックスラッシュも、後に続くものが展開されるのを |
874 | 570 | 防ぐことになりますが、動作を混同しないでください。 |
875 | 571 | X<\> X<reference> X<backslash> |
876 | 572 | |
877 | 573 | =head2 Binding Operators |
878 | 574 | X<binding> X<operator, binding> X<=~> X<!~> |
879 | 575 | |
880 | 576 | (拘束演算子) |
881 | 577 | |
882 | 578 | =begin original |
883 | 579 | |
884 | Binary | |
580 | Binary "=~" binds a scalar expression to a pattern match. Certain operations | |
885 | search or modify the string | |
581 | search or modify the string $_ by default. This operator makes that kind | |
886 | 582 | of operation work on some other string. The right argument is a search |
887 | 583 | pattern, substitution, or transliteration. The left argument is what is |
888 | 584 | supposed to be searched, substituted, or transliterated instead of the default |
889 | ||
585 | $_. When used in scalar context, the return value generally indicates the | |
890 | success of the operation. | |
586 | success of the operation. Behavior in list context depends on the particular | |
891 | ||
587 | operator. See L</"Regexp Quote-Like Operators"> for details and | |
892 | ||
588 | L<perlretut> for examples using these operators. | |
893 | Behavior in list context depends on the particular operator. | |
894 | See L</"Regexp Quote-Like Operators"> for details and L<perlretut> for | |
895 | examples using these operators. | |
896 | 589 | |
897 | 590 | =end original |
898 | 591 | |
899 | 二項演算子の | |
592 | 二項演算子の "=~" は、スカラ式をパターンマッチに拘束します。 | |
900 | デフォルトで | |
593 | デフォルトで $_ の文字列を検索したり、変更したりする演算があります。 | |
901 | 594 | この演算子は、そのような演算を他の文字列に対して行なわせるようにするものです。 |
902 | 595 | 右引数は、検索パターン、置換、文字変換のいずれかです。 |
903 | 左引数は、デフォルトの | |
596 | 左引数は、デフォルトの $_ の代わりに検索、置換、文字変換の対象となるものです。 | |
904 | 対象となるものです。 | |
905 | 597 | スカラコンテキストで使うと、返り値は一般的に演算の結果が成功したか否かです。 |
906 | 例外は、C</r> (非破壊) オプション付きの置換 (C<s///>) と | |
907 | 文字変換 (C<y///>) です; この場合は変換した結果を返します。 | |
908 | 598 | リストコンテキストでの振る舞いは演算子に依存します。 |
909 | 599 | 詳しくは L</"Regexp Quote-Like Operators"> を、これらの演算子を使った |
910 | 600 | 例については L<perlretut> を参照して下さい。 |
911 | 601 | |
912 | 602 | =begin original |
913 | 603 | |
914 | 604 | If the right argument is an expression rather than a search pattern, |
915 | 605 | substitution, or transliteration, it is interpreted as a search pattern at run |
916 | time. | |
606 | time. Note that this means that its contents will be interpolated twice, so | |
917 | contents will be interpolated twice, so | |
918 | 607 | |
919 | 608 | =end original |
920 | 609 | |
921 | 610 | 右引数が検索パターン、置換、文字変換ではなく、式であれば、 |
922 | 611 | それは実行時に決まる検索パターンと解釈されます。 |
923 | 612 | これは、内容が 2 回展開されることを意味することに注意してください; |
924 | 613 | つまり: |
925 | 614 | |
926 | | |
615 | '\\' =~ q'\\'; | |
927 | 616 | |
928 | 617 | =begin original |
929 | 618 | |
930 | 619 | is not ok, as the regex engine will end up trying to compile the |
931 | 620 | pattern C<\>, which it will consider a syntax error. |
932 | 621 | |
933 | 622 | =end original |
934 | 623 | |
935 | 624 | は正しくありません; 正規表現エンジンは最終的にパターン C<\> を |
936 | 625 | コンパイルしようとして、これは文法エラーと考えるからです。 |
937 | 626 | |
938 | 627 | =begin original |
939 | 628 | |
940 | Binary | |
629 | Binary "!~" is just like "=~" except the return value is negated in | |
941 | 630 | the logical sense. |
942 | 631 | |
943 | 632 | =end original |
944 | 633 | |
945 | 二項演算子の | |
634 | 二項演算子の "!~" は、返される値が論理否定されることを除いて | |
946 | ||
635 | "=~" と同じです。 | |
947 | 636 | |
948 | =begin original | |
949 | ||
950 | Binary 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 | ||
958 | 637 | =head2 Multiplicative Operators |
959 | 638 | X<operator, multiplicative> |
960 | 639 | |
961 | 640 | (乗法演算子) |
962 | 641 | |
963 | 642 | =begin original |
964 | 643 | |
965 | Binary | |
644 | Binary "*" multiplies two numbers. | |
966 | 645 | X<*> |
967 | 646 | |
968 | 647 | =end original |
969 | 648 | |
970 | 二項演算子の | |
649 | 二項演算子の "*" は 2 つの数値の積を返します。 | |
971 | 650 | X<*> |
972 | 651 | |
973 | 652 | =begin original |
974 | 653 | |
975 | Binary | |
654 | Binary "/" divides two numbers. | |
976 | 655 | X</> X<slash> |
977 | 656 | |
978 | 657 | =end original |
979 | 658 | |
980 | 二項演算子の | |
659 | 二項演算子の "/" は 2 つの数値の商を返します。 | |
981 | 660 | X</> X<slash> |
982 | 661 | |
983 | 662 | =begin original |
984 | 663 | |
985 | Binary | |
664 | Binary "%" is the modulo operator, which computes the division | |
986 | 665 | remainder of its first argument with respect to its second argument. |
987 | 666 | Given integer |
988 | operands C<$ | |
667 | operands C<$a> and C<$b>: If C<$b> is positive, then C<$a % $b> is | |
989 | C<$ | |
668 | C<$a> minus the largest multiple of C<$b> less than or equal to | |
990 | C<$ | |
669 | C<$a>. If C<$b> is negative, then C<$a % $b> is C<$a> minus the | |
991 | smallest multiple of C<$ | |
670 | smallest multiple of C<$b> that is not less than C<$a> (i.e. the | |
992 | 671 | result will be less than or equal to zero). If the operands |
993 | C<$ | |
672 | C<$a> and C<$b> are floating point values and the absolute value of | |
994 | C<$ | |
673 | C<$b> (that is C<abs($b)>) is less than C<(UV_MAX + 1)>, only | |
995 | the integer portion of C<$ | |
674 | the integer portion of C<$a> and C<$b> will be used in the operation | |
996 | 675 | (Note: here C<UV_MAX> means the maximum of the unsigned integer type). |
997 | If the absolute value of the right operand (C<abs($ | |
676 | If the absolute value of the right operand (C<abs($b)>) is greater than | |
998 | or equal to | |
677 | or equal to C<(UV_MAX + 1)>, "%" computes the floating-point remainder | |
999 | C<$r> in the equation | |
678 | C<$r> in the equation C<($r = $a - $i*$b)> where C<$i> is a certain | |
1000 | 679 | integer that makes C<$r> have the same sign as the right operand |
1001 | C<$ | |
680 | C<$b> (B<not> as the left operand C<$a> like C function C<fmod()>) | |
1002 | and the absolute value less than that of C<$ | |
681 | and the absolute value less than that of C<$b>. | |
1003 | Note that when | |
682 | Note that when C<use integer> is in scope, "%" gives you direct access | |
1004 | 683 | to the modulo operator as implemented by your C compiler. This |
1005 | 684 | operator is not as well defined for negative operands, but it will |
1006 | 685 | execute faster. |
1007 | 686 | X<%> X<remainder> X<modulo> X<mod> |
1008 | 687 | |
1009 | 688 | =end original |
1010 | 689 | |
1011 | 二項演算子の | |
690 | 二項演算子の "%" は剰余演算子で、一つ目の引数を二つ目の引数で割ったときの | |
1012 | 691 | 余りを返します。 |
1013 | C<$ | |
692 | C<$a> と C<$b> の二つの整数の被演算子を取ったとすると: | |
1014 | C<$ | |
693 | C<$b> が正の場合、C<$a % $b> は、C<$a> から C<$a> 以下の最大の | |
1015 | C<$ | |
694 | C<$b> の倍数を引いた値です。 | |
1016 | C<$ | |
695 | C<$b> が負の場合、C<$a % $b> は、C<$a> から C<$a> を下回らない | |
1017 | 最小の C<$ | |
696 | 最小の C<$b> の倍数を引いた値です。(従って結果はゼロ以下になります。) | |
1018 | オペランド C<$ | |
697 | オペランド C<$a> と C<$b> が浮動小数点数で、C<$b> の絶対値 | |
1019 | (つまり C<abs($ | |
698 | (つまり C<abs($b)>) が C<(UV_MAX + 1)> より小さい場合、 | |
1020 | C<$ | |
699 | C<$a> と C<$b> の整数部のみが操作で使われます | |
1021 | 700 | (注意: ここで C<UV_MAX> は符号なし整数の最大値を意味します)。 |
1022 | 右オペランドの絶対値 (C<abs($ | |
701 | 右オペランドの絶対値 (C<abs($b)>) が C<(UV_MAX + 1)> 以上の場合、 | |
1023 | ||
702 | "%" は、C<($r = $a - $i*$b)> となる浮動小数点剰余 C<$r> を計算します; | |
1024 | ここで C<$i> は、C<$r> が右オペランド C<$ | |
703 | ここで C<$i> は、C<$r> が右オペランド C<$b> と同じ符号 (C の | |
1025 | 関数 C<fmod()> のように左オペランド C<$ | |
704 | 関数 C<fmod()> のように左オペランド C<$a> B<ではありません>) で、 | |
1026 | 絶対値が C<$ | |
705 | 絶対値が C<$b> より小さいものになるような、ある整数です。 | |
1027 | ||
706 | C<use integer> がスコープ内にある場合、 | |
1028 | ||
707 | "%" は C コンパイラで実装された剰余演算子を使います。 | |
1029 | 708 | この演算子は被演算子が負の場合の挙動が不確実ですが、 |
1030 | 709 | より高速です。 |
1031 | 710 | X<%> X<remainder> X<modulo> X<mod> |
1032 | 711 | |
1033 | 712 | =begin original |
1034 | 713 | |
1035 | Binary | |
714 | Binary "x" is the repetition operator. In scalar context or if the left | |
1036 | ||
715 | operand is not enclosed in parentheses, it returns a string consisting | |
1037 | ||
716 | of the left operand repeated the number of times specified by the right | |
1038 | context | |
717 | operand. In list context, if the left operand is enclosed in | |
1039 | ||
718 | parentheses or is a list formed by C<qw/STRING/>, it repeats the list. | |
1040 | ||
719 | If the right operand is zero or negative, it returns an empty string | |
1041 | enclosed in parentheses or a C<qw//> list, it performs a list repetition. | |
1042 | In that case it supplies list context to the left operand, and returns | |
1043 | a list consisting of the left operand list repeated the number of times | |
1044 | specified by the right operand. | |
1045 | If the right operand is zero or negative (raising a warning on | |
1046 | negative), it returns an empty string | |
1047 | 720 | or an empty list, depending on the context. |
1048 | 721 | X<x> |
1049 | 722 | |
1050 | 723 | =end original |
1051 | 724 | |
1052 | 二項演算子の | |
725 | 二項演算子の "x" は繰り返し演算子です。 | |
1053 | スカラコンテキストまたは左辺値が | |
726 | スカラコンテキストまたは左辺値が括弧で括られていない場合は、 | |
1054 | ||
727 | 左被演算子を右被演算子に示す数だけ繰り返したもので構成される | |
1055 | この場合、左オペランドにスカラコンテキストを提供し、 | |
1056 | 右オペランドによって指定された回数繰り返された左オペランド文字列からなる | |
1057 | 728 | 文字列を返します。 |
1058 | ||
729 | リストコンテキストでは、左被演算子が括弧で括られているか、C<qw/STRING> の | |
1059 | ||
730 | 形のリストの場合、リストを繰り返します。 | |
1060 | ||
731 | 右オペランドが 0 か負数の場合、コンテキストによって、空文字列か空リストを | |
1061 | ||
732 | 返します。 | |
1062 | この場合、これは左オペランドへリストコンテキストを提供し、 | |
1063 | 右オペランドによって指定された回数繰り返された | |
1064 | 左オペランドのリストからなるリストを返します。 | |
1065 | 右オペランドが 0 か負数の場合(負数の場合は警告が出ます)、 | |
1066 | コンテキストによって空文字列か空リストを返します。 | |
1067 | 733 | X<x> |
1068 | 734 | |
1069 | 735 | print '-' x 80; # print row of dashes |
1070 | 736 | |
1071 | 737 | print "\t" x ($tab/8), ' ' x ($tab%8); # tab over |
1072 | 738 | |
1073 | 739 | @ones = (1) x 80; # a list of 80 1's |
1074 | 740 | @ones = (5) x @ones; # set all elements to 5 |
1075 | 741 | |
742 | ||
1076 | 743 | =head2 Additive Operators |
1077 | 744 | X<operator, additive> |
1078 | 745 | |
1079 | 746 | (加法演算子) |
1080 | 747 | |
1081 | 748 | =begin original |
1082 | 749 | |
1083 | Binary | |
750 | Binary "+" returns the sum of two numbers. | |
1084 | 751 | X<+> |
1085 | 752 | |
1086 | 753 | =end original |
1087 | 754 | |
1088 | 二項演算子の | |
755 | 二項演算子の "+" は 2 つの数値の和を返します。 | |
1089 | 756 | X<+> |
1090 | 757 | |
1091 | 758 | =begin original |
1092 | 759 | |
1093 | Binary | |
760 | Binary "-" returns the difference of two numbers. | |
1094 | 761 | X<-> |
1095 | 762 | |
1096 | 763 | =end original |
1097 | 764 | |
1098 | 二項演算子の | |
765 | 二項演算子の "-" は 2 つの数値の差を返します。 | |
1099 | 766 | X<-> |
1100 | 767 | |
1101 | 768 | =begin original |
1102 | 769 | |
1103 | Binary | |
770 | Binary "." concatenates two strings. | |
1104 | 771 | X<string, concatenation> X<concatenation> |
1105 | 772 | X<cat> X<concat> X<concatenate> X<.> |
1106 | 773 | |
1107 | 774 | =end original |
1108 | 775 | |
1109 | 二項演算子の | |
776 | 二項演算子の "." は 2 つの文字列を連結します。 | |
1110 | 777 | X<string, concatenation> X<concatenation> |
1111 | 778 | X<cat> X<concat> X<concatenate> X<.> |
1112 | 779 | |
1113 | 780 | =head2 Shift Operators |
1114 | 781 | X<shift operator> X<operator, shift> X<<< << >>> |
1115 | 782 | X<<< >> >>> X<right shift> X<left shift> X<bitwise shift> |
1116 | 783 | X<shl> X<shr> X<shift, right> X<shift, left> |
1117 | 784 | |
1118 | 785 | (シフト演算子) |
1119 | 786 | |
1120 | 787 | =begin original |
1121 | 788 | |
1122 | Binary | |
789 | Binary "<<" returns the value of its left argument shifted left by the | |
1123 | 790 | number of bits specified by the right argument. Arguments should be |
1124 | integers. (See also L< | |
791 | integers. (See also L<Integer Arithmetic>.) | |
1125 | 792 | |
1126 | 793 | =end original |
1127 | 794 | |
1128 | 二項演算子の | |
795 | 二項演算子の "<<" は左引数の値を、右引数で示すビット数だけ、 | |
1129 | 796 | 左にシフトした値を返します。 |
1130 | 797 | 引数は整数でなければなりません。 |
1131 | (L< | |
798 | (L<Integer Arithmetic> も参照して下さい。) | |
1132 | 799 | |
1133 | 800 | =begin original |
1134 | 801 | |
1135 | Binary | |
802 | Binary ">>" returns the value of its left argument shifted right by | |
1136 | 803 | the number of bits specified by the right argument. Arguments should |
1137 | be integers. (See also L< | |
804 | be integers. (See also L<Integer Arithmetic>.) | |
1138 | 805 | |
1139 | 806 | =end original |
1140 | 807 | |
1141 | 二項演算子の | |
808 | 二項演算子の ">>" は左引数の値を、右引数で示すビット数だけ、 | |
1142 | 809 | 右にシフトした値を返します。 |
1143 | 810 | 引数は整数でなければなりません。 |
1144 | (L< | |
811 | (L<Integer Arithmetic> も参照して下さい。) | |
1145 | 812 | |
1146 | 813 | =begin original |
1147 | 814 | |
1148 | ||
815 | Note that both "<<" and ">>" in Perl are implemented directly using | |
1149 | ||
816 | "<<" and ">>" in C. If C<use integer> (see L<Integer Arithmetic>) is | |
1150 | integers are used | |
817 | in force then signed C integers are used, else unsigned C integers are | |
1151 | ||
818 | used. Either way, the implementation isn't going to generate results | |
1152 | ||
819 | larger than the size of the integer type Perl was built with (32 bits | |
820 | or 64 bits). | |
1153 | 821 | |
1154 | 822 | =end original |
1155 | 823 | |
1156 | ||
824 | Perl での "<<" と ">>" は C での "<<" と ">>" を直接利用して | |
1157 | ||
825 | 実装されていることに注意してください。 | |
1158 | C | |
826 | C<use integer> (L<Integer Arithmetic> を参照してください)が有効な場合、 | |
1159 | ||
827 | C の符号付き整数が使われ、そうでない場合は C の符号なし整数が使われます。 | |
1160 | 左側から来ます。 | |
1161 | ||
1162 | =begin original | |
1163 | ||
1164 | Either way, the implementation isn't going to generate results larger | |
1165 | than the size of the integer type Perl was built with (32 bits or 64 bits). | |
1166 | ||
1167 | =end original | |
1168 | ||
1169 | 828 | どちらの場合も、この実装は Perl がビルドされた整数型のサイズ(32 ビットか |
1170 | 829 | 64 ビット)よりも大きい結果を生成することはありません。 |
1171 | 830 | |
1172 | 831 | =begin original |
1173 | 832 | |
1174 | ||
833 | The result of overflowing the range of the integers is undefined | |
1175 | ||
834 | because it is undefined also in C. In other words, using 32-bit | |
1176 | ||
835 | integers, C<< 1 << 32 >> is undefined. Shifting by a negative number | |
836 | of bits is also undefined. | |
1177 | 837 | |
1178 | 838 | =end original |
1179 | 839 | |
1180 | ||
840 | 整数の範囲をオーバーフローした場合の結果は、C でも未定義なので、未定義です。 | |
1181 | ||
841 | 言い換えると、32 ビット整数を使っているとき、C<< 1 << 32 >> は未定義です。 | |
1182 | ||
842 | シフトするビット数として負の数を指定した場合も未定義です。 | |
1183 | 843 | |
1184 | =begin original | |
1185 | ||
1186 | Shifting by more bits than the size of the integers means most of the | |
1187 | time zero (all bits fall off), except that under S<C<use integer>> | |
1188 | right overshifting a negative shiftee results in -1. This is unlike | |
1189 | in C, where shifting by too many bits is undefined. A common C | |
1190 | behavior 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 | ||
1205 | but that is completely accidental. | |
1206 | ||
1207 | =end original | |
1208 | ||
1209 | しかしこれは完全に偶然です。 | |
1210 | ||
1211 | =begin original | |
1212 | ||
1213 | If you get tired of being subject to your platform's native integers, | |
1214 | the 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 | ||
1227 | 844 | =head2 Named Unary Operators |
1228 | 845 | X<operator, named unary> |
1229 | 846 | |
1230 | 847 | (名前付き単項演算子) |
1231 | 848 | |
1232 | 849 | =begin original |
1233 | 850 | |
1234 | 851 | The various named unary operators are treated as functions with one |
1235 | 852 | argument, with optional parentheses. |
1236 | 853 | |
1237 | 854 | =end original |
1238 | 855 | |
1239 | 856 | さまざまな名前付き単項演算子が、引数を 1 つ持ち、括弧が省略可能な、 |
1240 | 857 | 関数として扱われます。 |
1241 | 858 | |
1242 | 859 | =begin original |
1243 | 860 | |
1244 | If any list operator ( | |
861 | If any list operator (print(), etc.) or any unary operator (chdir(), etc.) | |
1245 | 862 | is followed by a left parenthesis as the next token, the operator and |
1246 | 863 | arguments within parentheses are taken to be of highest precedence, |
1247 | 864 | just like a normal function call. For example, |
1248 | because named unary operators are higher precedence than | |
865 | because named unary operators are higher precedence than ||: | |
1249 | 866 | |
1250 | 867 | =end original |
1251 | 868 | |
1252 | リスト演算子 ( | |
869 | リスト演算子 (print() など) や単項演算子 (chdir() など) は、 | |
1253 | 870 | すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、 |
1254 | 871 | 通常の関数呼び出しのようにもっとも高い優先順位として扱われます。 |
1255 | たとえば、名前つき単項演算子は | |
872 | たとえば、名前つき単項演算子は || より優先順位が高いので、 | |
1256 | 873 | 以下のようになります: |
1257 | 874 | |
1258 | 875 | chdir $foo || die; # (chdir $foo) || die |
1259 | 876 | chdir($foo) || die; # (chdir $foo) || die |
1260 | 877 | chdir ($foo) || die; # (chdir $foo) || die |
1261 | 878 | chdir +($foo) || die; # (chdir $foo) || die |
1262 | 879 | |
1263 | 880 | =begin original |
1264 | 881 | |
1265 | but, because | |
882 | but, because * is higher precedence than named operators: | |
1266 | 883 | |
1267 | 884 | =end original |
1268 | 885 | |
1269 | しかし | |
886 | しかし * は名前つき演算子より優先順位が高いので、以下のようになります: | |
1270 | 887 | |
1271 | 888 | chdir $foo * 20; # chdir ($foo * 20) |
1272 | 889 | chdir($foo) * 20; # (chdir $foo) * 20 |
1273 | 890 | chdir ($foo) * 20; # (chdir $foo) * 20 |
1274 | 891 | chdir +($foo) * 20; # chdir ($foo * 20) |
1275 | 892 | |
1276 | 893 | rand 10 * 20; # rand (10 * 20) |
1277 | 894 | rand(10) * 20; # (rand 10) * 20 |
1278 | 895 | rand (10) * 20; # (rand 10) * 20 |
1279 | 896 | rand +(10) * 20; # rand (10 * 20) |
1280 | 897 | |
1281 | 898 | =begin original |
1282 | 899 | |
1283 | 900 | Regarding precedence, the filetest operators, like C<-f>, C<-M>, etc. are |
1284 | 901 | treated like named unary operators, but they don't follow this functional |
1285 | 902 | parenthesis rule. That means, for example, that C<-f($file).".bak"> is |
1286 | equivalent to | |
903 | equivalent to C<-f "$file.bak">. | |
1287 | 904 | X<-X> X<filetest> X<operator, filetest> |
1288 | 905 | |
1289 | 906 | =end original |
1290 | 907 | |
1291 | 908 | 優先順位に関して、C<-f> や C<-M> のようなファイルテスト演算子は、名前付き |
1292 | 909 | 単項演算子として扱われますが、この関数のかっこルールは適用されません。 |
1293 | これは、例えば C<-f($file).".bak"> は | |
910 | これは、例えば C<-f($file).".bak"> は C<-f "$file.bak"> と等価であることを | |
1294 | 911 | 意味します。 |
1295 | 912 | X<-X> X<filetest> X<operator, filetest> |
1296 | 913 | |
1297 | 914 | =begin original |
1298 | 915 | |
1299 | See also L< | |
916 | See also L<"Terms and List Operators (Leftward)">. | |
1300 | 917 | |
1301 | 918 | =end original |
1302 | 919 | |
1303 | L< | |
920 | L<"Terms and List Operators (Leftward)"> も参照して下さい。 | |
1304 | 921 | |
1305 | 922 | =head2 Relational Operators |
1306 | 923 | X<relational operator> X<operator, relational> |
1307 | 924 | |
1308 | 925 | (比較演算子) |
1309 | 926 | |
1310 | 927 | =begin original |
1311 | 928 | |
1312 | ||
929 | Binary "<" returns true if the left argument is numerically less than | |
1313 | that can be safely used as numbers. For example, the relational | |
1314 | operators in this section and the equality operators in the next | |
1315 | one return C<1> for true and a special version of the defined empty | |
1316 | string, C<"">, which counts as a zero but is exempt from warnings | |
1317 | about improper numeric conversions, just as S<C<"0 but true">> is. | |
1318 | ||
1319 | =end original | |
1320 | ||
1321 | 真か偽の値を返す Perl 演算子は一般的に安全に数値として使える値を返します。 | |
1322 | 例えば、この節の関係演算子と次の節の等価演算子は、真および、 | |
1323 | S<C<"0 but true">> と同様、ゼロとカウントされるけれども不適切な数値変換に | |
1324 | 関する警告の出ない、定義された空文字列 C<""> の特別版に対して C<1> を | |
1325 | 返します。 | |
1326 | ||
1327 | =begin original | |
1328 | ||
1329 | Binary C<< "<" >> returns true if the left argument is numerically less than | |
1330 | 930 | the right argument. |
1331 | 931 | X<< < >> |
1332 | 932 | |
1333 | 933 | =end original |
1334 | 934 | |
1335 | 二項演算子の | |
935 | 二項演算子の "<" は左引数が数値的に右引数よりも小さければ、 | |
1336 | 936 | 真を返します。 |
1337 | X<< < >> | |
1338 | 937 | |
1339 | 938 | =begin original |
1340 | 939 | |
1341 | Binary | |
940 | Binary ">" returns true if the left argument is numerically greater | |
1342 | 941 | than the right argument. |
1343 | 942 | X<< > >> |
1344 | 943 | |
1345 | 944 | =end original |
1346 | 945 | |
1347 | 二項演算子の | |
946 | 二項演算子の ">" は左引数が数値的に右引数よりも大きければ、 | |
1348 | 947 | 真を返します。 |
1349 | X<< > >> | |
1350 | 948 | |
1351 | 949 | =begin original |
1352 | 950 | |
1353 | Binary | |
951 | Binary "<=" returns true if the left argument is numerically less than | |
1354 | 952 | or equal to the right argument. |
1355 | 953 | X<< <= >> |
1356 | 954 | |
1357 | 955 | =end original |
1358 | 956 | |
1359 | 二項演算子の | |
957 | 二項演算子の "<=" は左引数が数値的に右引数よりも小さいか等しければ、 | |
1360 | 958 | 真を返します。 |
1361 | X<< <= >> | |
1362 | 959 | |
1363 | 960 | =begin original |
1364 | 961 | |
1365 | Binary | |
962 | Binary ">=" returns true if the left argument is numerically greater | |
1366 | 963 | than or equal to the right argument. |
1367 | 964 | X<< >= >> |
1368 | 965 | |
1369 | 966 | =end original |
1370 | 967 | |
1371 | 二項演算子の | |
968 | 二項演算子の ">=" は左引数が数値的に右引数よりも大きいか等しければ、 | |
1372 | 969 | 真を返します。 |
1373 | X<< >= >> | |
1374 | 970 | |
1375 | 971 | =begin original |
1376 | 972 | |
1377 | Binary | |
973 | Binary "lt" returns true if the left argument is stringwise less than | |
1378 | 974 | the right argument. |
1379 | 975 | X<< lt >> |
1380 | 976 | |
1381 | 977 | =end original |
1382 | 978 | |
1383 | 二項演算子の | |
979 | 二項演算子の "lt" は左引数が文字列的に右引数よりも小さければ、 | |
1384 | ||
980 | 真を返します。 | |
1385 | 981 | |
1386 | 982 | =begin original |
1387 | 983 | |
1388 | Binary | |
984 | Binary "gt" returns true if the left argument is stringwise greater | |
1389 | 985 | than the right argument. |
1390 | 986 | X<< gt >> |
1391 | 987 | |
1392 | 988 | =end original |
1393 | 989 | |
1394 | 二項演算子の | |
990 | 二項演算子の "gt" は左引数が文字列的に右引数よりも大きければ、 | |
1395 | ||
991 | 真を返します。 | |
1396 | 992 | |
1397 | 993 | =begin original |
1398 | 994 | |
1399 | Binary | |
995 | Binary "le" returns true if the left argument is stringwise less than | |
1400 | 996 | or equal to the right argument. |
1401 | 997 | X<< le >> |
1402 | 998 | |
1403 | 999 | =end original |
1404 | 1000 | |
1405 | 二項演算子の | |
1001 | 二項演算子の "le" は左引数が文字列的に右引数よりも小さいか等しければ、 | |
1406 | 1002 | 真を返します。 |
1407 | X<< le >> | |
1408 | 1003 | |
1409 | 1004 | =begin original |
1410 | 1005 | |
1411 | Binary | |
1006 | Binary "ge" returns true if the left argument is stringwise greater | |
1412 | 1007 | than or equal to the right argument. |
1413 | 1008 | X<< ge >> |
1414 | 1009 | |
1415 | 1010 | =end original |
1416 | 1011 | |
1417 | 二項演算子の | |
1012 | 二項演算子の "ge" は左引数が文字列的に右引数よりも大きいか等しければ、 | |
1418 | 1013 | 真を返します。 |
1419 | X<< ge >> | |
1420 | 1014 | |
1421 | =begin original | |
1422 | ||
1423 | A 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 | |
1425 | the section L</"Operator Precedence and Associativity">. | |
1426 | Beware that they do not chain with equality operators, which have lower | |
1427 | precedence. | |
1428 | ||
1429 | =end original | |
1430 | ||
1431 | S<C<"$x E<lt> $y E<lt>= $z">> のような比較演算子の並びは、 | |
1432 | L</"Operator Precedence and Associativity"> 節で述べたような形で | |
1433 | 連鎖比較を実行します。 | |
1434 | より低い優先順位を持つ等価演算子とは連鎖しないことに注意してください。 | |
1435 | ||
1436 | 1015 | =head2 Equality Operators |
1437 | 1016 | X<equality> X<equal> X<equals> X<operator, equality> |
1438 | 1017 | |
1439 | 1018 | (等価演算子) |
1440 | 1019 | |
1441 | 1020 | =begin original |
1442 | 1021 | |
1443 | Binary | |
1022 | Binary "==" returns true if the left argument is numerically equal to | |
1444 | 1023 | the right argument. |
1445 | 1024 | X<==> |
1446 | 1025 | |
1447 | 1026 | =end original |
1448 | 1027 | |
1449 | 二項演算子の | |
1028 | 二項演算子の "==" は左引数が数値的に右引数と等しければ、 | |
1450 | ||
1029 | 真を返します。 | |
1451 | 1030 | |
1452 | 1031 | =begin original |
1453 | 1032 | |
1454 | Binary | |
1033 | Binary "!=" returns true if the left argument is numerically not equal | |
1455 | 1034 | to the right argument. |
1456 | 1035 | X<!=> |
1457 | 1036 | |
1458 | 1037 | =end original |
1459 | 1038 | |
1460 | 二項演算子の | |
1039 | 二項演算子の "!=" は左引数が数値的に右引数と等しくなければ、 | |
1461 | 返します。 | |
1040 | 真を返します。 | |
1462 | X<!=> | |
1463 | 1041 | |
1464 | 1042 | =begin original |
1465 | 1043 | |
1466 | Binary | |
1044 | Binary "<=>" returns -1, 0, or 1 depending on whether the left | |
1467 | ||
1045 | argument is numerically less than, equal to, or greater than the right | |
1468 | ||
1046 | argument. If your platform supports NaNs (not-a-numbers) as numeric | |
1047 | values, using them with "<=>" returns undef. NaN is not "<", "==", ">", | |
1048 | "<=" or ">=" anything (even NaN), so those 5 return false. NaN != NaN | |
1049 | returns true, as does NaN != anything else. If your platform doesn't | |
1050 | support NaNs then NaN is just a string with numeric value 0. | |
1051 | X<< <=> >> X<spaceship> | |
1469 | 1052 | |
1470 | 1053 | =end original |
1471 | 1054 | |
1472 | 二項演算子の | |
1055 | 二項演算子の "<=>" は左引数が数値的に右引数より小さいか、等しいか、 | |
1473 | ||
1056 | 大きいかに従って、-1, 0, 1 を返します。 | |
1057 | 数値として NaN (非数) に対応しているプラットフォームでは、 | |
1058 | NaN に対して "<=>" を使うと undef を返します。 | |
1059 | NaN はどの値に対しても(NaN に対してでさえも) "<", "==", ">", | |
1060 | "<=", ">=" のいずれも成立しないので、これらは全て偽となります。 | |
1061 | NaN != NaN は真を返しますが、その他のどの値に対しても != は偽を返します。 | |
1062 | NaN に対応していないプラットフォームでは、NaN は単に数としての値 0 を持つ | |
1063 | 文字列です。 | |
1474 | 1064 | |
1475 | ||
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' | |
1476 | 1067 | |
1477 | Binary C<"ne"> returns true if the left argument is stringwise not equal | |
1478 | to the right argument. | |
1479 | X<ne> | |
1480 | ||
1481 | =end original | |
1482 | ||
1483 | 二項演算子の C<"ne"> は左引数が文字列的に右引数と等しくなければ、真を | |
1484 | 返します。 | |
1485 | X<ne> | |
1486 | ||
1487 | 1068 | =begin original |
1488 | 1069 | |
1489 | ||
1070 | Binary "eq" returns true if the left argument is stringwise equal to | |
1490 | ||
1071 | the right argument. | |
1491 | ||
1072 | X<eq> | |
1492 | Beware that they do not chain with relational operators, which have | |
1493 | higher precedence. | |
1494 | 1073 | |
1495 | 1074 | =end original |
1496 | 1075 | |
1497 | ||
1076 | 二項演算子の "eq" は左引数が文字列的に右引数と等しければ、 | |
1498 | ||
1077 | 真を返します。 | |
1499 | 連鎖比較を実行します。 | |
1500 | より高い優先順位を持つ比較演算子とは連鎖しないことに注意してください。 | |
1501 | 1078 | |
1502 | 1079 | =begin original |
1503 | 1080 | |
1504 | Binary | |
1081 | Binary "ne" returns true if the left argument is stringwise not equal | |
1505 | ||
1082 | to the right argument. | |
1506 | ||
1083 | X<ne> | |
1507 | values, using them with C<< "<=>" >> returns undef. C<NaN> is not | |
1508 | C<< "<" >>, C<< "==" >>, C<< ">" >>, C<< "<=" >> or C<< ">=" >> anything | |
1509 | (even C<NaN>), so those 5 return false. S<C<< NaN != NaN >>> returns | |
1510 | true, as does S<C<NaN !=> I<anything else>>. If your platform doesn't | |
1511 | support C<NaN>'s then C<NaN> is just a string with numeric value 0. | |
1512 | X<< <=> >> | |
1513 | X<spaceship> | |
1514 | 1084 | |
1515 | 1085 | =end original |
1516 | 1086 | |
1517 | 二項演算子の | |
1087 | 二項演算子の "ne" は左引数が文字列的に右引数と等しくなければ、 | |
1518 | ||
1088 | 真を返します。 | |
1519 | 数値として C<NaN> (非数) に対応しているプラットフォームでは、 | |
1520 | それに対して C<< "<=>" >> を使うと undef を返します。 | |
1521 | C<NaN> はどの値に対しても(C<NaN> に対してでさえも) C<< "<" >>, C<< "==" >>, | |
1522 | C<< ">" >>, C<< "<=" >>, C<< ">=" >> のいずれも成立しないので、これらは全て | |
1523 | 偽となります。 | |
1524 | S<C<< NaN != NaN >>> は真を返しますが、 | |
1525 | S<C<NaN !=> I<その他のどの値でも>> です。 | |
1526 | C<NaN> に対応していないプラットフォームでは、C<NaN> は単に数としての値 0 を | |
1527 | 持つ文字列です。 | |
1528 | X<< <=> >> X<spaceship> | |
1529 | 1089 | |
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 | ||
1533 | 1090 | =begin original |
1534 | 1091 | |
1535 | ||
1092 | Binary "cmp" returns -1, 0, or 1 depending on whether the left | |
1536 | support C<"NaN">.) | |
1537 | ||
1538 | =end original | |
1539 | ||
1540 | (L<bigint>, L<bigrat>, L<bignum> プラグマは全て C<"NaN"> に対応していることに | |
1541 | 注意してください。) | |
1542 | ||
1543 | =begin original | |
1544 | ||
1545 | Binary C<"cmp"> returns -1, 0, or 1 depending on whether the left | |
1546 | 1093 | argument is stringwise less than, equal to, or greater than the right |
1547 | 1094 | argument. |
1095 | X<cmp> | |
1548 | 1096 | |
1549 | 1097 | =end original |
1550 | 1098 | |
1551 | 二項演算子の | |
1099 | 二項演算子の "cmp" は左引数が文字列的に右引数より小さいか、 | |
1552 | 1100 | 等しいか、大きいかに従って、-1, 0, 1 を返します。 |
1553 | 1101 | |
1554 | 1102 | =begin original |
1555 | 1103 | |
1556 | ||
1104 | Binary "~~" does a smart match between its arguments. Smart matching | |
1105 | is 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.) | |
1568 | X<cmp> | |
1569 | ||
1570 | =end original | |
1571 | ||
1572 | (gt と >, lt と <, なども同様です) | |
1573 | X<cmp> | |
1574 | ||
1575 | =begin original | |
1576 | ||
1577 | Binary C<"~~"> does a smartmatch between its arguments. Smart matching | |
1578 | is described in the next section. | |
1579 | 1106 | X<~~> |
1580 | 1107 | |
1581 | 1108 | =end original |
1582 | 1109 | |
1583 | 二項演算子の | |
1110 | 二項演算子の "~~" はスマートマッチングとして働きます。 | |
1584 | スマートマッチングについては | |
1111 | スマートマッチングについては L<perlsyn/"Smart matching in detail"> で | |
1112 | 述べられています。 | |
1585 | 1113 | X<~~> |
1586 | 1114 | |
1587 | 1115 | =begin original |
1588 | 1116 | |
1589 | ||
1117 | "lt", "le", "ge", "gt" and "cmp" use the collation (sort) order specified | |
1590 | ||
1118 | by the current locale if C<use locale> is in effect. See L<perllocale>. | |
1591 | other and with respect to the equality operators of the same precedence. | |
1592 | 1119 | |
1593 | 1120 | =end original |
1594 | 1121 | |
1595 | ||
1122 | "lt", "le", "ge", "gt", "cmp" は C<use locale> が有効な場合は | |
1596 | ||
1123 | 現在のロケールで指定された辞書(ソート)順が使われます。 | |
1597 | 演算子に対しては非結合です。 | |
1598 | ||
1599 | =begin original | |
1600 | ||
1601 | C<"lt">, C<"le">, C<"ge">, C<"gt"> and C<"cmp"> use the collation (sort) | |
1602 | order specified by the current C<LC_COLLATE> locale if a S<C<use | |
1603 | locale>> form that includes collation is in effect. See L<perllocale>. | |
1604 | Do not mix these with Unicode, | |
1605 | only use them with legacy 8-bit locale encodings. | |
1606 | The standard C<L<Unicode::Collate>> and | |
1607 | C<L<Unicode::Collate::Locale>> modules offer much more powerful | |
1608 | solutions to collation issues. | |
1609 | ||
1610 | =end original | |
1611 | ||
1612 | C<"lt">, C<"le">, C<"ge">, C<"gt">, C<"cmp"> は照合を含む | |
1613 | S<C<use locale>> 型式が有効な場合は、現在の C<LC_COLLATE> ロケールで | |
1614 | 指定された照合(ソート)順が使われます。 | |
1615 | 1124 | L<perllocale> を参照して下さい。 |
1616 | これらを Unicode と混ぜないでください; | |
1617 | 伝統的な 8 ビットロケールコーディングでのみ使ってください。 | |
1618 | 標準の C<L<Unicode::Collate>> と C<L<Unicode::Collate::Locale>> モジュールは、 | |
1619 | 照合問題に関する遥かに強力な解決法を提供します。 | |
1620 | 1125 | |
1621 | =begin original | |
1622 | ||
1623 | For case-insensitive comparisons, look at the L<perlfunc/fc> case-folding | |
1624 | function, available in Perl v5.16 or later: | |
1625 | ||
1626 | =end original | |
1627 | ||
1628 | 大文字小文字を無視した比較に関しては、Perl v5.16 以降で利用可能な | |
1629 | L<perlfunc/fc> 大文字小文字畳み込み関数を参照してください: | |
1630 | ||
1631 | if ( fc($x) eq fc($y) ) { ... } | |
1632 | ||
1633 | =head2 Class Instance Operator | |
1634 | X<isa operator> | |
1635 | ||
1636 | (クラスインスタンス演算子) | |
1637 | ||
1638 | =begin original | |
1639 | ||
1640 | Binary C<isa> evaluates to true when the left argument is an object instance of | |
1641 | the class (or a subclass derived from that class) given by the right argument. | |
1642 | If the left argument is not defined, not a blessed object instance, nor does | |
1643 | not derive from the class given by the right argument, the operator evaluates | |
1644 | as false. The right argument may give the class either as a bareword or a | |
1645 | scalar 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 | ||
1664 | This feature is available from Perl 5.31.6 onwards when enabled by | |
1665 | C<use feature 'isa'>. This feature is enabled automatically by a | |
1666 | C<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 | ||
1681 | First available in Perl 5.10.1 (the 5.10.0 version behaved differently), | |
1682 | binary C<~~> does a "smartmatch" between its arguments. This is mostly | |
1683 | used implicitly in the C<when> construct described in L<perlsyn>, although | |
1684 | not all C<when> clauses call the smartmatch operator. Unique among all of | |
1685 | Perl's operators, the smartmatch operator can recurse. The smartmatch | |
1686 | operator is L<experimental|perlpolicy/experimental> and its behavior is | |
1687 | subject to change. | |
1688 | ||
1689 | =end original | |
1690 | ||
1691 | Perl 5.10.1 で最初に現れた (5.10.0 版は異なった振る舞いでした)、2 項 C<~~> は | |
1692 | 引数に対する「スマートマッチング」を行います。 | |
1693 | これはほとんど L<perlsyn> で記述されている C<when> 構文で暗黙に使われますが、 | |
1694 | C<when> 節だけがスマートマッチング演算子を呼び出すわけではありません。 | |
1695 | 全ての Perl の演算子の中で唯一、スマートマッチング演算子は再帰できます。 | |
1696 | スマートマッチング演算子は L<実験的|perlpolicy/experimental> で、その振る舞いは | |
1697 | 変更されることがあります。 | |
1698 | ||
1699 | =begin original | |
1700 | ||
1701 | It is also unique in that all other Perl operators impose a context | |
1702 | (usually string or numeric context) on their operands, autoconverting | |
1703 | those operands to those imposed contexts. In contrast, smartmatch | |
1704 | I<infers> contexts from the actual types of its operands and uses that | |
1705 | type information to select a suitable comparison mechanism. | |
1706 | ||
1707 | =end original | |
1708 | ||
1709 | また、その他全ての Perl 演算子はそのオペランドにコンテキスト(通常は | |
1710 | 文字列または数値コンテキスト)を割り当てて、オペランドを割り当てた | |
1711 | コンテキストに自動変換します。 | |
1712 | 一方、スマートマッチングはそのオペランドの実際の型からコンテキストを | |
1713 | I<推論> して、適切な比較機構を選択するためにその型情報を使います。 | |
1714 | ||
1715 | =begin original | |
1716 | ||
1717 | The C<~~> operator compares its operands "polymorphically", determining how | |
1718 | to compare them according to their actual types (numeric, string, array, | |
1719 | hash, etc.). Like the equality operators with which it shares the same | |
1720 | precedence, C<~~> returns 1 for true and C<""> for false. It is often best | |
1721 | read aloud as "in", "inside of", or "is contained in", because the left | |
1722 | operand is often looked for I<inside> the right operand. That makes the | |
1723 | order of the operands to the smartmatch operand often opposite that of | |
1724 | the regular match operator. In other words, the "smaller" thing is usually | |
1725 | placed in the left operand and the larger one in the right. | |
1726 | ||
1727 | =end original | |
1728 | ||
1729 | C<~~> 演算子はオペランドを「多態的に」比較します; どのように比較するかの | |
1730 | 決定は、実際の型 (数値、文字列、配列、ハッシュなど) に基づきます。 | |
1731 | 同じ優先順位を共有する等価演算子のように、 | |
1732 | C<~~> は真では 1 を、偽では C<""> を返します。 | |
1733 | これはしばしば "in", "inside of", "is contained in" と呼ぶのが最良です; | |
1734 | なぜなら左オペランドはしばしば右オペランドの I<内側> を探すからです。 | |
1735 | これにより、スマートマッチングオペランドへのオペランドの順序はしばしば | |
1736 | 正規表現演算子のものと逆になります。 | |
1737 | 言い換えると、「より小さい」ものが普通は左オペランドに置かれ、より | |
1738 | 大きいものが右側に置かれます。 | |
1739 | ||
1740 | =begin original | |
1741 | ||
1742 | The behavior of a smartmatch depends on what type of things its arguments | |
1743 | are, as determined by the following table. The first row of the table | |
1744 | whose types apply determines the smartmatch behavior. Because what | |
1745 | actually happens is mostly determined by the type of the second operand, | |
1746 | the 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 | ||
1964 | Notes: | |
1965 | ||
1966 | =end original | |
1967 | ||
1968 | 注意: | |
1969 | ||
1970 | =over | |
1971 | ||
1972 | =item 1. | |
1973 | Empty hashes or arrays match. | |
1974 | ||
1975 | (1. 空ハッシュや配列はマッチングします。) | |
1976 | ||
1977 | =item 2. | |
1978 | That is, each element smartmatches the element of the same index in the other array.[3] | |
1979 | ||
1980 | (2. つまり、それぞれの要素は他の配列の同じインデックスの要素とスマートマッチングします。[3]) | |
1981 | ||
1982 | =item 3. | |
1983 | If a circular reference is found, fall back to referential equality. | |
1984 | ||
1985 | (3. 循環参照が見つかると、参照の等価性にフォールバックします。) | |
1986 | ||
1987 | =item 4. | |
1988 | Either an actual number, or a string that looks like one. | |
1989 | ||
1990 | (4. 実際の数値か、数値に見える文字列のどちらかです。) | |
1991 | ||
1992 | =back | |
1993 | ||
1994 | =begin original | |
1995 | ||
1996 | The smartmatch implicitly dereferences any non-blessed hash or array | |
1997 | reference, so the C<I<HASH>> and C<I<ARRAY>> entries apply in those cases. | |
1998 | For blessed references, the C<I<Object>> entries apply. Smartmatches | |
1999 | involving hashes only consider hash keys, never hash values. | |
2000 | ||
2001 | =end original | |
2002 | ||
2003 | スマートマッチングは bless されていないハッシュや配列のリファレンスを暗黙に | |
2004 | デリファレンスするので、それらの場合では C<I<HASH>> と C<I<ARRAY>> の | |
2005 | エントリが適用されます。 | |
2006 | bless されたリファレンスでは、C<I<Object>> エントリが適用されます。 | |
2007 | ハッシュに関連するスマートマッチングはキーのみを考慮し、ハッシュの値は | |
2008 | 考慮しません。 | |
2009 | ||
2010 | =begin original | |
2011 | ||
2012 | The "like" code entry is not always an exact rendition. For example, the | |
2013 | smartmatch operator short-circuits whenever possible, but C<grep> does | |
2014 | not. Also, C<grep> in scalar context returns the number of matches, but | |
2015 | C<~~> returns only true or false. | |
2016 | ||
2017 | =end original | |
2018 | ||
2019 | "like" コードエントリは常に正確な処理を行うわけではありません。 | |
2020 | 例えば、スマートマッチング演算子は可能なら短絡しますが、C<grep> はしません。 | |
2021 | また、スカラコンテキストでは C<grep> はマッチングした数を返しますが、 | |
2022 | C<~~> は真か偽かのみを返します。 | |
2023 | ||
2024 | =begin original | |
2025 | ||
2026 | Unlike most operators, the smartmatch operator knows to treat C<undef> | |
2027 | specially: | |
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 | ||
2040 | Each operand is considered in a modified scalar context, the modification | |
2041 | being that array and hash variables are passed by reference to the | |
2042 | operator, which implicitly dereferences them. Both elements | |
2043 | of 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 | ||
2071 | Two arrays smartmatch if each element in the first array smartmatches | |
2072 | (that is, is "in") the corresponding element in the second array, | |
2073 | recursively. | |
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 | ||
2089 | Because the smartmatch operator recurses on nested arrays, this | |
2090 | will 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 | ||
2104 | If two arrays smartmatch each other, then they are deep | |
2105 | copies 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 | ||
2131 | If you were to set S<C<$b[3] = 4>>, then instead of reporting that "a and b | |
2132 | are deep copies of each other", it now reports that C<"b smartmatches in a">. | |
2133 | That's because the corresponding position in C<@a> contains an array that | |
2134 | (eventually) has a 4 in it. | |
2135 | ||
2136 | =end original | |
2137 | ||
2138 | S<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 | ||
2145 | Smartmatching one hash against another reports whether both contain the | |
2146 | same keys, no more and no less. This could be used to see whether two | |
2147 | records have the same field names, without caring what values those fields | |
2148 | might 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 | ||
2172 | However, this only does what you mean if C<$init_fields> is indeed a hash | |
2173 | reference. The condition C<$init_fields ~~ $REQUIRED_FIELDS> also allows the | |
2174 | strings C<"name">, C<"rank">, C<"serial_num"> as well as any array reference | |
2175 | that contains C<"name"> or C<"rank"> or C<"serial_num"> anywhere to pass | |
2176 | through. | |
2177 | ||
2178 | =end original | |
2179 | ||
2180 | しかし、これは C<$init_fields> が確かにハッシュリファレンスである場合にのみ | |
2181 | 考えている通りに動作します。 | |
2182 | C<$init_fields ~~ $REQUIRED_FIELDS> という条件は、文字列 | |
2183 | C<"name">, C<"rank">, C<"serial_num"> および、 | |
2184 | C<"name">, C<"rank">, C<"serial_num"> のいずれかを含むような | |
2185 | 配列リファレンスでも成立します。 | |
2186 | ||
2187 | =begin original | |
2188 | ||
2189 | The smartmatch operator is most often used as the implicit operator of a | |
2190 | C<when> clause. See the section on "Switch Statements" in L<perlsyn>. | |
2191 | ||
2192 | =end original | |
2193 | ||
2194 | スマートマッチング演算子は C<when> 節の暗黙の演算子としてもっともよく | |
2195 | 使われます。 | |
2196 | L<perlsyn> の "Switch Statements" の節を参照してください。 | |
2197 | ||
2198 | =head3 Smartmatching of Objects | |
2199 | ||
2200 | (オブジェクトのスマートマッチング) | |
2201 | ||
2202 | =begin original | |
2203 | ||
2204 | To avoid relying on an object's underlying representation, if the | |
2205 | smartmatch's right operand is an object that doesn't overload C<~~>, | |
2206 | it raises the exception "C<Smartmatching a non-overloaded object | |
2207 | breaks encapsulation>". That's because one has no business digging | |
2208 | around to see whether something is "in" an object. These are all | |
2209 | illegal 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 | ||
2226 | However, you can change the way an object is smartmatched by overloading | |
2227 | the C<~~> operator. This is allowed to | |
2228 | extend the usual smartmatch semantics. | |
2229 | For objects that do have an C<~~> overload, see L<overload>. | |
2230 | ||
2231 | =end original | |
2232 | ||
2233 | しかし、C<~~> 演算子をオーバーロードすることにオブジェクトが | |
2234 | スマートマッチングする方法を変更できます。 | |
2235 | これにより通常のスマートマッチングの意味論を拡張できます。 | |
2236 | C<~~> のオーバーロードを持つオブジェクトについては、L<overload> を | |
2237 | 参照してください。 | |
2238 | ||
2239 | =begin original | |
2240 | ||
2241 | Using an object as the left operand is allowed, although not very useful. | |
2242 | Smartmatching rules take precedence over overloading, so even if the | |
2243 | object in the left operand has smartmatch overloading, this will be | |
2244 | ignored. A left operand that is a non-overloaded object falls back on a | |
2245 | string or numeric comparison of whatever the C<ref> operator returns. That | |
2246 | means 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 | ||
2263 | does I<not> invoke the overload method with C<I<X>> as an argument. | |
2264 | Instead the above table is consulted as normal, and based on the type of | |
2265 | C<I<X>>, overloading may or may not be invoked. For simple strings or | |
2266 | numbers, "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 | ||
2280 | For 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 | ||
2296 | That's because it treats C<$fh> as a string like | |
2297 | C<"IO::Handle=GLOB(0x8039e0)">, then pattern matches against that. | |
2298 | ||
2299 | =end original | |
2300 | ||
2301 | これは、C<$fh> を C<"IO::Handle=GLOB(0x8039e0)"> のような文字列として扱い、 | |
2302 | それからパターンはこれに対してマッチングするからです。 | |
2303 | ||
2304 | 1126 | =head2 Bitwise And |
2305 | 1127 | X<operator, bitwise, and> X<bitwise and> X<&> |
2306 | 1128 | |
2307 | 1129 | (ビットごとの AND) |
2308 | 1130 | |
2309 | 1131 | =begin original |
2310 | 1132 | |
2311 | Binary | |
1133 | Binary "&" returns its operands ANDed together bit by bit. | |
2312 | ||
1134 | (See also L<Integer Arithmetic> and L<Bitwise String Operators>.) | |
2313 | is performed on operands that aren't either numbers (see | |
2314 | L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String Operators>). | |
2315 | 1135 | |
2316 | 1136 | =end original |
2317 | 1137 | |
2318 | 二項演算子の | |
1138 | 二項演算子の "&" は、両オペランドのビットごとに論理積をとって、 | |
2319 | 1139 | その結果を返します。 |
2320 | ||
1140 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) | |
2321 | (L</Bitwise String Operators> 参照) でもないオペランドに対してこの演算を | |
2322 | 実行した場合、現在のところ警告は出ませんが、結果は未定義です。 | |
2323 | 1141 | |
2324 | 1142 | =begin original |
2325 | 1143 | |
2326 | Note that | |
1144 | Note that "&" has lower priority than relational operators, so for example | |
2327 | the | |
1145 | the brackets are essential in a test like | |
2328 | 1146 | |
2329 | 1147 | =end original |
2330 | 1148 | |
2331 | ||
1149 | "&" は関係演算子より優先順位が低いので、例えば以下のようなテストでは、 | |
2332 | 1150 | かっこが重要です: |
2333 | 1151 | |
2334 | ||
1152 | print "Even\n" if ($x & 1) == 0; | |
2335 | 1153 | |
2336 | =begin original | |
2337 | ||
2338 | If the "bitwise" feature is enabled via S<C<use feature 'bitwise'>> or | |
2339 | C<use v5.28>, then this operator always treats its operands as numbers. | |
2340 | Before Perl 5.28 this feature produced a warning in the | |
2341 | C<"experimental::bitwise"> category. | |
2342 | ||
2343 | =end original | |
2344 | ||
2345 | S<C<use feature 'bitwise'>> か C<use v5.28> によって "bitwise" 機能が | |
2346 | 有効になっている場合、この演算子はオペランドを常に数値として扱います。 | |
2347 | Perl 5.28 より前ではこの機能は C<"experimental::bitwise"> カテゴリの | |
2348 | 警告が出力されていました。 | |
2349 | ||
2350 | 1154 | =head2 Bitwise Or and Exclusive Or |
2351 | 1155 | X<operator, bitwise, or> X<bitwise or> X<|> X<operator, bitwise, xor> |
2352 | 1156 | X<bitwise xor> X<^> |
2353 | 1157 | |
2354 | 1158 | (ビットごとの OR と XOR) |
2355 | 1159 | |
2356 | 1160 | =begin original |
2357 | 1161 | |
2358 | Binary | |
1162 | Binary "|" returns its operands ORed together bit by bit. | |
1163 | (See also L<Integer Arithmetic> and L<Bitwise String Operators>.) | |
2359 | 1164 | |
2360 | 1165 | =end original |
2361 | 1166 | |
2362 | 二項演算子の | |
1167 | 二項演算子の "|" は、両オペランドのビットごとに論理和をとって、 | |
2363 | 1168 | その結果を返します。 |
1169 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) | |
2364 | 1170 | |
2365 | 1171 | =begin original |
2366 | 1172 | |
2367 | Binary | |
1173 | Binary "^" returns its operands XORed together bit by bit. | |
1174 | (See also L<Integer Arithmetic> and L<Bitwise String Operators>.) | |
2368 | 1175 | |
2369 | 1176 | =end original |
2370 | 1177 | |
2371 | 二項演算子の | |
1178 | 二項演算子の "^" は、両オペランドのビットごとに排他論理和をとって、 | |
2372 | 1179 | その結果を返します。 |
1180 | (L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) | |
2373 | 1181 | |
2374 | 1182 | =begin original |
2375 | 1183 | |
2376 | ||
1184 | Note that "|" and "^" have lower priority than relational operators, so | |
2377 | ||
1185 | for example the brackets are essential in a test like | |
2378 | numbers (see L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String | |
2379 | Operators>). | |
2380 | 1186 | |
2381 | 1187 | =end original |
2382 | 1188 | |
2383 | ||
1189 | "|" と "^" は関係演算子より優先順位が低いので、例えば以下のような | |
2384 | (L</Bitwise String Operators> 参照) でもないオペランドに対してこれらの演算を | |
2385 | 実行した場合、現在のところ警告は出ませんが、結果は未定義です。 | |
2386 | ||
2387 | =begin original | |
2388 | ||
2389 | Note that C<"|"> and C<"^"> have lower priority than relational operators, so | |
2390 | for example the parentheses are essential in a test like | |
2391 | ||
2392 | =end original | |
2393 | ||
2394 | C<"|"> と C<"^"> は関係演算子より優先順位が低いので、例えば以下のような | |
2395 | 1190 | テストでは、かっこが重要です: |
2396 | 1191 | |
2397 | ||
1192 | print "false\n" if (8 | 2) != 10; | |
2398 | 1193 | |
2399 | =begin original | |
2400 | ||
2401 | If the "bitwise" feature is enabled via S<C<use feature 'bitwise'>> or | |
2402 | C<use v5.28>, then this operator always treats its operands as numbers. | |
2403 | Before Perl 5.28. this feature produced a warning in the | |
2404 | C<"experimental::bitwise"> category. | |
2405 | ||
2406 | =end original | |
2407 | ||
2408 | S<C<use feature 'bitwise'>> か C<use v5.28> によって "bitwise" 機能が | |
2409 | 有効になっている場合、この演算子はオペランドを常に数値として扱います。 | |
2410 | Perl 5.28 より前ではこの機能は C<"experimental::bitwise"> カテゴリの | |
2411 | 警告が出力されていました。 | |
2412 | ||
2413 | 1194 | =head2 C-style Logical And |
2414 | 1195 | X<&&> X<logical and> X<operator, logical, and> |
2415 | 1196 | |
2416 | 1197 | (C スタイルの論理積) |
2417 | 1198 | |
2418 | 1199 | =begin original |
2419 | 1200 | |
2420 | Binary | |
1201 | Binary "&&" performs a short-circuit logical AND operation. That is, | |
2421 | 1202 | if the left operand is false, the right operand is not even evaluated. |
2422 | 1203 | Scalar or list context propagates down to the right operand if it |
2423 | 1204 | is evaluated. |
2424 | 1205 | |
2425 | 1206 | =end original |
2426 | 1207 | |
2427 | 二項演算子の | |
1208 | 二項演算子の "&&" は、短絡の論理積演算を行ないます。 | |
2428 | 1209 | つまり、左被演算子が偽であれば、右被演算子は評価さえ |
2429 | 1210 | 行なわれないということです。 |
2430 | 1211 | 評価される場合には、スカラーかリストかというコンテキストは、 |
2431 | 1212 | 右被演算子にも及びます。 |
2432 | 1213 | |
2433 | 1214 | =head2 C-style Logical Or |
2434 | 1215 | X<||> X<operator, logical, or> |
2435 | 1216 | |
2436 | 1217 | (C スタイルの論理和) |
2437 | 1218 | |
2438 | 1219 | =begin original |
2439 | 1220 | |
2440 | Binary | |
1221 | Binary "||" performs a short-circuit logical OR operation. That is, | |
2441 | 1222 | if the left operand is true, the right operand is not even evaluated. |
2442 | 1223 | Scalar or list context propagates down to the right operand if it |
2443 | 1224 | is evaluated. |
2444 | 1225 | |
2445 | 1226 | =end original |
2446 | 1227 | |
2447 | 二項演算子の | |
1228 | 二項演算子の "||" は、短絡の論理和演算を行ないます。 | |
2448 | 1229 | つまり、左被演算子が真であれば、右被演算子は評価さえ |
2449 | 1230 | 行なわれないということです。 |
2450 | 1231 | 評価される場合には、スカラーかリストかというコンテキストは、 |
2451 | 1232 | 右被演算子にも及びます。 |
2452 | 1233 | |
2453 | =head2 Logical Defined-Or | |
1234 | =head2 C-style Logical Defined-Or | |
2454 | 1235 | X<//> X<operator, logical, defined-or> |
2455 | 1236 | |
2456 | ( | |
1237 | (C スタイルの定義性和) | |
2457 | 1238 | |
2458 | 1239 | =begin original |
2459 | 1240 | |
2460 | 1241 | Although it has no direct equivalent in C, Perl's C<//> operator is related |
2461 | to its C-style | |
1242 | to its C-style or. In fact, it's exactly the same as C<||>, except that it | |
2462 | tests the left hand side's definedness instead of its truth. Thus, | |
1243 | tests the left hand side's definedness instead of its truth. Thus, C<$a // $b> | |
2463 | ||
1244 | is similar to C<defined($a) || $b> (except that it returns the value of C<$a> | |
2464 | ||
1245 | rather than the value of C<defined($a)>) and is exactly equivalent to | |
2465 | ||
1246 | C<defined($a) ? $a : $b>. This is very useful for providing default values | |
2466 | in t | |
1247 | for variables. If you actually want to test if at least one of C<$a> and | |
2467 | ||
1248 | C<$b> is defined, use C<defined($a // $b)>. | |
2468 | the ternary-operator form can be used as a lvalue, while S<C<< EXPR1 // EXPR2 >>> | |
2469 | cannot). This is very useful for | |
2470 | providing default values for variables. If you actually want to test if | |
2471 | at least one of C<$x> and C<$y> is defined, use S<C<defined($x // $y)>>. | |
2472 | 1249 | |
2473 | 1250 | =end original |
2474 | 1251 | |
2475 | 1252 | C では直接等価なものはありませんが、Perl の C<//> 演算子は C スタイル |
2476 | 1253 | 論理和に関連しています。 |
2477 | 1254 | 実際、左辺の真偽ではなく定義されているかを判定することを除けば |
2478 | 1255 | C<||> と同じです。 |
2479 | ||
1256 | したがって C<$a // $b> は C<defined($a) || $b> と似ていて (ただし、 | |
2480 | ||
1257 | C<defined($a)> ではなく C<$a> の値を返します)、C<defined($a) ? $a : $b> と | |
2481 | ||
1258 | 完全に等価です。 | |
2482 | コンテキストで評価されます。) | |
2483 | 普通はこれは S<C<< defined(EXPR1) ? EXPR1 : EXPR2 >>> と同じ結果になり | |
2484 | 完全に等価です (例外は 3 項演算子形式は左辺値として使えますが、 | |
2485 | S<C<< EXPR1 // EXPR2 >>> は使えません)。 | |
2486 | 1259 | これは、変数に対するデフォルト値を設定するのにとても有用です。 |
2487 | 実際に、C<$ | |
1260 | 実際に、C<$a> と C<$b> の少なくとも片方が定義されているかを判定したいなら、 | |
2488 | ||
1261 | C<defined($a // $b)> を使ってください。 | |
2489 | 1262 | |
2490 | 1263 | =begin original |
2491 | 1264 | |
2492 | 1265 | The C<||>, C<//> and C<&&> operators return the last value evaluated |
2493 | (unlike C's C<||> and C<&&>, which return 0 or 1). | |
1266 | (unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably | |
2494 | 1267 | portable way to find out the home directory might be: |
2495 | 1268 | |
2496 | 1269 | =end original |
2497 | 1270 | |
2498 | 1271 | C<||>, C<//>, C<&&> 演算子は、(C のように 単に 0 や 1 を返すのではなく) |
2499 | 1272 | 最後に評価された値を返します。 |
2500 | 1273 | これにより、かなり一般的に使えるホームディレクトリを探す方法は: |
2501 | 1274 | |
2502 | $home = | |
1275 | $home = $ENV{'HOME'} // $ENV{'LOGDIR'} // | |
2503 | | |
1276 | (getpwuid($<))[7] // die "You're homeless!\n"; | |
2504 | // (getpwuid($<))[7] | |
2505 | // die "You're homeless!\n"; | |
2506 | 1277 | |
2507 | 1278 | =begin original |
2508 | 1279 | |
2509 | 1280 | In particular, this means that you shouldn't use this |
2510 | 1281 | for selecting between two aggregates for assignment: |
2511 | 1282 | |
2512 | 1283 | =end original |
2513 | 1284 | |
2514 | 1285 | 特に、これは代入のために二つの集合を選択するためには |
2515 | 1286 | 使うべきではないことを意味します。 |
2516 | 1287 | |
2517 | @a = @b || @c; | |
1288 | @a = @b || @c; # this is wrong | |
2518 | @a = scalar(@b) || @c; | |
1289 | @a = scalar(@b) || @c; # really meant this | |
2519 | @a = @b ? @b : @c; | |
1290 | @a = @b ? @b : @c; # this works fine, though | |
2520 | 1291 | |
2521 | 1292 | =begin original |
2522 | 1293 | |
2523 | As alternatives to C<&&> and C<||> when used for | |
1294 | As more readable alternatives to C<&&> and C<||> when used for | |
2524 | 1295 | control flow, Perl provides the C<and> and C<or> operators (see below). |
2525 | The short-circuit behavior is identical. The precedence of | |
1296 | The short-circuit behavior is identical. The precedence of "and" | |
2526 | and | |
1297 | and "or" is much lower, however, so that you can safely use them after a | |
2527 | 1298 | list operator without the need for parentheses: |
2528 | 1299 | |
2529 | 1300 | =end original |
2530 | 1301 | |
2531 | Perl では、フロー制御に使う場合の C<&&> と C<||> の | |
1302 | Perl では、フロー制御に使う場合の多少読みやすい C<&&> と C<||> の | |
2532 | C<and> 演算子と C<or> 演算子が用意されています (下記参照)。 | |
1303 | 同義語として、C<and> 演算子と C<or> 演算子が用意されています (下記参照)。 | |
2533 | 1304 | 短絡の動作は全く同じです。 |
2534 | しかし、 | |
1305 | しかし、"and" と "or" の優先順位はかなり低くしてあるので、引数に括弧を | |
2535 | 1306 | 使っていないリスト演算子のあとに続けて使う場合にも、 |
2536 | 1307 | 安心して使うことができます: |
2537 | 1308 | |
2538 | 1309 | unlink "alpha", "beta", "gamma" |
2539 | 1310 | or gripe(), next LINE; |
2540 | 1311 | |
2541 | 1312 | =begin original |
2542 | 1313 | |
2543 | 1314 | With the C-style operators that would have been written like this: |
2544 | 1315 | |
2545 | 1316 | =end original |
2546 | 1317 | |
2547 | 1318 | C スタイルの演算子では以下のように書く必要があります。 |
2548 | 1319 | |
2549 | 1320 | unlink("alpha", "beta", "gamma") |
2550 | 1321 | || (gripe(), next LINE); |
2551 | 1322 | |
2552 | 1323 | =begin original |
2553 | 1324 | |
2554 | ||
1325 | Using "or" for assignment is unlikely to do what you want; see below. | |
2555 | 1326 | |
2556 | 1327 | =end original |
2557 | 1328 | |
2558 | ||
1329 | 代入で "or" を使うと、したいことと違うことになります。 | |
1330 | 以下を参照して下さい。 | |
2559 | 1331 | |
2560 | unless(unlink("alpha", "beta", "gamma")) { | |
2561 | gripe(); | |
2562 | next LINE; | |
2563 | } | |
2564 | ||
2565 | =begin original | |
2566 | ||
2567 | Using C<"or"> for assignment is unlikely to do what you want; see below. | |
2568 | ||
2569 | =end original | |
2570 | ||
2571 | 代入で C<"or"> を使うと、したいことと違うことになります; 以下を | |
2572 | 参照して下さい。 | |
2573 | ||
2574 | 1332 | =head2 Range Operators |
2575 | 1333 | X<operator, range> X<range> X<..> X<...> |
2576 | 1334 | |
2577 | 1335 | (範囲演算子) |
2578 | 1336 | |
2579 | 1337 | =begin original |
2580 | 1338 | |
2581 | Binary | |
1339 | Binary ".." is the range operator, which is really two different | |
2582 | 1340 | operators depending on the context. In list context, it returns a |
2583 | 1341 | list of values counting (up by ones) from the left value to the right |
2584 | 1342 | value. If the left value is greater than the right value then it |
2585 | 1343 | returns the empty list. The range operator is useful for writing |
2586 | ||
1344 | C<foreach (1..10)> loops and for doing slice operations on arrays. In | |
2587 | 1345 | the current implementation, no temporary array is created when the |
2588 | 1346 | range operator is used as the expression in C<foreach> loops, but older |
2589 | 1347 | versions of Perl might burn a lot of memory when you write something |
2590 | 1348 | like this: |
2591 | 1349 | |
2592 | 1350 | =end original |
2593 | 1351 | |
2594 | 二項演算子の | |
1352 | 二項演算子の ".." は範囲演算子で、使われるコンテキストによって | |
2595 | 1353 | 異なる動作をする 2 つの演算子を合わせたものです。 |
2596 | 1354 | リストコンテキストでは、左の値から右の値まで (1 づつ昇順で) 数えあげた値から |
2597 | 1355 | なるリストを返します。 |
2598 | 1356 | 左側の値が右側の値より大きい場合は、空リストを返します。 |
2599 | 範囲演算子は、 | |
1357 | 範囲演算子は、C<foreach (1..10)> のようなループを書くときや、 | |
2600 | 1358 | 配列のスライス演算を行なうときに便利です。 |
2601 | 1359 | 現状の実装では、C<foreach> ループの式の中で範囲演算子を使っても |
2602 | 1360 | 一時配列は作りませんが、古い Perl は以下のようなことを書くと、 |
2603 | 1361 | 大量のメモリを消費することになります: |
2604 | 1362 | |
2605 | 1363 | for (1 .. 1_000_000) { |
2606 | 1364 | # code |
2607 | 1365 | } |
2608 | 1366 | |
2609 | 1367 | =begin original |
2610 | 1368 | |
2611 | The range operator also works on strings, using the magical | |
1369 | The range operator also works on strings, using the magical auto-increment, | |
2612 | ||
1370 | see below. | |
2613 | 1371 | |
2614 | 1372 | =end original |
2615 | 1373 | |
2616 | 範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します | |
1374 | 範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します。 | |
2617 | 1375 | 以下を参照してください。 |
2618 | 1376 | |
2619 | 1377 | =begin original |
2620 | 1378 | |
2621 | In scalar context, | |
1379 | In scalar context, ".." returns a boolean value. The operator is | |
2622 | bistable, like a flip-flop, and emulates the line-range (comma) | |
1380 | bistable, like a flip-flop, and emulates the line-range (comma) operator | |
2623 | o | |
1381 | of B<sed>, B<awk>, and various editors. Each ".." operator maintains its | |
2624 | ||
1382 | own boolean state. It is false as long as its left operand is false. | |
2625 | that contains it. It is false as long as its left operand is false. | |
2626 | 1383 | Once the left operand is true, the range operator stays true until the |
2627 | 1384 | right operand is true, I<AFTER> which the range operator becomes false |
2628 | again. It doesn't become false till the next time the range operator | |
1385 | again. It doesn't become false till the next time the range operator is | |
2629 | ||
1386 | evaluated. It can test the right operand and become false on the same | |
2630 | ||
1387 | evaluation it became true (as in B<awk>), but it still returns true once. | |
2631 | ||
1388 | If you don't want it to test the right operand till the next | |
2632 | ||
1389 | evaluation, as in B<sed>, just use three dots ("...") instead of | |
2633 | two. In all other regards, | |
1390 | two. In all other regards, "..." behaves just like ".." does. | |
2634 | 1391 | |
2635 | 1392 | =end original |
2636 | 1393 | |
2637 | スカラコンテキストで使われたときには、 | |
1394 | スカラコンテキストで使われたときには、".." は真偽値を返します。 | |
2638 | 1395 | この演算子は、フリップフロップのように 2 値安定で、 |
2639 | 1396 | B<sed> や B<awk> や多くのエディタでの行範囲 (コンマ) 演算子を |
2640 | 1397 | エミュレートするものとなります。 |
2641 | 各々の | |
1398 | 各々の ".." 演算子がそれぞれに独立して自分の真偽状態を管理します。 | |
2642 | またいでも、それぞれに独立して自分の真偽状態を管理します。 | |
2643 | 1399 | はじめは、左被演算子が偽である間、演算全体も偽となっています。 |
2644 | いったん左被演算子が真になると | |
1400 | 範囲演算子は、いったん左被演算子が真になると、右被演算子が真である間、 | |
2645 | 真を返すようになります | |
1401 | 真を返すようになります。 | |
2646 | ||
1402 | 右被演算子が偽になると、演算子も偽を返すようになります。 | |
1403 | (次に範囲演算子が評価されるまでは、偽とはなりません。 | |
2647 | 1404 | (B<awk> でのように) 真となった、その評価の中で右被演算子をテストし、 |
2648 | 1405 | 偽とすることができますが、1 度は真を返すことになります。 |
2649 | 1406 | B<sed> でのように、次に評価されるまで右被演算子をテストしたくなければ、 |
2650 | 2 個のドットの代わりに 3 つのドット ( | |
1407 | 2 個のドットの代わりに 3 つのドット ("...") を使ってください。 | |
2651 | その他の点では、 | |
1408 | その他の点では、"..." は ".." と同様に振舞います. | |
2652 | 1409 | |
2653 | 1410 | =begin original |
2654 | 1411 | |
2655 | 1412 | The right operand is not evaluated while the operator is in the |
2656 | 1413 | "false" state, and the left operand is not evaluated while the |
2657 | 1414 | operator is in the "true" state. The precedence is a little lower |
2658 | 1415 | than || and &&. The value returned is either the empty string for |
2659 | false, or a sequence number (beginning with 1) for true. The | |
1416 | false, or a sequence number (beginning with 1) for true. The | |
2660 | number is reset for each range encountered. The final | |
1417 | sequence number is reset for each range encountered. The final | |
2661 | in a range has the string | |
1418 | sequence number in a range has the string "E0" appended to it, which | |
2662 | its numeric value, but gives you something to search | |
1419 | doesn't affect its numeric value, but gives you something to search | |
2663 | to exclude the endpoint. You can exclude the | |
1420 | for if you want to exclude the endpoint. You can exclude the | |
2664 | waiting for the sequence number to be greater | |
1421 | beginning point by waiting for the sequence number to be greater | |
1422 | than 1. | |
2665 | 1423 | |
2666 | 1424 | =end original |
2667 | 1425 | |
2668 | 1426 | 右被演算子は、演算子の状態が「偽」である間は評価されることがなく、 |
2669 | 1427 | 左被演算子は、演算子の状態が「真」である間は評価されることがありません。 |
2670 | 1428 | 優先順位は、|| と && の少し下です。 |
2671 | 1429 | 偽としては空文字列が返され、 |
2672 | 真としては (1 から始まる) | |
1430 | 真としては (1 から始まる) 順に並んだ数値が返されます。 | |
2673 | 1431 | この通し番号は、新たに範囲が始まるごとにリセットされます。 |
2674 | 範囲の最後の | |
1432 | 範囲の最後の数字には、文字列 "E0" が末尾につけられます。 | |
2675 | 数値としては何の影響もありませんが、範囲の終わりで何か特別なことを | |
1433 | これは、数値としては何の影響もありませんが、範囲の終わりで何か特別なことを | |
2676 | 場合に、目印として使うことができます。 | |
1434 | したい場合に、目印として使うことができます。 | |
2677 | 範囲の始まり | |
1435 | 範囲の始まりで何かしたい場合には、通し番号が 1 よりも大きくなるのを | |
2678 | 待てばよいでしょう。 | |
1436 | 待っていればよいでしょう。 | |
2679 | 1437 | |
2680 | 1438 | =begin original |
2681 | 1439 | |
2682 | If either operand of scalar | |
1440 | If either operand of scalar ".." is a constant expression, | |
2683 | 1441 | that operand is considered true if it is equal (C<==>) to the current |
2684 | 1442 | input line number (the C<$.> variable). |
2685 | 1443 | |
2686 | 1444 | =end original |
2687 | 1445 | |
2688 | スカラの | |
1446 | スカラの ".." の被演算子が定数表現であるときは、その被演算子は暗黙に、 | |
2689 | 1447 | 現在の入力行番号(変数 C<$.>) と等しい(C<==>) 場合に真となります。 |
2690 | 1448 | |
2691 | 1449 | =begin original |
2692 | 1450 | |
2693 | To be pedantic, the comparison is actually | |
1451 | To be pedantic, the comparison is actually C<int(EXPR) == int(EXPR)>, | |
2694 | 1452 | but that is only an issue if you use a floating point expression; when |
2695 | 1453 | implicitly using C<$.> as described in the previous paragraph, the |
2696 | comparison is | |
1454 | comparison is C<int(EXPR) == int($.)> which is only an issue when C<$.> | |
2697 | 1455 | is set to a floating point value and you are not reading from a file. |
2698 | Furthermore, | |
1456 | Furthermore, C<"span" .. "spat"> or C<2.18 .. 3.14> will not do what | |
2699 | 1457 | you want in scalar context because each of the operands are evaluated |
2700 | 1458 | using their integer representation. |
2701 | 1459 | |
2702 | 1460 | =end original |
2703 | 1461 | |
2704 | とても細かい話をすると、比較は実際には | |
1462 | とても細かい話をすると、比較は実際には C<int(EXPR) == int(EXPR)> ですが、 | |
2705 | 1463 | これは浮動小数点数を使うときにだけ問題になります; 前の段落で記述したように |
2706 | 明示的に C<$.> を使ったとき、比較は | |
1464 | 明示的に C<$.> を使ったとき、比較は C<int(EXPR) == int($.)> となり、 | |
2707 | 1465 | C<$.> に浮動小数点数がセットされ、ファイルから読み込みを行わない場合にのみ |
2708 | 1466 | 問題になります。 |
2709 | さらに、 | |
1467 | さらに、C<"span" .. "spat"> や C<2.18 .. 3.14> は、それぞれのオペランドが | |
2710 | ||
1468 | 整数表現を使って評価されるため、スカラコンテキストでは望みどおりの結果に | |
2711 | ||
1469 | なりません。 | |
2712 | 1470 | |
2713 | 1471 | =begin original |
2714 | 1472 | |
2715 | 1473 | Examples: |
2716 | 1474 | |
2717 | 1475 | =end original |
2718 | 1476 | |
2719 | 1477 | 例: |
2720 | 1478 | |
2721 | 1479 | =begin original |
2722 | 1480 | |
2723 | 1481 | As a scalar operator: |
2724 | 1482 | |
2725 | 1483 | =end original |
2726 | 1484 | |
2727 | 1485 | スカラ演算子として: |
2728 | 1486 | |
2729 | 1487 | if (101 .. 200) { print; } # print 2nd hundred lines, short for |
2730 | # if ($. == 101 .. $. == 200) { print; } | |
1488 | # if ($. == 101 .. $. == 200) { print; } | |
2731 | 1489 | |
2732 | 1490 | next LINE if (1 .. /^$/); # skip header lines, short for |
2733 | 1491 | # next LINE if ($. == 1 .. /^$/); |
2734 | 1492 | # (typically in a loop labeled LINE) |
2735 | 1493 | |
2736 | 1494 | s/^/> / if (/^$/ .. eof()); # quote body |
2737 | 1495 | |
2738 | 1496 | # parse mail messages |
2739 | 1497 | while (<>) { |
2740 | 1498 | $in_header = 1 .. /^$/; |
2741 | 1499 | $in_body = /^$/ .. eof; |
2742 | 1500 | if ($in_header) { |
2743 | 1501 | # do something |
2744 | 1502 | } else { # in body |
2745 | 1503 | # do something else |
2746 | 1504 | } |
2747 | 1505 | } continue { |
2748 | 1506 | close ARGV if eof; # reset $. each file |
2749 | 1507 | } |
2750 | 1508 | |
2751 | 1509 | =begin original |
2752 | 1510 | |
2753 | 1511 | Here's a simple example to illustrate the difference between |
2754 | 1512 | the two range operators: |
2755 | 1513 | |
2756 | 1514 | =end original |
2757 | 1515 | |
2758 | 1516 | 以下は二つの範囲演算子の違いを示す単純な例です: |
2759 | 1517 | |
2760 | 1518 | @lines = (" - Foo", |
2761 | 1519 | "01 - Bar", |
2762 | 1520 | "1 - Baz", |
2763 | 1521 | " - Quux"); |
2764 | 1522 | |
2765 | 1523 | foreach (@lines) { |
2766 | 1524 | if (/0/ .. /1/) { |
2767 | 1525 | print "$_\n"; |
2768 | 1526 | } |
2769 | 1527 | } |
2770 | 1528 | |
2771 | 1529 | =begin original |
2772 | 1530 | |
2773 | This program will print only the line containing "Bar". | |
1531 | This program will print only the line containing "Bar". If | |
2774 | 1532 | the range operator is changed to C<...>, it will also print the |
2775 | 1533 | "Baz" line. |
2776 | 1534 | |
2777 | 1535 | =end original |
2778 | 1536 | |
2779 | 1537 | このプログラムは "Bar" を含む行だけを表示します。 |
2780 | 1538 | 範囲演算子を C<...> に変更すると、"Baz" の行も表示します。 |
2781 | 1539 | |
2782 | 1540 | =begin original |
2783 | 1541 | |
2784 | 1542 | And now some examples as a list operator: |
2785 | 1543 | |
2786 | 1544 | =end original |
2787 | 1545 | |
2788 | 1546 | これはリスト演算子の例です: |
2789 | 1547 | |
2790 | ||
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 | |
2791 | 1551 | |
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 | ||
2802 | 1552 | =begin original |
2803 | 1553 | |
2804 | ||
1554 | The range operator (in list context) makes use of the magical | |
2805 | ||
1555 | auto-increment algorithm if the operands are strings. You | |
1556 | can say | |
2806 | 1557 | |
2807 | 1558 | =end original |
2808 | 1559 | |
2809 | ||
1560 | (リストコンテキストでの) 範囲演算子は、被演算子が文字列であるときには、 | |
2810 | リ | |
1561 | マジカルインクリメントの機能を使います。 | |
1562 | 以下のように書くと: | |
2811 | 1563 | |
2812 | ||
1564 | @alphabet = ('A' .. 'Z'); | |
2813 | 1565 | |
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 | ||
2820 | 1566 | =begin original |
2821 | 1567 | |
2822 | ||
1568 | to get all normal letters of the English alphabet, or | |
2823 | auto-increment algorithm if both operands are strings, subject to the | |
2824 | following rules: | |
2825 | 1569 | |
2826 | 1570 | =end original |
2827 | 1571 | |
2828 | ||
1572 | 英語の大文字すべてを得られますし: | |
2829 | 文字列であるときには、次の規則に従って、マジカルインクリメントの | |
2830 | 機能が使えます。 | |
2831 | 1573 | |
2832 | ||
1574 | $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15]; | |
2833 | 1575 | |
2834 | =item * | |
2835 | ||
2836 | 1576 | =begin original |
2837 | 1577 | |
2838 | ||
1578 | to get a hexadecimal digit, or | |
2839 | the magic increment will not be applied, and the strings will be treated | |
2840 | as numbers (more specifically, integers) instead. | |
2841 | 1579 | |
2842 | 1580 | =end original |
2843 | 1581 | |
2844 | ||
1582 | と書けば、16 進の数字が得られますし、 | |
2845 | 両側の文字列が Perl にとって数値に見える場合、 | |
2846 | マジカルインクリメントは適用されず、代わりにその文字列は数値として | |
2847 | (より正確には、整数として)扱われます。 | |
2848 | 1583 | |
2849 | = | |
1584 | @z2 = ('01' .. '31'); print $z2[$mday]; | |
2850 | 1585 | |
2851 | For example, C<"-2".."2"> is the same as C<-2..2>, and | |
2852 | C<"2.18".."3.14"> produces C<2, 3>. | |
2853 | ||
2854 | =end original | |
2855 | ||
2856 | 例えば、C<"-2".."2"> は C<-2..2> と同じで、 | |
2857 | C<"2.18".."3.14"> は C<2, 3> を生成します。 | |
2858 | ||
2859 | =item * | |
2860 | ||
2861 | 1586 | =begin original |
2862 | 1587 | |
2863 | ||
1588 | to get dates with leading zeros. | |
2864 | C<0> and is longer than one character, in this case the magic increment | |
2865 | I<will> be applied, even though strings like C<"01"> would normally look | |
2866 | like a number to Perl. | |
2867 | 1589 | |
2868 | 1590 | =end original |
2869 | 1591 | |
2870 | ||
1592 | とすれば、0 付きの日付が得られます。 | |
2871 | 左側の文字列が C<0> で始まっていて 2 文字以上の場合、 | |
2872 | C<"01"> のような文字列は通常 Perl にとって数値に見えるにも関わらず、 | |
2873 | この場合はマジカルインクリメントは I<適用されます> 。 | |
2874 | 1593 | |
2875 | 1594 | =begin original |
2876 | 1595 | |
2877 | For example, C<"01".."04"> produces C<"01", "02", "03", "04">, and | |
2878 | C<"00".."-1"> produces C<"00"> through C<"99"> - this may seem | |
2879 | surprising, but see the following rules for why it works this way. | |
2880 | To get dates with leading zeros, you can say: | |
2881 | ||
2882 | =end original | |
2883 | ||
2884 | 例えば、C<"01".."04"> は C<"01", "02", "03", "04"> を生成し、 | |
2885 | C<"00".."-1"> は C<"00"> から C<"99"> を生成します - これは驚きがあるかも | |
2886 | しれませんが、なぜこれがこのように動作するのかについては | |
2887 | 次の規則を見てください。 | |
2888 | 先頭に 0 があるものから日付を取り出すために、次のようにできます: | |
2889 | ||
2890 | @z2 = ("01" .. "31"); | |
2891 | print $z2[$mday]; | |
2892 | ||
2893 | =begin original | |
2894 | ||
2895 | If 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 | ||
2905 | B<Note:> In Perl versions 5.30 and below, I<any> string on the left-hand | |
2906 | side beginning with C<"0">, including the string C<"0"> itself, would | |
2907 | cause the magic string increment behavior. This means that on these Perl | |
2908 | versions, C<"0".."-1"> would produce C<"0"> through C<"99">, which was | |
2909 | inconsistent with C<0..-1>, which produces the empty list. This also means | |
2910 | that C<"0".."9"> now produces a list of integers instead of a list of | |
2911 | strings. | |
2912 | ||
2913 | =end original | |
2914 | ||
2915 | B<注意:> Perl バージョン 5.30 以下では、左側が C<"0"> で始まる | |
2916 | I<任意の> 文字列 (文字列 C<"0"> 自体を含む)は、マジカル文字列 | |
2917 | インクリメントを引き起こします。 | |
2918 | つまり、これらの Perl バージョンでは、C<"0".."-1"> は C<"0"> から | |
2919 | C<"99"> までを生成しますが、これは空のリストを生成する C<0..-1> と | |
2920 | 矛盾していました。 | |
2921 | また、C<"0".."9"> では、文字列のリストではなく整数のリストが | |
2922 | 生成されるということです。 | |
2923 | ||
2924 | =item * | |
2925 | ||
2926 | =begin original | |
2927 | ||
2928 | If the initial value specified isn't part of a magical increment | |
2929 | sequence (that is, a non-empty string matching C</^[a-zA-Z]*[0-9]*\z/>), | |
2930 | only 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 | ||
2940 | For example, C<"ax".."az"> produces C<"ax", "ay", "az">, but | |
2941 | C<"*x".."az"> produces only C<"*x">. | |
2942 | ||
2943 | =end original | |
2944 | ||
2945 | 例えば、C<"ax".."az"> は C<"ax", "ay", "az"> を生成しますが、 | |
2946 | C<"*x".."az"> は C<"*x"> だけを生成します。 | |
2947 | ||
2948 | =item * | |
2949 | ||
2950 | =begin original | |
2951 | ||
2952 | For other initial values that are strings that do follow the rules of the | |
2953 | magical increment, the corresponding sequence will be returned. | |
2954 | ||
2955 | =end original | |
2956 | ||
2957 | マジカルインクリメントの規則に従う文字列がもう片方の初期値の場合、 | |
2958 | 対応する並びが返されます。 | |
2959 | ||
2960 | =begin original | |
2961 | ||
2962 | For example, you can say | |
2963 | ||
2964 | =end original | |
2965 | ||
2966 | 例えば以下のように書くと: | |
2967 | ||
2968 | @alphabet = ("A" .. "Z"); | |
2969 | ||
2970 | =begin original | |
2971 | ||
2972 | to 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 | ||
2982 | to get a hexadecimal digit. | |
2983 | ||
2984 | =end original | |
2985 | ||
2986 | と書けば、16 進の数字が得られます。 | |
2987 | ||
2988 | =item * | |
2989 | ||
2990 | =begin original | |
2991 | ||
2992 | 1596 | If the final value specified is not in the sequence that the magical |
2993 | 1597 | increment would produce, the sequence goes until the next value would |
2994 | be longer than the final value specified. | |
1598 | be longer than the final value specified. | |
2995 | string is shorter than the first, the empty list is returned. | |
2996 | 1599 | |
2997 | ||
2998 | 1600 | =end original |
2999 | 1601 | |
3000 | 1602 | マジカルインクリメントによって得られる値の中に指定した最終値に |
3001 | 1603 | ちょうど一致するものが見つからないような場合には、 |
3002 | 1604 | マジカルインクリメントによって得られる次の値の文字列長が、 |
3003 | 1605 | 最終値として指定した値のものより長くなるまでインクリメントが続けられます。 |
3004 | 最終値の文字列の長さが最初のものより短い場合、 | |
3005 | 空リストが返されます。 | |
3006 | 1606 | |
3007 | 1607 | =begin original |
3008 | 1608 | |
3009 | ||
1609 | If the initial value specified isn't part of a magical increment | |
3010 | ||
1610 | sequence (that is, a non-empty string matching "/^[a-zA-Z]*[0-9]*\z/"), | |
3011 | li | |
1611 | only the initial value will be returned. So the following will only | |
1612 | return an alpha: | |
3012 | 1613 | |
3013 | 1614 | =end original |
3014 | 1615 | |
3015 | ||
1616 | 指定された初期値がマジカルインクリメント処理の一部でない場合 | |
3016 | ||
1617 | (つまり、"/^[a-zA-Z]*[0-9]*\z/" にマッチングする、空でない文字列の場合)、 | |
3017 | ||
1618 | 初期値のみが返されます。 | |
1619 | 従って、以下はαのみを返します: | |
3018 | 1620 | |
3019 | ||
1621 | use charnames 'greek'; | |
3020 | ||
3021 | =begin original | |
3022 | ||
3023 | As of Perl 5.26, the list-context range operator on strings works as expected | |
3024 | in the scope of L<< S<C<"use feature 'unicode_strings">>|feature/The | |
3025 | 'unicode_strings' feature >>. In previous versions, and outside the scope of | |
3026 | that feature, it exhibits L<perlunicode/The "Unicode Bug">: its behavior | |
3027 | depends on the internal encoding of the range endpoint. | |
3028 | ||
3029 | =end original | |
3030 | ||
3031 | Perl 5.26 から、文字列に対するリストコンテキスト範囲演算子は、 | |
3032 | L<< 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 | ||
3041 | Because the magical increment only works on non-empty strings matching | |
3042 | C</^[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"; | |
3051 | 1622 | my @greek_small = ("\N{alpha}" .. "\N{omega}"); |
3052 | 1623 | |
3053 | 1624 | =begin original |
3054 | 1625 | |
3055 | To get | |
1626 | To get lower-case greek letters, use this instead: | |
3056 | you could use this instead: | |
3057 | 1627 | |
3058 | 1628 | =end original |
3059 | 1629 | |
3060 | ||
1630 | 小文字のギリシャ文字を得るためには、代わりに以下のようにしてください: | |
3061 | 代わりに以下のようにしてください: | |
3062 | 1631 | |
3063 | | |
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 | ); | |
3068 | 1633 | |
3069 | 1634 | =begin original |
3070 | 1635 | |
3071 | ||
1636 | Because each operand is evaluated in integer form, C<2.18 .. 3.14> will | |
3072 | ||
1637 | return two elements in list context. | |
3073 | you could use the pattern C</(?:(?=\p{Greek})\p{Lower})+/> (or the | |
3074 | L<experimental feature|perlrecharclass/Extended Bracketed Character | |
3075 | Classes> C<S</(?[ \p{Greek} & \p{Lower} ])+/>>). | |
3076 | 1638 | |
3077 | 1639 | =end original |
3078 | 1640 | |
3079 | ||
1641 | それぞれのオペランドは整数の形で評価されるので、C<2.18 .. 3.14> は | |
3080 | ||
1642 | リストコンテキストでは二つの要素を返します。 | |
3081 | C</(?:(?=\p{Greek})\p{Lower})+/> というパターン (または | |
3082 | L<実験的機能|perlrecharclass/Extended Bracketed Character Classes> である | |
3083 | C<S</(?[ \p{Greek} & \p{Lower} ])+/>>) を使います。 | |
3084 | 1643 | |
1644 | @list = (2.18 .. 3.14); # same as @list = (2 .. 3); | |
1645 | ||
3085 | 1646 | =head2 Conditional Operator |
3086 | 1647 | X<operator, conditional> X<operator, ternary> X<ternary> X<?:> |
3087 | 1648 | |
3088 | 1649 | (条件演算子) |
3089 | 1650 | |
3090 | 1651 | =begin original |
3091 | 1652 | |
3092 | Ternary | |
1653 | Ternary "?:" is the conditional operator, just as in C. It works much | |
3093 | like an if-then-else. If the argument before the | |
1654 | like an if-then-else. If the argument before the ? is true, the | |
3094 | argument before the | |
1655 | argument before the : is returned, otherwise the argument after the : | |
3095 | ||
1656 | is returned. For example: | |
3096 | 1657 | |
3097 | 1658 | =end original |
3098 | 1659 | |
3099 | 三項演算子の | |
1660 | 三項演算子の "?:" は、C の場合と同じ条件演算子です。 | |
3100 | 1661 | これは、if-then-else のように働きます。 |
3101 | ||
1662 | "?" の前の引数が真であれば ":" の前の引数が返されますが、 | |
3102 | 真でなければ、 | |
1663 | 真でなければ、":" の後の引数が返されます。 | |
3103 | 1664 | 例えば: |
3104 | 1665 | |
3105 | 1666 | printf "I have %d dog%s.\n", $n, |
3106 | ($n == 1) ? | |
1667 | ($n == 1) ? '' : "s"; | |
3107 | 1668 | |
3108 | 1669 | =begin original |
3109 | 1670 | |
3110 | 1671 | Scalar or list context propagates downward into the 2nd |
3111 | 1672 | or 3rd argument, whichever is selected. |
3112 | 1673 | |
3113 | 1674 | =end original |
3114 | 1675 | |
3115 | 1676 | スカラコンテキストかリストコンテキストかという状況は、 |
3116 | 1677 | 選択された 2 番目もしくは 3 番目の引数にまで伝わります。 |
3117 | 1678 | |
3118 | = | |
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! | |
3119 | 1682 | |
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 | ||
3130 | 1683 | =begin original |
3131 | 1684 | |
3132 | 1685 | The operator may be assigned to if both the 2nd and 3rd arguments are |
3133 | 1686 | legal lvalues (meaning that you can assign to them): |
3134 | 1687 | |
3135 | 1688 | =end original |
3136 | 1689 | |
3137 | 1690 | 2 番目と 3 番目の引数双方が左辺値 (代入可能ということ)であれば、 |
3138 | 1691 | この演算子に代入を行なうこともできます: |
3139 | 1692 | |
3140 | ($ | |
1693 | ($a_or_b ? $a : $b) = $c; | |
3141 | 1694 | |
3142 | 1695 | =begin original |
3143 | 1696 | |
3144 | 1697 | Because this operator produces an assignable result, using assignments |
3145 | 1698 | without parentheses will get you in trouble. For example, this: |
3146 | 1699 | |
3147 | 1700 | =end original |
3148 | 1701 | |
3149 | 1702 | この演算子は代入可能な結果を生み出すので、 |
3150 | 1703 | 括弧なしで代入を行うとおかしくなるかもしれません。 |
3151 | 1704 | 例えばこれは: |
3152 | 1705 | |
3153 | $ | |
1706 | $a % 2 ? $a += 10 : $a += 2 | |
3154 | 1707 | |
3155 | 1708 | =begin original |
3156 | 1709 | |
3157 | 1710 | Really means this: |
3158 | 1711 | |
3159 | 1712 | =end original |
3160 | 1713 | |
3161 | 1714 | 以下を意味し: |
3162 | 1715 | |
3163 | (($ | |
1716 | (($a % 2) ? ($a += 10) : $a) += 2 | |
3164 | 1717 | |
3165 | 1718 | =begin original |
3166 | 1719 | |
3167 | 1720 | Rather than this: |
3168 | 1721 | |
3169 | 1722 | =end original |
3170 | 1723 | |
3171 | 1724 | 以下のようにはなりません: |
3172 | 1725 | |
3173 | ($ | |
1726 | ($a % 2) ? ($a += 10) : ($a += 2) | |
3174 | 1727 | |
3175 | 1728 | =begin original |
3176 | 1729 | |
3177 | 1730 | That should probably be written more simply as: |
3178 | 1731 | |
3179 | 1732 | =end original |
3180 | 1733 | |
3181 | 1734 | 恐らく以下のようにもっと単純に書くべきでしょう: |
3182 | 1735 | |
3183 | $ | |
1736 | $a += ($a % 2) ? 10 : 2; | |
3184 | 1737 | |
3185 | 1738 | =head2 Assignment Operators |
3186 | 1739 | X<assignment> X<operator, assignment> X<=> X<**=> X<+=> X<*=> X<&=> |
3187 | 1740 | X<<< <<= >>> X<&&=> X<-=> X</=> X<|=> X<<< >>= >>> X<||=> X<//=> X<.=> |
3188 | X<%=> X<^=> X<x=> | |
1741 | X<%=> X<^=> X<x=> | |
3189 | 1742 | |
3190 | 1743 | (代入演算子) |
3191 | 1744 | |
3192 | 1745 | =begin original |
3193 | 1746 | |
3194 | ||
1747 | "=" is the ordinary assignment operator. | |
3195 | 1748 | |
3196 | 1749 | =end original |
3197 | 1750 | |
3198 | ||
1751 | "=" は通常の代入演算子です。 | |
3199 | 1752 | |
3200 | 1753 | =begin original |
3201 | 1754 | |
3202 | 1755 | Assignment operators work as in C. That is, |
3203 | 1756 | |
3204 | 1757 | =end original |
3205 | 1758 | |
3206 | 1759 | 代入演算子は C の場合と同様の働きをします。 |
3207 | 1760 | つまり、 |
3208 | 1761 | |
3209 | $ | |
1762 | $a += 2; | |
3210 | 1763 | |
3211 | 1764 | =begin original |
3212 | 1765 | |
3213 | 1766 | is equivalent to |
3214 | 1767 | |
3215 | 1768 | =end original |
3216 | 1769 | |
3217 | 1770 | は以下と等価です: |
3218 | 1771 | |
3219 | $ | |
1772 | $a = $a + 2; | |
3220 | 1773 | |
3221 | 1774 | =begin original |
3222 | 1775 | |
3223 | 1776 | although without duplicating any side effects that dereferencing the lvalue |
3224 | might trigger, such as from | |
1777 | might trigger, such as from tie(). Other assignment operators work similarly. | |
3225 | 1778 | The following are recognized: |
3226 | 1779 | |
3227 | 1780 | =end original |
3228 | 1781 | |
3229 | しかし、 | |
1782 | しかし、tie() のようなもので起こる左辺値の被参照による | |
3230 | 1783 | 副作用が 2 回起こることはありません。 |
3231 | 1784 | 他の代入演算も同様に働きます。 |
3232 | 1785 | 以下のものが認識されます: |
3233 | 1786 | |
3234 | **= += *= &= | |
1787 | **= += *= &= <<= &&= | |
3235 | -= /= |= | |
1788 | -= /= |= >>= ||= | |
3236 | .= %= ^= | |
1789 | .= %= ^= //= | |
3237 | 1790 | x= |
3238 | 1791 | |
3239 | 1792 | =begin original |
3240 | 1793 | |
3241 | 1794 | Although these are grouped by family, they all have the precedence |
3242 | of assignment. | |
1795 | of assignment. | |
3243 | scalars, whereas the ordinary assignment operator can assign to arrays, | |
3244 | hashes, lists and even references. (See L<"Context"|perldata/Context> | |
3245 | and L<perldata/List value constructors>, and L<perlref/Assigning to | |
3246 | References>.) | |
3247 | 1796 | |
3248 | 1797 | =end original |
3249 | 1798 | |
3250 | 1799 | グループ分けしてありますが、これらはいずれも代入演算子として |
3251 | 1800 | 同じ優先順位となっています。 |
3252 | これらの複合代入演算子はスカラとしてのみ動作しますが、一方 | |
3253 | 通常の代入演算子は配列、スカラ、リスト、リファレンスに代入できます。 | |
3254 | (L<"Context"|perldata/Context>, L<perldata/List value constructors>, | |
3255 | L<perlref/Assigning to References> を参照してください。) | |
3256 | 1801 | |
3257 | 1802 | =begin original |
3258 | 1803 | |
3259 | 1804 | Unlike in C, the scalar assignment operator produces a valid lvalue. |
3260 | 1805 | Modifying an assignment is equivalent to doing the assignment and |
3261 | 1806 | then modifying the variable that was assigned to. This is useful |
3262 | 1807 | for modifying a copy of something, like this: |
3263 | 1808 | |
3264 | 1809 | =end original |
3265 | 1810 | |
3266 | 1811 | C と違って、スカラ代入演算子は有効な左辺値を作り出します。 |
3267 | 1812 | 代入を修正することは、代入を行なってから、その代入された変数を修正するのと |
3268 | 1813 | 同じことになります。 |
3269 | 1814 | これは、以下のように何かのコピーを変更したいときに便利です: |
3270 | 1815 | |
3271 | ($tmp = $global) =~ tr | |
1816 | ($tmp = $global) =~ tr [A-Z] [a-z]; | |
3272 | 1817 | |
3273 | 1818 | =begin original |
3274 | 1819 | |
3275 | Although 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 | ||
3286 | 1820 | Likewise, |
3287 | 1821 | |
3288 | 1822 | =end original |
3289 | 1823 | |
3290 | 1824 | 同様に、 |
3291 | 1825 | |
3292 | ($ | |
1826 | ($a += 2) *= 3; | |
3293 | 1827 | |
3294 | 1828 | =begin original |
3295 | 1829 | |
3296 | 1830 | is equivalent to |
3297 | 1831 | |
3298 | 1832 | =end original |
3299 | 1833 | |
3300 | 1834 | は以下と等価です: |
3301 | 1835 | |
3302 | $ | |
1836 | $a += 2; | |
3303 | $ | |
1837 | $a *= 3; | |
3304 | 1838 | |
3305 | 1839 | =begin original |
3306 | 1840 | |
3307 | 1841 | Similarly, a list assignment in list context produces the list of |
3308 | 1842 | lvalues assigned to, and a list assignment in scalar context returns |
3309 | 1843 | the number of elements produced by the expression on the right hand |
3310 | 1844 | side of the assignment. |
3311 | 1845 | |
3312 | 1846 | =end original |
3313 | 1847 | |
3314 | 1848 | 同様に、リストコンテキストでのリストへの代入は代入可能な左辺値のリストとなり、 |
3315 | 1849 | スカラコンテキストでのリストへの代入は代入の右側の式で作成された |
3316 | 1850 | 要素の数を返します。 |
3317 | 1851 | |
3318 | =begin original | |
3319 | ||
3320 | The three dotted bitwise assignment operators (C<&.=> C<|.=> C<^.=>) are new in | |
3321 | Perl 5.22. See L</Bitwise String Operators>. | |
3322 | ||
3323 | =end original | |
3324 | ||
3325 | 三つのドット付きビット単位代入演算子 (C<&.=> C<|.=> C<^.=>) は | |
3326 | Perl 5.22 からの新しいものです。 | |
3327 | L</Bitwise String Operators> を参照してください。 | |
3328 | ||
3329 | 1852 | =head2 Comma Operator |
3330 | 1853 | X<comma> X<operator, comma> X<,> |
3331 | 1854 | |
3332 | 1855 | (コンマ演算子) |
3333 | 1856 | |
3334 | 1857 | =begin original |
3335 | 1858 | |
3336 | Binary | |
1859 | Binary "," is the comma operator. In scalar context it evaluates | |
3337 | 1860 | its left argument, throws that value away, then evaluates its right |
3338 | 1861 | argument and returns that value. This is just like C's comma operator. |
3339 | 1862 | |
3340 | 1863 | =end original |
3341 | 1864 | |
3342 | 二項演算子の | |
1865 | 二項演算子の "," はコンマ演算子です。 | |
3343 | 1866 | スカラコンテキストではその左引数を評価し、その値を捨てて、 |
3344 | 1867 | それから右引数を評価し、その値を返します。 |
3345 | 1868 | これはちょうど、C のコンマ演算子と同じです。 |
3346 | 1869 | |
3347 | 1870 | =begin original |
3348 | 1871 | |
3349 | 1872 | In list context, it's just the list argument separator, and inserts |
3350 | 1873 | both its arguments into the list. These arguments are also evaluated |
3351 | 1874 | from left to right. |
3352 | 1875 | |
3353 | 1876 | =end original |
3354 | 1877 | |
3355 | 1878 | リストコンテキストでは、これは単にリスト引数の区切り文字で、 |
3356 | 1879 | 双方の引数をそのリストに挿入する働きがあります。 |
3357 | 1880 | これらの引数も左から右に評価されます。 |
3358 | 1881 | |
3359 | 1882 | =begin original |
3360 | 1883 | |
3361 | The C<< => >> operator | |
1884 | The C<< => >> operator is a synonym for the comma except that it causes | |
3362 | for t | |
1885 | its left operand to be interpreted as a string if it begins with a letter | |
3363 | word on its left to be interpreted as a string if it begins with a letter | |
3364 | 1886 | or underscore and is composed only of letters, digits and underscores. |
3365 | 1887 | This includes operands that might otherwise be interpreted as operators, |
3366 | constants, single number v-strings or function calls. | |
1888 | constants, single number v-strings or function calls. If in doubt about | |
3367 | this behavior, the left operand can be quoted explicitly. | |
1889 | this behaviour, the left operand can be quoted explicitly. | |
3368 | 1890 | |
3369 | 1891 | =end original |
3370 | 1892 | |
3371 | C<< => >> 演算子 | |
1893 | C<< => >> 演算子はコンマ演算子の同義語ですが、 | |
3372 | ||
1894 | もし左オペランドが文字か下線で始まっていて、かつ文字、数字、下線でのみ | |
3373 | ||
1895 | 構成されている場合、これを文字列として扱うという効果もあります。 | |
3374 | 1896 | これには他の場所では演算子、定数、v-文字列、関数呼び出しとして扱われる |
3375 | 1897 | オペランドを含みます。 |
3376 | 1898 | この振る舞いについて迷うことがあるなら、左オペランドを明示的に |
3377 | 1899 | クォートすることも出来ます。 |
3378 | 1900 | |
3379 | 1901 | =begin original |
3380 | 1902 | |
3381 | 1903 | Otherwise, the C<< => >> operator behaves exactly as the comma operator |
3382 | 1904 | or list argument separator, according to context. |
3383 | 1905 | |
3384 | 1906 | =end original |
3385 | 1907 | |
3386 | 1908 | さもなければ、C<< => >> 演算子はコンテキストによって、 |
3387 | 1909 | カンマ演算子かリスト引数の区切り文字と全く同様に振る舞います。 |
3388 | 1910 | |
3389 | 1911 | =begin original |
3390 | 1912 | |
3391 | 1913 | For example: |
3392 | 1914 | |
3393 | 1915 | =end original |
3394 | 1916 | |
3395 | 1917 | 例えば: |
3396 | 1918 | |
3397 | 1919 | use constant FOO => "something"; |
3398 | 1920 | |
3399 | 1921 | my %h = ( FOO => 23 ); |
3400 | 1922 | |
3401 | 1923 | =begin original |
3402 | 1924 | |
3403 | 1925 | is equivalent to: |
3404 | 1926 | |
3405 | 1927 | =end original |
3406 | 1928 | |
3407 | 1929 | は、以下と等価です: |
3408 | 1930 | |
3409 | 1931 | my %h = ("FOO", 23); |
3410 | 1932 | |
3411 | 1933 | =begin original |
3412 | 1934 | |
3413 | 1935 | It is I<NOT>: |
3414 | 1936 | |
3415 | 1937 | =end original |
3416 | 1938 | |
3417 | 1939 | これは I<違います>: |
3418 | 1940 | |
3419 | 1941 | my %h = ("something", 23); |
3420 | 1942 | |
3421 | 1943 | =begin original |
3422 | 1944 | |
3423 | 1945 | The C<< => >> operator is helpful in documenting the correspondence |
3424 | 1946 | between keys and values in hashes, and other paired elements in lists. |
3425 | 1947 | |
3426 | 1948 | =end original |
3427 | 1949 | |
3428 | 1950 | C<< => >> 演算子は、ハッシュのキーと値や、その他のリスト中の組となる |
3429 | 1951 | 要素の関係を表現するのに便利です。 |
3430 | 1952 | |
3431 | %hash = ( $key => $value ); | |
1953 | %hash = ( $key => $value ); | |
3432 | login( $username => $password ); | |
1954 | login( $username => $password ); | |
3433 | 1955 | |
3434 | =begin original | |
3435 | ||
3436 | The special quoting behavior ignores precedence, and hence may apply to | |
3437 | I<part> of the left operand: | |
3438 | ||
3439 | =end original | |
3440 | ||
3441 | 特殊なクォートの振る舞いは優先順位を無視し、従って左オペランドの I<一部> に | |
3442 | 適用されることがあります: | |
3443 | ||
3444 | print time.shift => "bbb"; | |
3445 | ||
3446 | =begin original | |
3447 | ||
3448 | That example prints something like C<"1314363215shiftbbb">, because the | |
3449 | C<< => >> implicitly quotes the C<shift> immediately on its left, ignoring | |
3450 | the fact that C<time.shift> is the entire left operand. | |
3451 | ||
3452 | =end original | |
3453 | ||
3454 | この例は C<"1314363215shiftbbb"> のようなものを表示します; なぜなら | |
3455 | C<< => >> は暗黙にすぐ左にある C<shift> をクォートし、 C<time.shift> 全体が | |
3456 | 左オペランドであるという事実を無視するからです。 | |
3457 | ||
3458 | 1956 | =head2 List Operators (Rightward) |
3459 | 1957 | X<operator, list, rightward> X<list operator> |
3460 | 1958 | |
3461 | 1959 | (リスト演算子 (右方向)) |
3462 | 1960 | |
3463 | 1961 | =begin original |
3464 | 1962 | |
3465 | On the right side of a list operator, t | |
1963 | On the right side of a list operator, it has very low precedence, | |
3466 | 1964 | such that it controls all comma-separated expressions found there. |
3467 | 1965 | The only operators with lower precedence are the logical operators |
3468 | ||
1966 | "and", "or", and "not", which may be used to evaluate calls to list | |
3469 | operators without the need for parentheses: | |
1967 | operators without the need for extra parentheses: | |
3470 | 1968 | |
3471 | 1969 | =end original |
3472 | 1970 | |
3473 | リスト演算子の右側のものにとって、 | |
1971 | リスト演算子の右側のものにとって、リスト演算子はとても低い優先順位になります。 | |
3474 | 1972 | これによってコンマで区切った式をリスト演算子の引数として |
3475 | 1973 | 置くことができます。 |
3476 | これよりも優先順位が低いものは、論理演算子の | |
1974 | これよりも優先順位が低いものは、論理演算子の "and", "or", "not" のみで、 | |
3477 | ||
1975 | 余分な括弧を付けないリスト演算子の呼び出しを評価するために使えます: | |
3478 | 1976 | |
3479 | open HANDLE, " | |
1977 | open HANDLE, "filename" | |
3480 | ||
1978 | or die "Can't open: $!\n"; | |
3481 | 1979 | |
3482 | 1980 | =begin original |
3483 | 1981 | |
3484 | ||
1982 | See also discussion of list operators in L<Terms and List Operators (Leftward)>. | |
3485 | it with parentheses: | |
3486 | 1983 | |
3487 | 1984 | =end original |
3488 | 1985 | |
3489 | ||
1986 | L<Terms and List Operators (Leftward)> のリスト演算子の議論も参照して下さい。 | |
3490 | 1987 | |
3491 | open(HANDLE, "< :encoding(UTF-8)", "filename") | |
3492 | or die "Can't open: $!\n"; | |
3493 | ||
3494 | =begin original | |
3495 | ||
3496 | in 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 | ||
3507 | See also discussion of list operators in L</Terms and List Operators (Leftward)>. | |
3508 | ||
3509 | =end original | |
3510 | ||
3511 | L</Terms and List Operators (Leftward)> のリスト演算子の議論も参照して下さい。 | |
3512 | ||
3513 | 1988 | =head2 Logical Not |
3514 | 1989 | X<operator, logical, not> X<not> |
3515 | 1990 | |
3516 | 1991 | (論理否定) |
3517 | 1992 | |
3518 | 1993 | =begin original |
3519 | 1994 | |
3520 | Unary | |
1995 | Unary "not" returns the logical negation of the expression to its right. | |
3521 | It's the equivalent of | |
1996 | It's the equivalent of "!" except for the very low precedence. | |
3522 | 1997 | |
3523 | 1998 | =end original |
3524 | 1999 | |
3525 | 単項演算子の | |
2000 | 単項演算子の "not" は右側に来る式の否定を返します。 | |
3526 | これは、優先順位がずっと低いことを除いては | |
2001 | これは、優先順位がずっと低いことを除いては "!" と等価です。 | |
3527 | 2002 | |
3528 | 2003 | =head2 Logical And |
3529 | 2004 | X<operator, logical, and> X<and> |
3530 | 2005 | |
3531 | 2006 | (論理積) |
3532 | 2007 | |
3533 | 2008 | =begin original |
3534 | 2009 | |
3535 | Binary | |
2010 | Binary "and" returns the logical conjunction of the two surrounding | |
3536 | expressions. It's equivalent to | |
2011 | expressions. It's equivalent to && except for the very low | |
3537 | precedence. This means that it short-circuits: the right | |
2012 | precedence. This means that it short-circuits: i.e., the right | |
3538 | 2013 | expression is evaluated only if the left expression is true. |
3539 | 2014 | |
3540 | 2015 | =end original |
3541 | 2016 | |
3542 | 二項演算子の | |
2017 | 二項演算子の "and" は両側の式の論理積を返します。 | |
3543 | これは、優先順位がずっと低いことを除けば | |
2018 | これは、優先順位がずっと低いことを除けば && と等価です。 | |
3544 | 2019 | つまり、これも短絡演算を行ない、右側の式は左側の式が |
3545 | 2020 | 「真」であった場合にのみ評価されます。 |
3546 | 2021 | |
3547 | =head2 Logical or and Exclusive Or | |
2022 | =head2 Logical or, Defined or, and Exclusive Or | |
3548 | 2023 | X<operator, logical, or> X<operator, logical, xor> |
3549 | X<operator, logical, exclusive or> | |
2024 | X<operator, logical, defined or> X<operator, logical, exclusive or> | |
3550 | 2025 | X<or> X<xor> |
3551 | 2026 | |
3552 | (論理和と排他論理和) | |
2027 | (論理和と定義性和と排他論理和) | |
3553 | 2028 | |
3554 | 2029 | =begin original |
3555 | 2030 | |
3556 | Binary | |
2031 | Binary "or" returns the logical disjunction of the two surrounding | |
3557 | expressions. It's equivalent to | |
2032 | expressions. It's equivalent to || except for the very low precedence. | |
3558 | This makes it useful for control flow | |
2033 | This makes it useful for control flow | |
3559 | 2034 | |
3560 | 2035 | =end original |
3561 | 2036 | |
3562 | 二項演算子の | |
2037 | 二項演算子の "or" は両側の式の論理和を返します。 | |
3563 | これは、優先順位がずっと低いことを除いて | |
2038 | これは、優先順位がずっと低いことを除いて || と等価です。 | |
3564 | 2039 | これはフローを制御するのに有用です: |
3565 | 2040 | |
3566 | 2041 | print FH $data or die "Can't write to FH: $!"; |
3567 | 2042 | |
3568 | 2043 | =begin original |
3569 | 2044 | |
3570 | This means that it short-circuits: the right expression is evaluated | |
2045 | This means that it short-circuits: i.e., the right expression is evaluated | |
3571 | only if the left expression is false. Due to its precedence, you | |
2046 | only if the left expression is false. Due to its precedence, you should | |
3572 | b | |
2047 | probably avoid using this for assignment, only for control flow. | |
3573 | It usually works out better for flow control than in assignments: | |
3574 | 2048 | |
3575 | 2049 | =end original |
3576 | 2050 | |
3577 | 2051 | つまり、これも短絡演算を行ない、右側の式は左側の式が |
3578 | 2052 | 「偽」であった場合にのみ評価されます。 |
3579 | 優先度の関係で、これ | |
2053 | 優先度の関係で、これは代入には使わず、フローの制御のみに使うべきです。 | |
3580 | 避けなければなりません。 | |
3581 | これは普通代入よりも、フローの制御でうまく動作します: | |
3582 | 2054 | |
3583 | =b | |
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 | |
3584 | 2058 | |
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 | ||
3595 | 2059 | =begin original |
3596 | 2060 | |
3597 | 2061 | However, when it's a list-context assignment and you're trying to use |
3598 | ||
2062 | "||" for control flow, you probably need "or" so that the assignment | |
3599 | 2063 | takes higher precedence. |
3600 | 2064 | |
3601 | 2065 | =end original |
3602 | 2066 | |
3603 | しかし、代入がリストコンテキストの時に | |
2067 | しかし、代入がリストコンテキストの時に "||" をフロー制御に使おうとする場合、 | |
3604 | 代入により大きな優先順位を持たせるために | |
2068 | 代入により大きな優先順位を持たせるために "or" が必要かもしれません。 | |
3605 | 2069 | |
3606 | =begin original | |
3607 | ||
3608 | 2070 | @info = stat($file) || die; # oops, scalar sense of stat! |
3609 | 2071 | @info = stat($file) or die; # better, now @info gets its due |
3610 | 2072 | |
3611 | =end original | |
3612 | ||
3613 | @info = stat($file) || die; # うわ、stat がスカラの意味だ! | |
3614 | @info = stat($file) or die; # よりよい; @info はその目的を果たす | |
3615 | ||
3616 | 2073 | =begin original |
3617 | 2074 | |
3618 | 2075 | Then again, you could always use parentheses. |
3619 | 2076 | |
3620 | 2077 | =end original |
3621 | 2078 | |
3622 | 2079 | もちろん、常に括弧をつけてもよいです。 |
3623 | 2080 | |
3624 | 2081 | =begin original |
3625 | 2082 | |
3626 | Binary | |
2083 | Binary "xor" returns the exclusive-OR of the two surrounding expressions. | |
3627 | It cannot short | |
2084 | It cannot short circuit, of course. | |
3628 | 2085 | |
3629 | 2086 | =end original |
3630 | 2087 | |
3631 | 二項演算子の | |
2088 | 二項演算子の "xor" は両側の式の排他論理和を返します。 | |
3632 | これは | |
2089 | これはもちろん、短絡ではありません。 | |
3633 | 2090 | |
3634 | =begin original | |
3635 | ||
3636 | There is no low precedence operator for defined-OR. | |
3637 | ||
3638 | =end original | |
3639 | ||
3640 | 定義性論理和の低優先順位版はありません。 | |
3641 | ||
3642 | 2091 | =head2 C Operators Missing From Perl |
3643 | 2092 | X<operator, missing from perl> X<&> X<*> |
3644 | 2093 | X<typecasting> X<(TYPE)> |
3645 | 2094 | |
3646 | 2095 | (Perl にない C の演算子) |
3647 | 2096 | |
3648 | 2097 | =begin original |
3649 | 2098 | |
3650 | 2099 | Here is what C has that Perl doesn't: |
3651 | 2100 | |
3652 | 2101 | =end original |
3653 | 2102 | |
3654 | 2103 | C にあって Perl に無いものは以下の通りです: |
3655 | 2104 | |
3656 | 2105 | =over 8 |
3657 | 2106 | |
3658 | 2107 | =item unary & |
3659 | 2108 | |
3660 | 2109 | =begin original |
3661 | 2110 | |
3662 | Address-of operator. (But see the | |
2111 | Address-of operator. (But see the "\" operator for taking a reference.) | |
3663 | 2112 | |
3664 | 2113 | =end original |
3665 | 2114 | |
3666 | 2115 | アドレス演算子。 |
3667 | ( | |
2116 | ("\" 演算子がリファレンスのために用いられます。) | |
3668 | 2117 | |
3669 | 2118 | =item unary * |
3670 | 2119 | |
3671 | 2120 | =begin original |
3672 | 2121 | |
3673 | Dereference-address operator. | |
2122 | Dereference-address operator. (Perl's prefix dereferencing | |
3674 | operators are typed: | |
2123 | operators are typed: $, @, %, and &.) | |
3675 | 2124 | |
3676 | 2125 | =end original |
3677 | 2126 | |
3678 | 2127 | 被アドレス参照演算子。 |
3679 | (Perl の被参照プリフィクス演算子が型づけを行ないます: | |
2128 | (Perl の被参照プリフィクス演算子が型づけを行ないます: $, @, %, &。) | |
3680 | C<$>, C<@>, C<%>, C<&>。) | |
3681 | 2129 | |
3682 | 2130 | =item (TYPE) |
3683 | 2131 | |
3684 | 2132 | =begin original |
3685 | 2133 | |
3686 | 2134 | Type-casting operator. |
3687 | 2135 | |
3688 | 2136 | =end original |
3689 | 2137 | |
3690 | 2138 | 型のキャスト演算子。 |
3691 | 2139 | |
3692 | 2140 | =back |
3693 | 2141 | |
3694 | 2142 | =head2 Quote and Quote-like Operators |
3695 | 2143 | X<operator, quote> X<operator, quote-like> X<q> X<qq> X<qx> X<qw> X<m> |
3696 | 2144 | X<qr> X<s> X<tr> X<'> X<''> X<"> X<""> X<//> X<`> X<``> X<<< << >>> |
3697 | 2145 | X<escape sequence> X<escape> |
3698 | 2146 | |
3699 | 2147 | (クォートとクォート風の演算子) |
3700 | 2148 | |
3701 | 2149 | =begin original |
3702 | 2150 | |
3703 | 2151 | While we usually think of quotes as literal values, in Perl they |
3704 | 2152 | function as operators, providing various kinds of interpolating and |
3705 | 2153 | pattern matching capabilities. Perl provides customary quote characters |
3706 | 2154 | for these behaviors, but also provides a way for you to choose your |
3707 | 2155 | quote character for any of them. In the following table, a C<{}> represents |
3708 | 2156 | any pair of delimiters you choose. |
3709 | 2157 | |
3710 | 2158 | =end original |
3711 | 2159 | |
3712 | 2160 | クォートはリテラル値であると考えるのが普通ですが、Perl において、 |
3713 | 2161 | クォートは演算子として働き、さまざまな展開やパターンマッチの機能を |
3714 | 2162 | 持っています。 |
3715 | 2163 | そのような動作をさせるのに、Perl は慣習的にクォート文字を使っていますが、 |
3716 | 2164 | どの種類のクォートも、自分でクォート文字を選べるようになっています。 |
3717 | 2165 | 以下の表では、{} がその選んだ区切文字のペアを示しています。 |
3718 | 2166 | |
3719 | 2167 | =begin original |
3720 | 2168 | |
3721 | 2169 | Customary Generic Meaning Interpolates |
3722 | 2170 | '' q{} Literal no |
3723 | 2171 | "" qq{} Literal yes |
3724 | 2172 | `` qx{} Command yes* |
3725 | 2173 | qw{} Word list no |
3726 | 2174 | // m{} Pattern match yes* |
3727 | 2175 | qr{} Pattern yes* |
3728 | 2176 | s{}{} Substitution yes* |
3729 | 2177 | tr{}{} Transliteration no (but see below) |
3730 | y{}{} Transliteration no (but see below) | |
3731 | 2178 | <<EOF here-doc yes* |
3732 | 2179 | |
2180 | * unless the delimiter is ''. | |
2181 | ||
3733 | 2182 | =end original |
3734 | 2183 | |
3735 | 2184 | 通常記法 汎用記法 意味 展開 |
3736 | 2185 | ================================================= |
3737 | 2186 | '' q{} リテラル 不可 |
3738 | 2187 | "" qq{} リテラル 可 |
3739 | 2188 | `` qx{} コマンド 可 * |
3740 | 2189 | qw{} 単語リスト 不可 |
3741 | 2190 | // m{} パターンマッチ 可 * |
3742 | 2191 | qr{} パターン 可 * |
3743 | 2192 | s{}{} 置換 可 * |
3744 | 2193 | tr{}{} 変換 不可 (但し以下を参照のこと) |
3745 | y{}{} 変換 不可 (但し以下を参照のこと) | |
3746 | 2194 | <<EOF ヒアドキュメント 可 * |
3747 | 2195 | |
3748 | =begin original | |
3749 | ||
3750 | * unless the delimiter is ''. | |
3751 | ||
3752 | =end original | |
3753 | ||
3754 | 2196 | * '' がデリミタでない場合のみ |
3755 | 2197 | |
3756 | 2198 | =begin original |
3757 | 2199 | |
3758 | 2200 | Non-bracketing delimiters use the same character fore and aft, but the four |
3759 | sorts of | |
2201 | sorts of brackets (round, angle, square, curly) will all nest, which means | |
3760 | 2202 | that |
3761 | 2203 | |
3762 | 2204 | =end original |
3763 | 2205 | |
3764 | 2206 | 選んだ区切文字が括弧の類でない場合には、前後の文字として同一のものを |
3765 | 使いますが、4 つの | |
2207 | 使いますが、4 つの括弧 ((), <>, [], {}) の場合にはネストできます。 | |
3766 | 2208 | つまり、以下のものは、 |
3767 | 2209 | |
3768 | ||
2210 | q{foo{bar}baz} | |
3769 | 2211 | |
3770 | 2212 | =begin original |
3771 | 2213 | |
3772 | 2214 | is the same as |
3773 | 2215 | |
3774 | 2216 | =end original |
3775 | 2217 | |
3776 | 2218 | 以下と同じです。 |
3777 | 2219 | |
3778 | ||
2220 | 'foo{bar}baz' | |
3779 | 2221 | |
3780 | 2222 | =begin original |
3781 | 2223 | |
3782 | 2224 | Note, however, that this does not always work for quoting Perl code: |
3783 | 2225 | |
3784 | 2226 | =end original |
3785 | 2227 | |
3786 | 2228 | しかし、以下のコードはクォートされた Perl コードでは |
3787 | 2229 | いつも正しく動くわけではないことに注意してください: |
3788 | 2230 | |
3789 | ||
2231 | $s = q{ if($a eq "}") ... }; # WRONG | |
3790 | 2232 | |
3791 | 2233 | =begin original |
3792 | 2234 | |
3793 | is a syntax error. | |
2235 | is a syntax error. The C<Text::Balanced> module (from CPAN, and | |
3794 | an | |
2236 | starting from Perl 5.8 part of the standard distribution) is able | |
2237 | to do this properly. | |
3795 | 2238 | |
3796 | 2239 | =end original |
3797 | 2240 | |
3798 | 2241 | これは文法エラーとなります。 |
3799 | C< | |
2242 | C<Text::Balanced> モジュール(CPAN から、または Perl 5.8 からは標準配布の | |
3800 | ||
2243 | 一部です)はこれを適切に行います。 | |
3801 | 2244 | |
3802 | 2245 | =begin original |
3803 | 2246 | |
3804 | There can | |
2247 | There can be whitespace between the operator and the quoting | |
3805 | and the quoting | |
3806 | 2248 | characters, except when C<#> is being used as the quoting character. |
3807 | C<q#foo#> is parsed as the string C<foo>, while | |
2249 | C<q#foo#> is parsed as the string C<foo>, while C<q #foo#> is the | |
3808 | 2250 | operator C<q> followed by a comment. Its argument will be taken |
3809 | 2251 | from the next line. This allows you to write: |
3810 | 2252 | |
3811 | 2253 | =end original |
3812 | 2254 | |
3813 | 演算子とクォート文字の間に空白を置くことも出来ます | |
2255 | 演算子とクォート文字の間に空白を置くことも出来ます。 | |
3814 | ||
2256 | ただし、C<#> をクォート文字として使う場合は例外です。 | |
3815 | 2257 | C<q#foo#> は文字列 C<foo> としてパースされますが、 |
3816 | ||
2258 | C<q #foo#> は C<q> 演算子の後にコメントがあるとみなされます。 | |
3817 | 2259 | この引数は次の行から取られます。つまり、以下のように書けます: |
3818 | 2260 | |
3819 | =begin original | |
3820 | ||
3821 | 2261 | s {foo} # Replace foo |
3822 | 2262 | {bar} # with bar. |
3823 | 2263 | |
3824 | =end original | |
3825 | ||
3826 | s {foo} # foo を | |
3827 | {bar} # bar で置き換える | |
3828 | ||
3829 | 2264 | =begin original |
3830 | 2265 | |
3831 | The | |
2266 | The following escape sequences are available in constructs that interpolate | |
3832 | ||
2267 | and in transliterations. | |
2268 | X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> | |
3833 | 2269 | |
3834 | 2270 | =end original |
3835 | 2271 | |
3836 | ||
2272 | 以下のエスケープシーケンスが展開と文字変換の構文で利用可能です。 | |
3837 | ||
2273 | X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> | |
3838 | 2274 | |
3839 | q XfooX # Works: means the string 'foo' | |
3840 | qXfooX # WRONG! | |
3841 | ||
3842 | 2275 | =begin original |
3843 | 2276 | |
3844 | ||
2277 | \t tab (HT, TAB) | |
3845 | ||
2278 | \n newline (NL) | |
3846 | ||
2279 | \r return (CR) | |
3847 | ||
2280 | \f form feed (FF) | |
3848 | ||
2281 | \b backspace (BS) | |
3849 | ||
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 | |
3850 | 2289 | |
3851 | 2290 | =end original |
3852 | 2291 | |
3853 | ||
2292 | \t タブ (HT, TAB) | |
3854 | ||
2293 | \n 改行 (NL) | |
3855 | ||
2294 | \r 復帰 (CR) | |
3856 | ||
2295 | \f 改ページ (FF) | |
3857 | ||
2296 | \b バックスペース (BS) | |
3858 | ||
2297 | \a アラーム (BEL) | |
3859 | ||
2298 | \e エスケープ (ESC) | |
2299 | \033 8 進数で表した文字 (例: ESC) | |
2300 | \x1b 16 進数で表した文字 (例: ESC) | |
2301 | \x{263a} 16 進数で表したワイド文字 (例: SMILEY) | |
2302 | \c[ コントロール文字 (例: ESC) | |
2303 | \N{name} 名前つき Unicode 文字 | |
3860 | 2304 | |
3861 | 2305 | =begin original |
3862 | 2306 | |
3863 | | |
2307 | The character following C<\c> is mapped to some other character by | |
3864 | | |
2308 | converting letters to upper case and then (on ASCII systems) by inverting | |
3865 | | |
2309 | the 7th bit (0x40). The most interesting range is from '@' to '_' | |
3866 | | |
2310 | (0x40 through 0x5F), resulting in a control character from 0x00 | |
3867 | | |
2311 | through 0x1F. A '?' maps to the DEL character. On EBCDIC systems only | |
3868 | \ | |
2312 | '@', the letters, '[', '\', ']', '^', '_' and '?' will work, resulting | |
3869 | | |
2313 | in 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) | |
3880 | 2314 | |
3881 | 2315 | =end original |
3882 | 2316 | |
3883 | | |
2317 | C<\c> に引き続く文字は、英字は大文字に変換された後、(ASCII システムでは) | |
3884 | | |
2318 | 7 番目のビット (0x40) を反転させることで別の文字にマッピングされます。 | |
3885 | | |
2319 | 最も興味深い範囲は '@' から '_' (0x40 から 0x5F) で、結果として | |
3886 | | |
2320 | コントロール文字 0x00 から 0x1F になります。 | |
3887 | | |
2321 | '?' は DEL 文字にマッピングされます。 | |
3888 | \ | |
2322 | EBCDIC システムでは '@', 英字, '[', '\', ']', '^', '_', '?' のみが | |
3889 | | |
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) | |
3900 | 2324 | |
3901 | 2325 | =begin original |
3902 | 2326 | |
3903 | N | |
2327 | B<NOTE>: Unlike C and other languages, Perl has no \v escape sequence for | |
3904 | ||
2328 | the vertical tab (VT - ASCII 11), but you may use C<\ck> or C<\x0b>. | |
3905 | with and inside of the braces, as illustrated above by the second | |
3906 | S<C<\x{ }>> example. | |
3907 | 2329 | |
3908 | 2330 | =end original |
3909 | 2331 | |
3910 | ||
2332 | B<注意>: C やその他の言語と違って、Perl は垂直タブ (VT - ASCII 11) のための | |
3911 | ||
2333 | \v エスケープシーケンスはありませんが、C<\ck> または C<\x0b> が使えます。 | |
3912 | 位置にオプションの空白(タブまたはスペース文字)を置くことができます。 | |
3913 | 2334 | |
3914 | =over 4 | |
3915 | ||
3916 | =item [1] | |
3917 | ||
3918 | 2335 | =begin original |
3919 | 2336 | |
3920 | The | |
2337 | The following escape sequences are available in constructs that interpolate | |
3921 | the 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 | ||
3930 | Blanks (tab or space characters) may separate the number from either or | |
3931 | both of the braces. | |
3932 | ||
3933 | =end original | |
3934 | ||
3935 | 空白 (タブまたはスペース文字) は、片方または両方の中かっこから数字を | |
3936 | 分離します。 | |
3937 | ||
3938 | =begin original | |
3939 | ||
3940 | Otherwise, only hexadecimal digits are valid between the braces. If an | |
3941 | invalid character is encountered, a warning will be issued and the | |
3942 | invalid character and all subsequent characters (valid or invalid) | |
3943 | within the braces will be discarded. | |
3944 | ||
3945 | =end original | |
3946 | ||
3947 | さもなければ、中かっこの中には 16 進数字のみが妥当です。 | |
3948 | 不正な文字に遭遇すると、警告が発生し、中かっこの内側の不正な文字と | |
3949 | それ以降の文字(妥当でも不正でも)は捨てられます。 | |
3950 | ||
3951 | =begin original | |
3952 | ||
3953 | If there are no valid digits between the braces, the generated character is | |
3954 | the NULL character (C<\x{00}>). However, an explicit empty brace (C<\x{}>) | |
3955 | will 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 | ||
3967 | The result is the character specified by the hexadecimal number in the range | |
3968 | 0x00 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 | ||
3977 | Only hexadecimal digits are valid following C<\x>. When C<\x> is followed | |
3978 | by fewer than two valid digits, any valid digits will be zero-padded. This | |
3979 | means that C<\x7> will be interpreted as C<\x07>, and a lone C<"\x"> will be | |
3980 | interpreted as C<\x00>. Except at the end of a string, having fewer than | |
3981 | two valid digits will result in a warning. Note that although the warning | |
3982 | says the illegal character is ignored, it is only ignored as part of the | |
3983 | escape and will still be used as the subsequent character in the string. | |
3984 | For example: | |
3985 | ||
3986 | =end original | |
3987 | ||
3988 | C<\x> に引き続くのは 16 進数字のみが妥当です。 | |
3989 | C<\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 | ||
4008 | The result is the Unicode character or character sequence given by I<name>. | |
4009 | See L<charnames>. | |
4010 | ||
4011 | =end original | |
4012 | ||
4013 | 結果は I<name> で指定される Unicode 文字または文字の並びです。 | |
4014 | L<charnames> を参照してください。 | |
4015 | ||
4016 | =item [4] | |
4017 | ||
4018 | =begin original | |
4019 | ||
4020 | S<C<\N{U+I<hexadecimal number>}>> means the Unicode character whose Unicode code | |
4021 | point is I<hexadecimal number>. | |
4022 | ||
4023 | =end original | |
4024 | ||
4025 | S<C<\N{U+I<hexadecimal number>}>> は、Unicode 符号位置が | |
4026 | I<hexadecimal number> の Unicode 文字を意味します。 | |
4027 | ||
4028 | =item [5] | |
4029 | ||
4030 | =begin original | |
4031 | ||
4032 | The character following C<\c> is mapped to some other character as shown in the | |
4033 | table: | |
4034 | ||
4035 | =end original | |
4036 | ||
4037 | C<\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 | ||
4058 | In other words, it's the character whose code point has had 64 xor'd with | |
4059 | its uppercase. C<\c?> is DELETE on ASCII platforms because | |
4060 | S<C<ord("?") ^ 64>> is 127, and | |
4061 | C<\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 して大文字にした文字です。 | |
4066 | S<C<ord("?") ^ 64>> は 127 なので C<\c?> は ASCII プラットフォームでは | |
4067 | DELETE で、C<"@"> は 64 のために | |
4068 | 64 で xor すると 0 になるので C<\c@> は NUL です。 | |
4069 | ||
4070 | =begin original | |
4071 | ||
4072 | Also, C<\c\I<X>> yields S<C< chr(28) . "I<X>">> for any I<X>, but cannot come at the | |
4073 | end of a string, because the backslash would be parsed as escaping the end | |
4074 | quote. | |
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 | ||
4084 | On ASCII platforms, the resulting characters from the list above are the | |
4085 | complete set of ASCII controls. This isn't the case on EBCDIC platforms; see | |
4086 | L<perlebcdic/OPERATOR DIFFERENCES> for a full discussion of the | |
4087 | differences between these for ASCII versus EBCDIC platforms. | |
4088 | ||
4089 | =end original | |
4090 | ||
4091 | ASCII プラットフォームでは、上述の一覧からの結果の文字は ASCII 制御文字の | |
4092 | 完全な集合です。 | |
4093 | これは EBCDIC プラットフォームには当てはまりません; これに関する | |
4094 | ASCII プラットフォームと EBCDIC プラットフォームとの違いの完全な議論については | |
4095 | L<perlebcdic/OPERATOR DIFFERENCES> を参照してください。 | |
4096 | ||
4097 | =begin original | |
4098 | ||
4099 | Use of any other character following the C<"c"> besides those listed above is | |
4100 | discouraged, and as of Perl v5.20, the only characters actually allowed | |
4101 | are the printable ASCII ones, minus the left brace C<"{">. What happens | |
4102 | for any of the allowed other characters is that the value is derived by | |
4103 | xor'ing with the seventh bit, which is 64, and a warning raised if | |
4104 | enabled. Using the non-allowed characters generates a fatal error. | |
4105 | ||
4106 | =end original | |
4107 | ||
4108 | C<"c"> に引き続いて上述した以外の文字を使うことは非推奨であり、 | |
4109 | as of Perl v5.20, the only characters actually allowed | |
4110 | are the printable ASCII ones, minus the left brace C<"{">. | |
4111 | 許されている他の文字を置いたときに起こることは、値が第 7 ビット; | |
4112 | つまり 64 で xor を取ったものになり、 | |
4113 | 有効の場合は警告が発生します。 | |
4114 | 許されていない文字を使うと致命的エラーが発生します。 | |
4115 | ||
4116 | =begin original | |
4117 | ||
4118 | To 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 | ||
4128 | The result is the character specified by the octal number between the braces. | |
4129 | See L</[8]> below for details on which character. | |
4130 | ||
4131 | =end original | |
4132 | ||
4133 | 結果は中かっこで囲まれた 8 進数で指定された文字です。 | |
4134 | その文字に関する詳細については以下の L</[8]> を参照してください。 | |
4135 | ||
4136 | =begin original | |
4137 | ||
4138 | Blanks (tab or space characters) may separate the number from either or | |
4139 | both of the braces. | |
4140 | ||
4141 | =end original | |
4142 | ||
4143 | 空白 (タブまたはスペース文字) は、片方または両方の中かっこから数字を | |
4144 | 分離します。 | |
4145 | ||
4146 | =begin original | |
4147 | ||
4148 | Otherwise, if a character that isn't an octal digit is encountered, a | |
4149 | warning is raised, and the value is based on the octal digits before it, | |
4150 | discarding it and all following characters up to the closing brace. It | |
4151 | is a fatal error if there are no octal digits at all. | |
4152 | ||
4153 | =end original | |
4154 | ||
4155 | さもなければ、8 進数でない文字に遭遇すると、警告が発生し、値はそこまでの | |
4156 | 8 進数字を基として、それ以降閉じ中かっこまでの全ての文字を捨てます。 | |
4157 | 8 進数字がまったくないと致命的エラーになります。 | |
4158 | ||
4159 | =item [7] | |
4160 | ||
4161 | =begin original | |
4162 | ||
4163 | The result is the character specified by the three-digit octal number in the | |
4164 | range 000 to 777 (but best to not use above 077, see next paragraph). See | |
4165 | L</[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 | ||
4175 | Some contexts allow 2 or even 1 digit, but any usage without exactly | |
4176 | three digits, the first being a zero, may give unintended results. (For | |
4177 | example, in a regular expression it may be confused with a backreference; | |
4178 | see L<perlrebackslash/Octal escapes>.) Starting in Perl 5.14, you may | |
4179 | use C<\o{}> instead, which avoids all these problems. Otherwise, it is best to | |
4180 | use this construct only for ordinals C<\077> and below, remembering to pad to | |
4181 | the left with zeros to make three digits. For larger ordinals, either use | |
4182 | C<\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 | |
4184 | C<\x{}> instead. | |
4185 | ||
4186 | =end original | |
4187 | ||
4188 | 一部のコンテキストでは 2 桁や、1 桁ですら許されますが、正確に 3 桁かつ | |
4189 | 先頭が 0、以外の使い方は意図していない結果をもたらすかもしれません。 | |
4190 | (例えば、正規表現中では後方参照で混乱するかもしれません; | |
4191 | L<perlrebackslash/Octal escapes> を参照してください。) | |
4192 | Perl 5.14 から、代わりに C<\o{}> を使えます; これはこれらすべての問題を | |
4193 | 避けられます。 | |
4194 | さもなければ、この構文を値 C<\077> 以下でのみ使用するのが最良です; | |
4195 | 3 桁にするために左側にゼロをパッディングするのを忘れないでください。 | |
4196 | より大きな値では、C<\o{}> を使うか、代わりに 16 進数にして | |
4197 | C<\N{U+}> | |
4198 | (これは異なった文字集合のプラットフォーム間で移植性があります) を使うか、 | |
4199 | または C<\x{}> を | |
4200 | 使うような、他のものに変換してください。 | |
4201 | ||
4202 | =item [8] | |
4203 | ||
4204 | =begin original | |
4205 | ||
4206 | Several constructs above specify a character by a number. That number | |
4207 | gives the character's position in the character set encoding (indexed from 0). | |
4208 | This is called synonymously its ordinal, code position, or code point. Perl | |
4209 | works on platforms that have a native encoding currently of either ASCII/Latin1 | |
4210 | or EBCDIC, each of which allow specification of 256 characters. In general, if | |
4211 | the number is 255 (0xFF, 0377) or below, Perl interprets this in the platform's | |
4212 | native encoding. If the number is 256 (0x100, 0400) or above, Perl interprets | |
4213 | it as a Unicode code point and the result is the corresponding Unicode | |
4214 | character. For example C<\x{50}> and C<\o{120}> both are the number 80 in | |
4215 | decimal, which is less than 256, so the number is interpreted in the native | |
4216 | character set encoding. In ASCII the character in the 80th position (indexed | |
4217 | from 0) is the letter C<"P">, and in EBCDIC it is the ampersand symbol C<"&">. | |
4218 | C<\x{100}> and C<\o{400}> are both 256 in decimal, so the number is interpreted | |
4219 | as a Unicode code point no matter what the native encoding is. The name of the | |
4220 | character in the 256th position (indexed by 0) in Unicode is | |
4221 | C<LATIN CAPITAL LETTER A WITH MACRON>. | |
4222 | ||
4223 | =end original | |
4224 | ||
4225 | 上述のいくつかの構造は文字を数値で指定しています。 | |
4226 | この値は文字集合エンコーディングで (0 から始めた) 文字の位置を指定します。 | |
4227 | これは同意語として序数(ordinal)、コード位置(code position)、 | |
4228 | 符号位置(code point)と呼ばれます。 | |
4229 | Perl は現在のところ、それぞれ 256 文字を定義している ASCII/Latin1 または | |
4230 | EBCDIC のどちらかのネイティブエンコーディングを持つプラットフォームで | |
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 | 小さいので、数値はネイティブ文字集合エンコーディングとして解釈されます。 | |
4238 | ASCII では (0 から始めて) 80 番目の位置の文字は C<"P"> で、EBCDIC では | |
4239 | アンパサンド記号 C<"&"> です。 | |
4240 | C<\x{100}> と C<\o{400}> はどちらも 10 進数では 256 なので、数値は | |
4241 | ネイティブエンコーディングが何かに関わらず Unicode 符号位置として | |
4242 | 解釈されます。 | |
4243 | Unicode での (0 から始めて) 256 番目の位置の文字の名前は | |
4244 | C<LATIN CAPITAL LETTER A WITH MACRON> です。 | |
4245 | ||
4246 | =begin original | |
4247 | ||
4248 | An exception to the above rule is that S<C<\N{U+I<hex number>}>> is | |
4249 | always interpreted as a Unicode code point, so that C<\N{U+0050}> is C<"P"> even | |
4250 | on 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 | ||
4261 | B<NOTE>: Unlike C and other languages, Perl has no C<\v> escape sequence for | |
4262 | the vertical tab (VT, which is 11 in both ASCII and EBCDIC), but you may | |
4263 | use C<\N{VT}>, C<\ck>, C<\N{U+0b}>, or C<\x0b>. (C<\v> | |
4264 | does have meaning in regular expression patterns in Perl, see L<perlre>.) | |
4265 | ||
4266 | =end original | |
4267 | ||
4268 | B<注意>: C やその他の言語と違って、Perl は垂直タブ (VT - ASCII と EBCDIC の | |
4269 | 両方で 11) のための \v エスケープシーケンスはありませんが、C<\N{VT}>, C<\ck>, | |
4270 | C<\N{U+0b}>, C<\x0b> が使えます。 | |
4271 | (C<\v> は Perl の正規表現パターンでは意味があります; L<perlre> を | |
4272 | 参照してください。) | |
4273 | ||
4274 | =begin original | |
4275 | ||
4276 | The following escape sequences are available in constructs that interpolate, | |
4277 | 2338 | but not in transliterations. |
4278 | X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> | |
2339 | X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> | |
4279 | 2340 | |
4280 | 2341 | =end original |
4281 | 2342 | |
4282 | 2343 | 以下のエスケープシーケンスが展開と文字変換の構文で利用可能です。 |
4283 | 2344 | X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> |
4284 | X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> X<\F> | |
4285 | 2345 | |
4286 | 2346 | =begin original |
4287 | 2347 | |
4288 | \l lowercase next char | |
2348 | \l lowercase next char | |
4289 | \u | |
2349 | \u uppercase next char | |
4290 | \L lowercase | |
2350 | \L lowercase till \E | |
4291 | \U uppercase | |
2351 | \U uppercase till \E | |
4292 | \ | |
2352 | \E end case modification | |
4293 | \Q | |
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) | |
4297 | 2354 | |
4298 | 2355 | =end original |
4299 | 2356 | |
4300 | \l | |
2357 | \l 次の文字を小文字にする | |
4301 | \u | |
2358 | \u 次の文字を大文字にする | |
4302 | \L | |
2359 | \L \E まで小文字にする | |
4303 | \U | |
2360 | \U \E まで大文字にする | |
4304 | \ | |
2361 | \E 変更の終わり | |
4305 | \Q | |
2362 | \Q \E まで非単語文字をクォートする | |
4306 | \E 大文字小文字変換かクォート部分(どちらか最後に現れたもの)を | |
4307 | 終了させる | |
4308 | 2363 | |
4309 | 2364 | =begin original |
4310 | 2365 | |
4311 | ||
2366 | If C<use locale> is in effect, the case map used by C<\l>, C<\L>, | |
4312 | are | |
2367 | C<\u> and C<\U> is taken from the current locale. See L<perllocale>. | |
2368 | If Unicode (for example, C<\N{}> or wide hex characters of 0x100 or | |
2369 | beyond) is being used, the case map used by C<\l>, C<\L>, C<\u> and | |
2370 | C<\U> is as defined by Unicode. For documentation of C<\N{name}>, | |
2371 | see L<charnames>. | |
4313 | 2372 | |
4314 | 2373 | =end original |
4315 | 2374 | |
4316 | C< | |
2375 | C<use locale> が有効の場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる | |
4317 | 参照してください。 | |
4318 | ||
4319 | =begin original | |
4320 | ||
4321 | C<\L>, C<\U>, C<\F>, and C<\Q> can stack, in which case you need one | |
4322 | C<\E> for each. For example: | |
4323 | ||
4324 | =end original | |
4325 | ||
4326 | C<\L>, C<\U>, C<\F>, C<\Q> はスタックできます; この場合それぞれに対して | |
4327 | C<\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 | ||
4335 | If a S<C<use locale>> form that includes C<LC_CTYPE> is in effect (see | |
4336 | L<perllocale>), the case map used by C<\l>, C<\L>, C<\u>, and C<\U> is | |
4337 | taken from the current locale. If Unicode (for example, C<\N{}> or code | |
4338 | points of 0x100 or beyond) is being used, the case map used by C<\l>, | |
4339 | C<\L>, C<\u>, and C<\U> is as defined by Unicode. That means that | |
4340 | case-mapping a single character can sometimes produce a sequence of | |
4341 | several characters. | |
4342 | Under S<C<use locale>>, C<\F> produces the same results as C<\L> | |
4343 | for all locales but a UTF-8 one, where it instead uses the Unicode | |
4344 | definition. | |
4345 | ||
4346 | =end original | |
4347 | ||
4348 | C<LC_CTYPE> を含む S<C<use locale>> 型式が有効の場合(L<perllocale> を | |
4349 | 参照して下さい)、C<\l>, C<\L>, C<\u>, C<\U> で使われる | |
4350 | 2376 | 大文字小文字テーブルは現在のロケールのものが使われます。 |
4351 | ||
2377 | L<perllocale> を参照して下さい。 | |
2378 | (例えば、C<\N{}> や、0x100 以上のワイド 16 進文字を使った) Unicode が | |
4352 | 2379 | 使われている場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる大文字小文字 |
4353 | 2380 | テーブルは Unicode で定義されているものになります。 |
4354 | ||
2381 | C<\N{name}> のドキュメントに関しては、L<charnames> を参照して下さい。 | |
4355 | あるということです。 | |
4356 | S<C<use locale>> の基では、C<\F> は、Unicode の定義が使われる UTF-8 以外の | |
4357 | 全てのロケールにおいて、C<\L> と同じ結果を生成します。 | |
4358 | 2382 | |
4359 | 2383 | =begin original |
4360 | 2384 | |
4361 | 2385 | All systems use the virtual C<"\n"> to represent a line terminator, |
4362 | 2386 | called a "newline". There is no such thing as an unvarying, physical |
4363 | 2387 | newline character. It is only an illusion that the operating system, |
4364 | 2388 | device drivers, C libraries, and Perl all conspire to preserve. Not all |
4365 | 2389 | systems read C<"\r"> as ASCII CR and C<"\n"> as ASCII LF. For example, |
4366 | on | |
2390 | on a Mac, these are reversed, and on systems without line terminator, | |
4367 | ||
2391 | printing C<"\n"> may emit no actual data. In general, use C<"\n"> when | |
4368 | printing C<"\n"> might emit no actual data. In general, use C<"\n"> when | |
4369 | 2392 | you mean a "newline" for your system, but use the literal ASCII when you |
4370 | 2393 | need an exact character. For example, most networking protocols expect |
4371 | 2394 | and prefer a CR+LF (C<"\015\012"> or C<"\cM\cJ">) for line terminators, |
4372 | 2395 | and although they often accept just C<"\012">, they seldom tolerate just |
4373 | 2396 | C<"\015">. If you get in the habit of using C<"\n"> for networking, |
4374 | 2397 | you may be burned some day. |
4375 | 2398 | X<newline> X<line terminator> X<eol> X<end of line> |
4376 | 2399 | X<\n> X<\r> X<\r\n> |
4377 | 2400 | |
4378 | 2401 | =end original |
4379 | 2402 | |
4380 | 2403 | 全てのシステムでは "newline" と呼ばれる行端末子を表現するために |
4381 | 2404 | 仮想的な C<"\n"> が用いられます。 |
4382 | 2405 | 普遍の、物理的な "newline" 文字と言うものはありません。 |
4383 | 2406 | オペレーティングシステム、デバイスドライバ、C ライブラリ、Perl が全て |
4384 | 2407 | 協力して保存しようとすると言うのは単なる幻想です。 |
4385 | 2408 | 全てのシステムで C<"\r"> を ASCII CR として、また C<"\n"> を |
4386 | 2409 | ASCII LF として読み込むわけではありません。 |
4387 | 例えば | |
2410 | 例えば Mac ではこれらは保存され、行端末子のないシステムでは、 | |
4388 | ||
2411 | C<"\n"> を print しても実際のデータは何も出力しません。 | |
4389 | 2412 | 一般に、システムで "newline" を意味したいときには C<"\n"> を使いますが、 |
4390 | 2413 | 正確な文字が必要な場合はリテラルな ASCII を使います。 |
4391 | 2414 | 例えば、ほとんどのネットワークプロトコルでは行端末子として |
4392 | 2415 | CR+LF (C<"\015\012"> または C<"\cM\cJ">) を予想し、また好みますが、 |
4393 | 2416 | しばしば C<"\012"> だけでも許容し、さらに時々は C<"\015"> だけでも認めます。 |
4394 | 2417 | もしネットワーク関係で C<"\n"> を使う習慣がついていると、 |
4395 | 2418 | いつか痛い目を見ることになるでしょう。 |
4396 | X<newline> X<line terminator> X<eol> X<end of line> | |
4397 | X<\n> X<\r> X<\r\n> | |
4398 | 2419 | |
4399 | 2420 | =begin original |
4400 | 2421 | |
4401 | 2422 | For constructs that do interpolate, variables beginning with "C<$>" |
4402 | 2423 | or "C<@>" are interpolated. Subscripted variables such as C<$a[3]> or |
4403 | 2424 | C<< $href->{key}[0] >> are also interpolated, as are array and hash slices. |
4404 | 2425 | But method calls such as C<< $obj->meth >> are not. |
4405 | 2426 | |
4406 | 2427 | =end original |
4407 | 2428 | |
4408 | 2429 | 展開が行なわれる構文では、"C<$>" や "C<@>" で始まる変数が展開されます。 |
4409 | ||
2430 | Subscripted variables such as C<$a[3]> or | |
4410 | 配列やハッシュのスライスのように展開されます。 | |
2431 | C<< $href->{key}[0] >> もまた配列やハッシュのスライスのように展開されます。 | |
4411 | 2432 | しかし、C<< $obj->meth >> のようなメソッド呼び出しは展開されません。 |
4412 | 2433 | |
4413 | 2434 | =begin original |
4414 | 2435 | |
4415 | 2436 | Interpolating an array or slice interpolates the elements in order, |
4416 | 2437 | separated by the value of C<$">, so is equivalent to interpolating |
4417 | ||
2438 | C<join $", @array>. "Punctuation" arrays such as C<@*> are only | |
4418 | interpolated | |
2439 | interpolated if the name is enclosed in braces C<@{*}>, but special | |
4419 | arrays C<@_>, C<@+>, and C<@-> are interpolated even without braces. | |
2440 | arrays C<@_>, C<@+>, and C<@-> are interpolated, even without braces. | |
4420 | 2441 | |
4421 | 2442 | =end original |
4422 | 2443 | |
4423 | 2444 | 配列やスライスの展開は、要素を順番に、C<$"> の値で分割して展開されるので、 |
4424 | ||
2445 | C<join $", @array> の展開と等価です。 | |
4425 | C<@*> のような「句読点」配列は | |
2446 | C<@*> のような「句読点」配列は名前が C<@{*}> のように中かっこで囲われている | |
4426 | ||
2447 | 場合にのみ展開されますが、特殊配列 C<@_>, C<@+>, C<@-> は中かっこなしでも | |
4427 | ||
2448 | 展開されます。 | |
4428 | 2449 | |
4429 | 2450 | =begin original |
4430 | 2451 | |
4431 | ||
2452 | You cannot include a literal C<$> or C<@> within a C<\Q> sequence. | |
4432 | interpolat | |
2453 | An unescaped C<$> or C<@> interpolates the corresponding variable, | |
2454 | while escaping will cause the literal string C<\$> to be inserted. | |
2455 | You'll need to write something like C<m/\Quser\E\@\Qhost/>. | |
4433 | 2456 | |
4434 | 2457 | =end original |
4435 | 2458 | |
4436 | ||
2459 | C<\Q> シーケンスの中にリテラルな C<$> や C<@> を入れることはできません。 | |
4437 | エスケープ | |
2460 | エスケープされない C<$> や C<@> は対応する変数に変換されます。 | |
2461 | 一方、エスケープすると、リテラルな文字列 C<\$> が挿入されます。 | |
4439 | "abc\Qfoo\tbar$s\Exyz" | |
4440 | ||
4441 | =begin original | |
4442 | ||
4443 | is equivalent to | |
4444 | ||
4445 | =end original | |
4446 | ||
4447 | は以下と等価です: | |
4448 | ||
4449 | "abc" . quotemeta("foo\tbar$s") . "xyz" | |
4450 | ||
4451 | =begin original | |
4452 | ||
4453 | For the pattern of regex operators (C<qr//>, C<m//> and C<s///>), | |
4454 | the quoting from C<\Q> is applied after interpolation is processed, | |
4455 | but before escapes are processed. This allows the pattern to match | |
4456 | literally (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 | ||
4469 | Because C<$> or C<@> trigger interpolation, you'll need to use something | |
4470 | like C</\Quser\E\@\Qhost/> to match them literally. | |
4471 | ||
4472 | =end original | |
4473 | ||
4474 | C<$> や C<@> は変換を引き起こすので、リテラルにマッチングさせるためには | |
4475 | 2462 | C<m/\Quser\E\@\Qhost/> などという風に書く必要があります。 |
4476 | 2463 | |
4477 | 2464 | =begin original |
4478 | 2465 | |
4479 | 2466 | Patterns are subject to an additional level of interpretation as a |
4480 | 2467 | regular expression. This is done as a second pass, after variables are |
4481 | 2468 | interpolated, so that regular expressions may be incorporated into the |
4482 | 2469 | pattern from the variables. If this is not what you want, use C<\Q> to |
4483 | 2470 | interpolate a variable literally. |
4484 | 2471 | |
4485 | 2472 | =end original |
4486 | 2473 | |
4487 | 2474 | パターンはさらに、正規表現として展開が行なわれます。 |
4488 | これは、変数が展開された後の 2 回目のパスで行なわれるので、変数に | |
2475 | これは、変数が展開された後の 2 回目のパスで行なわれるので、変数に | |
4489 | 含めておき、パターンの中へ展開することができます。 | |
2476 | 正規表現を含めておき、パターンの中へ展開することができます。 | |
4490 | 2477 | もし、そうしたくないのであれば、C<\Q> を使うと変数の内容を文字通りに |
4491 | 2478 | 展開することができます。 |
4492 | 2479 | |
4493 | 2480 | =begin original |
4494 | 2481 | |
4495 | 2482 | Apart from the behavior described above, Perl does not expand |
4496 | 2483 | multiple levels of interpolation. In particular, contrary to the |
4497 | 2484 | expectations of shell programmers, back-quotes do I<NOT> interpolate |
4498 | 2485 | within double quotes, nor do single quotes impede evaluation of |
4499 | 2486 | variables when used within double quotes. |
4500 | 2487 | |
4501 | 2488 | =end original |
4502 | 2489 | |
4503 | 2490 | 上記の振る舞いを除けば、Perl は 複数の段階を踏んで展開を行ないません。 |
4504 | 特に、シェルのプログラマの期待とは裏腹に、 | |
2491 | 特に、シェルのプログラマの期待とは裏腹に、 | |
4505 | 中では展開されませんし、シングルクォートが | |
2492 | バッククォートはダブルクォートの中では展開されませんし、シングルクォートが | |
4506 | 変数の展開を妨げることは I<ありません>。 | |
2493 | ダブルクォートの中で使われても、変数の展開を妨げることは I<ありません>。 | |
4507 | 2494 | |
4508 | 2495 | =head2 Regexp Quote-Like Operators |
4509 | 2496 | X<operator, regexp> |
4510 | 2497 | |
4511 | 2498 | (正規表現のクォート風の演算子) |
4512 | 2499 | |
4513 | 2500 | =begin original |
4514 | 2501 | |
4515 | 2502 | Here are the quote-like operators that apply to pattern |
4516 | 2503 | matching and related activities. |
4517 | 2504 | |
4518 | 2505 | =end original |
4519 | 2506 | |
4520 | 2507 | 以下はパターンマッチングと関連する行動に関するクォート風の演算子です。 |
4521 | 2508 | |
4522 | 2509 | =over 8 |
4523 | 2510 | |
4524 | =item | |
2511 | =item qr/STRING/msixpo | |
4525 | 2512 | X<qr> X</i> X</m> X</o> X</s> X</x> X</p> |
4526 | 2513 | |
4527 | 2514 | =begin original |
4528 | 2515 | |
4529 | 2516 | This operator quotes (and possibly compiles) its I<STRING> as a regular |
4530 | 2517 | expression. I<STRING> is interpolated the same way as I<PATTERN> |
4531 | in C<m/ | |
2518 | in C<m/PATTERN/>. If "'" is used as the delimiter, no interpolation | |
4532 | i | |
2519 | is done. Returns a Perl value which may be used instead of the | |
4533 | corresponding C</ | |
2520 | corresponding C</STRING/msixpo> expression. The returned value is a | |
4534 | normalized version of the original pattern. | |
2521 | normalized version of the original pattern. It magically differs from | |
4535 | a string containing the same characters: C<ref(qr/x/)> returns "Regexp" | |
2522 | a string containing the same characters: C<ref(qr/x/)> returns "Regexp", | |
4536 | ||
2523 | even though dereferencing the result returns undef. | |
4537 | normalized version of the original pattern, but this may change). | |
4538 | 2524 | |
4539 | 2525 | =end original |
4540 | 2526 | |
4541 | 2527 | この演算子は I<STRING> を正規表現としてクォートします |
4542 | 2528 | (そして可能ならコンパイルします)。 |
4543 | I<STRING> は C<m/ | |
2529 | I<STRING> は C<m/PATTERN/> 内の I<PATTERN> と同様に文字変換されます。 | |
4544 | ||
2530 | "'" がデリミタとして使用された場合、文字変換は行われません。 | |
4545 | 対応する C</ | |
2531 | 対応する C</STRING/msixpo> 表現の代わりに使われた Perl の値を返します。 | |
4546 | 2532 | 返り値は元のパターンを正規化したものです。 |
4547 | 2533 | これは不思議なことに、同じ文字を含む文字列とは異なります: |
4548 | C<ref(qr/x/)> は | |
2534 | C<ref(qr/x/)> は、結果をデリファレンスすると未定義値を返すにも関わらず、 | |
4549 | ||
2535 | "Regexp" を返します。 | |
4550 | 変更されるかもしれません)。 | |
4551 | 2536 | |
4552 | 2537 | =begin original |
4553 | 2538 | |
4554 | 2539 | For example, |
4555 | 2540 | |
4556 | 2541 | =end original |
4557 | 2542 | |
4558 | 2543 | 例えば: |
4559 | 2544 | |
4560 | 2545 | $rex = qr/my.STRING/is; |
4561 | 2546 | print $rex; # prints (?si-xm:my.STRING) |
4562 | 2547 | s/$rex/foo/; |
4563 | 2548 | |
4564 | 2549 | =begin original |
4565 | 2550 | |
4566 | 2551 | is equivalent to |
4567 | 2552 | |
4568 | 2553 | =end original |
4569 | 2554 | |
4570 | 2555 | は以下と等価です: |
4571 | 2556 | |
4572 | 2557 | s/my.STRING/foo/is; |
4573 | 2558 | |
4574 | 2559 | =begin original |
4575 | 2560 | |
4576 | 2561 | The result may be used as a subpattern in a match: |
4577 | 2562 | |
4578 | 2563 | =end original |
4579 | 2564 | |
4580 | 2565 | 結果はマッチのサブパターンとして使えます: |
4581 | 2566 | |
4582 | =begin original | |
4583 | ||
4584 | 2567 | $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 | |
4587 | 2569 | $string =~ $re; # or used standalone |
4588 | 2570 | $string =~ /$re/; # or this way |
4589 | 2571 | |
4590 | =end original | |
4591 | ||
4592 | $re = qr/$pattern/; | |
4593 | $string =~ /foo${re}bar/; # 他のパターンに展開できる | |
4594 | $string =~ $re; # または単独で使う | |
4595 | $string =~ /$re/; # またはこのようにする | |
4596 | ||
4597 | 2572 | =begin original |
4598 | 2573 | |
4599 | Since Perl may compile the pattern at the moment of execution of | |
2574 | Since Perl may compile the pattern at the moment of execution of qr() | |
4600 | operator, using | |
2575 | operator, using qr() may have speed advantages in some situations, | |
4601 | notably if the result of | |
2576 | notably if the result of qr() is used standalone: | |
4602 | 2577 | |
4603 | 2578 | =end original |
4604 | 2579 | |
4605 | Perl は | |
2580 | Perl は qr() 演算子を実行する瞬間にパターンをコンパイルするので、 | |
4606 | ||
2581 | qr() を使うことでいくつかの場面で速度的に有利になります。 | |
4607 | 特に | |
2582 | 特に qr() の結果が独立して使われる場合に有利になります。 | |
4608 | 2583 | |
4609 | 2584 | sub match { |
4610 | 2585 | my $patterns = shift; |
4611 | 2586 | my @compiled = map qr/$_/i, @$patterns; |
4612 | 2587 | grep { |
4613 | 2588 | my $success = 0; |
4614 | 2589 | foreach my $pat (@compiled) { |
4615 | 2590 | $success = 1, last if /$pat/; |
4616 | 2591 | } |
4617 | 2592 | $success; |
4618 | 2593 | } @_; |
4619 | 2594 | } |
4620 | 2595 | |
4621 | 2596 | =begin original |
4622 | 2597 | |
4623 | 2598 | Precompilation of the pattern into an internal representation at |
4624 | the moment of | |
2599 | the moment of qr() avoids a need to recompile the pattern every | |
4625 | 2600 | time a match C</$pat/> is attempted. (Perl has many other internal |
4626 | 2601 | optimizations, but none would be triggered in the above example if |
4627 | we did not use | |
2602 | we did not use qr() operator.) | |
4628 | 2603 | |
4629 | 2604 | =end original |
4630 | 2605 | |
4631 | ||
2606 | qr() の時点でパターンを内部表現にプリコンパイルすることにより、C</$pat/> を | |
4632 | ||
2607 | 試みる毎に毎回パターンを再コンパイルするのを避けることができます | |
4633 | ||
2608 | (Perl はその他にも多くの内部最適化を行いますが、上の例で qr() 演算子を | |
4634 | ||
2609 | 使わなかった場合はどの最適化も行われません)。 | |
4635 | 使わなかった場合はどの最適化も行われません。) | |
4636 | 2610 | |
4637 | 2611 | =begin original |
4638 | 2612 | |
4639 | Options | |
2613 | Options are: | |
4640 | 2614 | |
4641 | 2615 | =end original |
4642 | 2616 | |
4643 | ||
2617 | オプションは以下の通りです: | |
4644 | 2618 | |
4645 | 2619 | =begin original |
4646 | 2620 | |
4647 | 2621 | m Treat string as multiple lines. |
4648 | 2622 | s Treat string as single line. (Make . match a newline) |
4649 | 2623 | i Do case-insensitive pattern matching. |
4650 | x | |
2624 | x Use extended regular expressions. | |
4651 | x's means \t and the SPACE character are ignored within | |
4652 | square-bracketed character classes | |
4653 | 2625 | 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) | |
4657 | 2627 | 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... | |
4666 | 2628 | |
4667 | 2629 | =end original |
4668 | 2630 | |
4669 | m 文字列を複数行として扱う | |
2631 | m 文字列を複数行として扱う | |
4670 | s 文字列を一行として扱う | |
2632 | s 文字列を一行として扱う (. が 改行にマッチングするようにする) | |
4671 | i パターンマッチにおいて大文字小文字を区別しない | |
2633 | i パターンマッチにおいて大文字小文字を区別しない | |
4672 | x | |
2634 | x 拡張正規表現を使う | |
4673 | \t と SPACE 文字は大かっこ文字クラスの中では無視されます | |
4674 | 2635 | p マッチング時にマッチングした文字列を保存するので、 |
4675 | 2636 | ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} が定義される |
4676 | | |
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 などを埋めない | |
4685 | 2638 | |
4686 | 2639 | =begin original |
4687 | 2640 | |
4688 | 2641 | If a precompiled pattern is embedded in a larger pattern then the effect |
4689 | of | |
2642 | of 'msixp' will be propagated appropriately. The effect of the 'o' | |
4690 | ||
2643 | modifier has is not propagated, being restricted to those patterns | |
4691 | 2644 | explicitly using it. |
4692 | 2645 | |
4693 | 2646 | =end original |
4694 | 2647 | |
4695 | 2648 | プリコンパイルされたパターンがより大きいパターンに組み込まれている場合、 |
4696 | ||
2649 | 'msixp' の効果は適切に伝播します。 | |
4697 | ||
2650 | 'o' 修飾子の効果は伝播せず、明示的に使われたパターンに制限されます。 | |
4698 | 2651 | |
4699 | 2652 | =begin original |
4700 | 2653 | |
4701 | ||
2654 | See L<perlre> for additional information on valid syntax for STRING, and | |
4702 | ||
2655 | for a detailed look at the semantics of regular expressions. | |
4703 | to want to specify explicitly; the other three are selected | |
4704 | automatically by various pragmas. | |
4705 | 2656 | |
4706 | 2657 | =end original |
4707 | 2658 | |
4708 | ||
2659 | STRING として有効な文法に関する追加の情報と、正規表現の意味論に関する | |
4709 | 文字集合の規則を制御しますが、明示的に指定したいと思いそうなものは | |
4710 | C</a> だけでしょう; | |
4711 | その他の三つはさまざまなプラグマによって自動的に選択されます。 | |
4712 | ||
4713 | =begin original | |
4714 | ||
4715 | See L<perlre> for additional information on valid syntax for I<STRING>, and | |
4716 | for a detailed look at the semantics of regular expressions. In | |
4717 | particular, all modifiers except the largely obsolete C</o> are further | |
4718 | explained in L<perlre/Modifiers>. C</o> is described in the next section. | |
4719 | ||
4720 | =end original | |
4721 | ||
4722 | I<STRING> として有効な文法に関する追加の情報と、正規表現の意味論に関する | |
4723 | 2660 | 詳細については、L<perlre> を参照してください。 |
4724 | 特に、かなり古いものである C</o> 以外の全ての修飾子については | |
4725 | L<perlre/Modifiers> でさらに説明されています。 | |
4726 | C</o> は次の節に記述されています。 | |
4727 | 2661 | |
4728 | =item | |
2662 | =item m/PATTERN/msixpogc | |
4729 | 2663 | X<m> X<operator, match> |
4730 | 2664 | X<regexp, options> X<regexp> X<regex, options> X<regex> |
4731 | 2665 | X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> |
4732 | 2666 | |
4733 | =item | |
2667 | =item /PATTERN/msixpogc | |
4734 | 2668 | |
4735 | 2669 | =begin original |
4736 | 2670 | |
4737 | 2671 | Searches a string for a pattern match, and in scalar context returns |
4738 | 2672 | true if it succeeds, false if it fails. If no string is specified |
4739 | via the C<=~> or C<!~> operator, the | |
2673 | via the C<=~> or C<!~> operator, the $_ string is searched. (The | |
4740 | 2674 | string specified with C<=~> need not be an lvalue--it may be the |
4741 | 2675 | result of an expression evaluation, but remember the C<=~> binds |
4742 | rather tightly.) See also L<perlre>. | |
2676 | rather tightly.) See also L<perlre>. See L<perllocale> for | |
2677 | discussion of additional considerations that apply when C<use locale> | |
2678 | is in effect. | |
4743 | 2679 | |
4744 | 2680 | =end original |
4745 | 2681 | |
4746 | 2682 | パターンマッチで文字列検索を行ない、スカラコンテキストでは成功したときは真、 |
4747 | 2683 | 失敗したときは偽を返します。 |
4748 | 2684 | C<=~> 演算子か C<!~> 演算子で検索対象の文字列を示さなかったときには、 |
4749 | 2685 | C<$_> の文字列が検索対象となります。 |
4750 | (C<=~> で指定される文字列は、左辺値である必要はありません | |
2686 | (C<=~> で指定される文字列は、左辺値である必要はありません。 | |
4751 | 2687 | 式を評価した結果でもかまいませんが、C<=~> の優先順位がいくぶん高いことに |
4752 | 2688 | 注意してください。) |
4753 | 2689 | L<perlre> も参照してください。 |
2690 | C<use locale> が有効の場合の議論については L<perllocale> を参照して下さい。 | |
4754 | 2691 | |
4755 | 2692 | =begin original |
4756 | 2693 | |
4757 | Options are as described in C<qr//> | |
2694 | Options are as described in C<qr//>; in addition, the following match | |
4758 | 2695 | process modifiers are available: |
4759 | 2696 | |
4760 | 2697 | =end original |
4761 | 2698 | |
4762 | オプションは | |
2699 | オプションは C<qr//> に記述されています; さらに、以下のマッチング処理 | |
4763 | ||
2700 | 修飾子が利用可能です: | |
4764 | 2701 | |
4765 | 2702 | =begin original |
4766 | 2703 | |
4767 | | |
2704 | g Match globally, i.e., find all occurrences. | |
4768 | | |
2705 | c Do not reset search position on a failed match when /g is in effect. | |
4769 | in effect. | |
4770 | 2706 | |
4771 | 2707 | =end original |
4772 | 2708 | |
4773 | g グローバルにマッチ | |
2709 | g グローバルにマッチ、つまり、すべてを探し出す | |
4774 | | |
2710 | c /g が有効なとき、マッチングに失敗しても検索位置をリセットしない | |
4775 | 2711 | |
4776 | 2712 | =begin original |
4777 | 2713 | |
4778 | If | |
2714 | If "/" is the delimiter then the initial C<m> is optional. With the C<m> | |
4779 | you can use any pair of non-whitespace | |
2715 | you can use any pair of non-alphanumeric, non-whitespace characters | |
4780 | 2716 | as delimiters. This is particularly useful for matching path names |
4781 | that contain | |
2717 | that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is | |
4782 | the delimiter, then | |
2718 | the delimiter, then the match-only-once rule of C<?PATTERN?> applies. | |
4783 | ||
2719 | If "'" is the delimiter, no interpolation is performed on the PATTERN. | |
4784 | no variable interpolation is performed on the I<PATTERN>. | |
4785 | When using a delimiter character valid in an identifier, whitespace is required | |
4786 | after the C<m>. | |
4787 | 2720 | |
4788 | 2721 | =end original |
4789 | 2722 | |
4790 | 区切文字が | |
2723 | 区切文字が "/" のときには、最初の C<m> は付けても付けなくてもかまいません。 | |
4791 | C<m> を付けるときには、 | |
2724 | C<m> を付けるときには、英数字でも空白でもない、任意の文字のペアを | |
4792 | 2725 | 区切文字として使うことができます。 |
4793 | これは特に、 | |
2726 | これは特に、"/" を含むパス名にパターンマッチングを行なうときに、 | |
4794 | 2727 | LTS (傾斜楊枝症候群) を避けるために便利でしょう。 |
4795 | " | |
2728 | "'" がデリミタの場合、PATTERN に対する展開は行われません。 | |
4796 | ルールが適用されます。 | |
4797 | C<"'"> (シングルクォート) がデリミタの場合、I<PATTERN> に対する変数展開は | |
4798 | 行われません。 | |
4799 | 識別子として有効な区切り文字を使う場合、C<m> の後に空白が必要です。 | |
4800 | 2729 | |
4801 | 2730 | =begin original |
4802 | 2731 | |
4803 | ||
2732 | PATTERN may contain variables, which will be interpolated (and the | |
4804 | every time the pattern search is evaluated, except | |
2733 | pattern recompiled) every time the pattern search is evaluated, except | |
4805 | 2734 | for when the delimiter is a single quote. (Note that C<$(>, C<$)>, and |
4806 | 2735 | C<$|> are not interpolated because they look like end-of-string tests.) |
4807 | ||
2736 | If you want such a pattern to be compiled only once, add a C</o> after | |
4808 | ||
2737 | the trailing delimiter. This avoids expensive run-time recompilations, | |
4809 | ||
2738 | and is useful when the value you are interpolating won't change over | |
4810 | ||
2739 | the life of the script. However, mentioning C</o> constitutes a promise | |
4811 | ||
2740 | that you won't change the variables in the pattern. If you change them, | |
4812 | ||
2741 | Perl won't even notice. See also L<"qr/STRING/msixpo">. | |
4813 | interests of speed. But now, the only reasons to use C</o> are one of: | |
4814 | 2742 | |
4815 | 2743 | =end original |
4816 | 2744 | |
4817 | ||
2745 | PATTERN には、変数が含まれていてもよく、パターンが評価されるごとに、 | |
4818 | 2746 | (デリミタがシングルクォートでない限り)変数は展開され |
4819 | 2747 | (パターンが再コンパイルされ) ます。 |
4820 | 2748 | (変数 C<$(>, C<$)>, C<$|> は文字列の終わりを調べるパターンであると |
4821 | 2749 | 解釈されるので、展開されません。) |
4822 | ||
2750 | パターンがコンパイルされるのを 1 度だけにしたい場合には、 | |
4823 | ||
2751 | 終わりの区切文字の後に C</o> 修飾子を付けます。 | |
4824 | ||
2752 | これにより、実行時に再コンパイルが頻繁に起こることが避けられ、展開する | |
4825 | ||
2753 | 値がスクリプトの実行中に変化しない場合に有効なものとなります。 | |
4826 | ||
2754 | しかし、C</o> を付けることは、パターンの中の変数を変更しないことを | |
4827 | ||
2755 | 約束するものです。 | |
2756 | 変更したとしても、Perl がそれに気付くことはありません。 | |
2757 | L<"qr/STRING/msixpo"> も参照して下さい。 | |
4828 | 2758 | |
4829 | = | |
2759 | =item The empty pattern // | |
4830 | 2760 | |
4831 | ||
2761 | (空パターン //) | |
4832 | 2762 | |
4833 | 2763 | =begin original |
4834 | 2764 | |
4835 | ||
2765 | If the PATTERN evaluates to the empty string, the last | |
4836 | don't change, and you need to wring out the last little bit of speed by | |
4837 | having Perl skip testing for that. (There is a maintenance penalty for | |
4838 | doing this, as mentioning C</o> constitutes a promise that you won't | |
4839 | change the variables in the pattern. If you do change them, Perl won't | |
4840 | even notice.) | |
4841 | ||
4842 | =end original | |
4843 | ||
4844 | 変数が数千文字の長さで、これが変更されないことが分かっており、これに対する | |
4845 | テストを飛ばすことであともう少しだけ速度を稼ぐ必要がある。 | |
4846 | (こうすることには保守上のペナルティがあります; なぜなら C</o> と | |
4847 | 言及することでパターン内の変数を変更しないことを約束したことになるからです。 | |
4848 | 変更しても、Perl は気づきもしません。) | |
4849 | ||
4850 | =item 2 | |
4851 | ||
4852 | =begin original | |
4853 | ||
4854 | you want the pattern to use the initial values of the variables | |
4855 | regardless of whether they change or not. (But there are saner ways | |
4856 | of accomplishing this than using C</o>.) | |
4857 | ||
4858 | =end original | |
4859 | ||
4860 | 変数が変更されようが変更されまいが、変数の初期値を使ったパターンがほしい。 | |
4861 | (しかしこれを達成するための、C</o> を使うよりもまともな方法があります。) | |
4862 | ||
4863 | =item 3 | |
4864 | ||
4865 | =begin original | |
4866 | ||
4867 | If 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 | ||
4879 | then perl will recompile each time, even though the pattern string hasn't | |
4880 | changed, to ensure that the current value of C<$x> is seen each time. | |
4881 | Use C</o> if you want to avoid this. | |
4882 | ||
4883 | =end original | |
4884 | ||
4885 | C<$x> の現在の値を毎回確認するために、例えパターン文字列が | |
4886 | 変更されていなくても、毎回再コンパイルされます。 | |
4887 | これを避けたい場合は C</o> を使ってください。 | |
4888 | ||
4889 | =back | |
4890 | ||
4891 | =begin original | |
4892 | ||
4893 | The 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 | ||
4905 | If the I<PATTERN> evaluates to the empty string, the last | |
4906 | 2766 | I<successfully> matched regular expression is used instead. In this |
4907 | case, only the C<g> and C<c> flags on the empty pattern | |
2767 | case, only the C<g> and C<c> flags on the empty pattern is honoured - | |
4908 | other flags are taken from the original pattern. If no match has | |
2768 | the other flags are taken from the original pattern. If no match has | |
4909 | 2769 | previously succeeded, this will (silently) act instead as a genuine |
4910 | empty pattern (which will always match). | |
2770 | empty pattern (which will always match). | |
4911 | a 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 | |
4913 | cases you are recommended to replace C<m/$pattern/> with | |
4914 | C<m/(?:$pattern)/> to avoid this behavior. | |
4915 | 2771 | |
4916 | 2772 | =end original |
4917 | 2773 | |
4918 | ||
2774 | PATTERN を評価した結果が空文字列となった場合には、最後にマッチに | |
4919 | 2775 | I<成功した> 正規表現が、代わりに使われます。 |
4920 | この場合、空パターンに対して C<g> の C<c> フラグだけが有効です | |
2776 | この場合、空パターンに対して C<g> の C<c> フラグだけが有効です - | |
4921 | 2777 | その他のフラグは元のパターンから取られます。 |
4922 | 2778 | 以前に成功したマッチングがない場合、これは(暗黙に)真の空パターンとして |
4923 | 2779 | 動作します(つまり常にマッチングします)。 |
4924 | ユーザーが提供した文字列をパターンとして使うことは、 | |
4925 | 文字列が空だった場合に「最後に成功したマッチング」の振る舞いを引き起こし、 | |
4926 | とても混乱するというリスクがあります。 | |
4927 | このような場合では、この振る舞いを避けるために、 | |
4928 | C<m/$pattern/> を C<m/(?:$pattern)/> に置き換えることを勧めます。 | |
4929 | 2780 | |
4930 | 2781 | =begin original |
4931 | 2782 | |
4932 | The last successful pattern may be accessed as a variable via | |
4933 | C<${^LAST_SUCCESSFUL_PATTERN}>. Matching against it, or the empty | |
4934 | pattern should have the same effect, with the exception that when there | |
4935 | is no last successful pattern the empty pattern will silently match, | |
4936 | whereas using the C<${^LAST_SUCCESSFUL_PATTERN}> variable will produce | |
4937 | undefined warnings (if warnings are enabled). You can check | |
4938 | C<defined(${^LAST_SUCCESSFUL_PATTERN})> to test if there is a "last | |
4939 | successful match" in the current scope. | |
4940 | ||
4941 | =end original | |
4942 | ||
4943 | 最後に成功したパターンは、 C<${^LAST_SUCCESSFUL_PATTERN}> 変数経由で | |
4944 | アクセスされます。 | |
4945 | これと、空文字列に対するマッチングは同じ効果を持ちますが、 | |
4946 | 空文字列が暗黙にマッチングする最後に成功したパターンはないけれども、 | |
4947 | C<${^LAST_SUCCESSFUL_PATTERN}> 変数を使うと(警告がが有効なら) | |
4948 | 未定義警告発生するという例外があります。 | |
4949 | 現在のスコープで「最後に成功したマッチング」が | |
4950 | あるかどうかをテストにするには、 | |
4951 | C<defined(${^LAST_SUCCESSFUL_PATTERN})> とします。 | |
4952 | ||
4953 | =begin original | |
4954 | ||
4955 | 2783 | Note that it's possible to confuse Perl into thinking C<//> (the empty |
4956 | 2784 | regex) is really C<//> (the defined-or operator). Perl is usually pretty |
4957 | 2785 | good about this, but some pathological cases might trigger this, such as |
4958 | C<$ | |
2786 | C<$a///> (is that C<($a) / (//)> or C<$a // />?) and C<print $fh //> | |
4959 | ( | |
2787 | (C<print $fh(//> or C<print($fh //>?). In all of these examples, Perl | |
4960 | 2788 | will assume you meant defined-or. If you meant the empty regex, just |
4961 | 2789 | use parentheses or spaces to disambiguate, or even prefix the empty |
4962 | 2790 | regex with an C<m> (so C<//> becomes C<m//>). |
4963 | 2791 | |
4964 | 2792 | =end original |
4965 | 2793 | |
4966 | 2794 | Perl が C<//> (空正規表現) と C<//> (定義性和演算子) を混同する |
4967 | 2795 | 可能性があることに注意してください。 |
4968 | Perl は普通これをかなりうまく処理しますが、C<$ | |
2796 | Perl は普通これをかなりうまく処理しますが、C<$a///> (C<($a) / (//)> | |
4969 | それとも | |
2797 | それとも C<$a // />?) や C<print $fh //> (C<print $fh(//> それとも | |
4970 | ||
2798 | C<print($fh //>?) のような病的な状況ではこれが起こりえます。 | |
4971 | 2799 | これらの例の全てでは、Perl は定義性和を意味していると仮定します。 |
4972 | 2800 | もし空正規表現を意味したいなら、あいまいさをなくすために単に |
4973 | 2801 | かっこや空白を使うか、空正規表現に接頭辞 C<m> を付けてください |
4974 | 2802 | (つまり C<//> を C<m//> にします)。 |
4975 | 2803 | |
4976 | 2804 | =item Matching in list context |
4977 | 2805 | |
4978 | 2806 | (リストコンテキストでのマッチング) |
4979 | 2807 | |
4980 | 2808 | =begin original |
4981 | 2809 | |
4982 | 2810 | If the C</g> option is not used, C<m//> in list context returns a |
4983 | 2811 | list consisting of the subexpressions matched by the parentheses in the |
4984 | pattern, | |
2812 | pattern, i.e., (C<$1>, C<$2>, C<$3>...). (Note that here C<$1> etc. are | |
4985 | also set | |
2813 | also set, and that this differs from Perl 4's behavior.) When there are | |
4986 | value is the list C<(1)> for | |
2814 | no parentheses in the pattern, the return value is the list C<(1)> for | |
4987 | With or without parentheses, an empty list is returned upon | |
2815 | success. With or without parentheses, an empty list is returned upon | |
2816 | failure. | |
4988 | 2817 | |
4989 | 2818 | =end original |
4990 | 2819 | |
4991 | C</g> | |
2820 | C</g>オプションが使われなかった場合、リストコンテキストでのC<m//>は | |
4992 | 2821 | パターンの中の括弧で括られた部分列にマッチしたもので構成されるリストを |
4993 | 返します | |
2822 | 返します。 | |
4994 | ||
2823 | これは、(C<$1>, C<$2>, C<$3>, ...) ということです。 | |
2824 | (この場合、C<$1> なども設定されます。 | |
2825 | この点で Perl 4 の動作と違っています。) | |
4995 | 2826 | パターンに括弧がない場合は、返り値は成功時はリスト C<(1)> です。 |
4996 | 2827 | 括弧のあるなしに関わらず、失敗時は空リストを返します。 |
4997 | 2828 | |
4998 | 2829 | =begin original |
4999 | 2830 | |
5000 | 2831 | Examples: |
5001 | 2832 | |
5002 | 2833 | =end original |
5003 | 2834 | |
5004 | 2835 | 例: |
5005 | 2836 | |
5006 | open(TTY, | |
2837 | open(TTY, '/dev/tty'); | |
5007 | | |
2838 | <TTY> =~ /^y/i && foo(); # do foo if desired | |
5008 | 2839 | |
5009 | | |
2840 | if (/Version: *([0-9.]*)/) { $version = $1; } | |
5010 | 2841 | |
5011 | | |
2842 | next if m#^/usr/spool/uucp#; | |
5012 | 2843 | |
5013 | | |
2844 | <#ins> poor man's grep | |
2845 | $arg = shift; | |
2846 | while (<>) { | |
2847 | print if /$arg/o; # compile only once | |
2848 | } | |
5014 | 2849 | |
5015 | | |
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 | } | |
5020 | 2851 | |
5021 | if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/)) | |
5022 | ||
5023 | 2852 | =begin original |
5024 | 2853 | |
5025 | This last example splits | |
2854 | This last example splits $foo into the first two words and the | |
5026 | remainder of the line, and assigns those three fields to | |
2855 | remainder of the line, and assigns those three fields to $F1, $F2, and | |
5027 | ||
2856 | $Etc. The conditional is true if any variables were assigned, i.e., if | |
5028 | ||
2857 | the pattern matched. | |
5029 | 2858 | |
5030 | 2859 | =end original |
5031 | 2860 | |
5032 | 最後の例は、 | |
2861 | 最後の例は、$foo を最初の 2 つの単語と行の残りに分解し、 | |
5033 | ||
2862 | $F1 と $F2 と $Etc に代入しています。 | |
5034 | 2863 | 変数に代入されれば、すなわちパターンがマッチすれば、 |
5035 | 2864 | if の条件が真となります。 |
5036 | 2865 | |
5037 | 2866 | =begin original |
5038 | 2867 | |
5039 | 2868 | The C</g> modifier specifies global pattern matching--that is, |
5040 | 2869 | matching as many times as possible within the string. How it behaves |
5041 | 2870 | depends on the context. In list context, it returns a list of the |
5042 | 2871 | substrings matched by any capturing parentheses in the regular |
5043 | 2872 | expression. If there are no parentheses, it returns a list of all |
5044 | 2873 | the matched strings, as if there were parentheses around the whole |
5045 | 2874 | pattern. |
5046 | 2875 | |
5047 | 2876 | =end original |
5048 | 2877 | |
5049 | 2878 | C</g> 修飾子は、グローバルなパターンマッチを指定するもので、 |
5050 | 2879 | 文字列の中で可能な限りたくさんマッチを行ないます。 |
5051 | 2880 | この動作は、コンテキストに依存します。 |
5052 | 2881 | リストコンテキストでは、正規表現内の括弧付けされたものにマッチした |
5053 | 2882 | 部分文字列のリストが返されます。 |
5054 | 2883 | 括弧がなければ、パターン全体を括弧で括っていたかのように、 |
5055 | 2884 | すべてのマッチした文字列のリストが返されます。 |
5056 | 2885 | |
5057 | 2886 | =begin original |
5058 | 2887 | |
5059 | 2888 | In scalar context, each execution of C<m//g> finds the next match, |
5060 | 2889 | returning true if it matches, and false if there is no further match. |
5061 | The position after the last match can be read or set using the | |
2890 | The position after the last match can be read or set using the pos() | |
5062 | function; see L<perlfunc/pos>. A failed match normally resets the | |
2891 | function; see L<perlfunc/pos>. A failed match normally resets the | |
5063 | 2892 | search position to the beginning of the string, but you can avoid that |
5064 | by adding the C</c> modifier ( | |
2893 | by adding the C</c> modifier (e.g. C<m//gc>). Modifying the target | |
5065 | 2894 | string also resets the search position. |
5066 | 2895 | |
5067 | 2896 | =end original |
5068 | 2897 | |
5069 | スカラコンテキストでは、C<m//g> を実行する毎に次のマッチを探します | |
2898 | スカラコンテキストでは、C<m//g> を実行する毎に次のマッチを探します。 | |
5070 | 2899 | マッチした場合は真を返し、もうマッチしなくなったら偽を返します。 |
5071 | 最後のマッチの位置は | |
2900 | 最後のマッチの位置は pos() 関数で読み出しや設定ができます。 | |
5072 | 2901 | L<perlfunc/pos> を参照して下さい。 |
5073 | 2902 | マッチに失敗すると通常は検索位置を文字列の先頭にリセットしますが、 |
5074 | C</c> 修飾子をつける( | |
2903 | C</c> 修飾子をつける(つまり C<m//gc>)ことでこれを防ぐことができます。 | |
5075 | 2904 | ターゲットとなる文字列が変更された場合も検索位置はリセットされます。 |
5076 | 2905 | |
5077 | =item | |
2906 | =item \G assertion | |
5078 | 2907 | |
5079 | ( | |
2908 | (\G アサート) | |
5080 | 2909 | |
5081 | 2910 | =begin original |
5082 | 2911 | |
5083 | 2912 | You can intermix C<m//g> matches with C<m/\G.../g>, where C<\G> is a |
5084 | zero-width assertion that matches the exact position where the | |
2913 | zero-width assertion that matches the exact position where the previous | |
5085 | ||
2914 | C<m//g>, if any, left off. Without the C</g> modifier, the C<\G> assertion | |
5086 | ||
2915 | still anchors at pos(), but the match is of course only attempted once. | |
5087 | th | |
2916 | Using C<\G> without C</g> on a target string that has not previously had a | |
5088 | at | |
2917 | C</g> match applied to it is the same as using the C<\A> assertion to match | |
5089 | ||
2918 | the beginning of the string. Note also that, currently, C<\G> is only | |
5090 | ||
2919 | properly supported when anchored at the very beginning of the pattern. | |
5091 | that, currently, C<\G> is only properly supported when anchored at the | |
5092 | very beginning of the pattern. | |
5093 | 2920 | |
5094 | 2921 | =end original |
5095 | 2922 | |
5096 | C<m//g> マッチを C<m/\G.../g> と混ぜることもできます | |
2923 | C<m//g> マッチを C<m/\G.../g> と混ぜることもできます。 | |
5097 | C<m//g> があればその同じ位置でマッチする | |
2924 | C<\G> は前回の C<m//g> があればその同じ位置でマッチする | |
5098 | ||
2925 | ゼロ文字幅のアサートです。 | |
5099 | ||
2926 | C</g> 修飾子なしの場合、C<\G> アサートは pos() に固定しますが、 | |
2927 | マッチはもちろん一度だけ試されます。 | |
5100 | 2928 | 以前に C</g> マッチを適用していないターゲット文字列に対して C</g> なしで |
5101 | 2929 | C<\G> を使うと、文字列の先頭にマッチする C<\A> アサートを使うのと |
5102 | 2930 | 同じことになります。 |
5103 | 2931 | C<\G> は現在のところ、パターンのまさに先頭を示す場合にのみ正しく |
5104 | 2932 | 対応することにも注意してください。 |
5105 | 2933 | |
5106 | 2934 | =begin original |
5107 | 2935 | |
5108 | 2936 | Examples: |
5109 | 2937 | |
5110 | 2938 | =end original |
5111 | 2939 | |
5112 | 2940 | 例: |
5113 | 2941 | |
5114 | 2942 | # list context |
5115 | 2943 | ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g); |
5116 | 2944 | |
5117 | 2945 | # scalar context |
5118 | | |
2946 | $/ = ""; | |
5119 | while ($paragraph = <>) { | |
2947 | while (defined($paragraph = <>)) { | |
5120 | while ($paragraph =~ / | |
2948 | while ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) { | |
5121 | 2949 | $sentences++; |
5122 | 2950 | } |
5123 | 2951 | } |
5124 | | |
2952 | print "$sentences\n"; | |
5125 | 2953 | |
5126 | ||
2954 | # using m//gc with \G | |
5127 | ||
5128 | Here'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 | ||
5186 | Here's how to use C<m//gc> with C<\G>: | |
5187 | ||
5188 | =end original | |
5189 | ||
5190 | 以下は C<m//gc> を C<\G> で使う方法です: | |
5191 | ||
5192 | 2955 | $_ = "ppooqppqq"; |
5193 | 2956 | while ($i++ < 2) { |
5194 | 2957 | print "1: '"; |
5195 | 2958 | print $1 while /(o)/gc; print "', pos=", pos, "\n"; |
5196 | 2959 | print "2: '"; |
5197 | 2960 | print $1 if /\G(q)/gc; print "', pos=", pos, "\n"; |
5198 | 2961 | print "3: '"; |
5199 | 2962 | print $1 while /(p)/gc; print "', pos=", pos, "\n"; |
5200 | 2963 | } |
5201 | 2964 | print "Final: '$1', pos=",pos,"\n" if /\G(.)/; |
5202 | 2965 | |
5203 | 2966 | =begin original |
5204 | 2967 | |
5205 | 2968 | The last example should print: |
5206 | 2969 | |
5207 | 2970 | =end original |
5208 | 2971 | |
5209 | 2972 | 最後のものは以下のものを表示するはずです: |
5210 | 2973 | |
5211 | 2974 | 1: 'oo', pos=4 |
5212 | 2975 | 2: 'q', pos=5 |
5213 | 2976 | 3: 'pp', pos=7 |
5214 | 2977 | 1: '', pos=7 |
5215 | 2978 | 2: 'q', pos=8 |
5216 | 2979 | 3: '', pos=8 |
5217 | 2980 | Final: 'q', pos=8 |
5218 | 2981 | |
5219 | 2982 | =begin original |
5220 | 2983 | |
5221 | 2984 | Notice that the final match matched C<q> instead of C<p>, which a match |
5222 | without the C<\G> anchor would have done. | |
2985 | without the C<\G> anchor would have done. Also note that the final match | |
5223 | did not update C<pos> | |
2986 | did not update C<pos> -- C<pos> is only updated on a C</g> match. If the | |
5224 | 2987 | final match did indeed match C<p>, it's a good bet that you're running an |
5225 | ||
2988 | older (pre-5.6.0) Perl. | |
5226 | 2989 | |
5227 | 2990 | =end original |
5228 | 2991 | |
5229 | 2992 | C<\G> なしでのマッチが行われたため、最後のマッチでは C<p> ではなく |
5230 | 2993 | C<q> がマッチすることに注意してください。 |
5231 | 2994 | また、最後のマッチは C<pos> を更新しないことに注意してください。 |
5232 | 2995 | C<pos> は C</g> マッチでのみ更新されます。 |
5233 | 2996 | もし最後のマッチで C<p> にマッチした場合、かなりの確率で |
5234 | ||
2997 | 古い (5.6.0 以前の) Perl で実行しているはずです。 | |
5235 | 2998 | |
5236 | 2999 | =begin original |
5237 | 3000 | |
5238 | 3001 | A useful idiom for C<lex>-like scanners is C</\G.../gc>. You can |
5239 | 3002 | combine several regexps like this to process a string part-by-part, |
5240 | 3003 | doing different actions depending on which regexp matched. Each |
5241 | 3004 | regexp tries to match where the previous one leaves off. |
5242 | 3005 | |
5243 | 3006 | =end original |
5244 | 3007 | |
5245 | 3008 | C<lex> 風にスキャンするために便利な指定は C</\G.../gc> です。 |
5246 | 文字列を部分ごとに処理するためにいくつかの正規表現をつなげて、どの | |
3009 | 文字列を部分ごとに処理するためにいくつかの正規表現をつなげて、どの | |
5247 | マッチ | |
3010 | 正規表現にマッチしたかによって異なる処理をすることができます。 | |
5248 | それぞれの正規表現は前の正規表現が飛ばした部分に対して | |
3011 | それぞれの正規表現は前の正規表現が飛ばした部分に対して | |
3012 | マッチを試みます。 | |
5249 | 3013 | |
5250 | 3014 | $_ = <<'EOL'; |
5251 | $url = URI::URL->new( "http:// | |
3015 | $url = URI::URL->new( "http://www/" ); die if $url eq "xXx"; | |
5252 | die if $url eq "xXx"; | |
5253 | 3016 | 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 | } | |
5254 | 3028 | |
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 | ||
5270 | 3029 | =begin original |
5271 | 3030 | |
5272 | 3031 | Here is the output (split into several lines): |
5273 | 3032 | |
5274 | 3033 | =end original |
5275 | 3034 | |
5276 | 3035 | 出力は以下のようになります(何行かに分割しています): |
5277 | 3036 | |
5278 | line-noise lowercase line-noise UPPERCASE line-noise | |
3037 | line-noise lowercase line-noise lowercase UPPERCASE line-noise | |
5279 | line-noise lowercase line-noise lowercase line-noi | |
3038 | UPPERCASE line-noise lowercase line-noise lowercase line-noise | |
5280 | lowercase line-noise lowercase lowercase line-noi | |
3039 | lowercase lowercase line-noise lowercase lowercase line-noise | |
5281 | | |
3040 | MiXeD line-noise. That's all! | |
5282 | 3041 | |
5283 | =item | |
3042 | =item ?PATTERN? | |
5284 | X<?> | |
3043 | X<?> | |
5285 | 3044 | |
5286 | 3045 | =begin original |
5287 | 3046 | |
5288 | This is just like the C< | |
3047 | This is just like the C</pattern/> search, except that it matches only | |
5289 | on | |
3048 | once between calls to the reset() operator. This is a useful | |
5290 | 3049 | optimization when you want to see only the first occurrence of |
5291 | something in each file of a set of files, for instance. Only C< | |
3050 | something in each file of a set of files, for instance. Only C<??> | |
5292 | 3051 | patterns local to the current package are reset. |
5293 | 3052 | |
5294 | 3053 | =end original |
5295 | 3054 | |
5296 | これは、 | |
3055 | これは、reset() 演算子を呼び出すごとに 1 度だけしか | |
5297 | 除いては C< | |
3056 | マッチしないことを除いては C</pattern/> による検索と全く同じです。 | |
5298 | 3057 | たとえば、ファイルの集まりの中で個々のファイルについて、あるものを |
5299 | 3058 | 探すとき、最初の 1 つだけの存在がわかれば良いのであれば、この機能を |
5300 | 3059 | 使って最適化をはかることができます。 |
5301 | 現在のパッケージにローカルとなっている C< | |
3060 | 現在のパッケージにローカルとなっている C<??> のパターンだけが | |
5302 | 3061 | リセットされます。 |
5303 | 3062 | |
5304 | 3063 | while (<>) { |
5305 | if ( | |
3064 | if (?^$?) { | |
5306 | 3065 | # blank line between header and body |
5307 | 3066 | } |
5308 | 3067 | } continue { |
5309 | reset if eof; # clear | |
3068 | reset if eof; # clear ?? status for next file | |
5310 | 3069 | } |
5311 | 3070 | |
5312 | 3071 | =begin original |
5313 | 3072 | |
5314 | ||
3073 | This usage is vaguely deprecated, which means it just might possibly | |
5315 | ||
3074 | be removed in some distant future version of Perl, perhaps somewhere | |
3075 | around the year 2168. | |
5316 | 3076 | |
5317 | 3077 | =end original |
5318 | 3078 | |
5319 | ||
3079 | この方法は、あまりお勧めしません。 | |
5320 | ||
3080 | Perl の遠い将来のバージョン(おそらく 2168 年頃)では削除されるかもしれません。 | |
5321 | 3081 | |
5322 | | |
3082 | =item s/PATTERN/REPLACEMENT/msixpogce | |
3083 | X<substitute> X<substitution> X<replace> X<regexp, replace> | |
3084 | X<regexp, substitute> X</m> X</s> X</i> X</x> X</p> X</o> X</g> X</c> X</e> | |
5323 | 3085 | |
5324 | 3086 | =begin original |
5325 | 3087 | |
5326 | The match-once behavior is controlled by the match delimiter being | |
5327 | C<?>; 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 | ||
5336 | In the past, the leading C<m> in C<m?I<PATTERN>?> was optional, but omitting it | |
5337 | would produce a deprecation warning. As of v5.22.0, omitting it produces a | |
5338 | syntax error. If you encounter this construct in older code, you can just add | |
5339 | C<m>. | |
5340 | ||
5341 | =end original | |
5342 | ||
5343 | 過去には、C<m?I<PATTERN>?> の先頭の C<m> は省略可能でしたが、これを省略すると | |
5344 | 廃止予定警告が出ていました。 | |
5345 | v5.22.0 から、これを省略すると文法エラーが発生します。 | |
5346 | もし古いコードでこの構文に遭遇した場合は、単に C<m> を追加してください。 | |
5347 | ||
5348 | =item C<s/I<PATTERN>/I<REPLACEMENT>/msixpodualngcer> | |
5349 | X<s> X<substitute> X<substitution> X<replace> X<regexp, replace> | |
5350 | X<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 | ||
5354 | 3088 | Searches a string for a pattern, and if found, replaces that pattern |
5355 | 3089 | with the replacement text and returns the number of substitutions |
5356 | made. Otherwise it returns false (a | |
3090 | made. Otherwise it returns false (specifically, the empty string). | |
5357 | and numeric zero (C<0>) as described in L</Relational Operators>). | |
5358 | 3091 | |
5359 | 3092 | =end original |
5360 | 3093 | |
5361 | 3094 | 文字列中でパターンを検索し、もし見つかれば、置換テキストで置き換え、 |
5362 | 3095 | 置換した数を返します。 |
5363 | 見つからなければ、偽 (空文字列 | |
3096 | 見つからなければ、偽 (具体的には、空文字列) を返します。 | |
5364 | 返します。 | |
5365 | 3097 | |
5366 | 3098 | =begin original |
5367 | 3099 | |
5368 | If the C</r> (non-destructive) option is used then it runs the | |
5369 | substitution on a copy of the string and instead of returning the | |
5370 | number of substitutions, it returns the copy whether or not a | |
5371 | substitution occurred. The original string is never changed when | |
5372 | C</r> is used. The copy will always be a plain string, even if the | |
5373 | input is an object or a tied variable. | |
5374 | ||
5375 | =end original | |
5376 | ||
5377 | C</r> (非破壊) オプションが使われると、文字列のコピーに対して置換が行われ、 | |
5378 | 置換された数ではなく、置換が行われたかどうかにかかわらずこのコピーが | |
5379 | 返されます。 | |
5380 | C</r> が使われた場合、元の文字列は決して変更されません。 | |
5381 | コピーは、たとえ入力がオブジェクトや tie された変数でも、常に | |
5382 | プレーンな文字列です。 | |
5383 | ||
5384 | =begin original | |
5385 | ||
5386 | 3100 | If no string is specified via the C<=~> or C<!~> operator, the C<$_> |
5387 | variable is searched and modified. | |
3101 | variable is searched and modified. (The string specified with C<=~> must | |
5388 | ||
3102 | be scalar variable, an array element, a hash element, or an assignment | |
5389 | ||
3103 | to one of those, i.e., an lvalue.) | |
5390 | scalar lvalue. | |
5391 | 3104 | |
5392 | 3105 | =end original |
5393 | 3106 | |
5394 | 3107 | C<=~> 演算子や C<!~> 演算子によって文字列が指定されていなければ、 |
5395 | 3108 | 変数 C<$_> が検索され、修正されます。 |
5396 | C< | |
3109 | (C<=~> で指定される文字列は、スカラ変数、配列要素、ハッシュ要素、 | |
5397 | ||
3110 | あるいは、これらへの代入式といった左辺値でなければなりません。) | |
5398 | あるいは、これらへの代入式といったある種の | |
5399 | スカラ左辺値でなければなりません。 | |
5400 | 3111 | |
5401 | 3112 | =begin original |
5402 | 3113 | |
5403 | If the delimiter chosen is a single quote, no | |
3114 | If the delimiter chosen is a single quote, no interpolation is | |
5404 | done on either the | |
3115 | done on either the PATTERN or the REPLACEMENT. Otherwise, if the | |
5405 | ||
3116 | PATTERN contains a $ that looks like a variable rather than an | |
5406 | 3117 | end-of-string test, the variable will be interpolated into the pattern |
5407 | 3118 | at run-time. If you want the pattern compiled only once the first time |
5408 | 3119 | the variable is interpolated, use the C</o> option. If the pattern |
5409 | 3120 | evaluates to the empty string, the last successfully executed regular |
5410 | 3121 | expression is used instead. See L<perlre> for further explanation on these. |
3122 | See L<perllocale> for discussion of additional considerations that apply | |
3123 | when C<use locale> is in effect. | |
5411 | 3124 | |
5412 | 3125 | =end original |
5413 | 3126 | |
3127 | あとで述べますが、区切り文字はスラッシュとは限りません。 | |
5414 | 3128 | シングルクォートを区切り文字として使った場合には、 |
5415 | ||
3129 | PATTERN にも REPLACEMENT にも変数の展開を行ないません。 | |
5416 | それ以外の場合、文字列の最後を表わすものには見えない | |
3130 | それ以外の場合、文字列の最後を表わすものには見えない $ が | |
5417 | ||
3131 | PATTERN に含まれると、実行時に変数がパターン内に展開されます。 | |
5418 | 3132 | 最初に変数が展開されるときにだけパターンのコンパイルを行ないたいときには、 |
5419 | 3133 | C</o> オプションを使ってください。 |
5420 | 3134 | パターンの評価結果が空文字列になった場合には、最後に成功した正規表現が |
5421 | 3135 | 代わりに使われます。 |
5422 | 3136 | これについてさらに詳しくは、L<perlre> を参照してください。 |
3137 | C<use locale> が有効の場合の議論については L<perllocale> を参照して下さい。 | |
5423 | 3138 | |
5424 | 3139 | =begin original |
5425 | 3140 | |
5426 | Options are as with | |
3141 | Options are as with m// with the addition of the following replacement | |
5427 | 3142 | specific options: |
5428 | 3143 | |
5429 | 3144 | =end original |
5430 | 3145 | |
5431 | オプションは、 | |
3146 | オプションは、m// のものに加えて、以下の置換固有のものがあります: | |
5432 | 3147 | |
5433 | 3148 | =begin original |
5434 | 3149 | |
5435 | 3150 | 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. | |
5440 | 3152 | |
5441 | 3153 | =end original |
5442 | 3154 | |
5443 | e 式の右側の評価を行なう | |
3155 | e 式の右側の評価を行なう | |
5444 | ee 右側を文字列として評価して、その結果を評価する | |
3156 | ee 右側を文字列として評価して、その結果を評価する | |
5445 | r 置換した結果を返し、もとの文字列はそのままにする。 | |
5446 | 3157 | |
5447 | 3158 | =begin original |
5448 | 3159 | |
5449 | Any non-whitespace delimiter may replace the | |
3160 | Any non-alphanumeric, non-whitespace delimiter may replace the | |
5450 | ||
3161 | slashes. If single quotes are used, no interpretation is done on the | |
5451 | ||
3162 | replacement string (the C</e> modifier overrides this, however). Unlike | |
5452 | ||
3163 | Perl 4, Perl 5 treats backticks as normal delimiters; the replacement | |
5453 | ||
3164 | text is not evaluated as a command. If the | |
5454 | ||
3165 | PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own | |
5455 | ||
3166 | pair of quotes, which may or may not be bracketing quotes, e.g., | |
5456 | 3167 | C<s(foo)(bar)> or C<< s<foo>/bar/ >>. A C</e> will cause the |
5457 | 3168 | replacement portion to be treated as a full-fledged Perl expression |
5458 | 3169 | and evaluated right then and there. It is, however, syntax checked at |
5459 | compile-time. | |
3170 | compile-time. A second C<e> modifier will cause the replacement portion | |
5460 | 3171 | to be C<eval>ed before being run as a Perl expression. |
5461 | 3172 | |
5462 | 3173 | =end original |
5463 | 3174 | |
5464 | 空白ではない任意の区切り文字で、スラッシュを置き換えられます。 | |
3175 | 英数字、空白ではない任意の区切り文字で、スラッシュを置き換えられます。 | |
5465 | 識別子として許されている文字を使うときには C<s> の後に空白を | |
5466 | 追加してください。 | |
5467 | 3176 | 先に述べたように、シングルクォートを使うと置換文字列での展開は |
5468 | 3177 | されません (C</e>修飾子を使えば可能です)。 |
5469 | バッククォートを通常のデリミタとして扱 | |
3178 | Perl 4 と違って、 Perl 5 はバッククォートを通常のデリミタとして扱います。 | |
5470 | 3179 | 置換テキストはコマンドとして評価されません。 |
5471 | ||
3180 | PATTERN を括弧類で括った場合には、REPLACEMENT 用にもう一組の区切り文字を | |
5472 | 用意します | |
3181 | 用意します。 | |
5473 | C<s(foo)(bar)> や | |
3182 | これは、括弧類であっても、なくてもかまいません; C<s(foo)(bar)> や | |
3183 | C<< s<foo>/bar/ >>。 | |
5474 | 3184 | C</e> は置換文字列を完全な Perl の式として扱い、その場所で直ちに解釈します。 |
5475 | 3185 | しかし、これはコンパイル時に構文チェックされます。 |
5476 | 3186 | 二番目の C<e> 修飾子を指定すると、置換部分がまず Perl の式として |
5477 | 3187 | C<eval> されます。 |
5478 | 3188 | |
5479 | 3189 | =begin original |
5480 | 3190 | |
5481 | 3191 | Examples: |
5482 | 3192 | |
5483 | 3193 | =end original |
5484 | 3194 | |
5485 | 3195 | 例: |
5486 | 3196 | |
5487 | s/\bgreen\b/mauve/g; | |
3197 | s/\bgreen\b/mauve/g; # don't change wintergreen | |
5488 | 3198 | |
5489 | 3199 | $path =~ s|/usr/bin|/usr/local/bin|; |
5490 | 3200 | |
5491 | 3201 | s/Login: $foo/Login: $bar/; # run-time pattern |
5492 | 3202 | |
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 | |
5503 | 3204 | |
5504 | $count = ($paragraph =~ s/Mister\b/Mr./g); # get change-cnt | |
3205 | $count = ($paragraph =~ s/Mister\b/Mr./g); # get change-count | |
5505 | 3206 | |
5506 | 3207 | $_ = 'abc123xyz'; |
5507 | 3208 | s/\d+/$&*2/e; # yields 'abc246xyz' |
5508 | 3209 | s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz' |
5509 | 3210 | s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz' |
5510 | 3211 | |
5511 | 3212 | s/%(.)/$percent{$1}/g; # change percent escapes; no /e |
5512 | 3213 | s/%(.)/$percent{$1} || $&/ge; # expr now, so /e |
5513 | 3214 | s/^=(\w+)/pod($1)/ge; # use function call |
5514 | 3215 | |
5515 | $_ = 'abc123xyz'; | |
5516 | $x = s/abc/def/r; # $x is 'def123xyz' and | |
5517 | # $_ remains 'abc123xyz'. | |
5518 | ||
5519 | 3216 | # expand variables in $_, but dynamics only, using |
5520 | 3217 | # symbolic dereferencing |
5521 | 3218 | s/\$(\w+)/${$1}/g; |
5522 | 3219 | |
5523 | 3220 | # Add one to the value of any numbers in the string |
5524 | 3221 | s/(\d+)/1 + $1/eg; |
5525 | 3222 | |
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 | ||
5530 | 3223 | # This will expand any embedded scalar variable |
5531 | 3224 | # (including lexicals) in $_ : First $1 is interpolated |
5532 | 3225 | # to the variable name, and then evaluated |
5533 | 3226 | s/(\$\w+)/$1/eeg; |
5534 | 3227 | |
5535 | 3228 | # Delete (most) C comments. |
5536 | 3229 | $program =~ s { |
5537 | 3230 | /\* # Match the opening delimiter. |
5538 | 3231 | .*? # Match a minimal number of characters. |
5539 | 3232 | \*/ # Match the closing delimiter. |
5540 | 3233 | } []gsx; |
5541 | 3234 | |
5542 | s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_, | |
3235 | s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_, expensively | |
5543 | # expensively | |
5544 | 3236 | |
5545 | for ($variable) { # trim whitespace in $variable, | |
3237 | for ($variable) { # trim whitespace in $variable, cheap | |
5546 | # cheap | |
5547 | 3238 | s/^\s+//; |
5548 | 3239 | s/\s+$//; |
5549 | 3240 | } |
5550 | 3241 | |
5551 | 3242 | s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields |
5552 | 3243 | |
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 | ||
5557 | 3244 | =begin original |
5558 | 3245 | |
5559 | Note the use of | |
3246 | Note the use of $ instead of \ in the last example. Unlike | |
5560 | B<sed>, we use the \<I<digit>> form only | |
3247 | B<sed>, we use the \<I<digit>> form in only the left hand side. | |
5561 | 3248 | Anywhere else it's $<I<digit>>. |
5562 | 3249 | |
5563 | 3250 | =end original |
5564 | 3251 | |
5565 | 最後の例で | |
3252 | 最後の例で \ の代わりに $ を使っているのに注意してください。 | |
5566 | 3253 | B<sed> と違って、\<I<数字>> の形式はパターンの方でのみ使用できます。 |
5567 | 3254 | その他の場所では、$<I<数字>> を使います。 |
5568 | 3255 | |
5569 | 3256 | =begin original |
5570 | 3257 | |
5571 | 3258 | Occasionally, you can't use just a C</g> to get all the changes |
5572 | 3259 | to occur that you might want. Here are two common cases: |
5573 | 3260 | |
5574 | 3261 | =end original |
5575 | 3262 | |
5576 | 3263 | ときには、C</g> を付けるだけでは、あなたが望んでいるような形で |
5577 | 3264 | すべてを変更することができないことがあります。 |
5578 | 3265 | 良くある例を 2 つ示します: |
5579 | 3266 | |
5580 | 3267 | # put commas in the right places in an integer |
5581 | 3268 | 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g; |
5582 | 3269 | |
5583 | 3270 | # expand tabs to 8-column spacing |
5584 | 3271 | 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e; |
5585 | 3272 | |
5586 | =begin original | |
5587 | ||
5588 | X</c>While C<s///> accepts the C</c> flag, it has no effect beyond | |
5589 | producing a warning if warnings are enabled. | |
5590 | ||
5591 | =end original | |
5592 | ||
5593 | X</c> | |
5594 | C<s///> は C</c> フラグを受け付けますが、 | |
5595 | 警告が有効の場合に警告を出す以外の効果はありません。 | |
5596 | ||
5597 | 3273 | =back |
5598 | 3274 | |
5599 | 3275 | =head2 Quote-Like Operators |
5600 | 3276 | X<operator, quote-like> |
5601 | 3277 | |
5602 | 3278 | (クォート風演算子) |
5603 | 3279 | |
5604 | 3280 | =over 4 |
5605 | 3281 | |
5606 | =item | |
3282 | =item q/STRING/ | |
5607 | 3283 | X<q> X<quote, single> X<'> X<''> |
5608 | 3284 | |
5609 | =item | |
3285 | =item 'STRING' | |
5610 | 3286 | |
5611 | 3287 | =begin original |
5612 | 3288 | |
5613 | 3289 | A single-quoted, literal string. A backslash represents a backslash |
5614 | 3290 | unless followed by the delimiter or another backslash, in which case |
5615 | 3291 | the delimiter or backslash is interpolated. |
5616 | 3292 | |
5617 | 3293 | =end original |
5618 | 3294 | |
5619 | 3295 | シングルクォートされた、リテラル文字列です。 |
5620 | 3296 | バックスラッシュは、後ろに続くものが区切文字か、別のバックスラッシュで |
5621 | ある場合を除いて単なるバックスラッシュです | |
3297 | ある場合を除いて単なるバックスラッシュです。 | |
5622 | 3298 | 区切文字やバックスラッシュが続く場合には、その区切文字自身もしくは |
5623 | 3299 | バックスラッシュそのものが展開されます。 |
5624 | 3300 | |
5625 | 3301 | $foo = q!I said, "You said, 'She said it.'"!; |
5626 | 3302 | $bar = q('This is it.'); |
5627 | 3303 | $baz = '\n'; # a two-character string |
5628 | 3304 | |
5629 | =item | |
3305 | =item qq/STRING/ | |
5630 | 3306 | X<qq> X<quote, double> X<"> X<""> |
5631 | 3307 | |
5632 | =item | |
3308 | =item "STRING" | |
5633 | 3309 | |
5634 | 3310 | =begin original |
5635 | 3311 | |
5636 | 3312 | A double-quoted, interpolated string. |
5637 | 3313 | |
5638 | 3314 | =end original |
5639 | 3315 | |
5640 | 3316 | ダブルクォートされた、リテラル文字列です。 |
5641 | 3317 | |
5642 | 3318 | $_ .= qq |
5643 | 3319 | (*** The previous line contains the naughty word "$1".\n) |
5644 | 3320 | if /\b(tcl|java|python)\b/i; # :-) |
5645 | 3321 | $baz = "\n"; # a one-character string |
5646 | 3322 | |
5647 | =item | |
3323 | =item qx/STRING/ | |
5648 | 3324 | X<qx> X<`> X<``> X<backtick> |
5649 | 3325 | |
5650 | =item | |
3326 | =item `STRING` | |
5651 | 3327 | |
5652 | 3328 | =begin original |
5653 | 3329 | |
5654 | 3330 | A string which is (possibly) interpolated and then executed as a |
5655 | system command | |
3331 | system command with C</bin/sh> or its equivalent. Shell wildcards, | |
5656 | ||
3332 | pipes, and redirections will be honored. The collected standard | |
5657 | ||
3333 | output of the command is returned; standard error is unaffected. In | |
5658 | ||
3334 | scalar context, it comes back as a single (potentially multi-line) | |
5659 | ||
3335 | string, or undef if the command failed. In list context, returns a | |
5660 | ||
3336 | list of lines (however you've defined lines with $/ or | |
5661 | ||
3337 | $INPUT_RECORD_SEPARATOR), or an empty list if the command failed. | |
5662 | list of lines (however you've defined lines with C<$/> or | |
5663 | C<$INPUT_RECORD_SEPARATOR>), or an empty list if the shell (or command) | |
5664 | could not be started. | |
5665 | 3338 | |
5666 | 3339 | =end original |
5667 | 3340 | |
5668 | 展開され、 | |
3341 | 展開され、C</bin/sh> またはそれと等価なものでシステムのコマンドとして | |
5669 | ||
3342 | 実行される(であろう)文字列です。 | |
5670 | 3343 | シェルのワイルドカード、パイプ、リダイレクトが有効です。 |
5671 | ||
3344 | そのコマンドの、標準出力を集めたものが返されます。 | |
5672 | ||
3345 | 標準エラーは影響を与えません。 | |
5673 | そのコマンドの、標準出力を集めたものが返されます; 標準エラーは影響を | |
5674 | 与えません。 | |
5675 | 3346 | スカラコンテキストでは、(複数行を含むかもしれない) |
5676 | 1 つの文字列が戻ってきます | |
3347 | 1 つの文字列が戻ってきます。 | |
5677 | ||
3348 | コマンドが失敗したときは未定義値を返します。 | |
5678 | リストコンテキストでは、( | |
3349 | リストコンテキストでは、($/ もしくは $INPUT_RECORD_SEPARATOR を | |
5679 | どのように設定していても) 行のリストを返します | |
3350 | どのように設定していても) 行のリストを返します。 | |
5680 | ||
3351 | コマンドが失敗したときは空リストを返します。 | |
5681 | 空リストを返します。 | |
5682 | 3352 | |
5683 | 3353 | =begin original |
5684 | 3354 | |
5685 | 3355 | Because backticks do not affect standard error, use shell file descriptor |
5686 | 3356 | syntax (assuming the shell supports this) if you care to address this. |
5687 | 3357 | To capture a command's STDERR and STDOUT together: |
5688 | 3358 | |
5689 | 3359 | =end original |
5690 | 3360 | |
5691 | 3361 | バッククォートは標準エラーには影響を与えないので、標準エラーを |
5692 | 3362 | 使いたい場合は(シェルが対応しているものとして)シェルのファイル記述子の |
5693 | 3363 | 文法を使ってください。 |
5694 | 3364 | コマンドの STDERR と STDOUT を共に取得したい場合は: |
5695 | 3365 | |
5696 | 3366 | $output = `cmd 2>&1`; |
5697 | 3367 | |
5698 | 3368 | =begin original |
5699 | 3369 | |
5700 | 3370 | To capture a command's STDOUT but discard its STDERR: |
5701 | 3371 | |
5702 | 3372 | =end original |
5703 | 3373 | |
5704 | 3374 | コマンドの STDOUT は取得するが STDERR は捨てる場合は: |
5705 | 3375 | |
5706 | 3376 | $output = `cmd 2>/dev/null`; |
5707 | 3377 | |
5708 | 3378 | =begin original |
5709 | 3379 | |
5710 | 3380 | To capture a command's STDERR but discard its STDOUT (ordering is |
5711 | 3381 | important here): |
5712 | 3382 | |
5713 | 3383 | =end original |
5714 | 3384 | |
5715 | 3385 | コマンドの STDERR は取得するが STDOUT は捨てる場合は |
5716 | 3386 | (ここでは順序が重要です): |
5717 | 3387 | |
5718 | 3388 | $output = `cmd 2>&1 1>/dev/null`; |
5719 | 3389 | |
5720 | 3390 | =begin original |
5721 | 3391 | |
5722 | 3392 | To exchange a command's STDOUT and STDERR in order to capture the STDERR |
5723 | 3393 | but leave its STDOUT to come out the old STDERR: |
5724 | 3394 | |
5725 | 3395 | =end original |
5726 | 3396 | |
5727 | 3397 | STDERR を取得するが、STDOUT は古い STDERR のために残しておくために |
5728 | 3398 | STDOUT と STDERR を交換するには: |
5729 | 3399 | |
5730 | 3400 | $output = `cmd 3>&1 1>&2 2>&3 3>&-`; |
5731 | 3401 | |
5732 | 3402 | =begin original |
5733 | 3403 | |
5734 | 3404 | To read both a command's STDOUT and its STDERR separately, it's easiest |
5735 | 3405 | to redirect them separately to files, and then read from those files |
5736 | 3406 | when the program is done: |
5737 | 3407 | |
5738 | 3408 | =end original |
5739 | 3409 | |
5740 | 3410 | コマンドの STDOUT と STDERR の両方を別々に読み込みたい場合、 |
5741 | 3411 | 一番簡単な方法は別々のファイルにリダイレクトし、 |
5742 | 3412 | プログラムが終了してからそのファイルを読むことです: |
5743 | 3413 | |
5744 | 3414 | system("program args 1>program.stdout 2>program.stderr"); |
5745 | 3415 | |
5746 | 3416 | =begin original |
5747 | 3417 | |
5748 | 3418 | The STDIN filehandle used by the command is inherited from Perl's STDIN. |
5749 | 3419 | For example: |
5750 | 3420 | |
5751 | 3421 | =end original |
5752 | 3422 | |
5753 | 3423 | コマンドによって使われる STDIN ファイルハンドルは Perl の STDIN を |
5754 | 3424 | 継承します。 |
5755 | 3425 | 例えば: |
5756 | 3426 | |
5757 | open | |
3427 | open BLAM, "blam" || die "Can't open: $!"; | |
5758 | open | |
3428 | open STDIN, "<&BLAM"; | |
5759 | print | |
3429 | print `sort`; | |
5760 | 3430 | |
5761 | 3431 | =begin original |
5762 | 3432 | |
5763 | will print the sorted contents of the file | |
3433 | will print the sorted contents of the file "blam". | |
5764 | 3434 | |
5765 | 3435 | =end original |
5766 | 3436 | |
5767 | は | |
3437 | はファイル "blam" の内容をソートして表示します。 | |
5768 | 3438 | |
5769 | 3439 | =begin original |
5770 | 3440 | |
5771 | 3441 | Using single-quote as a delimiter protects the command from Perl's |
5772 | 3442 | double-quote interpolation, passing it on to the shell instead: |
5773 | 3443 | |
5774 | 3444 | =end original |
5775 | 3445 | |
5776 | 3446 | シングルクォートをデリミタとして使うと Perl のダブルクォート展開から |
5777 | 3447 | 保護され、そのままシェルに渡されます: |
5778 | 3448 | |
5779 | 3449 | $perl_info = qx(ps $$); # that's Perl's $$ |
5780 | 3450 | $shell_info = qx'ps $$'; # that's the new shell's $$ |
5781 | 3451 | |
5782 | 3452 | =begin original |
5783 | 3453 | |
5784 | 3454 | How that string gets evaluated is entirely subject to the command |
5785 | 3455 | interpreter on your system. On most platforms, you will have to protect |
5786 | 3456 | shell metacharacters if you want them treated literally. This is in |
5787 | 3457 | practice difficult to do, as it's unclear how to escape which characters. |
5788 | See L<perlsec> for a clean and safe example of a manual | |
3458 | See L<perlsec> for a clean and safe example of a manual fork() and exec() | |
5789 | 3459 | to emulate backticks safely. |
5790 | 3460 | |
5791 | 3461 | =end original |
5792 | 3462 | |
5793 | 3463 | この文字列がどのように評価されるかは完全にシステムのコマンドインタプリタに |
5794 | 3464 | 依存します。 |
5795 | 3465 | ほとんどのプラットフォームでは、シェルのメタ文字をリテラルに |
5796 | 3466 | 扱ってほしい場合はそれを守る必要があります。 |
5797 | 3467 | 文字をエスケープする方法が明確ではないので、これは理論的には難しいことです。 |
5798 | 逆クォートを安全にエミュレートするために手動で | |
3468 | 逆クォートを安全にエミュレートするために手動で fork() と exec() を | |
5799 | 3469 | 行うためのきれいで安全な例については L<perlsec> を参照してください。 |
5800 | 3470 | |
5801 | 3471 | =begin original |
5802 | 3472 | |
5803 | 3473 | On some platforms (notably DOS-like ones), the shell may not be |
5804 | 3474 | capable of dealing with multiline commands, so putting newlines in |
5805 | 3475 | the string may not get you what you want. You may be able to evaluate |
5806 | 3476 | multiple commands in a single line by separating them with the command |
5807 | separator character, if your shell supports that ( | |
3477 | separator character, if your shell supports that (e.g. C<;> on many Unix | |
5808 | ||
3478 | shells; C<&> on the Windows NT C<cmd> shell). | |
5809 | 3479 | |
5810 | 3480 | =end original |
5811 | 3481 | |
5812 | 3482 | (特に DOS 風の)プラットフォームには、シェルが複数行のコマンドを扱うことが |
5813 | 3483 | できないものがあるので、文字列に改行を入れるとあなたの望まない結果に |
5814 | 3484 | なる場合があります。 |
5815 | 3485 | シェルが対応していれば、コマンド分割文字で分割することで |
5816 | 3486 | 1 行に複数のコマンドを入れて解釈させることができます |
5817 | 3487 | (この文字は、多くの Unix シェルでは C<;>、Windows NT C<cmd> シェルでは |
5818 | 3488 | C<&> です)。 |
5819 | 3489 | |
5820 | 3490 | =begin original |
5821 | 3491 | |
5822 | Perl will attempt to flush all files opened for | |
3492 | Beginning with v5.6.0, Perl will attempt to flush all files opened for | |
5823 | 3493 | output before starting the child process, but this may not be supported |
5824 | 3494 | on some platforms (see L<perlport>). To be safe, you may need to set |
5825 | C<$|> ( | |
3495 | C<$|> ($AUTOFLUSH in English) or call the C<autoflush()> method of | |
5826 | C< | |
3496 | C<IO::Handle> on any open handles. | |
5827 | 3497 | |
5828 | 3498 | =end original |
5829 | 3499 | |
5830 | Perl は子プロセスの実行前に書き込み用に開いている全ての | |
3500 | v5.6.0 以降、Perl は子プロセスの実行前に書き込み用に開いている全ての | |
5831 | 3501 | ファイルをフラッシュしようとしますが、これに対応していない |
5832 | 3502 | プラットフォームもあります(L<perlport> を参照してください)。 |
5833 | 安全のためには、C<$|> ( | |
3503 | 安全のためには、C<$|> (English モジュールでは $AUTOFLUSH)をセットするか、 | |
5834 | ||
3504 | 開いている全てのハンドルに対して C<IO::Handle> の C<autoflush()> メソッドを | |
5835 | ||
3505 | 呼び出す必要があります。 | |
5836 | 3506 | |
5837 | 3507 | =begin original |
5838 | 3508 | |
5839 | 3509 | Beware that some command shells may place restrictions on the length |
5840 | 3510 | of the command line. You must ensure your strings don't exceed this |
5841 | 3511 | limit after any necessary interpolations. See the platform-specific |
5842 | 3512 | release notes for more details about your particular environment. |
5843 | 3513 | |
5844 | 3514 | =end original |
5845 | 3515 | |
5846 | 3516 | コマンド行の長さに制限があるコマンドシェルがあることに注意してください。 |
5847 | 3517 | 全ての必要な変換が行われた後、コマンド文字列がこの制限を越えないことを |
5848 | 3518 | 保障する必要があります。 |
5849 | 3519 | 特定の環境に関するさらなる詳細についてはプラットフォーム固有の |
5850 | 3520 | リリースノートを参照してください。 |
5851 | 3521 | |
5852 | 3522 | =begin original |
5853 | 3523 | |
5854 | 3524 | Using this operator can lead to programs that are difficult to port, |
5855 | 3525 | because the shell commands called vary between systems, and may in |
5856 | 3526 | fact not be present at all. As one example, the C<type> command under |
5857 | 3527 | the POSIX shell is very different from the C<type> command under DOS. |
5858 | 3528 | That doesn't mean you should go out of your way to avoid backticks |
5859 | 3529 | when they're the right way to get something done. Perl was made to be |
5860 | 3530 | a glue language, and one of the things it glues together is commands. |
5861 | 3531 | Just understand what you're getting yourself into. |
5862 | 3532 | |
5863 | 3533 | =end original |
5864 | 3534 | |
5865 | この演算子を使うと、プログラムの移殖が困難になります | |
3535 | この演算子を使うと、プログラムの移殖が困難になります。 | |
5866 | コマンドはシステムによって異なり、実際全く | |
3536 | 呼び出されるシェルコマンドはシステムによって異なり、実際全く | |
3537 | 存在しないこともあるからです。 | |
5867 | 3538 | 一つの例としては、POSIX シェルの C<type> コマンドは DOS の C<type> コマンドと |
5868 | 3539 | 大きく異なっています。 |
5869 | 3540 | これは、何かを為すために正しい方法として逆クォートを使うことを |
5870 | 3541 | 避けるべきであることを意味しません。 |
5871 | 3542 | Perl は接着剤のような言語として作られ、接着されるべきものの一つは |
5872 | 3543 | コマンドです。 |
5873 | 3544 | 単にあなたが何をしようとしているかを理解しておいてください。 |
5874 | 3545 | |
5875 | 3546 | =begin original |
5876 | 3547 | |
5877 | Like C<system>, backticks put the child process exit code in C<$?>. | |
5878 | If you'd like to manually inspect failure, you can check all possible | |
5879 | failure modes by inspecting C<$?> like this: | |
5880 | ||
5881 | =end original | |
5882 | ||
5883 | C<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 | ||
5901 | Use the L<open> pragma to control the I/O layers used when reading the | |
5902 | output 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 | ||
5914 | C<qx//> can also be called like a function with L<perlfunc/readpipe>. | |
5915 | ||
5916 | =end original | |
5917 | ||
5918 | C<qx//> はまた、L<perlfunc/readpipe> のような関数から呼び出されます。 | |
5919 | ||
5920 | =begin original | |
5921 | ||
5922 | 3548 | See L</"I/O Operators"> for more discussion. |
5923 | 3549 | |
5924 | 3550 | =end original |
5925 | 3551 | |
5926 | 3552 | さらなる議論については L</"I/O Operators"> を参照して下さい。 |
5927 | 3553 | |
5928 | =item | |
3554 | =item qw/STRING/ | |
5929 | 3555 | X<qw> X<quote, list> X<quote, words> |
5930 | 3556 | |
5931 | 3557 | =begin original |
5932 | 3558 | |
5933 | Evaluates to a list of the words extracted out of | |
3559 | Evaluates to a list of the words extracted out of STRING, using embedded | |
5934 | 3560 | whitespace as the word delimiters. It can be understood as being roughly |
5935 | 3561 | equivalent to: |
5936 | 3562 | |
5937 | 3563 | =end original |
5938 | 3564 | |
5939 | 埋め込まれた空白を区切文字として、 | |
3565 | 埋め込まれた空白を区切文字として、STRING から抜き出した単語のリストを | |
5940 | 3566 | 評価します。 |
5941 | 3567 | これは、以下の式と大体同じと考えられます: |
5942 | 3568 | |
5943 | split( | |
3569 | split(' ', q/STRING/); | |
5944 | 3570 | |
5945 | 3571 | =begin original |
5946 | 3572 | |
5947 | the differences being that it | |
3573 | the differences being that it generates a real list at compile time, and | |
5948 | generates a real list at compile time, and | |
5949 | 3574 | in scalar context it returns the last element in the list. So |
5950 | 3575 | this expression: |
5951 | 3576 | |
5952 | 3577 | =end original |
5953 | 3578 | |
5954 | 違いは、 | |
3579 | 違いは、実際のリストをコンパイル時に生成し、スカラコンテキストではリストの | |
5955 | ||
3580 | 最後の要素を返すことです。 | |
5956 | 3581 | 従って、以下の表現は: |
5957 | 3582 | |
5958 | 3583 | qw(foo bar baz) |
5959 | 3584 | |
5960 | 3585 | =begin original |
5961 | 3586 | |
5962 | 3587 | is semantically equivalent to the list: |
5963 | 3588 | |
5964 | 3589 | =end original |
5965 | 3590 | |
5966 | 3591 | 以下のリストと文法的に等価です。 |
5967 | 3592 | |
5968 | | |
3593 | 'foo', 'bar', 'baz' | |
5969 | 3594 | |
5970 | 3595 | =begin original |
5971 | 3596 | |
5972 | 3597 | Some frequently seen examples: |
5973 | 3598 | |
5974 | 3599 | =end original |
5975 | 3600 | |
5976 | 3601 | よく行なわれる例としては以下のものです: |
5977 | 3602 | |
5978 | 3603 | use POSIX qw( setlocale localeconv ) |
5979 | 3604 | @EXPORT = qw( foo bar baz ); |
5980 | 3605 | |
5981 | 3606 | =begin original |
5982 | 3607 | |
5983 | A common mistake is to try to separate the words with comma | |
3608 | A common mistake is to try to separate the words with comma or to | |
5984 | 3609 | put comments into a multi-line C<qw>-string. For this reason, the |
5985 | ||
3610 | C<use warnings> pragma and the B<-w> switch (that is, the C<$^W> variable) | |
5986 | produces warnings if the | |
3611 | produces warnings if the STRING contains the "," or the "#" character. | |
5987 | 3612 | |
5988 | 3613 | =end original |
5989 | 3614 | |
5990 | 3615 | よくある間違いは、単語をカンマで区切ったり、複数行の C<qw> 文字列の中に |
5991 | 3616 | コメントを書いたりすることです。 |
5992 | このために、 | |
3617 | このために、C<usr warnings> プラグマと B<-w> スイッチ | |
5993 | (つまり、C<$^W> 変数) は | |
3618 | (つまり、C<$^W> 変数) は STRING に "," や "#" の文字が入っていると | |
5994 | 3619 | 警告を出します。 |
5995 | 3620 | |
5996 | ||
3622 | =item tr/SEARCHLIST/REPLACEMENTLIST/cds | |
5997 | 3623 | X<tr> X<y> X<transliterate> X</c> X</d> X</s> |
5998 | 3624 | |
5999 | =item | |
3625 | =item y/SEARCHLIST/REPLACEMENTLIST/cds | |
6000 | 3626 | |
6001 | 3627 | =begin original |
6002 | 3628 | |
6003 | Transliterates all occurrences of the characters found | |
3629 | Transliterates all occurrences of the characters found in the search list | |
6004 | i | |
3630 | with the corresponding character in the replacement list. It returns | |
6005 | ||
3631 | the number of characters replaced or deleted. If no string is | |
6006 | ||
3632 | specified via the =~ or !~ operator, the $_ string is transliterated. (The | |
6007 | n | |
3633 | string specified with =~ must be a scalar variable, an array element, a | |
6008 | ||
3634 | hash element, or an assignment to one of those, i.e., an lvalue.) | |
6009 | 3635 | |
6010 | 3636 | =end original |
6011 | 3637 | |
6012 | 検索リスト (SEARCHLIST) に含まれる | |
3638 | 検索リスト (SEARCHLIST) に含まれる文字を、対応する置換リスト | |
6013 | ( | |
3639 | (REPLACEMENTLIST) の文字に変換します。 | |
6014 | 置換リスト (REPLACEMENTLIST) の位置的に対応する文字に変換します | |
6015 | (指定された修飾子によっては一部が削除されることもあります)。 | |
6016 | 3640 | 置換または削除が行なわれた、文字数を返します。 |
6017 | ||
3641 | =~ 演算子や =! 演算子で文字列が指定されていなければ、$_ の文字列が | |
6018 | 3642 | 変換されます。 |
3643 | (=~ で指定される文字列は、スカラ変数、配列要素、ハッシュ要素、 | |
3644 | あるいはこれらへの代入式といった左辺値でなければなりません。) | |
6019 | 3645 | |
6020 | 3646 | =begin original |
6021 | 3647 | |
6022 | ||
3648 | A character range may be specified with a hyphen, so C<tr/A-J/0-9/> | |
3649 | does the same replacement as C<tr/ACEGIBDFHJ/0246813579/>. | |
3650 | For B<sed> devotees, C<y> is provided as a synonym for C<tr>. If the | |
3651 | SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST has | |
3652 | its own pair of quotes, which may or may not be bracketing quotes, | |
3653 | e.g., C<tr[A-Z][a-z]> or C<tr(+\-*/)/ABCD/>. | |
6023 | 3654 | |
6024 | 3655 | =end original |
6025 | 3656 | |
6026 | ||
3657 | 文字の範囲はハイフンを使って指定できます。 | |
3658 | C<tr/A-J/0-9/> は C<tr/ACEGIBDFHJ/0246813579/> と同じ置換を行います。 | |
3659 | B<sed> の信仰者のために C<y> が C<tr> の同義語として提供されています。 | |
3660 | SEARCHLIST を括弧類で括った場合には、 | |
3661 | REPLACEMENTLIST 用に、もう一組の区切り文字を用意します。 | |
3662 | これは、括弧類であっても、なくてもかまいません。 | |
3663 | 例: C<tr[A-Z][a-z]> や C<tr(+\-*/)/ABCD/>。 | |
6027 | 3664 | |
6028 | 3665 | =begin original |
6029 | 3666 | |
6030 | ||
3667 | Note that C<tr> does B<not> do regular expression character classes | |
6031 | ||
3668 | such as C<\d> or C<[:lower:]>. The C<tr> operator is not equivalent to | |
6032 | ||
3669 | the tr(1) utility. If you want to map strings between lower/upper | |
6033 | ||
3670 | cases, see L<perlfunc/lc> and L<perlfunc/uc>, and in general consider | |
6034 | s | |
3671 | using the C<s> operator if you need regular expressions. | |
6035 | 3672 | |
6036 | 3673 | =end original |
6037 | 3674 | |
6038 | C< | |
3675 | C<tr> は C<\d> や C<[:lower:]> といった正規表現文字クラスを | |
6039 | ||
3676 | B<使わない> ことに注意してください。 | |
6040 | ||
3677 | C<tr> 演算子は tr(1) ユーティリティと等価ではありません。 | |
6041 | ||
3678 | 文字列の大文字小文字をマップしたい場合は、 | |
6042 | ||
3679 | L<perlfunc/lc> と L<perlfunc/uc> を参照して下さい。 | |
3680 | また正規表現が必要な場合には一般的に C<s> 演算子を使うことを | |
3681 | 考慮してみてください。 | |
6043 | 3682 | |
6044 | 3683 | =begin original |
6045 | 3684 | |
6046 | ||
3685 | Note also that the whole range idea is rather unportable between | |
6047 | ||
3686 | character sets--and even within character sets they may cause results | |
6048 | o | |
3687 | you probably didn't expect. A sound principle is to use only ranges | |
3688 | that begin from and end at either alphabets of equal case (a-e, A-E), | |
3689 | or digits (0-4). Anything else is unsafe. If in doubt, spell out the | |
3690 | character sets in full. | |
6049 | 3691 | |
6050 | 3692 | =end original |
6051 | 3693 | |
6052 | ||
3694 | 範囲指定という考え方は文字セットが異なる場合はやや移植性に欠けることにも | |
6053 | ||
3695 | 注意してください -- そして同じ文字セットでも恐らく期待しているのとは違う | |
6054 | ||
3696 | 結果を引き起こすこともあります。 | |
3697 | 健全な原則としては、範囲の最初と最後をどちらもアルファベット | |
6056 | ||
3698 | (大文字小文字も同じ)(a-e, A-E)にするか、どちらも数字にする(0-4)ことです。 | |
3699 | それ以外は全て安全ではありません。 | |
6058 | The characters delimitting I<SEARCHLIST> and I<REPLACEMENTLIST> | |
6059 | can be any printable character, not just forward slashes. If they | |
6060 | are single quotes (C<tr'I<SEARCHLIST>'I<REPLACEMENTLIST>'>), the only | |
6061 | interpolation is removal of C<\> from pairs of C<\\>; so hyphens are | |
6062 | interpreted literally rather than specifying a character range. | |
6063 | ||
6064 | =end original | |
6065 | ||
6066 | I<SEARCHLIST> と I<REPLACEMENTLIST> を区切る文字は、 | |
6067 | スラッシュだけでなく、任意の表示文字が可能です。 | |
6068 | それがシングルクォート (C<tr'I<SEARCHLIST>'I<REPLACEMENTLIST>'>) の場合、 | |
6069 | C<\\> の組から C<\> を削除する変換のみが行われます; したがって、 | |
6070 | ハイフンは文字の範囲ではなく、リテラルに解釈されます。 | |
6071 | ||
6072 | =begin original | |
6073 | ||
6074 | Otherwise, a character range may be specified with a hyphen, so | |
6075 | C<tr/A-J/0-9/> does the same replacement as | |
6076 | C<tr/ACEGIBDFHJ/0246813579/>. | |
6077 | ||
6078 | =end original | |
6079 | ||
6080 | さもなければ、文字の範囲はハイフンを使って指定できます; C<tr/A-J/0-9/> は | |
6081 | C<tr/ACEGIBDFHJ/0246813579/> と同じ置換を行います。 | |
6082 | ||
6083 | =begin original | |
6084 | ||
6085 | If the I<SEARCHLIST> is delimited by bracketing quotes, the | |
6086 | I<REPLACEMENTLIST> must have its own pair of quotes, which may or may | |
6087 | not be bracketing quotes; for example, C<tr(aeiouy)(yuoiea)> or | |
6088 | C<tr[+\-*/]"ABCD">. This final example shows a way to visually clarify | |
6089 | what is going on for people who are more familiar with regular | |
6090 | expression patterns than with C<tr>, and who may think forward slash | |
6091 | delimiters imply that C<tr> is more like a regular expression pattern | |
6092 | than it actually is. (Another option might be to use C<tr[...][...]>.) | |
6093 | ||
6094 | =end original | |
6095 | ||
6096 | I<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 | ||
6108 | C<tr> isn't fully like bracketed character classes, just | |
6109 | (significantly) more like them than it is to full patterns. For | |
6110 | example, characters appearing more than once in either list behave | |
6111 | differently here than in patterns, and C<tr> lists do not allow | |
6112 | backslashed character classes such as C<\d> or C<\pL>, nor variable | |
6113 | interpolation, so C<"$"> and C<"@"> are always treated as literals. | |
6114 | ||
6115 | =end original | |
6116 | ||
6117 | C<tr> は完全に大かっこ文字クラスと同様ではなく、 | |
6118 | 単に完全なパターンよりは(かなり)そちらにより似ていると言うだけです。 | |
6119 | 例えば、どちらかのリストに複数回文字が現れた場合、パターンと | |
6120 | 異なる振る舞いをします; そして | |
6121 | C<tr> の一覧は C<\d> や C<\pL> といった逆スラッシュ文字クラスや | |
6122 | 変数展開を許さないので、C<"$"> と C<"@"> は | |
6123 | 常にリテラルとして扱われます。 | |
6124 | ||
6125 | =begin original | |
6126 | ||
6127 | The allowed elements are literals plus C<\'> (meaning a single quote). | |
6128 | If the delimiters aren't single quotes, also allowed are any of the | |
6129 | escape sequences accepted in double-quoted strings. Escape sequence | |
6130 | details are in L<the table near the beginning of this section|/Quote and | |
6131 | Quote-like Operators>. | |
6132 | ||
6133 | =end original | |
6134 | ||
6135 | 許される要素はリテラルと (シングルクォートを意味する) C<\'> です。 | |
6136 | 区切り文字がシングルクォートでない場合 | |
6137 | ダブルクォート文字列の中で受け入れられるエスケープシーケンスも | |
6138 | 許されます。 | |
6139 | エスケープシーケンスの詳細は | |
6140 | L<この節の先頭付近の表|/Quote and | |
6141 | Quote-like Operators> にあります。 | |
6142 | ||
6143 | =begin original | |
6144 | ||
6145 | A hyphen at the beginning or end, or preceded by a backslash is also | |
6146 | always considered a literal. Precede a delimiter character with a | |
6147 | backslash to allow it. | |
6148 | ||
6149 | =end original | |
6150 | ||
6151 | 先頭、末尾、および逆スラッシュが前置されたハイフンもまた常に | |
6152 | リテラルとして扱われます。 | |
6153 | それをするために逆スラッシュと共に区切り文字を前に置きます。 | |
6154 | ||
6155 | =begin original | |
6156 | ||
6157 | The C<tr> operator is not equivalent to the C<L<tr(1)>> utility. | |
6158 | C<tr[a-z][A-Z]> will uppercase the 26 letters "a" through "z", but for | |
6159 | case changing not confined to ASCII, use L<C<lc>|perlfunc/lc>, | |
6160 | L<C<uc>|perlfunc/uc>, L<C<lcfirst>|perlfunc/lcfirst>, | |
6161 | L<C<ucfirst>|perlfunc/ucfirst> (all documented in L<perlfunc>), or the | |
6162 | L<substitution operator | |
6163 | C<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 | |
6165 | I<REPLACEMENT> portion). | |
6166 | ||
6167 | =end original | |
6168 | ||
6169 | C<tr> 演算子は C<L<tr(1)>> ユーティリティと等価ではありません。 | |
6170 | C<tr[a-z][A-Z]> は "a" から "z" までの 26 文字を大文字にしますが、 | |
6171 | ASCII の範囲外の大文字小文字を変更する場合は、 | |
6172 | L<C<lc>|perlfunc/lc>, L<C<uc>|perlfunc/uc>, | |
6173 | L<C<lcfirst>|perlfunc/lcfirst>, L<C<ucfirst>|perlfunc/ucfirst> | |
6174 | (全て L<perlfunc> に文書化されています), あるいは | |
6175 | L<置換演算子 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 | ||
6181 | Most ranges are unportable between character sets, but certain ones | |
6182 | signal Perl to do special handling to make them portable. There are two | |
6183 | classes of portable ranges. The first are any subsets of the ranges | |
6184 | C<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 | ||
6197 | capitalizes the letters C<"h">, C<"i">, C<"j">, and C<"k"> and nothing | |
6198 | else, no matter what the platform's character set is. In contrast, all | |
6199 | of | |
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 | ||
6213 | do the same capitalizations as the previous example when run on ASCII | |
6214 | platforms, but something completely different on EBCDIC ones. | |
6215 | ||
6216 | =end original | |
6217 | ||
6218 | ASCII プラットフォームで実行した場合は前述の例と同じ大文字かを行いますが、 | |
6219 | EBCDIC プラットフォームでは全く違うことをします。 | |
6220 | ||
6221 | =begin original | |
6222 | ||
6223 | The second class of portable ranges is invoked when one or both of the | |
6224 | range's end points are expressed as C<\N{...}> | |
6225 | ||
6226 | =end original | |
6227 | ||
6228 | 移植性のある範囲の二つ目のクラスは、範囲の片方または両方の端が | |
6229 | C<\N{...}> として記述された場合です | |
6230 | ||
6231 | $string =~ tr/\N{U+20}-\N{U+7E}//d; | |
6232 | ||
6233 | =begin original | |
6234 | ||
6235 | removes from C<$string> all the platform's characters which are | |
6236 | equivalent to any of Unicode U+0020, U+0021, ... U+007D, U+007E. This | |
6237 | is a portable range, and has the same effect on every platform it is | |
6238 | run on. In this example, these are the ASCII | |
6239 | printable characters. So after this is run, C<$string> has only | |
6240 | controls 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 | ||
6254 | But, even for portable ranges, it is not generally obvious what is | |
6255 | included without having to look things up in the manual. A sound | |
6256 | principle is to use only ranges that both begin from, and end at, either | |
6257 | ASCII alphabetics of equal case (C<b-e>, C<B-E>), or digits (C<1-4>). | |
6258 | Anything else is unclear (and unportable unless C<\N{...}> is used). If | |
6259 | in 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 | 移植性はありません)。 | |
6271 | 3700 | 疑わしいときは、文字セットを完全に書き出してください。 |
6272 | 3701 | |
6273 | 3702 | =begin original |
6274 | 3703 | |
6275 | 3704 | Options: |
6276 | 3705 | |
6277 | 3706 | =end original |
6278 | 3707 | |
6279 | 3708 | オプションは以下の通りです: |
6280 | 3709 | |
6281 | 3710 | =begin original |
6282 | 3711 | |
6283 | 3712 | c Complement the SEARCHLIST. |
6284 | 3713 | d Delete found but unreplaced characters. |
6285 | r Return the modified string and leave the original string | |
6286 | untouched. | |
6287 | 3714 | s Squash duplicate replaced characters. |
6288 | 3715 | |
6289 | 3716 | =end original |
6290 | 3717 | |
6291 | c SEARCHLIST を補集合にする | |
3718 | c SEARCHLIST を補集合にする | |
6292 | d 見つかったが置換されなかった文字を削除する | |
3719 | d 見つかったが置換されなかった文字を削除する | |
6293 | | |
3720 | s 置換された文字が重なったときに圧縮する | |
6294 | s 置換された文字が重なったときに圧縮する。 | |
6295 | 3721 | |
6296 | ||
6297 | 3722 | =begin original |
6298 | 3723 | |
6299 | If the C</ | |
3724 | If the C</c> modifier is specified, the SEARCHLIST character set | |
6300 | ||
3725 | is complemented. If the C</d> modifier is specified, any characters | |
6301 | ||
3726 | specified by SEARCHLIST not found in REPLACEMENTLIST are deleted. | |
6302 | ||
3727 | (Note that this is slightly more flexible than the behavior of some | |
3728 | B<tr> programs, which delete anything they find in the SEARCHLIST, | |
3729 | period.) If the C</s> modifier is specified, sequences of characters | |
3730 | that were transliterated to the same character are squashed down | |
3731 | to a single instance of the character. | |
6303 | 3732 | |
6304 | 3733 | =end original |
6305 | 3734 | |
6306 | C</ | |
3735 | C</c> 修飾子が指定されると、SEARCHLIST には補集合が指定されたものと | |
6307 | ||
3736 | 解釈されます。 | |
6308 | ||
3737 | C</d> 修飾子が指定されると、SEARCHLIST に指定されて、 | |
3738 | REPLACEMENTLIST に対応するものがない文字が削除されます。 | |
3739 | (これは、SEARCHLIST で見つかったものを削除する、ただそれだけの、ある種の | |
6309 | 3740 | B<tr> プログラムの動作よりと比べれば、いく分柔軟なものになっています。) |
6310 | ||
6311 | =begin original | |
6312 | ||
6313 | If the C</s> modifier is specified, sequences of characters, all in a | |
6314 | row, that were transliterated to the same character are squashed down to | |
6315 | a single instance of that character. | |
6316 | ||
6317 | =end original | |
6318 | ||
6319 | 3741 | C</s> 修飾子が指定されると、同じ文字に文字変換された文字の並びを、 |
6320 | その文字 1 文字だけに圧縮します。 | |
3742 | その文字 1 文字だけに圧縮します。 | |
6321 | 3743 | |
6322 | my $a = "aaabbbca"; | |
6323 | $a =~ tr/ab/dd/s; # $a now is "dcd" | |
6324 | ||
6325 | 3744 | =begin original |
6326 | 3745 | |
6327 | If the C</d> modifier is used, the | |
3746 | If the C</d> modifier is used, the REPLACEMENTLIST is always interpreted | |
6328 | exactly as specified. Otherwise, if the | |
3747 | exactly as specified. Otherwise, if the REPLACEMENTLIST is shorter | |
6329 | than the | |
3748 | than the SEARCHLIST, the final character is replicated till it is long | |
6330 | ||
3749 | enough. If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated. | |
6331 | ||
3750 | This latter is useful for counting characters in a class or for | |
6332 | ||
3751 | squashing character sequences in a class. | |
6333 | for counting characters in a class, or for squashing character sequences | |
6334 | in a class. | |
6335 | 3752 | |
6336 | 3753 | =end original |
6337 | 3754 | |
6338 | C</d> 修飾子が使われると、 | |
3755 | C</d> 修飾子が使われると、REPLACEMENTLIST は、常に指定された通りに | |
6339 | 3756 | 解釈されます。 |
6340 | さ | |
3757 | C</d> が指定されない場合で、REPLACEMENTLIST が SEARCHLIST よりも短いと、 | |
6341 | 同じ長さになるまで、最後の文字が | |
3758 | 同じ長さになるまで、REPLACEMENTLIST の最後の文字が | |
6342 | ||
3759 | 繰り返されているものとして扱われます。 | |
6343 | ||
3760 | REPLACEMENTLIST が空文字列でのときには、SEARCHLIST と同じになります。 | |
6344 | ||
3761 | 後者は、ある文字クラスに含まれる文字数を数えるときや、 | |
6345 | 3762 | ある文字クラスの文字の並びを圧縮するようなときに便利です。 |
6346 | 3763 | |
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 | ||
6352 | 3764 | =begin original |
6353 | 3765 | |
6354 | ||
3766 | Examples: | |
6355 | are the ones NOT in I<SEARCHLIST>, that is, it is complemented. If | |
6356 | C</d> and/or C</s> are also specified, they apply to the complemented | |
6357 | I<SEARCHLIST>. Recall, that if I<REPLACEMENTLIST> is empty (except | |
6358 | under C</d>) a copy of I<SEARCHLIST> is used instead. That copy is made | |
6359 | after complementing under C</c>. I<SEARCHLIST> is sorted by code point | |
6360 | order after complementing, and any I<REPLACEMENTLIST> is applied to | |
6361 | that sorted result. This means that under C</c>, the order of the | |
6362 | characters specified in I<SEARCHLIST> is irrelevant. This can | |
6363 | lead to different results on EBCDIC systems if I<REPLACEMENTLIST> | |
6364 | contains more than one character, hence it is generally non-portable to | |
6365 | use C</c> with such a I<REPLACEMENTLIST>. | |
6366 | 3767 | |
6367 | 3768 | =end original |
6368 | 3769 | |
6369 | ||
3770 | 例: | |
6370 | つまり、補集合です。 | |
6371 | C</d> や C</s> も指定されている場合、これらは I<SEARCHLIST> の補集合に | |
6372 | 適用されます。 | |
6373 | I<REPLACEMENTLIST> が空の場合、(C</d> の下でなければ) | |
6374 | 代わりに I<SEARCHLIST> のコピーが使われることを思い出してください。 | |
6375 | このコピーは、C</c> の下の補集合化の後で行われます。 | |
6376 | I<SEARCHLIST> は補集合化の後で符号位置の順序でソートされ、 | |
6377 | I<REPLACEMENTLIST> はソートされた結果に対して適用されます。 | |
6378 | つまり、C</c> の下では、I<SEARCHLIST> で指定された文字の順序は | |
6379 | 無関係です。 | |
6380 | I<REPLACEMENTLIST> に複数の文字が含まれている場合、 | |
6381 | EBCDIC システムでは異なった結果を引き起こすことがあるので、 | |
6382 | そのような I<REPLACEMENTLIST> に対して | |
6383 | C</c> を使うのは一般的に移植性がありません。 | |
6384 | 3771 | |
6385 | = | |
3772 | $ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case | |
6386 | 3773 | |
6387 | ||
3774 | $cnt = tr/*/*/; # count the stars in $_ | |
6388 | If C</c> is specified, the I<SEARCHLIST> is sorted by code point order, | |
6389 | then complemented. If I<REPLACEMENTLIST> is empty and C</d> is not | |
6390 | specified, I<REPLACEMENTLIST> is replaced by a copy of I<SEARCHLIST> (as | |
6391 | modified under C</c>), and these potentially modified lists are used as | |
6392 | the basis for what follows. Any character in the target string that | |
6393 | isn't in I<SEARCHLIST> is passed through unchanged. Every other | |
6394 | character in the target string is replaced by the character in | |
6395 | I<REPLACEMENTLIST> that positionally corresponds to its mate in | |
6396 | I<SEARCHLIST>, except that under C</s>, the 2nd and following characters | |
6397 | are squeezed out in a sequence of characters in a row that all translate | |
6398 | to the same character. If I<SEARCHLIST> is longer than | |
6399 | I<REPLACEMENTLIST>, characters in the target string that match a | |
6400 | character in I<SEARCHLIST> that doesn't have a correspondence in | |
6401 | I<REPLACEMENTLIST> are either deleted from the target string if C</d> is | |
6402 | specified; or replaced by the final character in I<REPLACEMENTLIST> if | |
6403 | C</d> isn't specified. | |
6404 | 3775 | |
6405 | ||
3776 | $cnt = $sky =~ tr/*/*/; # count the stars in $sky | |
6406 | 3777 | |
6407 | ||
3778 | $cnt = tr/0-9//; # count the digits in $_ | |
6408 | C</c> が指定されると、I<SEARCHLIST> は符号位置順にソートされ、 | |
6409 | それから補集合化されます。 | |
6410 | I<REPLACEMENTLIST> が空で C</d> が指定されていないと、 | |
6411 | I<REPLACEMENTLIST> は (C</c> で修正された) I<SEARCHLIST> のコピーに | |
6412 | 置き換えられ、この変更されているかも知れないリストは引き続くものに対しての | |
6413 | 基礎として使われます。 | |
6414 | ターゲット文字列のうち、I<SEARCHLIST> ににない文字は変更されずに | |
6415 | そのままになります。 | |
6416 | ターゲット文字列のその他のそれぞれの文字は、 | |
6417 | I<SEARCHLIST> の中の文字に位置的に対応する、 | |
6418 | I<REPLACEMENTLIST> の中の文字に置き換えられます; | |
6419 | C</s> の下では例外で、 | |
6420 | 連続して同じ文字に変換された文字並びの 2 文字目以降の文字は削除されます。 | |
6421 | I<SEARCHLIST> が I<REPLACEMENTLIST> より長い場合、 | |
6422 | I<SEARCHLIST> の文字のうち I<REPLACEMENTLIST> に対応するものが | |
6423 | ない文字にマッチングしたターゲット文字列の中の文字は、 | |
6424 | C</d> が指定されていればターゲット文字列から削除されます; | |
6425 | C</d> が指定されていなければI<REPLACEMENTLIST> の最後の文字に | |
6426 | 置き換えられます。 | |
6427 | 3779 | |
6428 | ||
3780 | tr/a-zA-Z//s; # bookkeeper -> bokeper | |
6429 | 3781 | |
6430 | So | |
3782 | ($HOST = $host) =~ tr/a-z/A-Z/; | |
6431 | 3783 | |
6432 | ||
3784 | tr/a-zA-Z/ /cs; # change non-alphas to single space | |
6433 | 3785 | |
6434 | ||
3786 | tr [\200-\377] | |
3787 | [\000-\177]; # delete 8th bit | |
6435 | 3788 | |
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 | ||
6476 | 3789 | =begin original |
6477 | 3790 | |
6478 | 3791 | If multiple transliterations are given for a character, only the |
6479 | 3792 | first one is used: |
6480 | 3793 | |
6481 | 3794 | =end original |
6482 | 3795 | |
6483 | 3796 | 複数の文字変換が一つの文字について指定されると、最初のものだけが使われます。 |
6484 | 3797 | |
6485 | tr/AAA/XYZ/ | |
3798 | tr/AAA/XYZ/ | |
6486 | 3799 | |
6487 | 3800 | =begin original |
6488 | 3801 | |
6489 | 3802 | will transliterate any A to X. |
6490 | 3803 | |
6491 | 3804 | =end original |
6492 | 3805 | |
6493 | 3806 | は A を X に変換します。 |
6494 | 3807 | |
6495 | 3808 | =begin original |
6496 | 3809 | |
6497 | 3810 | Because the transliteration table is built at compile time, neither |
6498 | the | |
3811 | the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote | |
6499 | 3812 | interpolation. That means that if you want to use variables, you |
6500 | must use an | |
3813 | must use an eval(): | |
6501 | 3814 | |
6502 | 3815 | =end original |
6503 | 3816 | |
6504 | 変換テーブルはコンパイル時に作られるので、 | |
3817 | 変換テーブルはコンパイル時に作られるので、SEARCHLIST も | |
6505 | ||
3818 | REPLACEMENTLIST もダブルクォート展開の対象とはなりません。 | |
6506 | 変数を使いたい場合には、 | |
3819 | 変数を使いたい場合には、eval() を使わなければならないということです: | |
6507 | 3820 | |
6508 | eval "tr/$oldlist/$newlist/"; | |
3821 | eval "tr/$oldlist/$newlist/"; | |
6509 | die $@ if $@; | |
3822 | die $@ if $@; | |
6510 | 3823 | |
6511 | eval "tr/$oldlist/$newlist/, 1" or die $@; | |
3824 | eval "tr/$oldlist/$newlist/, 1" or die $@; | |
6512 | 3825 | |
6513 | =item | |
3826 | =item <<EOF | |
6514 | 3827 | X<here-doc> X<heredoc> X<here-document> X<<< << >>> |
6515 | 3828 | |
6516 | 3829 | =begin original |
6517 | 3830 | |
6518 | 3831 | A line-oriented form of quoting is based on the shell "here-document" |
6519 | 3832 | syntax. Following a C<< << >> you specify a string to terminate |
6520 | 3833 | the quoted material, and all lines following the current line down to |
6521 | 3834 | the terminating string are the value of the item. |
6522 | 3835 | |
6523 | 3836 | =end original |
6524 | 3837 | |
6525 | 3838 | クォートの行指向形式は、シェルの「ヒアドキュメント」構文を基にしています。 |
6526 | 3839 | C<< << >> に引き続いて、クォートされるテキストを終了させる文字列を指定でき、 |
6527 | 3840 | 現在の行の次の行から終端文字列までの全ての行がその項目の値となります。 |
6528 | 3841 | |
6529 | 3842 | =begin original |
6530 | 3843 | |
6531 | Prefixing the terminating string with a C<~> specifies that you | |
6532 | want 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 | ||
6541 | 3844 | The terminating string may be either an identifier (a word), or some |
6542 | 3845 | quoted text. An unquoted identifier works like double quotes. |
6543 | 3846 | There may not be a space between the C<< << >> and the identifier, |
6544 | unless the identifier is explicitly quoted. | |
3847 | unless the identifier is explicitly quoted. (If you put a space it | |
6545 | ||
3848 | will be treated as a null identifier, which is valid, and matches the | |
6546 | ||
3849 | first empty line.) The terminating string must appear by itself | |
3850 | (unquoted and with no surrounding whitespace) on the terminating line. | |
6547 | 3851 | |
6548 | 3852 | =end original |
6549 | 3853 | |
6550 | 3854 | 終端文字列には、識別子(単語) かクォートされたテキストが使えます。 |
6551 | 3855 | クォートされていない識別子は、ダブルクォートのように扱われます。 |
6552 | 3856 | 識別子がクォートされていない場合は、C<< << >> と識別子の間に |
6553 | 3857 | 空白を入れてはいけません。 |
3858 | (もし空白を入れると、空識別子として扱われます; これは有効で、 | |
3859 | 最初の空行にマッチするようになります。) | |
6554 | 3860 | 終端文字列は終端行に単体で(クォートされず、前後にも空白なしで) |
6555 | 3861 | 現れなければなりません。 |
6556 | 3862 | |
6557 | 3863 | =begin original |
6558 | 3864 | |
6559 | 3865 | If the terminating string is quoted, the type of quotes used determine |
6560 | 3866 | the treatment of the text. |
6561 | 3867 | |
6562 | 3868 | =end original |
6563 | 3869 | |
6564 | 3870 | 終端文字列がクォートされている場合には、そのクォートの種類によって、 |
6565 | 3871 | クォートされるテキストの扱い決められます。 |
6566 | 3872 | |
6567 | 3873 | =over 4 |
6568 | 3874 | |
6569 | 3875 | =item Double Quotes |
6570 | 3876 | |
6571 | (ダブルクォート) | |
6572 | ||
6573 | 3877 | =begin original |
6574 | 3878 | |
6575 | 3879 | Double quotes indicate that the text will be interpolated using exactly |
6576 | 3880 | the same rules as normal double quoted strings. |
6577 | 3881 | |
6578 | 3882 | =end original |
6579 | 3883 | |
6580 | 3884 | ダブルクォートは、通常のダブルクォートされた文字列と全く同じ規則を使って |
6581 | 3885 | 変数展開されることを示します。 |
6582 | 3886 | |
6583 | 3887 | print <<EOF; |
6584 | 3888 | The price is $Price. |
6585 | 3889 | EOF |
6586 | 3890 | |
6587 | 3891 | print << "EOF"; # same as above |
6588 | 3892 | The price is $Price. |
6589 | 3893 | EOF |
6590 | 3894 | |
6591 | 3895 | =item Single Quotes |
6592 | 3896 | |
6593 | (シングルクォート) | |
6594 | ||
6595 | 3897 | =begin original |
6596 | 3898 | |
6597 | 3899 | Single quotes indicate the text is to be treated literally with no |
6598 | interpolation of its content. | |
3900 | interpolation of its content. This is similar to single quoted | |
6599 | 3901 | strings except that backslashes have no special meaning, with C<\\> |
6600 | 3902 | being treated as two backslashes and not one as they would in every |
6601 | 3903 | other quoting construct. |
6602 | 3904 | |
6603 | 3905 | =end original |
6604 | 3906 | |
6605 | シングルクォートは、内容が展開されずにリテラルに扱われることを | |
3907 | シングルクォートは、内容が展開されずにリテラルに扱われることを | |
3908 | 意味します。 | |
6606 | 3909 | これはシングルクォート文字列と似ていますが、バックスラッシュは |
6607 | 3910 | 特別な意味を持たず、他の全てのクォート構造違って、C<\\> は |
6608 | 3911 | 二つのバックスラッシュとして扱われることが違います。 |
6609 | 3912 | |
6610 | 3913 | =begin original |
6611 | 3914 | |
6612 | Just as in the shell, a backslashed bareword following the C<<< << >>> | |
6613 | means 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 | ||
6630 | 3915 | This is the only form of quoting in perl where there is no need |
6631 | 3916 | to worry about escaping content, something that code generators |
6632 | 3917 | can and do make good use of. |
6633 | 3918 | |
6634 | 3919 | =end original |
6635 | 3920 | |
6636 | 3921 | これは、perl において、内容のエスケープについて心配する必要のない |
6637 | 3922 | 唯一の形で、コードジェネレータがうまく使えるものです。 |
6638 | 3923 | |
6639 | 3924 | =item Backticks |
6640 | 3925 | |
6641 | (逆スラッシュ) | |
6642 | ||
6643 | 3926 | =begin original |
6644 | 3927 | |
6645 | 3928 | The content of the here doc is treated just as it would be if the |
6646 | string were embedded in backticks. | |
3929 | string were embedded in backticks. Thus the content is interpolated | |
6647 | 3930 | as though it were double quoted and then executed via the shell, with |
6648 | 3931 | the results of the execution returned. |
6649 | 3932 | |
6650 | 3933 | =end original |
6651 | 3934 | |
6652 | ヒ | |
3935 | ヒヤドキュメントのの内容は、文字列がバッククォートで | |
6653 | 3936 | 埋め込まれているかのように扱われます。 |
6654 | 3937 | したがって、その内容、はダブルクォートされているかのように変数展開され、 |
6655 | 3938 | その後シェル経由で実行され、実行された結果になります。 |
6656 | 3939 | |
6657 | 3940 | print << `EOC`; # execute command and get results |
6658 | 3941 | echo hi there |
6659 | 3942 | EOC |
6660 | 3943 | |
6661 | 3944 | =back |
6662 | 3945 | |
6663 | =over 4 | |
6664 | ||
6665 | =item Indented Here-docs | |
6666 | ||
6667 | 3946 | =begin original |
6668 | 3947 | |
6669 | The here-doc modifier C<~> allows you to indent your here-docs to make | |
6670 | the 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 | ||
6685 | This 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 | ||
6703 | The line containing the delimiter that marks the end of the here-doc | |
6704 | determines the indentation template for the whole thing. Compilation | |
6705 | croaks if any non-empty line inside the here-doc does not begin with the | |
6706 | precise indentation of the terminating line. (An empty line consists of | |
6707 | the single character "\n".) For example, suppose the terminating line | |
6708 | begins with a tab character followed by 4 space characters. Every | |
6709 | non-empty line in the here-doc must begin with a tab followed by 4 | |
6710 | spaces. They are stripped from each line, and any leading white space | |
6711 | remaining on a line serves as the indentation for that line. Currently, | |
6712 | only the TAB and SPACE characters are treated as whitespace for this | |
6713 | purpose. Tabs and spaces may be mixed, but are matched exactly; tabs | |
6714 | remain 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 | ||
6736 | Additional beginning whitespace (beyond what preceded the | |
6737 | delimiter) 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 | ||
6751 | Finally, the modifier may be used with all of the forms | |
6752 | mentioned above: | |
6753 | ||
6754 | =end original | |
6755 | ||
6756 | 最後に、この修飾子は前述した全ての型式で使うことが出来ます: | |
6757 | ||
6758 | <<~\EOF; | |
6759 | <<~'EOF' | |
6760 | <<~"EOF" | |
6761 | <<~`EOF` | |
6762 | ||
6763 | =begin original | |
6764 | ||
6765 | And 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 | ||
6777 | 3948 | It is possible to stack multiple here-docs in a row: |
6778 | 3949 | |
6779 | 3950 | =end original |
6780 | 3951 | |
6781 | 複数のヒ | |
3952 | 複数のヒヤドキュメントを連続してスタックすることも可能です: | |
6782 | 3953 | |
6783 | 3954 | print <<"foo", <<"bar"; # you can stack them |
6784 | 3955 | I said foo. |
6785 | 3956 | foo |
6786 | 3957 | I said bar. |
6787 | 3958 | bar |
6788 | 3959 | |
6789 | 3960 | myfunc(<< "THIS", 23, <<'THAT'); |
6790 | 3961 | Here's a line |
6791 | 3962 | or two. |
6792 | 3963 | THIS |
6793 | 3964 | and here's another. |
6794 | 3965 | THAT |
6795 | 3966 | |
6796 | 3967 | =begin original |
6797 | 3968 | |
6798 | 3969 | Just don't forget that you have to put a semicolon on the end |
6799 | 3970 | to finish the statement, as Perl doesn't know you're not going to |
6800 | 3971 | try to do this: |
6801 | 3972 | |
6802 | 3973 | =end original |
6803 | 3974 | |
6804 | 3975 | 以下のようなことをしたいのではないということが Perl にはわからないので、 |
6805 | 3976 | 文を終わらせるためには末尾にセミコロンをつけなければならないことを |
6806 | 3977 | 忘れないで下さい: |
6807 | 3978 | |
6808 | 3979 | print <<ABC |
6809 | 3980 | 179231 |
6810 | 3981 | ABC |
6811 | 3982 | + 20; |
6812 | 3983 | |
6813 | 3984 | =begin original |
6814 | 3985 | |
6815 | 3986 | If you want to remove the line terminator from your here-docs, |
6816 | 3987 | use C<chomp()>. |
6817 | 3988 | |
6818 | 3989 | =end original |
6819 | 3990 | |
6820 | ヒ | |
3991 | ヒヤドキュメントから行終端子を除去したい場合は、C<chomp()> を使ってください。 | |
6821 | 3992 | |
6822 | 3993 | chomp($string = <<'END'); |
6823 | 3994 | This is a string. |
6824 | 3995 | END |
6825 | 3996 | |
6826 | 3997 | =begin original |
6827 | 3998 | |
6828 | 3999 | If you want your here-docs to be indented with the rest of the code, |
6829 | u | |
4000 | you'll need to remove leading whitespace from each line manually: | |
6830 | 4001 | |
6831 | 4002 | =end original |
6832 | 4003 | |
6833 | 4004 | ヒアドキュメントをソースのほかの部分からインデントしたい場合、 |
6834 | ||
4005 | 各行の先頭の空白は手動で取り除く必要があります: | |
6835 | 4006 | |
6836 | $quote = << | |
4007 | ($quote = <<'FINIS') =~ s/^\s+//gm; | |
6837 | 4008 | The Road goes ever on and on, |
6838 | 4009 | down from the door where it began. |
6839 | | |
4010 | FINIS | |
6840 | 4011 | |
6841 | 4012 | =begin original |
6842 | 4013 | |
6843 | 4014 | If you use a here-doc within a delimited construct, such as in C<s///eg>, |
6844 | the quoted material must | |
4015 | the quoted material must come on the lines following the final delimiter. | |
6845 | ||
4016 | So instead of | |
6846 | construct: | |
6847 | 4017 | |
6848 | 4018 | =end original |
6849 | 4019 | |
6850 | 4020 | C<s///eg> のようなデリミタ構造の中でヒアドキュメントを使う場合、 |
6851 | クォートされたものは | |
4021 | クォートされたものは最後のデリミタに引き続くものとして来なければなりません。 | |
6852 | ||
4022 | 従って、以下のようではなく: | |
6853 | 4023 | |
6854 | 4024 | s/this/<<E . 'that' |
6855 | 4025 | the other |
6856 | 4026 | E |
6857 | 4027 | . 'more '/eg; |
6858 | 4028 | |
6859 | 4029 | =begin original |
6860 | 4030 | |
6861 | ||
4031 | you have to write | |
6862 | you would have to write | |
6863 | 4032 | |
6864 | 4033 | =end original |
6865 | 4034 | |
6866 | ||
4035 | 以下のように書かなければなりません: | |
6867 | 歴史的には、これは一貫性がなく、以下のように | |
6868 | 4036 | |
6869 | 4037 | s/this/<<E . 'that' |
6870 | 4038 | . 'more '/eg; |
6871 | 4039 | the other |
6872 | 4040 | E |
6873 | 4041 | |
6874 | 4042 | =begin original |
6875 | 4043 | |
6876 | ||
4044 | If the terminating identifier is on the last line of the program, you | |
4045 | must be sure there is a newline after it; otherwise, Perl will give the | |
4046 | warning B<Can't find string terminator "END" anywhere before EOF...>. | |
6877 | 4047 | |
6878 | 4048 | =end original |
6879 | 4049 | |
6880 | ||
4050 | プログラムの最後の行に終端識別子がある場合、その後には | |
4051 | 改行がなければなりません; さもなければ、Perl は | |
4052 | B<Can't find string terminator "END" anywhere before EOF...> という | |
4053 | 警告を出します。 | |
6881 | 4054 | |
6882 | 4055 | =begin original |
6883 | 4056 | |
6884 | Additionally, quoting rules for the end | |
4057 | Additionally, the quoting rules for the end of string identifier are not | |
6885 | ||
4058 | related to Perl's quoting rules -- C<q()>, C<qq()>, and the like are not | |
6886 | 4059 | supported in place of C<''> and C<"">, and the only interpolation is for |
6887 | 4060 | backslashing the quoting character: |
6888 | 4061 | |
6889 | 4062 | =end original |
6890 | 4063 | |
6891 | 4064 | さらに、文字列終端識別子に対するクォートルールは Perl のクォートルールとは |
6892 | 関係がありません | |
4065 | 関係がありません -- C<q()>, C<qq()> および同種のものは C<''> や C<""> の | |
6893 | C<q()>, C<qq()> および同種のものは C<''> や C<""> の | |
6894 | 4066 | 代わりのなるものには対応しておらず、文字をクォートするための |
6895 | 4067 | バックスラッシュだけが展開されます: |
6896 | 4068 | |
6897 | 4069 | print << "abc\"def"; |
6898 | 4070 | testing... |
6899 | 4071 | abc"def |
6900 | 4072 | |
6901 | 4073 | =begin original |
6902 | 4074 | |
6903 | 4075 | Finally, quoted strings cannot span multiple lines. The general rule is |
6904 | 4076 | that the identifier must be a string literal. Stick with that, and you |
6905 | 4077 | should be safe. |
6906 | 4078 | |
6907 | 4079 | =end original |
6908 | 4080 | |
6909 | 4081 | 最後に、クォートされた文字列は複数行にかかることはありません。 |
6910 | 4082 | 識別子に関する一般的なルールは、文字列リテラルでなければならいことです。 |
6911 | 4083 | これに従っていれば、安全のはずです。 |
6912 | 4084 | |
6913 | 4085 | =back |
6914 | 4086 | |
6915 | 4087 | =head2 Gory details of parsing quoted constructs |
6916 | 4088 | X<quote, gory details> |
6917 | 4089 | |
6918 | 4090 | (クォートされた構造のパースに関する詳細) |
6919 | 4091 | |
6920 | 4092 | =begin original |
6921 | 4093 | |
6922 | 4094 | When presented with something that might have several different |
6923 | 4095 | interpretations, Perl uses the B<DWIM> (that's "Do What I Mean") |
6924 | 4096 | principle to pick the most probable interpretation. This strategy |
6925 | 4097 | is so successful that Perl programmers often do not suspect the |
6926 | 4098 | ambivalence of what they write. But from time to time, Perl's |
6927 | 4099 | notions differ substantially from what the author honestly meant. |
6928 | 4100 | |
6929 | 4101 | =end original |
6930 | 4102 | |
6931 | 4103 | 何か複数の解釈が可能な表現があった場合、Perl は最も確からしい解釈を |
6932 | 4104 | 選択するために B<DWIM> ("Do What I Mean")原則を使います。 |
6933 | 4105 | この戦略は非常に成功したので、Perl プログラマはしばしば |
6934 | 4106 | 自分が書いたものの矛盾を疑いません。 |
6935 | 4107 | しかし時間がたつにつれて、Perl の概念は作者が本当に意味していたものから |
6936 | 4108 | かなり変わりました。 |
6937 | 4109 | |
6938 | 4110 | =begin original |
6939 | 4111 | |
6940 | 4112 | This section hopes to clarify how Perl handles quoted constructs. |
6941 | 4113 | Although the most common reason to learn this is to unravel labyrinthine |
6942 | 4114 | regular expressions, because the initial steps of parsing are the |
6943 | 4115 | same for all quoting operators, they are all discussed together. |
6944 | 4116 | |
6945 | 4117 | =end original |
6946 | 4118 | |
6947 | 4119 | この章では Perl がどのようにクォートされた構造を扱うかを |
6948 | 4120 | 明確にしようと思います。 |
6949 | 4121 | これを学ぼうとする最もよくある理由は正規表現の迷宮をほぐすためですが、 |
6950 | 4122 | パースの初期ステップは全てのクォート演算子で同じなので、全て同時に扱います。 |
6951 | 4123 | |
6952 | 4124 | =begin original |
6953 | 4125 | |
6954 | 4126 | The most important Perl parsing rule is the first one discussed |
6955 | 4127 | below: when processing a quoted construct, Perl first finds the end |
6956 | 4128 | of that construct, then interprets its contents. If you understand |
6957 | 4129 | this rule, you may skip the rest of this section on the first |
6958 | 4130 | reading. The other rules are likely to contradict the user's |
6959 | 4131 | expectations much less frequently than this first one. |
6960 | 4132 | |
6961 | 4133 | =end original |
6962 | 4134 | |
6963 | 4135 | Perl のパースに関するルールで最も重要なものは以下で述べているうち |
6964 | 最初のものです | |
4136 | 最初のものです。 | |
6965 | 構造 | |
4137 | つまり、クォートされた構造を処理するときは、Perl はまずその構造の | |
4138 | 最後を探して、それから中身を解釈します。 | |
6966 | 4139 | このルールがわかれば、とりあえずはこの章の残りは読み飛ばしてもかまいません。 |
6967 | 4140 | その他のルールは最初のルールに比べてユーザーの予想に反する頻度は |
6968 | 4141 | はるかに少ないです。 |
6969 | 4142 | |
6970 | 4143 | =begin original |
6971 | 4144 | |
6972 | 4145 | Some passes discussed below are performed concurrently, but because |
6973 | 4146 | their results are the same, we consider them individually. For different |
6974 | 4147 | quoting constructs, Perl performs different numbers of passes, from |
6975 | 4148 | one to four, but these passes are always performed in the same order. |
6976 | 4149 | |
6977 | 4150 | =end original |
6978 | 4151 | |
6979 | 4152 | 以下で議論するパスには同時に実行されるものもありますが、 |
6980 | 4153 | 結果は同じことなので、別々に考えることにします。 |
6981 | 4154 | クォート構造の種類によって、Perl が実行するパスの数は |
6982 | 4155 | 1 から 4 まで異なりますが、これらのパスは常に同じ順番で実行されます。 |
6983 | 4156 | |
6984 | 4157 | =over 4 |
6985 | 4158 | |
6986 | 4159 | =item Finding the end |
6987 | 4160 | |
6988 | 4161 | (最後を探す) |
6989 | 4162 | |
6990 | 4163 | =begin original |
6991 | 4164 | |
6992 | The first pass is finding the end of the quoted construct | |
4165 | The first pass is finding the end of the quoted construct, where | |
6993 | ||
4166 | the information about the delimiters is used in parsing. | |
6994 | ||
4167 | During this search, text between the starting and ending delimiters | |
6995 | ||
4168 | is copied to a safe location. The text copied gets delimiter-independent. | |
6996 | 4169 | |
6997 | 4170 | =end original |
6998 | 4171 | |
6999 | 最初のパスは、クォート構造の末尾を探 | |
4172 | 最初のパスは、クォート構造の末尾を探して、パース中に使うデリミタの情報が | |
7000 | こ | |
4173 | どこかを探すものです。 | |
7001 | ||
4174 | 検索中に、開始デリミタと終了デリミタの間のテキストは安全な場所に | |
4175 | コピーされます。 | |
4176 | コピーされたテキストはデリミタに依存しません。 | |
7002 | 4177 | |
7003 | 4178 | =begin original |
7004 | 4179 | |
7005 | 4180 | If the construct is a here-doc, the ending delimiter is a line |
7006 | that has a terminating string as the content. | |
4181 | that has a terminating string as the content. Therefore C<<<EOF> is | |
7007 | 4182 | terminated by C<EOF> immediately followed by C<"\n"> and starting |
7008 | 4183 | from the first column of the terminating line. |
7009 | 4184 | When searching for the terminating line of a here-doc, nothing |
7010 | is skipped. | |
4185 | is skipped. In other words, lines after the here-doc syntax | |
7011 | 4186 | are compared with the terminating string line by line. |
7012 | 4187 | |
7013 | 4188 | =end original |
7014 | 4189 | |
7015 | 構造がヒ | |
4190 | 構造がヒヤドキュメントの場合、終了デリミタは内容として終端文字列を持つ | |
7016 | 4191 | 行です。 |
7017 | 4192 | 従って、C<<<EOF> は、C<"\n"> の直後の、終端行の最初の列から始まる C<EOF> で |
7018 | 4193 | 終端します。 |
7019 | ヒ | |
4194 | ヒヤドキュメントの終端行を探すとき、読み飛ばされるものはありません。 | |
7020 | 言い換えると、ヒ | |
4195 | 言い換えると、ヒヤドキュメント文法以降の行は、1 行毎に終端文字列と | |
7021 | 4196 | 比較されます。 |
7022 | 4197 | |
7023 | 4198 | =begin original |
7024 | 4199 | |
7025 | 4200 | For the constructs except here-docs, single characters are used as starting |
7026 | and ending delimiters. | |
4201 | and ending delimiters. If the starting delimiter is an opening punctuation | |
7027 | 4202 | (that is C<(>, C<[>, C<{>, or C<< < >>), the ending delimiter is the |
7028 | 4203 | corresponding closing punctuation (that is C<)>, C<]>, C<}>, or C<< > >>). |
7029 | 4204 | If the starting delimiter is an unpaired character like C</> or a closing |
7030 | punctuation, the ending delimiter is | |
4205 | punctuation, the ending delimiter is same as the starting delimiter. | |
7031 | 4206 | Therefore a C</> terminates a C<qq//> construct, while a C<]> terminates |
7032 | ||
4207 | C<qq[]> and C<qq]]> constructs. | |
7033 | 4208 | |
7034 | 4209 | =end original |
7035 | 4210 | |
7036 | ヒ | |
4211 | ヒヤドキュメント以外の構造では、1 文字が開始デリミタと終了デリミタとして | |
7037 | 4212 | 使われます。 |
7038 | 4213 | 開始デリミタが組の開き文字(C<(>, C<[>, C<{>, C<< < >> のいずれか)の場合、 |
7039 | 4214 | 終了デリミタは対応する組の閉じ文字(つまり C<)>, C<]>, C<}>, C<< > >>) です。 |
7040 | 4215 | 開始デリミタが C</> 組になる文字や、組の閉じ文字の場合、終了デリミタは |
7041 | 4216 | 開始デリミタと同じです。 |
7042 | 従って C</> は C<qq//> 構造を終端し、一方 C<]> は C<qq[]> と C<qq]]> | |
4217 | 従って C</> は C<qq//> 構造を終端し、一方 C<]> は C<qq[]> と C<qq]]> 構造を | |
7043 | ||
4218 | 終端します。 | |
7044 | 4219 | |
7045 | 4220 | =begin original |
7046 | 4221 | |
7047 | 4222 | When searching for single-character delimiters, escaped delimiters |
7048 | and C<\\> are skipped. | |
4223 | and C<\\> are skipped. For example, while searching for terminating C</>, | |
7049 | 4224 | combinations of C<\\> and C<\/> are skipped. If the delimiters are |
7050 | 4225 | bracketing, nested pairs are also skipped. For example, while searching |
7051 | for | |
4226 | for closing C<]> paired with the opening C<[>, combinations of C<\\>, C<\]>, | |
7052 | 4227 | and C<\[> are all skipped, and nested C<[> and C<]> are skipped as well. |
7053 | 4228 | However, when backslashes are used as the delimiters (like C<qq\\> and |
7054 | 4229 | C<tr\\\>), nothing is skipped. |
7055 | During the search for the end, backslashes that escape delimiters | |
4230 | During the search for the end, backslashes that escape delimiters | |
7056 | ||
4231 | are removed (exactly speaking, they are not copied to the safe location). | |
7057 | safe location). | |
7058 | 4232 | |
7059 | 4233 | =end original |
7060 | 4234 | |
7061 | 4235 | 1 文字のデリミタを探す場合、エスケープされたデリミタと C<\\> は |
7062 | 4236 | 読み飛ばします。 |
7063 | 4237 | 例えば、C</> を探しているときには、C<\\> と C<\/> の組み合わせを |
7064 | 4238 | 読み飛ばします。 |
7065 | 4239 | デリミタがかっこでくくられている場合は、ネストした組も読み飛ばされます。 |
7066 | 4240 | 例えば、開きの C<[> と組になる閉じの C<]> を探しているときには、 |
7067 | 4241 | C<\\>, C<\]>, C<\[> の組み合わせをは全て読み飛ばし、 |
7068 | 4242 | さらにネストした C<[>, C<]> も読み飛ばします。 |
7069 | 4243 | しかし、(C<qq\\> や C<tr\\\> のように)バックスラッシュがデリミタとして |
7070 | 4244 | 使われた場合、何も読み飛ばしません。 |
7071 | 末尾の検索中、デリミタ | |
4245 | 末尾の検索中、デリミタをエスケープするバックスラッシュは除去されます | |
7072 | ||
4246 | (述べたとおりに正確に、安全な場所にコピーはされません)。 | |
7073 | コピーはされません)。 | |
7074 | 4247 | |
7075 | 4248 | =begin original |
7076 | 4249 | |
7077 | 4250 | For constructs with three-part delimiters (C<s///>, C<y///>, and |
7078 | 4251 | C<tr///>), the search is repeated once more. |
7079 | If the first delimiter is not an opening punctuation, th | |
4252 | If the first delimiter is not an opening punctuation, three delimiters must | |
7080 | be | |
4253 | be same such as C<s!!!> and C<tr)))>, in which case the second delimiter | |
7081 | in which case the second delimiter | |
7082 | 4254 | terminates the left part and starts the right part at once. |
7083 | If the left part is delimited by bracketing punctuation (that is C<()>, | |
4255 | If the left part is delimited by bracketing punctuations (that is C<()>, | |
7084 | 4256 | C<[]>, C<{}>, or C<< <> >>), the right part needs another pair of |
7085 | delimiters such as C<s(){}> and C<tr[]//>. In these cases, whitespace | |
4257 | delimiters such as C<s(){}> and C<tr[]//>. In these cases, whitespaces | |
7086 | and comments are allowed between th | |
4258 | and comments are allowed between both parts, though the comment must follow | |
7087 | at least one whitespace | |
4259 | at least one whitespace; otherwise a character expected as the start of | |
7088 | ||
4260 | the comment may be regarded as the starting delimiter of the right part. | |
7089 | 4261 | |
7090 | 4262 | =end original |
7091 | 4263 | |
7092 | 4264 | 3 つのデリミタからなる構造 (C<s///>, C<y///>, C<tr///>) の場合、 |
7093 | 4265 | 検索はもう一度繰り返されます。 |
7094 | 4266 | 最初のデリミタが開きかっこでない場合、C<s!!!> and C<tr)))> のように |
7095 | 4267 | 3 つのデリミタは同じでなければなりません; |
7096 | 4268 | この場合、2 番目のデリミタが左側の終端と右側の開始を同時に行います。 |
7097 | 4269 | 左側のデリミタがかっこを構成するもの (これは C<()>, |
7098 | 4270 | C<[]>, C<{}>, C<< <> >> のいずれか) の場合、右側も |
7099 | 4271 | C<s(){}> や C<tr[]//> のようなデリミタである必要があります。 |
7100 | 4272 | これらの場合、右側と左側の間には空白やコメントを置けますが、 |
7101 | コメントは少なくとも一つの空白 | |
4273 | コメントは少なくとも一つの空白の後である必要があります; | |
7102 | 4274 | さもなければコメントの開始文字が右側の開始デリミタとして扱われてしまいます。 |
7103 | 4275 | |
7104 | 4276 | =begin original |
7105 | 4277 | |
7106 | 4278 | During this search no attention is paid to the semantics of the construct. |
7107 | 4279 | Thus: |
7108 | 4280 | |
7109 | 4281 | =end original |
7110 | 4282 | |
7111 | 4283 | 検索する間、構造の文脈は考慮しません。 |
7112 | 4284 | 従って: |
7113 | 4285 | |
7114 | 4286 | "$hash{"$foo/$bar"}" |
7115 | 4287 | |
7116 | 4288 | =begin original |
7117 | 4289 | |
7118 | 4290 | or: |
7119 | 4291 | |
7120 | 4292 | =end original |
7121 | 4293 | |
7122 | 4294 | または: |
7123 | 4295 | |
7124 | 4296 | m/ |
7125 | 4297 | bar # NOT a comment, this slash / terminated m//! |
7126 | 4298 | /x |
7127 | 4299 | |
7128 | 4300 | =begin original |
7129 | 4301 | |
7130 | 4302 | do not form legal quoted expressions. The quoted part ends on the |
7131 | 4303 | first C<"> and C</>, and the rest happens to be a syntax error. |
7132 | 4304 | Because the slash that terminated C<m//> was followed by a C<SPACE>, |
7133 | 4305 | the example above is not C<m//x>, but rather C<m//> with no C</x> |
7134 | 4306 | modifier. So the embedded C<#> is interpreted as a literal C<#>. |
7135 | 4307 | |
7136 | 4308 | =end original |
7137 | 4309 | |
7138 | 4310 | は正しいクォート表現ではありません。 |
7139 | 4311 | クォートは最初の C<"> や C</> で終わりとなり、残りの部分は文法エラーと |
7140 | 4312 | なります。 |
7141 | 4313 | C<m//> を終わらせているスラッシュの次に来ているのが C<空白> なので、 |
7142 | 4314 | 上の例では C<m//x> ではなく、C</x> なしの C<m//> となります。 |
7143 | 4315 | 従って、中にある C<#> はリテラルな C<#> として扱われます。 |
7144 | 4316 | |
7145 | 4317 | =begin original |
7146 | 4318 | |
7147 | 4319 | Also no attention is paid to C<\c\> (multichar control char syntax) during |
7148 | this search. | |
4320 | this search. Thus the second C<\> in C<qq/\c\/> is interpreted as a part | |
7149 | 4321 | of C<\/>, and the following C</> is not recognized as a delimiter. |
7150 | 4322 | Instead, use C<\034> or C<\x1c> at the end of quoted constructs. |
7151 | 4323 | |
7152 | 4324 | =end original |
7153 | 4325 | |
7154 | 4326 | この検索の間、C<\c\> (マルチバイト文字制御文法)に注意は払われません。 |
7155 | 4327 | 従って、C<qq/\c\/> の 2 番目の C<\> は C<\/> の一部として扱われ、 |
7156 | 4328 | 引き続く C</> はデリミタとして認識されません。 |
7157 | 4329 | 代わりに、クォート構造の末尾に C<\034> か C<\x1c> を使ってください。 |
7158 | 4330 | |
7159 | 4331 | =item Interpolation |
7160 | 4332 | X<interpolation> |
7161 | 4333 | |
7162 | 4334 | (展開) |
7163 | 4335 | |
7164 | 4336 | =begin original |
7165 | 4337 | |
7166 | 4338 | The next step is interpolation in the text obtained, which is now |
7167 | 4339 | delimiter-independent. There are multiple cases. |
7168 | 4340 | |
7169 | 4341 | =end original |
7170 | 4342 | |
7171 | 4343 | 次のステップは、得られた(デリミタに依存しない)テキストに対する展開です。 |
7172 | 4344 | 複数のケースがあります。 |
7173 | 4345 | |
7174 | 4346 | =over 4 |
7175 | 4347 | |
7176 | 4348 | =item C<<<'EOF'> |
7177 | 4349 | |
7178 | 4350 | =begin original |
7179 | 4351 | |
7180 | 4352 | No interpolation is performed. |
7181 | 4353 | Note that the combination C<\\> is left intact, since escaped delimiters |
7182 | 4354 | are not available for here-docs. |
7183 | 4355 | |
7184 | 4356 | =end original |
7185 | 4357 | |
7186 | 4358 | 展開は行われません。 |
7187 | 4359 | C<\\> の組み合わせはそのままであることに注意してください; |
7188 | ヒ | |
4360 | ヒヤドキュメントではデリミタのエスケープはできないからです。 | |
7189 | 4361 | |
7190 | 4362 | =item C<m''>, the pattern of C<s'''> |
7191 | 4363 | |
7192 | 4364 | =begin original |
7193 | 4365 | |
7194 | 4366 | No interpolation is performed at this stage. |
7195 | 4367 | Any backslashed sequences including C<\\> are treated at the stage |
7196 | o | |
4368 | to L</"parsing regular expressions">. | |
7197 | 4369 | |
7198 | 4370 | =end original |
7199 | 4371 | |
7200 | 4372 | このステージでは展開は行われません。 |
7201 | 4373 | C<\\> を含む、バックスラッシュ付きのシーケンスは |
7202 | L</" | |
4374 | L</"parsing regular expressions"> で扱われます。 | |
7203 | 4375 | |
7204 | 4376 | =item C<''>, C<q//>, C<tr'''>, C<y'''>, the replacement of C<s'''> |
7205 | 4377 | |
7206 | 4378 | =begin original |
7207 | 4379 | |
7208 | 4380 | The only interpolation is removal of C<\> from pairs of C<\\>. |
7209 | Therefore C< | |
4381 | Therefore C<-> in C<tr'''> and C<y'''> is treated literally | |
7210 | 4382 | as a hyphen and no character range is available. |
7211 | 4383 | C<\1> in the replacement of C<s'''> does not work as C<$1>. |
7212 | 4384 | |
7213 | 4385 | =end original |
7214 | 4386 | |
7215 | 4387 | C<\\> の組における C<\> の削除のみが行われます。 |
7216 | 従って、C<tr'''> や C<y'''> の中にある C< | |
4388 | 従って、C<tr'''> や C<y'''> の中にある C<-> は文字通りハイフンとして扱われ、 | |
7217 | 4389 | 文字範囲は使えません。 |
7218 | 4390 | C<s'''> の置換文字列での C<\1> は C<$1> としては動作しません。 |
7219 | 4391 | |
7220 | 4392 | =item C<tr///>, C<y///> |
7221 | 4393 | |
7222 | 4394 | =begin original |
7223 | 4395 | |
7224 | 4396 | No variable interpolation occurs. String modifying combinations for |
7225 | 4397 | case and quoting such as C<\Q>, C<\U>, and C<\E> are not recognized. |
7226 | 4398 | The other escape sequences such as C<\200> and C<\t> and backslashed |
7227 | 4399 | characters such as C<\\> and C<\-> are converted to appropriate literals. |
7228 | The character C< | |
4400 | The character C<-> is treated specially and therefore C<\-> is treated | |
7229 | as a literal C< | |
4401 | as a literal C<->. | |
7230 | 4402 | |
7231 | 4403 | =end original |
7232 | 4404 | |
7233 | 4405 | 変数展開は行われません。 |
7234 | 4406 | C<\Q>, C<\U>, C<\E> のような、大文字小文字やクォートに関する、文字列を |
7235 | 4407 | 変更するような組み合わせは認識されません。 |
7236 | 4408 | C<\200> や C<\t> のようなその他のエスケープシーケンスや、 |
7237 | 4409 | C<\\> や C<\-> のようなバックスラッシュ付きの文字は、適切なリテラルに |
7238 | 4410 | 変換されます。 |
7239 | 文字 C< | |
4411 | 文字 C<-> は特別扱いされるので、C<\-> はリテラルな C<-> として扱われます。 | |
7240 | 4412 | |
7241 | 4413 | =item C<"">, C<``>, C<qq//>, C<qx//>, C<< <file*glob> >>, C<<<"EOF"> |
7242 | 4414 | |
7243 | 4415 | =begin original |
7244 | 4416 | |
7245 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> | |
4417 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> (possibly paired with C<\E>) are | |
7246 | 4418 | converted to corresponding Perl constructs. Thus, C<"$foo\Qbaz$bar"> |
7247 | is converted to | |
4419 | is converted to C<$foo . (quotemeta("baz" . $bar))> internally. | |
7248 | 4420 | The other escape sequences such as C<\200> and C<\t> and backslashed |
7249 | 4421 | characters such as C<\\> and C<\-> are replaced with appropriate |
7250 | 4422 | expansions. |
7251 | 4423 | |
7252 | 4424 | =end original |
7253 | 4425 | |
7254 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> | |
4426 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> (おそらくは C<\E> との組)は | |
7255 | 4427 | 対応する Perl 構造に変換されます。 |
7256 | 4428 | 従って、C<"$foo\Qbaz$bar"> は内部的に |
7257 | ||
4429 | C<$foo . (quotemeta("baz" . $bar))> に変換されます。 | |
7258 | 4430 | C<\200> や C<\t> のような、その他のエスケープシーケンスや、C<\\> や |
7259 | 4431 | C<\-> のような、バックスラッシュがつけられた文字は適切な拡張に |
7260 | 4432 | 置換されます。 |
7261 | 4433 | |
7262 | 4434 | =begin original |
7263 | 4435 | |
7264 | 4436 | Let it be stressed that I<whatever falls between C<\Q> and C<\E>> |
7265 | 4437 | is interpolated in the usual way. Something like C<"\Q\\E"> has |
7266 | no C<\E> inside. | |
4438 | no C<\E> inside. instead, it has C<\Q>, C<\\>, and C<E>, so the | |
7267 | 4439 | result is the same as for C<"\\\\E">. As a general rule, backslashes |
7268 | 4440 | between C<\Q> and C<\E> may lead to counterintuitive results. So, |
7269 | 4441 | C<"\Q\t\E"> is converted to C<quotemeta("\t")>, which is the same |
7270 | 4442 | as C<"\\\t"> (since TAB is not alphanumeric). Note also that: |
7271 | 4443 | |
7272 | 4444 | =end original |
7273 | 4445 | |
7274 | 4446 | I<C<\Q> と C<\E> の間にある全てのもの> が通常の方法で展開されます。 |
7275 | C<"\Q\\E"> のようなものは内部にあるのは C<\E> では | |
4447 | C<"\Q\\E"> のようなものは内部にあるのは C<\E> ではなく、 | |
7276 | 4448 | C<\Q>, C<\\>, C<E> であるので、結果は C<"\\\\E"> と同じになります。 |
7277 | 4449 | 一般的なルールとして、C<\Q> と C<\E> の間にあるバックスラッシュは |
7278 | 4450 | 直感に反した結果になります。 |
7279 | 4451 | それで、C<"\Q\t\E"> は C<quotemeta("\t")> に変換され、これは(TAB は |
7280 | 4452 | 英数字ではないので C<"\\\t"> と同じです。 |
7281 | 4453 | 以下のようなことにも注意してください: |
7282 | 4454 | |
7283 | 4455 | $str = '\t'; |
7284 | 4456 | return "\Q$str"; |
7285 | 4457 | |
7286 | 4458 | =begin original |
7287 | 4459 | |
7288 | 4460 | may be closer to the conjectural I<intention> of the writer of C<"\Q\t\E">. |
7289 | 4461 | |
7290 | 4462 | =end original |
7291 | 4463 | |
7292 | 4464 | これは C<"\Q\t\E"> を書いた人の憶測上の I<意図> により近いです。 |
7293 | 4465 | |
7294 | 4466 | =begin original |
7295 | 4467 | |
7296 | 4468 | Interpolated scalars and arrays are converted internally to the C<join> and |
7297 | C< | |
4469 | C<.> catenation operations. Thus, C<"$foo XXX '@arr'"> becomes: | |
7298 | 4470 | |
7299 | 4471 | =end original |
7300 | 4472 | |
7301 | 展開されたスカラと配列は内部で C<join> と C< | |
4473 | 展開されたスカラと配列は内部で C<join> と C<.> の結合操作に変換されます。 | |
7302 | 従って、 | |
4474 | 従って、C<"$foo XXX '@arr'"> は以下のようになります: | |
7303 | 4475 | |
7304 | 4476 | $foo . " XXX '" . (join $", @arr) . "'"; |
7305 | 4477 | |
7306 | 4478 | =begin original |
7307 | 4479 | |
7308 | 4480 | All operations above are performed simultaneously, left to right. |
7309 | 4481 | |
7310 | 4482 | =end original |
7311 | 4483 | |
7312 | 4484 | 上記の全ての操作は、左から右に同時に行われます。 |
7313 | 4485 | |
7314 | 4486 | =begin original |
7315 | 4487 | |
7316 | Because the result of | |
4488 | Because the result of C<"\Q STRING \E"> has all metacharacters | |
7317 | 4489 | quoted, there is no way to insert a literal C<$> or C<@> inside a |
7318 | C<\Q\E> pair. If protected by C<\>, C<$> will be quoted to bec | |
4490 | C<\Q\E> pair. If protected by C<\>, C<$> will be quoted to became | |
7319 | 4491 | C<"\\\$">; if not, it is interpreted as the start of an interpolated |
7320 | 4492 | scalar. |
7321 | 4493 | |
7322 | 4494 | =end original |
7323 | 4495 | |
7324 | ||
4496 | C<"\Q STRING \E"> の結果は全てのメタ文字がクォートされているので、 | |
7325 | 4497 | C<\Q\E> の組の内側にリテラルの C<$> や C<@> を挿入する方法はありません。 |
7326 | 4498 | C<\> によって守られている場合、C<$> はクォートされて C<"\\\$"> と |
7327 | なります | |
4499 | なります。 | |
7328 | 解釈されます。 | |
4500 | そうでない場合、これは展開されるスカラ変数の開始として解釈されます。 | |
7329 | 4501 | |
7330 | 4502 | =begin original |
7331 | 4503 | |
7332 | 4504 | Note also that the interpolation code needs to make a decision on |
7333 | 4505 | where the interpolated scalar ends. For instance, whether |
7334 | ||
4506 | C<< "a $b -> {c}" >> really means: | |
7335 | 4507 | |
7336 | 4508 | =end original |
7337 | 4509 | |
7338 | 4510 | 展開コードは、展開するスカラ変数がどこで終わるかを決定する必要が |
7339 | 4511 | あることにも注意してください。 |
7340 | 例えば、 | |
4512 | 例えば、C<< "a $b -> {c}" >> が実際には以下のどちらかになります: | |
7341 | 4513 | |
7342 | "a " . $ | |
4514 | "a " . $b . " -> {c}"; | |
7343 | 4515 | |
7344 | 4516 | =begin original |
7345 | 4517 | |
7346 | 4518 | or: |
7347 | 4519 | |
7348 | 4520 | =end original |
7349 | 4521 | |
7350 | 4522 | または: |
7351 | 4523 | |
7352 | "a " . $ | |
4524 | "a " . $b -> {c}; | |
7353 | 4525 | |
7354 | 4526 | =begin original |
7355 | 4527 | |
7356 | 4528 | Most of the time, the longest possible text that does not include |
7357 | 4529 | spaces between components and which contains matching braces or |
7358 | 4530 | brackets. because the outcome may be determined by voting based |
7359 | 4531 | on heuristic estimators, the result is not strictly predictable. |
7360 | 4532 | Fortunately, it's usually correct for ambiguous cases. |
7361 | 4533 | |
7362 | 4534 | =end original |
7363 | 4535 | |
7364 | 4536 | ほとんどの場合、要素と、マッチする中かっこや大かっこの間に空白を含まない、 |
7365 | 4537 | 最も長いテキストになります。 |
7366 | 4538 | 出力は発見的な推定器をよる投票によって決定されるので、結果は厳密には |
7367 | 4539 | 予測できません。 |
7368 | 4540 | 幸い、紛らわしい場合でも普通は正しいです。 |
7369 | 4541 | |
7370 | =item | |
4542 | =item the replacement of C<s///> | |
7371 | 4543 | |
7372 | 4544 | =begin original |
7373 | 4545 | |
7374 | Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, | |
4546 | Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, and interpolation | |
7375 | 4547 | happens as with C<qq//> constructs. |
7376 | 4548 | |
7377 | 4549 | =end original |
7378 | 4550 | |
7379 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> | |
4551 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> の処理と展開が C<qq//> 構造と | |
7380 | 4552 | 同じように起こります。 |
7381 | 4553 | |
7382 | 4554 | =begin original |
7383 | 4555 | |
7384 | 4556 | It is at this step that C<\1> is begrudgingly converted to C<$1> in |
7385 | 4557 | the replacement text of C<s///>, in order to correct the incorrigible |
7386 | 4558 | I<sed> hackers who haven't picked up the saner idiom yet. A warning |
7387 | is emitted if the | |
4559 | is emitted if the C<use warnings> pragma or the B<-w> command-line flag | |
7388 | 4560 | (that is, the C<$^W> variable) was set. |
7389 | 4561 | |
7390 | 4562 | =end original |
7391 | 4563 | |
7392 | 4564 | このステップでは、より健全な文法をまだ導入していない、手に負えない I<sed> |
7393 | 4565 | ハッカーのために、C<s///> の置換テキストの中にある C<\1> を、しぶしぶながら |
7394 | 4566 | C<$1> に変換します。 |
7395 | ||
4567 | C<use warnings> プラグマやコマンドラインオプション B<-w> (これは C<$^W> | |
7396 | 4568 | 変数です) がセットされていると警告が生成されます。 |
7397 | 4569 | |
7398 | =item C<RE> in C< | |
4570 | =item C<RE> in C<?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>, | |
7399 | 4571 | |
7400 | 4572 | =begin original |
7401 | 4573 | |
7402 | Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\ | |
4574 | Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\E>, | |
7403 | 4575 | and interpolation happens (almost) as with C<qq//> constructs. |
7404 | 4576 | |
7405 | 4577 | =end original |
7406 | 4578 | |
7407 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> | |
4579 | C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> C<\E> の処理と展開が C<qq//> 構造と | |
7408 | 4580 | (ほとんど)同じように起こります。 |
7409 | 4581 | |
7410 | 4582 | =begin original |
7411 | 4583 | |
7412 | Processing of C<\N{...}> is also done here, and compiled into an intermediate | |
7413 | form for the regex compiler. (This is because, as mentioned below, the regex | |
7414 | compilation may be done at execution time, and C<\N{...}> is a compile-time | |
7415 | construct.) | |
7416 | ||
7417 | =end original | |
7418 | ||
7419 | C<\N{...}> の処理もここで行われ、正規表現コンパイラのための中間形式に | |
7420 | コンパイルされます。 | |
7421 | (これは、後述するように、正規表現のコンパイルは実行時に行われ、 | |
7422 | C<\N{...}> はコンパイル時の構文だからです。) | |
7423 | ||
7424 | =begin original | |
7425 | ||
7426 | 4584 | However any other combinations of C<\> followed by a character |
7427 | 4585 | are not substituted but only skipped, in order to parse them |
7428 | 4586 | as regular expressions at the following step. |
7429 | 4587 | As C<\c> is skipped at this step, C<@> of C<\c@> in RE is possibly |
7430 | 4588 | treated as an array symbol (for example C<@foo>), |
7431 | 4589 | even though the same text in C<qq//> gives interpolation of C<\c@>. |
7432 | 4590 | |
7433 | 4591 | =end original |
7434 | 4592 | |
7435 | 4593 | しかし、その他の、C<\> の後に文字が続く組み合わせは置換されず、単に |
7436 | 4594 | 読み飛ばされます; これは以下のステップで正規表現としてパースするためです。 |
7437 | 4595 | ここでは C<\c> は読み飛ばされるので、正規表現中の C<\c@> の C<@> は |
7438 | 4596 | 配列のシンボル(例えば C<@foo>) と扱われる可能性があります; |
7439 | 4597 | 一方 C<qq//> 内の同じテキストは C<\c@> と展開されます。 |
7440 | 4598 | |
7441 | 4599 | =begin original |
7442 | 4600 | |
7443 | ||
4601 | Moreover, inside C<(?{BLOCK})>, C<(?# comment )>, and | |
7444 | ||
4602 | a C<#>-comment in a C<//x>-regular expression, no processing is | |
7445 | subscript expression such as C<"foo$array[1+f("[xyz")]bar"> would be. | |
7446 | ||
7447 | =end original | |
7448 | ||
7449 | C<(?{BLOCK})> のようなコードブロックは、一時的に制御を perl パーサに | |
7450 | 渡すことで扱われます; これは C<"foo$array[1+f("[xyz")]bar"> のような、 | |
7451 | 変数展開される配列添え字表現と同様の方法です。 | |
7452 | ||
7453 | =begin original | |
7454 | ||
7455 | Moreover, inside C<(?{BLOCK})>, S<C<(?# comment )>>, and | |
7456 | a C<#>-comment in a C</x>-regular expression, no processing is | |
7457 | 4603 | performed whatsoever. This is the first step at which the presence |
7458 | of the C</x> modifier is relevant. | |
4604 | of the C<//x> modifier is relevant. | |
7459 | 4605 | |
7460 | 4606 | =end original |
7461 | 4607 | |
7462 | さらに、C<(?{BLOCK})>, | |
4608 | さらに、C<(?{BLOCK})>, C<(?# comment )>, C<//x> 正規表現での C<#> の | |
7463 | 4609 | コメントの中では、どのような処理も行われません。 |
7464 | これは C</x> 修飾子が影響を与える最初のステップです。 | |
4610 | これは C<//x> 修飾子が影響を与える最初のステップです。 | |
7465 | 4611 | |
7466 | 4612 | =begin original |
7467 | 4613 | |
7468 | 4614 | Interpolation in patterns has several quirks: C<$|>, C<$(>, C<$)>, C<@+> |
7469 | 4615 | and C<@-> are not interpolated, and constructs C<$var[SOMETHING]> are |
7470 | 4616 | voted (by several different estimators) to be either an array element |
7471 | 4617 | or C<$var> followed by an RE alternative. This is where the notation |
7472 | 4618 | C<${arr[$bar]}> comes handy: C</${arr[0-9]}/> is interpreted as |
7473 | 4619 | array element C<-9>, not as a regular expression from the variable |
7474 | 4620 | C<$arr> followed by a digit, which would be the interpretation of |
7475 | 4621 | C</$arr[0-9]/>. Since voting among different estimators may occur, |
7476 | 4622 | the result is not predictable. |
7477 | 4623 | |
7478 | 4624 | =end original |
7479 | 4625 | |
7480 | 4626 | パターン内の展開ではいくつか特殊な動作をします: |
7481 | 4627 | C<$|>, C<$(>, C<$)>, C<@+>, C<@-> は展開されず、 |
7482 | 4628 | C<$var[SOMETHING]> は(いくつかの異なる推定器によって)配列の要素か |
7483 | 4629 | C<$var> の後に正規表現が続いているのかが投票されます。 |
7484 | 4630 | これは C<${arr[$bar]}> が便利になるところです: C</${arr[0-9]}/> は |
7485 | 4631 | 配列要素 C<-9> として解釈され、C</$arr[0-9]/> の場合のように C<$arr> の後に |
7486 | 4632 | 数値が続いているような正規表現としては解釈されません。 |
7487 | 4633 | 異なった推定器によって投票されることがあるので、結果は予測できません。 |
7488 | 4634 | |
7489 | 4635 | =begin original |
7490 | 4636 | |
7491 | 4637 | The lack of processing of C<\\> creates specific restrictions on |
7492 | 4638 | the post-processed text. If the delimiter is C</>, one cannot get |
7493 | 4639 | the combination C<\/> into the result of this step. C</> will |
7494 | 4640 | finish the regular expression, C<\/> will be stripped to C</> on |
7495 | 4641 | the previous step, and C<\\/> will be left as is. Because C</> is |
7496 | 4642 | equivalent to C<\/> inside a regular expression, this does not |
7497 | 4643 | matter unless the delimiter happens to be character special to the |
7498 | RE engine, such as in C<s*foo*bar*>, C<m[foo]>, or C< | |
4644 | RE engine, such as in C<s*foo*bar*>, C<m[foo]>, or C<?foo?>; or an | |
7499 | 4645 | alphanumeric char, as in: |
7500 | 4646 | |
7501 | 4647 | =end original |
7502 | 4648 | |
7503 | 4649 | C<\\> を処理しないことにより、後処理したテキストに特定の制限があります。 |
7504 | 4650 | デリミタが C</> の場合、このステップの結果として C<\/> を得ることは |
7505 | 4651 | できません。 |
7506 | 4652 | C</> は正規表現を終わらせ、C<\/> は前のステップで C</> に展開され、 |
7507 | 4653 | C<\\/> はそのまま残されます。 |
7508 | 4654 | C</> は正規表現の中では C<\/> と等価なので、これはたまたまデリミタが |
7509 | 4655 | 正規検索エンジンにとって特別な文字の場合、つまり C<s*foo*bar*>, |
7510 | C<m[foo]>, C< | |
4656 | C<m[foo]>, C<?foo?> のような場合、あるいは以下のように英数字でなければ、 | |
7511 | 4657 | 問題にはなりません: |
7512 | 4658 | |
7513 | 4659 | m m ^ a \s* b mmx; |
7514 | 4660 | |
7515 | 4661 | =begin original |
7516 | 4662 | |
7517 | 4663 | In the RE above, which is intentionally obfuscated for illustration, the |
7518 | 4664 | delimiter is C<m>, the modifier is C<mx>, and after delimiter-removal the |
7519 | RE is the same as for | |
4665 | RE is the same as for C<m/ ^ a \s* b /mx>. There's more than one | |
7520 | 4666 | reason you're encouraged to restrict your delimiters to non-alphanumeric, |
7521 | 4667 | non-whitespace choices. |
7522 | 4668 | |
7523 | 4669 | =end original |
7524 | 4670 | |
7525 | 4671 | 上記の正規表現では、説明のために意図的にわかりにくくしていますが、 |
7526 | 4672 | デリミタは C<m> で、修飾子は C<mx> で、デリミタを取り除いた後の |
7527 | 正規表現は | |
4673 | 正規表現は C<m/ ^ a \s* b /mx> と同じです。 | |
7528 | 4674 | デリミタを英数字や空白でないものに制限するべきである理由は複数あります。 |
7529 | 4675 | |
7530 | 4676 | =back |
7531 | 4677 | |
7532 | 4678 | =begin original |
7533 | 4679 | |
7534 | 4680 | This step is the last one for all constructs except regular expressions, |
7535 | 4681 | which are processed further. |
7536 | 4682 | |
7537 | 4683 | =end original |
7538 | 4684 | |
7539 | これは正規表現以外の全ての構造にとって最後のステップです | |
4685 | これは正規表現以外の全ての構造にとって最後のステップです。 | |
7540 | さらに処理が続きます。 | |
4686 | 正規表現はさらに処理が続きます。 | |
7541 | 4687 | |
7542 | =item | |
4688 | =item parsing regular expressions | |
7543 | 4689 | X<regexp, parse> |
7544 | 4690 | |
7545 | 4691 | (正規表現のパース) |
7546 | 4692 | |
7547 | 4693 | =begin original |
7548 | 4694 | |
7549 | 4695 | Previous steps were performed during the compilation of Perl code, |
7550 | but this one happens at run time | |
4696 | but this one happens at run time--although it may be optimized to | |
7551 | 4697 | be calculated at compile time if appropriate. After preprocessing |
7552 | 4698 | described above, and possibly after evaluation if concatenation, |
7553 | 4699 | joining, casing translation, or metaquoting are involved, the |
7554 | 4700 | resulting I<string> is passed to the RE engine for compilation. |
7555 | 4701 | |
7556 | 4702 | =end original |
7557 | 4703 | |
7558 | 4704 | 以前のステップは Perl コードのコンパイル中に実行されますが、 |
7559 | これは実行時に起こります | |
4705 | これは実行時に起こります -- しかし、もし適切ならコンパイル時に | |
7560 | 4706 | 計算できるように最適化されることもあります。 |
7561 | 4707 | 上記の前処理の後、そして必要なら連結、結合、大文字小文字変換、 |
7562 | 4708 | メタクォート化が行われた後、結果の I<文字列> がコンパイルのために |
7563 | 4709 | 正規表現エンジンに渡されます。 |
7564 | 4710 | |
7565 | 4711 | =begin original |
7566 | 4712 | |
7567 | 4713 | Whatever happens in the RE engine might be better discussed in L<perlre>, |
7568 | 4714 | but for the sake of continuity, we shall do so here. |
7569 | 4715 | |
7570 | 4716 | =end original |
7571 | 4717 | |
7572 | 4718 | 正規表現エンジンで起こることについては L<perlre> で議論した方が |
7573 | 4719 | よいでしょうが、継続性のために、ここでそれを行います。 |
7574 | 4720 | |
7575 | 4721 | =begin original |
7576 | 4722 | |
7577 | This is another step where the presence of the C</x> modifier is | |
4723 | This is another step where the presence of the C<//x> modifier is | |
7578 | 4724 | relevant. The RE engine scans the string from left to right and |
7579 | converts it | |
4725 | converts it to a finite automaton. | |
7580 | 4726 | |
7581 | 4727 | =end original |
7582 | 4728 | |
7583 | これも C</x> 修飾子の存在が関連するステップの一つです。 | |
4729 | これも C<//x> 修飾子の存在が関連するステップの一つです。 | |
7584 | 4730 | 正規表現エンジンは文字列を左から右にスキャンして、有限状態オートマトンに |
7585 | 4731 | 変換します。 |
7586 | 4732 | |
7587 | 4733 | =begin original |
7588 | 4734 | |
7589 | 4735 | Backslashed characters are either replaced with corresponding |
7590 | 4736 | literal strings (as with C<\{>), or else they generate special nodes |
7591 | 4737 | in the finite automaton (as with C<\b>). Characters special to the |
7592 | 4738 | RE engine (such as C<|>) generate corresponding nodes or groups of |
7593 | 4739 | nodes. C<(?#...)> comments are ignored. All the rest is either |
7594 | 4740 | converted to literal strings to match, or else is ignored (as is |
7595 | whitespace and C<#>-style comments if C</x> is present). | |
4741 | whitespace and C<#>-style comments if C<//x> is present). | |
7596 | 4742 | |
7597 | 4743 | =end original |
7598 | 4744 | |
7599 | 4745 | バックスラッシュ付きの文字は(C<\{> のように)対応するリテラル文字列に |
7600 | 4746 | 置換されるか、あるいは(C<\b> のように)有限状態オートマトンの特別な |
7601 | 4747 | ノードを生成します。 |
7602 | 4748 | (C<|> のような)正規表現エンジンにとって特別な文字は対応するノードか |
7603 | 4749 | ノードのグループを生成します。 |
7604 | C<(?#...)> コメントは無視されます。 | |
7605 | 4750 | 残りの全てはマッチするリテラル文字列に変換されるか、そうでなければ |
7606 | (C</x> が指定された時の空白と C<#> スタイルのコメントと同様に) | |
4751 | (C<//x> が指定された時の空白と C<#> スタイルのコメントと同様に) | |
7607 | 4752 | 無視されます。 |
7608 | 4753 | |
7609 | 4754 | =begin original |
7610 | 4755 | |
7611 | 4756 | Parsing of the bracketed character class construct, C<[...]>, is |
7612 | 4757 | rather different than the rule used for the rest of the pattern. |
7613 | 4758 | The terminator of this construct is found using the same rules as |
7614 | 4759 | for finding the terminator of a C<{}>-delimited construct, the only |
7615 | 4760 | exception being that C<]> immediately following C<[> is treated as |
7616 | though preceded by a backslash. | |
4761 | though preceded by a backslash. Similarly, the terminator of | |
4762 | C<(?{...})> is found using the same rules as for finding the | |
4763 | terminator of a C<{}>-delimited construct. | |
7617 | 4764 | |
7618 | 4765 | =end original |
7619 | 4766 | |
7620 | 4767 | 文字クラス構造 C<[...]> のパースは他のパターンとはルールが異なります。 |
7621 | 4768 | この構造の終端は C<{}> でデリミタされた構造の終端を検索するのと同じルールで |
7622 | 4769 | 検索されます; 唯一の例外は、C<[> の直後の C<]> はバックスラッシュが |
7623 | 4770 | 先行しているものとして扱われます。 |
4771 | 同様に、C<(?{...})> の終端は C<{}> でデリミタされた構造の終端を | |
4772 | 検索されるのと同じルールで検索されます。 | |
7624 | 4773 | |
7625 | 4774 | =begin original |
7626 | 4775 | |
7627 | The terminator of runtime C<(?{...})> is found by temporarily switching | |
7628 | control to the perl parser, which should stop at the point where the | |
7629 | logically balancing terminating C<}> is found. | |
7630 | ||
7631 | =end original | |
7632 | ||
7633 | 実行時 C<(?{...})> の終端は、一時的に perl パーサに制御を切り替えることで | |
7634 | 見つけられ、論理的にバランスした終端 C<}> が見つかって場所で停止します。 | |
7635 | ||
7636 | =begin original | |
7637 | ||
7638 | 4776 | It is possible to inspect both the string given to RE engine and the |
7639 | 4777 | resulting finite automaton. See the arguments C<debug>/C<debugcolor> |
7640 | in the | |
4778 | in the C<use L<re>> pragma, as well as Perl's B<-Dr> command-line | |
7641 | 4779 | switch documented in L<perlrun/"Command Switches">. |
7642 | 4780 | |
7643 | 4781 | =end original |
7644 | 4782 | |
7645 | 4783 | 正規表現に与えられる文字列と、結果としての有限状態オートマトンの両方を |
7646 | 4784 | 検査できます。 |
7647 | ||
4785 | C<use L<re>> プラグマの C<debug>/C<debugcolor> 引数と、 | |
7648 | 4786 | L<perlrun/"Command Switches"> に記述されている B<-Dr> コマンドライン |
7649 | 4787 | オプションを参照してください。 |
7650 | 4788 | |
7651 | 4789 | =item Optimization of regular expressions |
7652 | 4790 | X<regexp, optimization> |
7653 | 4791 | |
7654 | (正規表現の最適化) | |
7655 | ||
7656 | 4792 | =begin original |
7657 | 4793 | |
7658 | 4794 | This step is listed for completeness only. Since it does not change |
7659 | 4795 | semantics, details of this step are not documented and are subject |
7660 | 4796 | to change without notice. This step is performed over the finite |
7661 | 4797 | automaton that was generated during the previous pass. |
7662 | 4798 | |
7663 | 4799 | =end original |
7664 | 4800 | |
7665 | 4801 | このステップは完全性のためだけにリストされています。 |
7666 | 4802 | これは意味論的には変化がないので、このステップの詳細は文書化されておらず、 |
7667 | 4803 | 将来予告なしに変更されることがあります。 |
7668 | 4804 | このステップはここまでの処理で生成された有限オートマトンに対して |
7669 | 4805 | 適用されます。 |
7670 | 4806 | |
7671 | 4807 | =begin original |
7672 | 4808 | |
7673 | 4809 | It is at this stage that C<split()> silently optimizes C</^/> to |
7674 | 4810 | mean C</^/m>. |
7675 | 4811 | |
7676 | 4812 | =end original |
7677 | 4813 | |
7678 | 4814 | C<split()> で C</^/> を暗黙に C</^/m> に最適化するのもこのステップです。 |
7679 | 4815 | |
7680 | 4816 | =back |
7681 | 4817 | |
7682 | 4818 | =head2 I/O Operators |
7683 | 4819 | X<operator, i/o> X<operator, io> X<io> X<while> X<filehandle> |
7684 | X<< <> >> X< | |
4820 | X<< <> >> X<@ARGV> | |
7685 | 4821 | |
7686 | 4822 | (I/O 演算子) |
7687 | 4823 | |
7688 | 4824 | =begin original |
7689 | 4825 | |
7690 | 4826 | There are several I/O operators you should know about. |
7691 | 4827 | |
7692 | 4828 | =end original |
7693 | 4829 | |
7694 | 4830 | 知っておいた方がよい I/O 演算子もいくつかあります。 |
7695 | 4831 | |
7696 | 4832 | =begin original |
7697 | 4833 | |
7698 | 4834 | A string enclosed by backticks (grave accents) first undergoes |
7699 | 4835 | double-quote interpolation. It is then interpreted as an external |
7700 | 4836 | command, and the output of that command is the value of the |
7701 | 4837 | backtick string, like in a shell. In scalar context, a single string |
7702 | 4838 | consisting of all output is returned. In list context, a list of |
7703 | 4839 | values is returned, one per line of output. (You can set C<$/> to use |
7704 | 4840 | a different line terminator.) The command is executed each time the |
7705 | 4841 | pseudo-literal is evaluated. The status value of the command is |
7706 | 4842 | returned in C<$?> (see L<perlvar> for the interpretation of C<$?>). |
7707 | 4843 | Unlike in B<csh>, no translation is done on the return data--newlines |
7708 | 4844 | remain newlines. Unlike in any of the shells, single quotes do not |
7709 | 4845 | hide variable names in the command from interpretation. To pass a |
7710 | 4846 | literal dollar-sign through to the shell you need to hide it with a |
7711 | backslash. The generalized form of backticks is C<qx//> | |
4847 | backslash. The generalized form of backticks is C<qx//>. (Because | |
7712 | call the L<perlfunc/readpipe> function. (Because | |
7713 | 4848 | backticks always undergo shell expansion as well, see L<perlsec> for |
7714 | 4849 | security concerns.) |
7715 | 4850 | X<qx> X<`> X<``> X<backtick> X<glob> |
7716 | 4851 | |
7717 | 4852 | =end original |
7718 | 4853 | |
7719 | 4854 | バッククォートで括られた文字列は、まず、ダブルクォート補完のように |
7720 | 4855 | 変数の展開が行なわれます。 |
7721 | 4856 | その後、シェルでの場合と同じように、外部コマンドとして解釈され、 |
7722 | 4857 | そのコマンドの出力がこのバッククォート文字列の値となります。 |
7723 | 4858 | スカラーコンテキストでは、出力すべてを含む一個の文字列が返されます。 |
7724 | 4859 | リストコンテキストでは、出力の 1 行 1 行が個々の要素となるリストが返されます。 |
7725 | 4860 | (C<$/> を設定すれば、行の終わりを示す文字を変えることができます。) |
7726 | 4861 | コマンドは、この擬似リテラルが評価されるごとに実行されます。 |
7727 | 4862 | コマンドのステータス値は C<$?> に返されます (C<$?> の解釈については、 |
7728 | 4863 | L<perlvar> を参照してください)。 |
7729 | 4864 | B<csh> での場合とは違って、結果のデータに対する変換は行なわれず、 |
7730 | 4865 | 改行は改行のままです。 |
7731 | 4866 | どのシェルとも違って、シングルクォートがコマンド中の変数名を |
7732 | 4867 | 解釈させないようにすることはありません。 |
7733 | 4868 | シェルにリテラルなドル記号を渡すには、バックスラッシュで |
7734 | 4869 | エスケープしなければなりません。 |
7735 | バッククォートの一般形は、C<qx//> | |
4870 | バッククォートの一般形は、C<qx//> です。 | |
7736 | ||
4871 | (バッククォートは常にシェル展開されます。 | |
7737 | ||
4872 | セキュリティに関しては L<perlsec> を参照して下さい) | |
7738 | セキュリティに関しては L<perlsec> を参照して下さい。) | |
7739 | 4873 | |
7740 | 4874 | =begin original |
7741 | 4875 | |
7742 | 4876 | In scalar context, evaluating a filehandle in angle brackets yields |
7743 | 4877 | the next line from that file (the newline, if any, included), or |
7744 | 4878 | C<undef> at end-of-file or on error. When C<$/> is set to C<undef> |
7745 | 4879 | (sometimes known as file-slurp mode) and the file is empty, it |
7746 | 4880 | returns C<''> the first time, followed by C<undef> subsequently. |
7747 | 4881 | |
7748 | 4882 | =end original |
7749 | 4883 | |
7750 | 4884 | スカラーコンテキストで山括弧の中のファイルハンドルを評価すると、 |
7751 | 4885 | そのファイルから、次の行を読み込むことになります |
7752 | (改行があればそれも含まれます) | |
4886 | (改行があればそれも含まれます)。 | |
7753 | C<undef> を返します。 | |
4887 | ファイルの最後またはエラーの場合は C<undef> を返します。 | |
7754 | 4888 | C<$/> が C<undef> に設定されている場合(ファイル吸い込みモードと呼ばれます) |
7755 | 4889 | でファイルが空の場合、 |
7756 | 4890 | 最初は C<''> を返し、次は C<undef> を返します。 |
7757 | 4891 | |
7758 | 4892 | =begin original |
7759 | 4893 | |
7760 | 4894 | Ordinarily you must assign the returned value to a variable, but |
7761 | 4895 | there is one situation where an automatic assignment happens. If |
7762 | 4896 | and only if the input symbol is the only thing inside the conditional |
7763 | 4897 | of a C<while> statement (even if disguised as a C<for(;;)> loop), |
7764 | the value is automatically assigned to the global variable | |
4898 | the value is automatically assigned to the global variable $_, | |
7765 | 4899 | destroying whatever was there previously. (This may seem like an |
7766 | 4900 | odd thing to you, but you'll use the construct in almost every Perl |
7767 | script you write.) The | |
4901 | script you write.) The $_ variable is not implicitly localized. | |
7768 | You'll have to put a | |
4902 | You'll have to put a C<local $_;> before the loop if you want that | |
7769 | to happen. | |
4903 | to happen. | |
7770 | of the input symbol to a scalar is used as a C<while>/C<for> condition, | |
7771 | then the condition actually tests for definedness of the expression's | |
7772 | value, not for its regular truth value. | |
7773 | 4904 | |
7774 | 4905 | =end original |
7775 | 4906 | |
7776 | 4907 | 通常は、返された値を変数に代入しなければなりませんが、自動的に |
7777 | 4908 | 代入される場合が 1 つだけあります。 |
7778 | 4909 | この入力シンボルが、while 文(C<for(;;)> の形になっていたとしても)の条件式中に |
7779 | 単独で現れた場合だけは、その値が自動的にグローバル変数 | |
4910 | 単独で現れた場合だけは、その値が自動的にグローバル変数 $_ に代入されます。 | |
7780 | 4911 | 以前の値は破壊されます。 |
7781 | 4912 | (これは、奇妙に思えるかもしれませんが、ほとんどすべての Perl スクリプトで |
7782 | 4913 | これが必要になることでしょう。) |
7783 | ||
4914 | $_ 変数は暗黙にはローカル化されません。 | |
7784 | そうしたい場合はループの前に | |
4915 | そうしたい場合はループの前に C<local $_;> と書く必要があります。 | |
7785 | さらに、入力シンボルまたは入力シンボルからスカラへの明示的な代入が | |
7786 | C<while>/C<for> の条件部として使われた場合、 | |
7787 | 条件は通常の真の値かどうかではなく、式の値が定義されているかどうかを | |
7788 | テストします。 | |
7789 | 4916 | |
7790 | 4917 | =begin original |
7791 | 4918 | |
7792 | Th | |
4919 | The following lines are equivalent: | |
7793 | 4920 | |
7794 | 4921 | =end original |
7795 | 4922 | |
7796 | ||
4923 | 以下のものは、お互いに同値なものです: | |
7797 | 4924 | |
7798 | 4925 | while (defined($_ = <STDIN>)) { print; } |
7799 | 4926 | while ($_ = <STDIN>) { print; } |
7800 | 4927 | while (<STDIN>) { print; } |
7801 | 4928 | for (;<STDIN>;) { print; } |
7802 | 4929 | print while defined($_ = <STDIN>); |
7803 | 4930 | print while ($_ = <STDIN>); |
7804 | 4931 | print while <STDIN>; |
7805 | 4932 | |
7806 | 4933 | =begin original |
7807 | 4934 | |
7808 | This also behaves similarly, but a | |
4935 | This also behaves similarly, but avoids $_ : | |
7809 | instead of to C<$_>: | |
7810 | 4936 | |
7811 | 4937 | =end original |
7812 | 4938 | |
7813 | 以下は同様の振る舞いをしますが、 | |
4939 | 以下は同様の振る舞いをしますが、$_ を使いません: | |
7814 | 代入します: | |
7815 | 4940 | |
7816 | 4941 | while (my $line = <STDIN>) { print $line } |
7817 | 4942 | |
7818 | 4943 | =begin original |
7819 | 4944 | |
7820 | 4945 | In these loop constructs, the assigned value (whether assignment |
7821 | 4946 | is automatic or explicit) is then tested to see whether it is |
7822 | defined. The defined test avoids problems where | |
4947 | defined. The defined test avoids problems where line has a string | |
7823 | value that would be treated as false by Perl | |
4948 | value that would be treated as false by Perl, for example a "" or | |
7824 | a | |
4949 | a "0" with no trailing newline. If you really mean for such values | |
7825 | 4950 | to terminate the loop, they should be tested for explicitly: |
7826 | 4951 | |
7827 | 4952 | =end original |
7828 | 4953 | |
7829 | 4954 | これらのループ構造の中で、代入された値は (代入が自動か明示的かに関わりなく) |
7830 | 4955 | 定義されているかどうかを見るためにテストされます。 |
7831 | 4956 | 定義テストは、行が Perl にとって偽となる文字列値を持っているかどうかの |
7832 | 問題を避けます | |
4957 | 問題を避けます。例えば newline のついていない "" や "0" です。 | |
7833 | 4958 | もし本当にこのような値でループを終了させたいときは、 |
7834 | 4959 | 以下のように明示的にテストするべきです: |
7835 | 4960 | |
7836 | 4961 | while (($_ = <STDIN>) ne '0') { ... } |
7837 | 4962 | while (<STDIN>) { last unless $_; ... } |
7838 | 4963 | |
7839 | 4964 | =begin original |
7840 | 4965 | |
7841 | In other boolean contexts, C<< <I< | |
4966 | In other boolean contexts, C<< <I<filehandle>> >> without an | |
7842 | explicit C<defined> test or comparison elicit | |
4967 | explicit C<defined> test or comparison elicit a warning if the | |
7843 | ||
4968 | C<use warnings> pragma or the B<-w> | |
7844 | 4969 | command-line switch (the C<$^W> variable) is in effect. |
7845 | 4970 | |
7846 | 4971 | =end original |
7847 | 4972 | |
7848 | 4973 | その他の真偽値コンテキストでは、明示的な C<defined> や比較なしに |
7849 | C<< <I< | |
4974 | C<< <I<filehandle>> >> を使うと、C<use warnings> プラグマや | |
7850 | 4975 | B<-w> コマンドラインスイッチ (C<$^W> 変数) が有効なときには、 |
7851 | 4976 | 警告を発生させます。 |
7852 | 4977 | |
7853 | 4978 | =begin original |
7854 | 4979 | |
7855 | 4980 | The filehandles STDIN, STDOUT, and STDERR are predefined. (The |
7856 | 4981 | filehandles C<stdin>, C<stdout>, and C<stderr> will also work except |
7857 | 4982 | in packages, where they would be interpreted as local identifiers |
7858 | 4983 | rather than global.) Additional filehandles may be created with |
7859 | the | |
4984 | the open() function, amongst others. See L<perlopentut> and | |
7860 | 4985 | L<perlfunc/open> for details on this. |
7861 | 4986 | X<stdin> X<stdout> X<sterr> |
7862 | 4987 | |
7863 | 4988 | =end original |
7864 | 4989 | |
7865 | 4990 | STDIN、STDOUT、STDERR というファイルハンドルは、あらかじめ定義されています。 |
7866 | 4991 | (C<stdin>、C<stdout>、C<stderr> というファイルハンドルも、 |
7867 | 4992 | ローカルな名前でこれらのグローバルな名前が見えなくなっている |
7868 | 4993 | パッケージを除けば、使用することができます。) |
7869 | その他のファイルハンドルは、 | |
4994 | その他のファイルハンドルは、open() 関数などで作ることができます。 | |
7870 | 4995 | これに関する詳細については L<perlopentut> と L<perlfunc/open> を |
7871 | 4996 | 参照して下さい。 |
7872 | 4997 | X<stdin> X<stdout> X<sterr> |
7873 | 4998 | |
7874 | 4999 | =begin original |
7875 | 5000 | |
7876 | If a | |
5001 | If a <FILEHANDLE> is used in a context that is looking for | |
7877 | 5002 | a list, a list comprising all input lines is returned, one line per |
7878 | 5003 | list element. It's easy to grow to a rather large data space this |
7879 | 5004 | way, so use with care. |
7880 | 5005 | |
7881 | 5006 | =end original |
7882 | 5007 | |
7883 | ||
5008 | <FILEHANDLE> がリストを必要とするコンテキストで用いられると、 | |
7884 | 5009 | 1 要素に 1 行の入力行すべてからなるリストが返されます。 |
7885 | 5010 | これを使うと簡単にかなり大きなデータになってしまいますので、 |
7886 | 5011 | 注意を要します。 |
7887 | 5012 | |
7888 | 5013 | =begin original |
7889 | 5014 | |
7890 | ||
5015 | <FILEHANDLE> may also be spelled C<readline(*FILEHANDLE)>. | |
7891 | 5016 | See L<perlfunc/readline>. |
7892 | 5017 | |
7893 | 5018 | =end original |
7894 | 5019 | |
7895 | ||
5020 | <FILEHANDLE> は C<readline(*FILEHANDLE)> とも書けます。 | |
7896 | 5021 | L<perlfunc/readline> を参照して下さい。 |
7897 | 5022 | |
7898 | 5023 | =begin original |
7899 | 5024 | |
7900 | The null filehandle | |
5025 | The null filehandle <> is special: it can be used to emulate the | |
7901 | ||
5026 | behavior of B<sed> and B<awk>. Input from <> comes either from | |
7902 | behavior of B<sed> and B<awk>, and any other Unix filter program | |
7903 | that takes a list of filenames, doing the same to each line | |
7904 | of input from all of them. Input from C<< <> >> comes either from | |
7905 | 5027 | standard input, or from each file listed on the command line. Here's |
7906 | how it works: the first time | |
5028 | how it works: the first time <> is evaluated, the @ARGV array is | |
7907 | checked, and if it is empty, C<$ARGV[0]> is set to | |
5029 | checked, and if it is empty, C<$ARGV[0]> is set to "-", which when opened | |
7908 | gives you standard input. The | |
5030 | gives you standard input. The @ARGV array is then processed as a list | |
7909 | 5031 | of filenames. The loop |
7910 | 5032 | |
7911 | 5033 | =end original |
7912 | 5034 | |
7913 | ヌルファイルハンドル | |
5035 | ヌルファイルハンドル <> は特別で、B<sed> や B<awk> の動作を | |
7914 | ||
5036 | エミュレートするために使われます。 | |
7915 | ||
5037 | <> からの入力は、標準入力からか、コマンドライン上に並べられた個々の | |
7916 | その他の Unix フィルタプログラムをエミュレートするために使われます。 | |
7917 | C<< <> >> からの入力は、標準入力からか、コマンドライン上に並べられた個々の | |
7918 | 5038 | ファイルから行なわれます。 |
7919 | 動作の概要は、以下のようになります | |
5039 | 動作の概要は、以下のようになります。 | |
7920 | ||
5040 | 最初に <> が評価されると、配列 @ARGV が調べられ、空であれば、 | |
5041 | C<$ARGV[0]> に "-"を設定します。 | |
7921 | 5042 | これは、open されるとき標準入力となります。 |
7922 | その後、配列 | |
5043 | その後、配列 @ARGV がファイル名のリストとして処理されます。 | |
7923 | 5044 | |
7924 | 5045 | while (<>) { |
7925 | 5046 | ... # code for each line |
7926 | 5047 | } |
7927 | 5048 | |
7928 | 5049 | =begin original |
7929 | 5050 | |
7930 | 5051 | is equivalent to the following Perl-like pseudo code: |
7931 | 5052 | |
7932 | 5053 | =end original |
7933 | 5054 | |
7934 | 5055 | は以下ののような Perl の擬似コードと等価です: |
7935 | 5056 | |
7936 | 5057 | unshift(@ARGV, '-') unless @ARGV; |
7937 | 5058 | while ($ARGV = shift) { |
7938 | 5059 | open(ARGV, $ARGV); |
7939 | 5060 | while (<ARGV>) { |
7940 | 5061 | ... # code for each line |
7941 | 5062 | } |
7942 | 5063 | } |
7943 | 5064 | |
7944 | 5065 | =begin original |
7945 | 5066 | |
7946 | 5067 | except that it isn't so cumbersome to say, and will actually work. |
7947 | It really does shift the | |
5068 | It really does shift the @ARGV array and put the current filename | |
7948 | into the | |
5069 | into the $ARGV variable. It also uses filehandle I<ARGV> | |
7949 | internally | |
5070 | internally--<> is just a synonym for <ARGV>, which | |
7950 | 5071 | is magical. (The pseudo code above doesn't work because it treats |
7951 | ||
5072 | <ARGV> as non-magical.) | |
7952 | 5073 | |
7953 | 5074 | =end original |
7954 | 5075 | |
7955 | 5076 | 但し、わずらわしく書かなくても、動作します。 |
7956 | 実際に | |
5077 | 実際に @ARGV を shift しますし、その時点のファイル名を変数 $ARGV に | |
7957 | 5078 | 入れています。 |
7958 | また、内部的にファイルハンドル | |
5079 | また、内部的にファイルハンドル ARGV を使っていて、<> はマジカルな | |
7959 | ||
5080 | <ARGV> の同義語となっています。 | |
7960 | (上記の擬似コードは、 | |
5081 | (上記の擬似コードは、<ARGV> を通常のものとして扱っているので、 | |
7961 | ||
5082 | うまく動作しません。) | |
7962 | 5083 | |
7963 | 5084 | =begin original |
7964 | 5085 | |
7965 | 5086 | Since the null filehandle uses the two argument form of L<perlfunc/open> |
7966 | 5087 | it interprets special characters, so if you have a script like this: |
7967 | 5088 | |
7968 | 5089 | =end original |
7969 | 5090 | |
7970 | 5091 | 空ファイルハンドルは 2 引数の L<perlfunc/open> を使った特別な文字列なので、 |
7971 | 5092 | もし以下のようなスクリプトを書いて: |
7972 | 5093 | |
7973 | 5094 | while (<>) { |
7974 | 5095 | print; |
7975 | 5096 | } |
7976 | 5097 | |
7977 | 5098 | =begin original |
7978 | 5099 | |
7979 | and call it with | |
5100 | and call it with C<perl dangerous.pl 'rm -rfv *|'>, it actually opens a | |
7980 | 5101 | pipe, executes the C<rm> command and reads C<rm>'s output from that pipe. |
7981 | 5102 | If you want all items in C<@ARGV> to be interpreted as file names, you |
7982 | can use the module C<ARGV::readonly> from CPAN | |
5103 | can use the module C<ARGV::readonly> from CPAN. | |
7983 | diamond bracket: | |
7984 | 5104 | |
7985 | 5105 | =end original |
7986 | 5106 | |
7987 | これを | |
5107 | これを C<perl dangerous.pl 'rm -rfv *|'> として呼び出すと、実際には | |
7988 | 5108 | パイプを開き、C<rm> コマンドを実行して、 C<rm> の出力をパイプから読みます。 |
7989 | 5109 | もし C<@ARGV> の全ての要素をファイル名として解釈させたいなら、 |
7990 | CPAN にある C<ARGV::readonly> モジュール | |
5110 | CPAN にある C<ARGV::readonly> モジュールが使えます。 | |
7991 | 使えます。 | |
7992 | 5111 | |
7993 | while (<<>>) { | |
7994 | print; | |
7995 | } | |
7996 | ||
7997 | 5112 | =begin original |
7998 | 5113 | |
7999 | ||
5114 | You can modify @ARGV before the first <> as long as the array ends up | |
8000 | three argument form (with the second argument being C<< < >>), so all | |
8001 | arguments in C<ARGV> are treated as literal filenames (including C<"-">). | |
8002 | (Note that for convenience, if you use C<< <<>> >> and if C<@ARGV> is | |
8003 | empty, it will still read from the standard input.) | |
8004 | ||
8005 | =end original | |
8006 | ||
8007 | while の中で二重角かっこを使うと、 | |
8008 | 3 引数型式 (かつ 2 番目の引数が C<< < >> の) open を引き起こすので、 | |
8009 | C<ARGV> の全ての引数は (C<"-"> を含めて) リテラル名ファイル名として | |
8010 | 扱われます。 | |
8011 | (便宜のため、C<< <<>> >> を使って C<@ARGV> が空の場合、標準入力から | |
8012 | 読み込みます。) | |
8013 | ||
8014 | =begin original | |
8015 | ||
8016 | You can modify C<@ARGV> before the first C<< <> >> as long as the array ends up | |
8017 | 5115 | containing the list of filenames you really want. Line numbers (C<$.>) |
8018 | 5116 | continue as though the input were one big happy file. See the example |
8019 | 5117 | in L<perlfunc/eof> for how to reset line numbers on each file. |
8020 | 5118 | |
8021 | 5119 | =end original |
8022 | 5120 | |
8023 | 最終的に、 | |
5121 | 最終的に、@ARGV に扱いたいと思っているファイル名が含まれるのであれば、 | |
8024 | 最初に | |
5122 | 最初に <> を評価する前に @ARGV を変更することも可能です。 | |
8025 | 5123 | 行番号 (C<$.>) は、入力ファイルがあたかも 1 つの大きなファイルで |
8026 | 5124 | あるかのように、続けてカウントされます。 |
8027 | 5125 | 個々のファイルごとにリセットする方法は、L<perlfunc/eof> の例を |
8028 | 5126 | 参照してください。 |
8029 | 5127 | |
8030 | 5128 | =begin original |
8031 | 5129 | |
8032 | If you want to set | |
5130 | If you want to set @ARGV to your own list of files, go right ahead. | |
8033 | This sets | |
5131 | This sets @ARGV to all plain text files if no @ARGV was given: | |
8034 | 5132 | |
8035 | 5133 | =end original |
8036 | 5134 | |
8037 | 最初から | |
5135 | 最初から @ARGV に自分でファイルのリストを設定してもかまいません。 | |
8038 | 以下は | |
5136 | 以下は @ARGV が与えられなかったときに全てのテキストファイルを | |
8039 | ||
5137 | @ARGV に設定します。 | |
8040 | 5138 | |
8041 | 5139 | @ARGV = grep { -f && -T } glob('*') unless @ARGV; |
8042 | 5140 | |
8043 | 5141 | =begin original |
8044 | 5142 | |
8045 | 5143 | You can even set them to pipe commands. For example, this automatically |
8046 | 5144 | filters compressed arguments through B<gzip>: |
8047 | 5145 | |
8048 | 5146 | =end original |
8049 | 5147 | |
8050 | 5148 | ここにパイプコマンドを置くことも出来ます。 |
8051 | 5149 | 例えば、以下は圧縮された引数を自動的に B<gzip> のフィルタに通します: |
8052 | 5150 | |
8053 | 5151 | @ARGV = map { /\.(gz|Z)$/ ? "gzip -dc < $_ |" : $_ } @ARGV; |
8054 | 5152 | |
8055 | 5153 | =begin original |
8056 | 5154 | |
8057 | 5155 | If you want to pass switches into your script, you can use one of the |
8058 | ||
5156 | Getopts modules or put a loop on the front like this: | |
8059 | 5157 | |
8060 | 5158 | =end original |
8061 | 5159 | |
8062 | スクリプトにスイッチを渡したいのであれば、 | |
5160 | スクリプトにスイッチを渡したいのであれば、Getopts モジュールを | |
8063 | 5161 | 使うこともできますし、実際の処理の前にのようなループを置くこともできます。 |
8064 | 5162 | |
8065 | 5163 | while ($_ = $ARGV[0], /^-/) { |
8066 | 5164 | shift; |
8067 | 5165 | last if /^--$/; |
8068 | 5166 | if (/^-D(.*)/) { $debug = $1 } |
8069 | 5167 | if (/^-v/) { $verbose++ } |
8070 | 5168 | # ... # other switches |
8071 | 5169 | } |
8072 | 5170 | |
8073 | 5171 | while (<>) { |
8074 | 5172 | # ... # code for each line |
8075 | 5173 | } |
8076 | 5174 | |
8077 | 5175 | =begin original |
8078 | 5176 | |
8079 | The | |
5177 | The <> symbol will return C<undef> for end-of-file only once. | |
8080 | 5178 | If you call it again after this, it will assume you are processing another |
8081 | ||
5179 | @ARGV list, and if you haven't set @ARGV, will read input from STDIN. | |
8082 | 5180 | |
8083 | 5181 | =end original |
8084 | 5182 | |
8085 | シンボル | |
5183 | シンボル <> がファイルの最後で C<undef> を返すのは一度きりです。 | |
8086 | そのあとでもう一度呼び出すと、新たに別の | |
5184 | そのあとでもう一度呼び出すと、新たに別の @ARGV を処理するものとみなされ、 | |
8087 | その時に | |
5185 | その時に @ARGV を設定しなおしていないと、STDIN からの入力を | |
8088 | 5186 | 読み込むことになります。 |
8089 | 5187 | |
8090 | 5188 | =begin original |
8091 | 5189 | |
8092 | If what the angle brackets contain is a simple scalar variable ( | |
5190 | If what the angle brackets contain is a simple scalar variable (e.g., | |
8093 | ||
5191 | <$foo>), then that variable contains the name of the | |
8094 | 5192 | filehandle to input from, or its typeglob, or a reference to the |
8095 | 5193 | same. For example: |
8096 | 5194 | |
8097 | 5195 | =end original |
8098 | 5196 | |
8099 | 山括弧の中の文字列が ( | |
5197 | 山括弧の中の文字列が (<$foo> のような) 単純スカラ変数であれば、 | |
8100 | 5198 | その変数が入力を行なうファイルハンドルの名前そのもの、名前への型グロブ、 |
8101 | 5199 | 名前へのリファレンスのいずれかを示しているとみなされます。 |
8102 | 例えば: | |
8103 | 5200 | |
8104 | 5201 | $fh = \*STDIN; |
8105 | 5202 | $line = <$fh>; |
8106 | 5203 | |
8107 | 5204 | =begin original |
8108 | 5205 | |
8109 | 5206 | If what's within the angle brackets is neither a filehandle nor a simple |
8110 | 5207 | scalar variable containing a filehandle name, typeglob, or typeglob |
8111 | 5208 | reference, it is interpreted as a filename pattern to be globbed, and |
8112 | 5209 | either a list of filenames or the next filename in the list is returned, |
8113 | 5210 | depending on context. This distinction is determined on syntactic |
8114 | grounds alone. That means C<< <$x> >> is always a | |
5211 | grounds alone. That means C<< <$x> >> is always a readline() from | |
8115 | an indirect handle, but C<< <$hash{key}> >> is always a | |
5212 | an indirect handle, but C<< <$hash{key}> >> is always a glob(). | |
8116 | That's because | |
5213 | That's because $x is a simple scalar variable, but C<$hash{key}> is | |
8117 | 5214 | not--it's a hash element. Even C<< <$x > >> (note the extra space) |
8118 | 5215 | is treated as C<glob("$x ")>, not C<readline($x)>. |
8119 | 5216 | |
8120 | 5217 | =end original |
8121 | 5218 | |
8122 | 5219 | 山括弧の中の文字列がファイルハンドルでもファイルハンドル名、型グロブ、 |
8123 | 5220 | 型グロブリファレンスのいずれかが入った単純スカラ変数でもなければ、 |
8124 | 5221 | グロブを行なうファイル名のパターンと解釈され、コンテキストによって |
8125 | 5222 | ファイル名のリストか、そのリストの次のファイル名が返されます。 |
8126 | 5223 | この区別は単に構文的に行われます。 |
8127 | C<< <$x> >> は常に間接ハンドルから | |
5224 | C<< <$x> >> は常に間接ハンドルから readline() しますが、 | |
8128 | C<< <$hash{key}> >> は常に | |
5225 | C<< <$hash{key}> >> は常に glob() します。 | |
8129 | ||
5226 | $x は単純スカラー変数ですが、C<$hash{key}> は違う(ハッシュ要素)からです。 | |
8130 | 5227 | C<< <$x > >> (余分な空白に注意) ですら C<readline($x)> ではなく |
8131 | 5228 | C<glob("$x ")> として扱われます。 |
8132 | 5229 | |
8133 | 5230 | =begin original |
8134 | 5231 | |
8135 | 5232 | One level of double-quote interpretation is done first, but you can't |
8136 | 5233 | say C<< <$foo> >> because that's an indirect filehandle as explained |
8137 | 5234 | in the previous paragraph. (In older versions of Perl, programmers |
8138 | 5235 | would insert curly brackets to force interpretation as a filename glob: |
8139 | 5236 | C<< <${foo}> >>. These days, it's considered cleaner to call the |
8140 | 5237 | internal function directly as C<glob($foo)>, which is probably the right |
8141 | 5238 | way to have done it in the first place.) For example: |
8142 | 5239 | |
8143 | 5240 | =end original |
8144 | 5241 | |
8145 | 5242 | まず、1 段階だけダブルクォート展開が行なわれますが、前の段落に書いた |
8146 | 5243 | 間接ファイルハンドルと同じになる、C<< <$foo> >> のようには書けません。 |
8147 | 5244 | (Perl の古いバージョンでは、ファイル名グロブと解釈させるために |
8148 | 5245 | C<< <${foo}> >> のように中括弧を入れていました。 |
8149 | 最近ではより明確にするために、C<glob($foo)> と内部関数を | |
5246 | 最近ではより明確にするために、C<glob($foo)> と内部関数を | |
8150 | できます | |
5247 | 呼ぶこともできます。 | |
8151 | ||
5248 | おそらく、まず、こちらの方で試すのが正解でしょう。) | |
5249 | 例: | |
8152 | 5250 | |
8153 | 5251 | while (<*.c>) { |
8154 | 5252 | chmod 0644, $_; |
8155 | 5253 | } |
8156 | 5254 | |
8157 | 5255 | =begin original |
8158 | 5256 | |
8159 | 5257 | is roughly equivalent to: |
8160 | 5258 | |
8161 | 5259 | =end original |
8162 | 5260 | |
8163 | 5261 | はだいたい以下と等価です: |
8164 | 5262 | |
8165 | 5263 | open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|"); |
8166 | 5264 | while (<FOO>) { |
8167 | 5265 | chomp; |
8168 | 5266 | chmod 0644, $_; |
8169 | 5267 | } |
8170 | 5268 | |
8171 | 5269 | =begin original |
8172 | 5270 | |
8173 | 5271 | except that the globbing is actually done internally using the standard |
8174 | C< | |
5272 | C<File::Glob> extension. Of course, the shortest way to do the above is: | |
8175 | 5273 | |
8176 | 5274 | =end original |
8177 | 5275 | |
8178 | 但し実際のグロブは内部的に標準の C< | |
5276 | 但し実際のグロブは内部的に標準の C<File::Glob> モジュールを使います。 | |
8179 | 5277 | もちろん、もっと簡単に以下のように書けます: |
8180 | 5278 | |
8181 | 5279 | chmod 0644, <*.c>; |
8182 | 5280 | |
8183 | 5281 | =begin original |
8184 | 5282 | |
8185 | 5283 | A (file)glob evaluates its (embedded) argument only when it is |
8186 | 5284 | starting a new list. All values must be read before it will start |
8187 | 5285 | over. In list context, this isn't important because you automatically |
8188 | 5286 | get them all anyway. However, in scalar context the operator returns |
8189 | 5287 | the next value each time it's called, or C<undef> when the list has |
8190 | 5288 | run out. As with filehandle reads, an automatic C<defined> is |
8191 | 5289 | generated when the glob occurs in the test part of a C<while>, |
8192 | because legal glob returns ( | |
5290 | because legal glob returns (e.g. a file called F<0>) would otherwise | |
8193 | a file called F<0>) would otherwise | |
8194 | 5291 | terminate the loop. Again, C<undef> is returned only once. So if |
8195 | 5292 | you're expecting a single value from a glob, it is much better to |
8196 | 5293 | say |
8197 | 5294 | |
8198 | 5295 | =end original |
8199 | 5296 | |
8200 | 5297 | (ファイル)グロブは新しいリストを開始するときにだけ(組み込みの)引数を |
8201 | 5298 | 評価します。 |
8202 | 5299 | 全ての値は開始する前に読み込んでいなければなりません。 |
8203 | 5300 | これはリストコンテキストでは、とにかく自動的に全てを取り込むので |
8204 | 5301 | 重要ではありません。 |
8205 | しかし、スカラコンテキストではこの演算子は呼び出された時の | |
5302 | しかし、スカラーコンテキストではこの演算子は呼び出された時の | |
8206 | 5303 | 次の値か、リストがなくなったときには C<undef> を返します。 |
8207 | 5304 | ファイルハンドルを読み込む場合は、グロブが C<while> の条件部にある場合は |
8208 | 自動的な C<defined> が生成されます | |
5305 | 自動的な C<defined> が生成されます。 | |
8209 | なぜならそうしないと、本来の glob の返り値 ( | |
5306 | なぜならそうしないと、本来の glob の返り値 (F<0> というファイル) が | |
8210 | 5307 | ループを終了させるからです。 |
8211 | 5308 | ここでも、C<undef> は一度だけ返されます。 |
8212 | 5309 | 従って、もしグロブから一つの値だけを想定している場合、 |
8213 | 5310 | 以下のように書くことが: |
8214 | 5311 | |
8215 | 5312 | ($file) = <blurch*>; |
8216 | 5313 | |
8217 | 5314 | =begin original |
8218 | 5315 | |
8219 | 5316 | than |
8220 | 5317 | |
8221 | 5318 | =end original |
8222 | 5319 | |
8223 | 5320 | 以下のように書くよりはるかに良いです: |
8224 | 5321 | |
8225 | 5322 | $file = <blurch*>; |
8226 | 5323 | |
8227 | 5324 | =begin original |
8228 | 5325 | |
8229 | 5326 | because the latter will alternate between returning a filename and |
8230 | 5327 | returning false. |
8231 | 5328 | |
8232 | 5329 | =end original |
8233 | 5330 | |
8234 | 5331 | なぜなら後者はファイル名を返す場合と偽を返す場合があるからです。 |
8235 | 5332 | |
8236 | 5333 | =begin original |
8237 | 5334 | |
8238 | 5335 | If you're trying to do variable interpolation, it's definitely better |
8239 | to use the | |
5336 | to use the glob() function, because the older notation can cause people | |
8240 | 5337 | to become confused with the indirect filehandle notation. |
8241 | 5338 | |
8242 | 5339 | =end original |
8243 | 5340 | |
8244 | 変数変換に挑戦する場合、明らかに | |
5341 | 変数変換に挑戦する場合、明らかに glob() 関数を使う方が良いです。 | |
8245 | 5342 | なぜなら古い表記は間接ファイルハンドル表記と混乱するかも知れないからです。 |
8246 | 5343 | |
8247 | 5344 | @files = glob("$dir/*.[ch]"); |
8248 | 5345 | @files = glob($files[$i]); |
8249 | 5346 | |
8250 | =begin original | |
8251 | ||
8252 | If an angle-bracket-based globbing expression is used as the condition of | |
8253 | a C<while> or C<for> loop, then it will be implicitly assigned to C<$_>. | |
8254 | If either a globbing expression or an explicit assignment of a globbing | |
8255 | expression to a scalar is used as a C<while>/C<for> condition, then | |
8256 | the condition actually tests for definedness of the expression's value, | |
8257 | not for its regular truth value. | |
8258 | ||
8259 | =end original | |
8260 | ||
8261 | 角かっこグロブ式が C<while> や C<for> ループの条件として使われた場合、 | |
8262 | これは暗黙に C<$_> に代入されます。 | |
8263 | さらに、グロブ式またはグロブ式からスカラへの明示的な代入が | |
8264 | C<while>/C<for> の条件部として使われた場合、 | |
8265 | 条件は通常の真の値かどうかではなく、式の値が定義されているかどうかを | |
8266 | テストします。 | |
8267 | ||
8268 | 5347 | =head2 Constant Folding |
8269 | 5348 | X<constant folding> X<folding> |
8270 | 5349 | |
8271 | 5350 | (定数の畳み込み) |
8272 | 5351 | |
8273 | 5352 | =begin original |
8274 | 5353 | |
8275 | 5354 | Like C, Perl does a certain amount of expression evaluation at |
8276 | 5355 | compile time whenever it determines that all arguments to an |
8277 | 5356 | operator are static and have no side effects. In particular, string |
8278 | 5357 | concatenation happens at compile time between literals that don't do |
8279 | 5358 | variable substitution. Backslash interpolation also happens at |
8280 | 5359 | compile time. You can say |
8281 | 5360 | |
8282 | 5361 | =end original |
8283 | 5362 | |
8284 | 5363 | C と同じように Perl でも、演算子に対するすべての引数がスタティックで、 |
8285 | 5364 | 副作用がないと判断できれば、コンパイル時に式の評価を行なってしまいます。 |
8286 | 5365 | 特に、変数置換の無いリテラルどうしの文字列連結はコンパイル時に行なわれます。 |
8287 | 5366 | バックスラッシュの解釈もコンパイル時に行なわれます。 |
8288 | 以下のように書けて、 | |
8289 | 5367 | |
8290 | | |
5368 | 'Now is the time for all' . "\n" . | |
8291 | . | |
5369 | 'good men to come to.' | |
8292 | . 'good men to come to.' | |
8293 | 5370 | |
8294 | 5371 | =begin original |
8295 | 5372 | |
8296 | 5373 | and this all reduces to one string internally. Likewise, if |
8297 | 5374 | you say |
8298 | 5375 | |
8299 | 5376 | =end original |
8300 | 5377 | |
8301 | 内部的に 1 つの文字列になります。同様に | |
5378 | と書いても、内部的に 1 つの文字列になります。同様に | |
8302 | 5379 | |
8303 | 5380 | foreach $file (@filenames) { |
8304 | 5381 | if (-s $file > 5 + 100 * 2**16) { } |
8305 | 5382 | } |
8306 | 5383 | |
8307 | 5384 | =begin original |
8308 | 5385 | |
8309 | the compiler precompute | |
5386 | the compiler will precompute the number which that expression | |
8310 | 5387 | represents so that the interpreter won't have to. |
8311 | 5388 | |
8312 | 5389 | =end original |
8313 | 5390 | |
8314 | 5391 | と書くとコンパイラは、式が表わす数値をあらかじめ計算しますので、 |
8315 | 5392 | インタプリタで計算する必要がなくなっています。 |
8316 | 5393 | |
8317 | 5394 | =head2 No-ops |
8318 | 5395 | X<no-op> X<nop> |
8319 | 5396 | |
8320 | 5397 | (無実行) |
8321 | 5398 | |
8322 | 5399 | =begin original |
8323 | 5400 | |
8324 | 5401 | Perl doesn't officially have a no-op operator, but the bare constants |
8325 | C<0> and C<1> are special-cased not | |
5402 | C<0> and C<1> are special-cased to not produce a warning in a void | |
8326 | 5403 | context, so you can for example safely do |
8327 | 5404 | |
8328 | 5405 | =end original |
8329 | 5406 | |
8330 | 5407 | Perl は公式には無実行演算子はありませんが、裸の定数 C<0> と C<1> は |
8331 | 5408 | 特別に無効コンテキストでも警告を出さないことになっているので、 |
8332 | 5409 | 例えば安全に以下のように書けます: |
8333 | 5410 | |
8334 | 5411 | 1 while foo(); |
8335 | 5412 | |
8336 | 5413 | =head2 Bitwise String Operators |
8337 | X<operator, bitwise, string> | |
5414 | X<operator, bitwise, string> | |
8338 | 5415 | |
8339 | 5416 | (ビット列演算子) |
8340 | 5417 | |
8341 | 5418 | =begin original |
8342 | 5419 | |
8343 | 5420 | Bitstrings of any size may be manipulated by the bitwise operators |
8344 | 5421 | (C<~ | & ^>). |
8345 | 5422 | |
8346 | 5423 | =end original |
8347 | 5424 | |
8348 | 5425 | 任意のサイズのビット列はビット単位演算子(C<~ | & ^>)で操作できます。 |
8349 | 5426 | |
8350 | 5427 | =begin original |
8351 | 5428 | |
8352 | 5429 | If the operands to a binary bitwise op are strings of different |
8353 | 5430 | sizes, B<|> and B<^> ops act as though the shorter operand had |
8354 | 5431 | additional zero bits on the right, while the B<&> op acts as though |
8355 | 5432 | the longer operand were truncated to the length of the shorter. |
8356 | 5433 | The granularity for such extension or truncation is one or more |
8357 | 5434 | bytes. |
8358 | 5435 | |
8359 | 5436 | =end original |
8360 | 5437 | |
8361 | 5438 | 二項ビット単位演算子のオペランドが異なった長さの文字列だった場合、 |
8362 | 5439 | B<|> と B<^> の演算子は短い側のオペランドの右側に追加のゼロが |
8363 | ついているとみなします | |
5440 | ついているとみなします。 | |
8364 | 切り詰められます。 | |
5441 | 一方 B<&> 演算子は長い方のオペランドが短い方に切り詰められます。 | |
8365 | 5442 | この拡張や短縮の粒度はバイト単位です。 |
8366 | 5443 | |
8367 | 5444 | # ASCII-based examples |
8368 | 5445 | print "j p \n" ^ " a h"; # prints "JAPH\n" |
8369 | 5446 | print "JA" | " ph\n"; # prints "japh\n" |
8370 | 5447 | print "japh\nJunk" & '_____'; # prints "JAPH\n"; |
8371 | 5448 | print 'p N$' ^ " E<H\n"; # prints "Perl\n"; |
8372 | 5449 | |
8373 | 5450 | =begin original |
8374 | 5451 | |
8375 | 5452 | If you are intending to manipulate bitstrings, be certain that |
8376 | 5453 | you're supplying bitstrings: If an operand is a number, that will imply |
8377 | 5454 | a B<numeric> bitwise operation. You may explicitly show which type of |
8378 | 5455 | operation you intend by using C<""> or C<0+>, as in the examples below. |
8379 | 5456 | |
8380 | 5457 | =end original |
8381 | 5458 | |
8382 | 5459 | ビット列を操作したい場合は、確実にビット列が渡されるようにしてください: |
8383 | 5460 | オペランドが数字の場合、B<数値> ビット単位演算を仮定します。 |
8384 | 5461 | 明示的に演算の型を指定するときには、以下の例のように |
8385 | 5462 | C<""> か C<0+> を使ってください。 |
8386 | 5463 | |
8387 | =begin original | |
8388 | ||
8389 | 5464 | $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF) |
8390 | 5465 | $foo = '150' | 105; # yields 255 |
8391 | 5466 | $foo = 150 | '105'; # yields 255 |
8392 | 5467 | $foo = '150' | '105'; # yields string '155' (under ASCII) |
8393 | 5468 | |
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 | ||
8403 | 5469 | $baz = 0+$foo & 0+$bar; # both ops explicitly numeric |
8404 | 5470 | $biz = "$foo" ^ "$bar"; # both ops explicitly stringy |
8405 | 5471 | |
8406 | =end original | |
8407 | ||
8408 | $baz = 0+$foo & 0+$bar; # どちらのオペランドも明示的に数値 | |
8409 | $biz = "$foo" ^ "$bar"; # どちらのオペランドも明示的に文字列 | |
8410 | ||
8411 | 5472 | =begin original |
8412 | 5473 | |
8413 | This somewhat unpredictable behavior can be avoided with the "bitwise" | |
8414 | feature, 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 | |
8416 | in the C<"experimental::bitwise"> category. Under this feature, the four | |
8417 | standard bitwise operators (C<~ | & ^>) are always numeric. Adding a dot | |
8418 | after each operator (C<~. |. &. ^.>) forces it to treat its operands as | |
8419 | strings: | |
8420 | ||
8421 | =end original | |
8422 | ||
8423 | この、いくらか予測の難しい振る舞いは、Perl 5.22 で導入された | |
8424 | "bitwise" 機能で避けることができます。 | |
8425 | これは S<C<use feature 'bitwise'>> か C<use v5.28> で有効にできます。 | |
8426 | Perl 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 | ||
8447 | The assignment variants of these operators (C<&= |= ^= &.= |.= ^.=>) | |
8448 | behave likewise under the feature. | |
8449 | ||
8450 | =end original | |
8451 | ||
8452 | この機能の基では、これらの演算子の亜種 (C<&= |= ^= &.= |.= ^.=>) も | |
8453 | 同様に振る舞います。 | |
8454 | ||
8455 | =begin original | |
8456 | ||
8457 | It is a fatal error if an operand contains a character whose ordinal | |
8458 | value is above 0xFF, and hence not expressible except in UTF-8. The | |
8459 | operation is performed on a non-UTF-8 copy for other operands encoded in | |
8460 | UTF-8. See L<perlunicode/Byte and Character Semantics>. | |
8461 | ||
8462 | =end original | |
8463 | ||
8464 | 値が 0xFF 以上で、UTF-8 以外で表現できない文字がオペランドに含まれている | |
8465 | 場合、これは致命的エラーになります。 | |
8466 | 他の UTF-8 でエンコードされたオペランドに対して | |
8467 | 非 UTF-8 のコピーに対して操作が行われます。 | |
8468 | L<perlunicode/Byte and Character Semantics> を参照してください。 | |
8469 | ||
8470 | =begin original | |
8471 | ||
8472 | 5474 | See L<perlfunc/vec> for information on how to manipulate individual bits |
8473 | 5475 | in a bit vector. |
8474 | 5476 | |
8475 | 5477 | =end original |
8476 | 5478 | |
8477 | 5479 | ビットベクタの個々のビットをどのように操作するかの情報については |
8478 | 5480 | L<perlfunc/vec> を参照して下さい。 |
8479 | 5481 | |
8480 | 5482 | =head2 Integer Arithmetic |
8481 | 5483 | X<integer> |
8482 | 5484 | |
8483 | 5485 | (整数演算) |
8484 | 5486 | |
8485 | 5487 | =begin original |
8486 | 5488 | |
8487 | 5489 | By default, Perl assumes that it must do most of its arithmetic in |
8488 | 5490 | floating point. But by saying |
8489 | 5491 | |
8490 | 5492 | =end original |
8491 | 5493 | |
8492 | 5494 | デフォルトでは、Perl は演算を浮動小数で行なわなければならないものと |
8493 | 5495 | しています。 |
8494 | 5496 | しかし、(もしそうしたいなら) |
8495 | 5497 | |
8496 | 5498 | use integer; |
8497 | 5499 | |
8498 | 5500 | =begin original |
8499 | 5501 | |
8500 | you may tell the compiler to use integer operations | |
5502 | you may tell the compiler that it's okay to use integer operations | |
8501 | ( | |
5503 | (if it feels like it) from here to the end of the enclosing BLOCK. | |
8502 | ||
5504 | An inner BLOCK may countermand this by saying | |
8503 | 5505 | |
8504 | 5506 | =end original |
8505 | 5507 | |
8506 | と書けば、その場所から現在の BLOCK の終わりまでは、整数演算 | |
5508 | と書けば、その場所から現在の BLOCK の終わりまでは、整数演算を | |
8507 | (詳しい説明は L<integer> を参照してください) を | |
8508 | 5509 | 行なってよいと、コンパイラに指示することができます。 |
8509 | 5510 | 内部の BLOCK で、 |
8510 | 5511 | |
8511 | 5512 | no integer; |
8512 | 5513 | |
8513 | 5514 | =begin original |
8514 | 5515 | |
8515 | 5516 | which lasts until the end of that BLOCK. Note that this doesn't |
8516 | mean everything is an integer, merely that Perl | |
5517 | mean everything is only an integer, merely that Perl may use integer | |
8517 | operations f | |
5518 | operations if it is so inclined. For example, even under C<use | |
8518 | ||
5519 | integer>, if you take the C<sqrt(2)>, you'll still get C<1.4142135623731> | |
8519 | ||
5520 | or so. | |
8520 | 5521 | |
8521 | 5522 | =end original |
8522 | 5523 | |
8523 | 5524 | と書けば、その BLOCK の終わりまでは、指示を取り消すことになります。 |
8524 | これは全てを整数だけを使って処理することを意味するわけではな | |
5525 | これは全てを整数だけを使って処理することを意味するわけではないことに | |
8525 | ||
5526 | 注意してください。 | |
8526 | ||
5527 | これは単に Perl が整数を使いたいと思ったときに使うかもしれない、 | |
8527 | ||
5528 | というだけです。 | |
5529 | 例えば、C<use integer> の指定があっても、C<sqrt(2)> とすると、 | |
8528 | 5530 | C<1.4142135623731> といった結果が返ってきます。 |
8529 | 5531 | |
8530 | 5532 | =begin original |
8531 | 5533 | |
8532 | Used on numbers, the bitwise operators ( | |
5534 | Used on numbers, the bitwise operators ("&", "|", "^", "~", "<<", | |
8533 | ||
5535 | and ">>") always produce integral results. (But see also | |
8534 | L< | |
5536 | L<Bitwise String Operators>.) However, C<use integer> still has meaning for | |
8535 | 5537 | them. By default, their results are interpreted as unsigned integers, but |
8536 | if | |
5538 | if C<use integer> is in effect, their results are interpreted | |
8537 | 5539 | as signed integers. For example, C<~0> usually evaluates to a large |
8538 | integral value. However, | |
5540 | integral value. However, C<use integer; ~0> is C<-1> on two's-complement | |
8539 | 5541 | machines. |
8540 | 5542 | |
8541 | 5543 | =end original |
8542 | 5544 | |
8543 | 数値を使う場合、ビット単位演算子 ( | |
5545 | 数値を使う場合、ビット単位演算子 ("&", "|", "^", "~", "<<", ">>") は | |
8544 | 常に整数の結果を生成します | |
5546 | 常に整数の結果を生成します(但し L<Bitwise String Operators> も | |
8545 | ||
5547 | 参照して下さい)。 | |
8546 | しかし、それでも | |
5548 | しかし、それでも C<use integer> は意味があります。 | |
8547 | 5549 | デフォルトでは、これらの結果は符号なし整数として解釈されますが、 |
8548 | ||
5550 | C<use integer> が有効の場合は、符号付き整数として解釈されます。 | |
8549 | 5551 | 例えば、C<~0> は通常大きな整数の値として評価されます。 |
8550 | しかし、 | |
5552 | しかし、C<use integer; ~0> は 2 の補数のマシンでは C<-1> になります。 | |
8551 | 5553 | |
8552 | 5554 | =head2 Floating-point Arithmetic |
8553 | ||
8554 | (浮動小数点数演算) | |
8555 | ||
8556 | 5555 | X<floating-point> X<floating point> X<float> X<real> |
8557 | 5556 | |
5557 | (浮動小数点演算) | |
5558 | ||
8558 | 5559 | =begin original |
8559 | 5560 | |
8560 | While | |
5561 | While C<use integer> provides integer-only arithmetic, there is no | |
8561 | 5562 | analogous mechanism to provide automatic rounding or truncation to a |
8562 | 5563 | certain number of decimal places. For rounding to a certain number |
8563 | of digits, | |
5564 | of digits, sprintf() or printf() is usually the easiest route. | |
8564 | 5565 | See L<perlfaq4>. |
8565 | 5566 | |
8566 | 5567 | =end original |
8567 | 5568 | |
8568 | ||
5569 | C<use integer> が整数演算を提供する一方、数を特定の桁で自動的に丸めたり | |
8569 | 5570 | 切り捨てたりする機構はありません。 |
8570 | 数を丸めるには、 | |
5571 | 数を丸めるには、sprintf() や printf() を使うのが一番簡単な方法です。 | |
8571 | 5572 | L<perlfaq4> を参照して下さい。 |
8572 | 5573 | |
8573 | 5574 | =begin original |
8574 | 5575 | |
8575 | 5576 | Floating-point numbers are only approximations to what a mathematician |
8576 | 5577 | would call real numbers. There are infinitely more reals than floats, |
8577 | 5578 | so some corners must be cut. For example: |
8578 | 5579 | |
8579 | 5580 | =end original |
8580 | 5581 | |
8581 | 5582 | 浮動小数点数は数学者が実数と呼ぶものの近似でしかありません。 |
8582 | 5583 | 実数は浮動小数点より無限に続くので、多少角が丸められます。 |
8583 | 例 | |
5584 | 例: | |
8584 | 5585 | |
8585 | 5586 | printf "%.20g\n", 123456789123456789; |
8586 | 5587 | # produces 123456789123456784 |
8587 | 5588 | |
8588 | 5589 | =begin original |
8589 | 5590 | |
8590 | Testing for exact floating-point equality or inequality is | |
5591 | Testing for exact equality of floating-point equality or inequality is | |
8591 | good idea. Here's a (relatively expensive) work-around to compare | |
5592 | not a good idea. Here's a (relatively expensive) work-around to compare | |
8592 | 5593 | whether two floating-point numbers are equal to a particular number of |
8593 | 5594 | decimal places. See Knuth, volume II, for a more robust treatment of |
8594 | 5595 | this topic. |
8595 | 5596 | |
8596 | 5597 | =end original |
8597 | 5598 | |
8598 | 浮動小数点数が等しいかどうかをちょうど同じかどうかで比較するのは | |
5599 | 浮動小数点数が等しいかどうかをちょうど同じかどうかで比較するのは | |
8599 | ||
5600 | いいアイデアではありません。 | |
8600 | 5601 | 以下に、二つの浮動小数点数が指定された桁まで等しいかどうかを |
8601 | 5602 | 比較する(比較的重い)次善の策を示します。 |
8602 | 5603 | この問題に関するより厳密な扱いについては Knuth, volume II を参照して下さい。 |
8603 | 5604 | |
8604 | 5605 | sub fp_equal { |
8605 | 5606 | my ($X, $Y, $POINTS) = @_; |
8606 | 5607 | my ($tX, $tY); |
8607 | 5608 | $tX = sprintf("%.${POINTS}g", $X); |
8608 | 5609 | $tY = sprintf("%.${POINTS}g", $Y); |
8609 | 5610 | return $tX eq $tY; |
8610 | 5611 | } |
8611 | 5612 | |
8612 | 5613 | =begin original |
8613 | 5614 | |
8614 | 5615 | The POSIX module (part of the standard perl distribution) implements |
8615 | ||
5616 | ceil(), floor(), and other mathematical and trigonometric functions. | |
8616 | The | |
5617 | The Math::Complex module (part of the standard perl distribution) | |
8617 | 5618 | defines mathematical functions that work on both the reals and the |
8618 | imaginary numbers. | |
5619 | imaginary numbers. Math::Complex not as efficient as POSIX, but | |
8619 | 5620 | POSIX can't work with complex numbers. |
8620 | 5621 | |
8621 | 5622 | =end original |
8622 | 5623 | |
8623 | POSIX モジュール(Perl 標準配布パッケージの一部) は | |
5624 | POSIX モジュール(Perl 標準配布パッケージの一部) は ceil(), floor() 及び | |
8624 | 5625 | その他の数学関数や三角関数を実装しています。 |
8625 | ||
5626 | Math::Complex モジュール(Perl 標準配布パッケージの一部)は | |
8626 | 両方で動作する数学関数を定義しています。 | |
5627 | 実数と虚数の両方で動作する数学関数を定義しています。 | |
8627 | ||
5628 | Math::Complex は POSIX ほど効率的ではありませんが、 | |
8628 | 扱えません。 | |
5629 | POSIX は複素数は扱えません。 | |
8629 | 5630 | |
8630 | 5631 | =begin original |
8631 | 5632 | |
8632 | 5633 | Rounding in financial applications can have serious implications, and |
8633 | 5634 | the rounding method used should be specified precisely. In these |
8634 | 5635 | cases, it probably pays not to trust whichever system rounding is |
8635 | 5636 | being used by Perl, but to instead implement the rounding function you |
8636 | 5637 | need yourself. |
8637 | 5638 | |
8638 | 5639 | =end original |
8639 | 5640 | |
8640 | 5641 | 金融アプリケーションにおける丸めは深刻な影響を与える可能性があり、 |
8641 | 5642 | 使用する丸めメソッドは指定された精度で行われるべきです。 |
8642 | 5643 | このような場合、Perl が使用するシステム丸めを信用せず、 |
8643 | 5644 | 代わりに自分自身で丸め関数を実装するべきです。 |
8644 | 5645 | |
8645 | 5646 | =head2 Bigger Numbers |
8646 | 5647 | X<number, arbitrary precision> |
8647 | 5648 | |
8648 | 5649 | (より大きな数) |
8649 | 5650 | |
8650 | 5651 | =begin original |
8651 | 5652 | |
8652 | The standard | |
5653 | The standard Math::BigInt and Math::BigFloat modules provide | |
8653 | C<L<Math::BigFloat>> modules, | |
8654 | along with the C<bignum>, C<bigint>, and C<bigrat> pragmas, provide | |
8655 | 5654 | variable-precision arithmetic and overloaded operators, although |
8656 | they're currently pretty slow. | |
5655 | they're currently pretty slow. At the cost of some space and | |
8657 | 5656 | considerable speed, they avoid the normal pitfalls associated with |
8658 | 5657 | limited-precision representations. |
8659 | 5658 | |
8660 | 5659 | =end original |
8661 | 5660 | |
8662 | 標準の | |
5661 | 標準の Math::BigInt と Math::BigFloat モジュールは多倍長演算を提供し、 | |
8663 | C<L<Math::BigFloat>> モジュールと | |
8664 | C<bignum>, C<bigint>, C<bigrat> プラグマは多倍長演算を提供し、 | |
8665 | 5662 | 演算子をオーバーロードしますが、これらは現在のところかなり遅いです。 |
8666 | 5663 | 多少の領域とかなりの速度を犠牲にして、桁数が制限されていることによる |
8667 | 5664 | ありがちな落とし穴を避けることができます。 |
8668 | 5665 | |
8669 | ||
5666 | use Math::BigInt; | |
8670 | ||
5667 | $x = Math::BigInt->new('123456789123456789'); | |
8671 | ||
5668 | print $x * $x; | |
8672 | say $x * $x; | |
8673 | +15241578780673678515622620750190521 | |
8674 | 5669 | |
8675 | ||
5670 | # prints +15241578780673678515622620750190521 | |
8676 | 5671 | |
8677 | Or 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 | ||
8692 | 5672 | =begin original |
8693 | 5673 | |
8694 | ||
5674 | There are several modules that let you calculate with (bound only by | |
8695 | ||
5675 | memory and cpu-time) unlimited or fixed precision. There are also | |
8696 | ||
5676 | some non-standard modules that provide faster implementations via | |
8697 | ||
5677 | external C libraries. | |
8698 | 5678 | |
8699 | 5679 | =end original |
8700 | 5680 | |
8701 | 5681 | (メモリと CPU 時間のみに依存する)無制限か固定の精度での計算ができる |
8702 | 5682 | モジュールがいくつかあります。 |
8703 | 5683 | さらに外部 C ライブラリを使ってより速い実装を提供する |
8704 | 5684 | 非標準のモジュールもあります。 |
8705 | 5685 | |
8706 | 5686 | =begin original |
8707 | 5687 | |
8708 | 5688 | Here is a short, but incomplete summary: |
8709 | 5689 | |
8710 | 5690 | =end original |
8711 | 5691 | |
8712 | 5692 | 以下は短いですが不完全なリストです。 |
8713 | 5693 | |
8714 | 5694 | =begin original |
8715 | 5695 | |
8716 | ||
5696 | Math::Fraction big, unlimited fractions like 9973 / 12967 | |
8717 | ||
5697 | Math::String treat string sequences like numbers | |
8718 | ||
5698 | Math::FixedPrecision calculate with a fixed precision | |
8719 | ||
5699 | Math::Currency for currency calculations | |
8720 | ||
5700 | Bit::Vector manipulate bit vectors fast (uses C) | |
8721 | ||
5701 | Math::BigIntFast Bit::Vector wrapper for big numbers | |
8722 | ||
5702 | Math::Pari provides access to the Pari C library | |
8723 | | |
5703 | Math::BigInteger uses an external C library | |
8724 | ||
5704 | Math::Cephes uses external Cephes C library (no big numbers) | |
8725 | ||
5705 | Math::Cephes::Fraction fractions via the Cephes library | |
8726 | ||
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 | |
8729 | 5707 | |
8730 | 5708 | =end original |
8731 | 5709 | |
8732 | ||
5710 | Math::Fraction 9973 / 12967 のような、大きくて無制限の分数 | |
8733 | ||
5711 | Math::String 文字列を数値のように扱う | |
8734 | ||
5712 | Math::FixedPrecision 固定精度で計算する | |
8735 | ||
5713 | Math::Currency 通貨の計算用 | |
8736 | ||
5714 | Bit::Vector (C を使って)ビットベクタを速く操作する | |
8737 | ||
5715 | Math::BigIntFast 大きな数のための Bit::Vector のラッパー | |
8738 | ||
5716 | Math::Pari Pari C ライブラリへのアクセスを提供する | |
8739 | ||
5717 | Math::BigInteger 外部 C ライブラリを使う | |
8740 | ||
5718 | Math::Cephes 外部の Cephes C を使う(大きな数はなし) | |
8741 | ||
5719 | Math::Cephes::Fraction Cephes ライブラリを使った分数 | |
8742 | ||
5720 | Math::GMP これも外部 C ライブラリを使う | |
8743 | Math::GMPf libgmp の浮動小数点のインターフェース | |
8744 | 5721 | |
8745 | 5722 | =begin original |
8746 | 5723 | |
8747 | 5724 | Choose wisely. |
8748 | 5725 | |
8749 | 5726 | =end original |
8750 | 5727 | |
8751 | 5728 | うまく選んでください。 |
8752 | 5729 | |
8753 | 5730 | =cut |
8754 | 5731 | |
8755 | 5732 | =begin meta |
8756 | 5733 | |
8757 | 5734 | Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp> (5.000) |
8758 | 5735 | Update: Kentaro Shirakata <argrath@ub32.org> (5.6.1-) |
8759 | 5736 | Status: completed |
8760 | 5737 | |
8761 | 5738 | =end meta |