1 |
argrath |
1.1 |
|
2 |
|
|
=encoding euc-jp |
3 |
|
|
|
4 |
|
|
=head1 NAME |
5 |
|
|
X<operator> |
6 |
|
|
|
7 |
|
|
=begin original |
8 |
|
|
|
9 |
|
|
perlop - Perl operators and precedence |
10 |
|
|
|
11 |
|
|
=end original |
12 |
|
|
|
13 |
|
|
perlop - Perl の演算子と優先順位 |
14 |
|
|
|
15 |
|
|
=head1 DESCRIPTION |
16 |
|
|
|
17 |
|
|
=begin original |
18 |
|
|
|
19 |
|
|
In Perl, the operator determines what operation is performed, |
20 |
|
|
independent of the type of the operands. For example S<C<$x + $y>> |
21 |
|
|
is always a numeric addition, and if C<$x> or C<$y> do not contain |
22 |
|
|
numbers, an attempt is made to convert them to numbers first. |
23 |
|
|
|
24 |
|
|
=end original |
25 |
|
|
|
26 |
|
|
Perl では、演算子はどんな演算を行うかをオペランドの型と独立して決定します。 |
27 |
|
|
例えば S<C<$x + $y>> は常に数値加算で、C<$x> や C<$y> に数値でないものが |
28 |
|
|
含まれている場合、まずそれらを数値に変換しようとします。 |
29 |
|
|
|
30 |
|
|
=begin original |
31 |
|
|
|
32 |
|
|
This is in contrast to many other dynamic languages, where the |
33 |
|
|
operation is determined by the type of the first argument. It also |
34 |
|
|
means that Perl has two versions of some operators, one for numeric |
35 |
|
|
and one for string comparison. For example S<C<$x == $y>> compares |
36 |
|
|
two numbers for equality, and S<C<$x eq $y>> compares two strings. |
37 |
|
|
|
38 |
|
|
=end original |
39 |
|
|
|
40 |
|
|
これは、最初の引数の型によって演算が決定されるその他の多くの動的言語と |
41 |
|
|
対照的です。 |
42 |
|
|
これはまた、数値比較用と文字列比較用の 2 種類の演算子があるということです。 |
43 |
|
|
例えば S<C<$x == $y>> は二つの数値の等価性を比較し、S<C<$x eq $y>> は二つの |
44 |
|
|
文字列を比較します。 |
45 |
|
|
|
46 |
|
|
=begin original |
47 |
|
|
|
48 |
|
|
There are a few exceptions though: C<x> can be either string |
49 |
|
|
repetition or list repetition, depending on the type of the left |
50 |
|
|
operand, and C<&>, C<|>, C<^> and C<~> can be either string or numeric bit |
51 |
|
|
operations. |
52 |
|
|
|
53 |
|
|
=end original |
54 |
|
|
|
55 |
|
|
しかし、いくつかの例外があります: C<x> は左側のオペランドの型によって、 |
56 |
|
|
文字列の繰り返しの場合とリストの繰り返しの場合があります; また |
57 |
|
|
C<&>, C<|>, C<^>, C<~> は文字列としてのビット演算と数値としてのビット演算の |
58 |
|
|
場合があります。 |
59 |
|
|
|
60 |
|
|
=head2 Operator Precedence and Associativity |
61 |
|
|
X<operator, precedence> X<precedence> X<associativity> |
62 |
|
|
|
63 |
|
|
(演算子の優先順位と結合性) |
64 |
|
|
|
65 |
|
|
=begin original |
66 |
|
|
|
67 |
|
|
Operator precedence and associativity work in Perl more or less like |
68 |
|
|
they do in mathematics. |
69 |
|
|
|
70 |
|
|
=end original |
71 |
|
|
|
72 |
|
|
Perl での演算子の優先順位と結合性は多かれ少なかれ数学のものと似ています。 |
73 |
|
|
|
74 |
|
|
=begin original |
75 |
|
|
|
76 |
|
|
I<Operator precedence> means some operators are evaluated before |
77 |
|
|
others. For example, in S<C<2 + 4 * 5>>, the multiplication has higher |
78 |
|
|
precedence so S<C<4 * 5>> is evaluated first yielding S<C<2 + 20 == |
79 |
|
|
22>> and not S<C<6 * 5 == 30>>. |
80 |
|
|
|
81 |
|
|
=end original |
82 |
|
|
|
83 |
|
|
I<演算子の優先順位> とは、他の演算子より先に評価される演算子があると |
84 |
|
|
いうことです。 |
85 |
|
|
例えば、S<C<2 + 4 * 5>> の場合、乗算が高い優先順位を持っているので、 |
86 |
|
|
S<C<4 * 5>> が先に評価され、結果は S<C<6 * 5 == 30>> ではなく、 |
87 |
|
|
S<C<2 + 20 == 22>> となります。 |
88 |
|
|
|
89 |
|
|
=begin original |
90 |
|
|
|
91 |
|
|
I<Operator associativity> defines what happens if a sequence of the |
92 |
|
|
same operators is used one after another: whether the evaluator will |
93 |
|
|
evaluate the left operations first, or the right first. For example, in |
94 |
|
|
S<C<8 - 4 - 2>>, subtraction is left associative so Perl evaluates the |
95 |
|
|
expression left to right. S<C<8 - 4>> is evaluated first making the |
96 |
|
|
expression S<C<4 - 2 == 2>> and not S<C<8 - 2 == 6>>. |
97 |
|
|
|
98 |
|
|
=end original |
99 |
|
|
|
100 |
|
|
I<演算子の結合性> は、同じ演算子が連続して現れた場合に何が起こるかを |
101 |
|
|
定義します: 評価器が左側を先に評価するか右側を先に評価するかです。 |
102 |
|
|
例えば、S<C<8 - 4 - 2>> の場合、減算は左結合なので、Perl は式を左から右に |
103 |
|
|
評価します。 |
104 |
|
|
S<C<8 - 4>> が先に評価されるので、S<C<8 - 2 == 6>> ではなく |
105 |
|
|
S<C<4 - 2 == 2>> となります。 |
106 |
|
|
|
107 |
|
|
=begin original |
108 |
|
|
|
109 |
|
|
Perl operators have the following associativity and precedence, |
110 |
|
|
listed from highest precedence to lowest. Operators borrowed from |
111 |
|
|
C keep the same precedence relationship with each other, even where |
112 |
|
|
C's precedence is slightly screwy. (This makes learning Perl easier |
113 |
|
|
for C folks.) With very few exceptions, these all operate on scalar |
114 |
|
|
values only, not array values. |
115 |
|
|
|
116 |
|
|
=end original |
117 |
|
|
|
118 |
|
|
Perl の演算子には、以下のような結合性と優先順位 (高い優先順位から |
119 |
|
|
低いものへ並べている) があります。 |
120 |
|
|
C から持ってきた演算子の優先順位は、C での優先順位が多少おかしくても、 |
121 |
|
|
そのままにしてあります。 |
122 |
|
|
(これによって、C を使っている方が Perl に移りやすくなっています。) |
123 |
|
|
ごく僅かな例外を別として、全ての演算子はスカラ値のみを持ち、 |
124 |
|
|
配列値を持ちません。 |
125 |
|
|
|
126 |
|
|
=begin original |
127 |
|
|
|
128 |
|
|
left terms and list operators (leftward) |
129 |
|
|
left -> |
130 |
|
|
nonassoc ++ -- |
131 |
|
|
right ** |
132 |
|
|
right ! ~ \ and unary + and - |
133 |
|
|
left =~ !~ |
134 |
|
|
left * / % x |
135 |
|
|
left + - . |
136 |
|
|
left << >> |
137 |
|
|
nonassoc named unary operators |
138 |
|
|
nonassoc < > <= >= lt gt le ge |
139 |
|
|
nonassoc == != <=> eq ne cmp ~~ |
140 |
|
|
left & |
141 |
|
|
left | ^ |
142 |
|
|
left && |
143 |
|
|
left || // |
144 |
|
|
nonassoc .. ... |
145 |
|
|
right ?: |
146 |
|
|
right = += -= *= etc. goto last next redo dump |
147 |
|
|
left , => |
148 |
|
|
nonassoc list operators (rightward) |
149 |
|
|
right not |
150 |
|
|
left and |
151 |
|
|
left or xor |
152 |
|
|
|
153 |
|
|
=end original |
154 |
|
|
|
155 |
|
|
左結合 項 リスト演算子 (左方向に対して) |
156 |
|
|
左結合 -> |
157 |
|
|
非結合 ++ -- |
158 |
|
|
右結合 ** |
159 |
|
|
右結合 ! ~ \ 単項の+ 単項の- |
160 |
|
|
左結合 =~ !~ |
161 |
|
|
左結合 * / % x |
162 |
|
|
左結合 + - . |
163 |
|
|
左結合 << >> |
164 |
|
|
非結合 名前付き単項演算子 |
165 |
|
|
非結合 < > <= >= lt gt le ge |
166 |
|
|
非結合 == != <=> eq ne cmp ~~ |
167 |
|
|
左結合 & |
168 |
|
|
左結合 | ^ |
169 |
|
|
左結合 && |
170 |
|
|
左結合 || // |
171 |
|
|
非結合 .. ... |
172 |
|
|
右結合 ?: |
173 |
|
|
右結合 = += -= *= など; goto last next redo dump |
174 |
|
|
左結合 , => |
175 |
|
|
非結合 リスト演算子 (右方向に対して) |
176 |
|
|
右結合 not |
177 |
|
|
左結合 and |
178 |
|
|
左結合 or xor |
179 |
|
|
|
180 |
|
|
=begin original |
181 |
|
|
|
182 |
|
|
In the following sections, these operators are covered in precedence order. |
183 |
|
|
|
184 |
|
|
=end original |
185 |
|
|
|
186 |
|
|
以下の章では、演算子は優先順位の順に記述されています。 |
187 |
|
|
|
188 |
|
|
=begin original |
189 |
|
|
|
190 |
|
|
Many operators can be overloaded for objects. See L<overload>. |
191 |
|
|
|
192 |
|
|
=end original |
193 |
|
|
|
194 |
|
|
多くの演算子はオブジェクトでオーバーロードできます。 |
195 |
|
|
L<overload> を参照して下さい。 |
196 |
|
|
|
197 |
|
|
=head2 Terms and List Operators (Leftward) |
198 |
|
|
X<list operator> X<operator, list> X<term> |
199 |
|
|
|
200 |
|
|
(項とリスト演算子 (左方向)) |
201 |
|
|
|
202 |
|
|
=begin original |
203 |
|
|
|
204 |
|
|
A TERM has the highest precedence in Perl. They include variables, |
205 |
|
|
quote and quote-like operators, any expression in parentheses, |
206 |
|
|
and any function whose arguments are parenthesized. Actually, there |
207 |
|
|
aren't really functions in this sense, just list operators and unary |
208 |
|
|
operators behaving as functions because you put parentheses around |
209 |
|
|
the arguments. These are all documented in L<perlfunc>. |
210 |
|
|
|
211 |
|
|
=end original |
212 |
|
|
|
213 |
|
|
「項」は Perl でもっとも優先順位が高いものです。 |
214 |
|
|
これには、変数、クォートとクォート的な演算子、括弧で括った任意の式、 |
215 |
|
|
引数を括弧で括った任意の関数が含まれます。 |
216 |
|
|
実際には、この意味では本当の関数はなく、リスト演算子と関数のように働く |
217 |
|
|
単項演算子が、引数を括弧で括るためそのように見えます。 |
218 |
|
|
これらはすべて L<perlfunc> に記述しています。 |
219 |
|
|
|
220 |
|
|
=begin original |
221 |
|
|
|
222 |
|
|
If any list operator (C<print()>, etc.) or any unary operator (C<chdir()>, etc.) |
223 |
|
|
is followed by a left parenthesis as the next token, the operator and |
224 |
|
|
arguments within parentheses are taken to be of highest precedence, |
225 |
|
|
just like a normal function call. |
226 |
|
|
|
227 |
|
|
=end original |
228 |
|
|
|
229 |
|
|
リスト演算子 (C<print()> など) や単項演算子 (C<chdir()> など) は、 |
230 |
|
|
すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、 |
231 |
|
|
通常の関数呼び出しのようにもっとも高い優先順位として扱われます。 |
232 |
|
|
|
233 |
|
|
=begin original |
234 |
|
|
|
235 |
|
|
In the absence of parentheses, the precedence of list operators such as |
236 |
|
|
C<print>, C<sort>, or C<chmod> is either very high or very low depending on |
237 |
|
|
whether you are looking at the left side or the right side of the operator. |
238 |
|
|
For example, in |
239 |
|
|
|
240 |
|
|
=end original |
241 |
|
|
|
242 |
|
|
括弧が無い場合には、C<print>、C<sort>、C<chmod> のようなリスト演算子の |
243 |
|
|
優先順位は、演算子の左側をからすると非常に高く、右側からすると |
244 |
|
|
非常に低く見えます。たとえば、 |
245 |
|
|
|
246 |
|
|
@ary = (1, 3, sort 4, 2); |
247 |
|
|
print @ary; # prints 1324 |
248 |
|
|
|
249 |
|
|
=begin original |
250 |
|
|
|
251 |
|
|
the commas on the right of the C<sort> are evaluated before the C<sort>, |
252 |
|
|
but the commas on the left are evaluated after. In other words, |
253 |
|
|
list operators tend to gobble up all arguments that follow, and |
254 |
|
|
then act like a simple TERM with regard to the preceding expression. |
255 |
|
|
Be careful with parentheses: |
256 |
|
|
|
257 |
|
|
=end original |
258 |
|
|
|
259 |
|
|
では、C<sort> の右のコンマは C<sort> よりも前に評価されますが、左側のコンマは |
260 |
|
|
後から評価されます。 |
261 |
|
|
言い方を変えると、リスト演算子は自分の後にある引数をすべて使って処理を行ない、 |
262 |
|
|
その結果を自分の前の式に対する「項」であるかのように見せるということです。 |
263 |
|
|
ただし、括弧には気を付けないといけません: |
264 |
|
|
|
265 |
|
|
=begin original |
266 |
|
|
|
267 |
|
|
# These evaluate exit before doing the print: |
268 |
|
|
print($foo, exit); # Obviously not what you want. |
269 |
|
|
print $foo, exit; # Nor is this. |
270 |
|
|
|
271 |
|
|
=end original |
272 |
|
|
|
273 |
|
|
# 以下は print を行なう前に exit を評価します: |
274 |
|
|
print($foo, exit); # 明らかにやりたいことではないでしょう。 |
275 |
|
|
print $foo, exit; # これでもない。 |
276 |
|
|
|
277 |
|
|
=begin original |
278 |
|
|
|
279 |
|
|
# These do the print before evaluating exit: |
280 |
|
|
(print $foo), exit; # This is what you want. |
281 |
|
|
print($foo), exit; # Or this. |
282 |
|
|
print ($foo), exit; # Or even this. |
283 |
|
|
|
284 |
|
|
=end original |
285 |
|
|
|
286 |
|
|
# 以下は exit を評価する前に print を行ないます: |
287 |
|
|
(print $foo), exit; # これがしたかった。 |
288 |
|
|
print($foo), exit; # これでもいい。 |
289 |
|
|
print ($foo), exit; # これも OK。 |
290 |
|
|
|
291 |
|
|
=begin original |
292 |
|
|
|
293 |
|
|
Also note that |
294 |
|
|
|
295 |
|
|
=end original |
296 |
|
|
|
297 |
|
|
また、 |
298 |
|
|
|
299 |
|
|
print ($foo & 255) + 1, "\n"; |
300 |
|
|
|
301 |
|
|
=begin original |
302 |
|
|
|
303 |
|
|
probably doesn't do what you expect at first glance. The parentheses |
304 |
|
|
enclose the argument list for C<print> which is evaluated (printing |
305 |
|
|
the result of S<C<$foo & 255>>). Then one is added to the return value |
306 |
|
|
of C<print> (usually 1). The result is something like this: |
307 |
|
|
|
308 |
|
|
=end original |
309 |
|
|
|
310 |
|
|
の動作を一目見ただけで判断するのは、難しいでしょう。 |
311 |
|
|
かっこは C<print> のために評価される引数リストを囲っています |
312 |
|
|
(S<C<$foo & 255>> の結果が表示されます)。 |
313 |
|
|
それから C<print> の返り値 (通常は 1) に 1 が加えられます。 |
314 |
|
|
結果は以下のようになります: |
315 |
|
|
|
316 |
|
|
=begin original |
317 |
|
|
|
318 |
|
|
1 + 1, "\n"; # Obviously not what you meant. |
319 |
|
|
|
320 |
|
|
=end original |
321 |
|
|
|
322 |
|
|
1 + 1, "\n"; # 明らかにやりたいことではないでしょう。 |
323 |
|
|
|
324 |
|
|
=begin original |
325 |
|
|
|
326 |
|
|
To do what you meant properly, you must write: |
327 |
|
|
|
328 |
|
|
=end original |
329 |
|
|
|
330 |
|
|
意味したいことを適切に行うには、以下のように書く必要があります: |
331 |
|
|
|
332 |
|
|
print(($foo & 255) + 1, "\n"); |
333 |
|
|
|
334 |
|
|
=begin original |
335 |
|
|
|
336 |
|
|
See L<Named Unary Operators> for more discussion of this. |
337 |
|
|
|
338 |
|
|
=end original |
339 |
|
|
|
340 |
|
|
詳しくは、L<Named Unary Operators> を参照してください。 |
341 |
|
|
|
342 |
|
|
=begin original |
343 |
|
|
|
344 |
|
|
Also parsed as terms are the S<C<do {}>> and S<C<eval {}>> constructs, as |
345 |
|
|
well as subroutine and method calls, and the anonymous |
346 |
|
|
constructors C<[]> and C<{}>. |
347 |
|
|
|
348 |
|
|
=end original |
349 |
|
|
|
350 |
|
|
この他に「項」として解析されるものには、S<C<do {}>> や S<C<eval {}>> の |
351 |
|
|
構成、サブルーティンやメソッドの呼び出し、無名のコンストラクタ |
352 |
|
|
C<[]> と C<{}> があります。 |
353 |
|
|
|
354 |
|
|
=begin original |
355 |
|
|
|
356 |
|
|
See also L<Quote and Quote-like Operators> toward the end of this section, |
357 |
|
|
as well as L</"I/O Operators">. |
358 |
|
|
|
359 |
|
|
=end original |
360 |
|
|
|
361 |
|
|
後の方のL<Quote and Quote-like Operators>や |
362 |
|
|
L</"I/O Operators">も参照してください。 |
363 |
|
|
|
364 |
|
|
=head2 The Arrow Operator |
365 |
|
|
X<arrow> X<dereference> X<< -> >> |
366 |
|
|
|
367 |
|
|
(矢印演算子) |
368 |
|
|
|
369 |
|
|
=begin original |
370 |
|
|
|
371 |
|
|
"C<< -> >>" is an infix dereference operator, just as it is in C |
372 |
|
|
and C++. If the right side is either a C<[...]>, C<{...}>, or a |
373 |
|
|
C<(...)> subscript, then the left side must be either a hard or |
374 |
|
|
symbolic reference to an array, a hash, or a subroutine respectively. |
375 |
|
|
(Or technically speaking, a location capable of holding a hard |
376 |
|
|
reference, if it's an array or hash reference being used for |
377 |
|
|
assignment.) See L<perlreftut> and L<perlref>. |
378 |
|
|
|
379 |
|
|
=end original |
380 |
|
|
|
381 |
|
|
C や C++ と同じように "C<< -> >>" は中置の被参照演算子です。 |
382 |
|
|
右側が C<[...]>, C<{...}>, C<(...)> のいずれかの形の添字であれば、左側は配列、 |
383 |
|
|
ハッシュ、サブルーチンへのハードリファレンスか |
384 |
|
|
シンボリックリファレンスでなければなりません。 |
385 |
|
|
(あるいは技術的には、配列またはハードリファレンスが代入可能であれば |
386 |
|
|
ハードリファレンスを保持できる場所です。) |
387 |
|
|
L<perlreftut> と L<perlref> を参照してください。 |
388 |
|
|
|
389 |
|
|
=begin original |
390 |
|
|
|
391 |
|
|
Otherwise, the right side is a method name or a simple scalar |
392 |
|
|
variable containing either the method name or a subroutine reference, |
393 |
|
|
and the left side must be either an object (a blessed reference) |
394 |
|
|
or a class name (that is, a package name). See L<perlobj>. |
395 |
|
|
|
396 |
|
|
=end original |
397 |
|
|
|
398 |
|
|
そうでなければ、右側はメソッド名かサブルーチンのリファレンスを持った |
399 |
|
|
単純スカラ変数で、左側はオブジェクト (bless されたリファレンス) か |
400 |
|
|
クラス名でなければなりません。 |
401 |
|
|
L<perlobj> を参照してください。 |
402 |
|
|
|
403 |
|
|
=begin original |
404 |
|
|
|
405 |
|
|
The dereferencing cases (as opposed to method-calling cases) are |
406 |
|
|
somewhat extended by the experimental C<postderef> feature. For the |
407 |
|
|
details of that feature, consult L<perlref/Postfix Dereference Syntax>. |
408 |
|
|
|
409 |
|
|
=end original |
410 |
|
|
|
411 |
|
|
(メソッド呼び出しの場合ではなく) デリファレンスの場合、実験的な |
412 |
|
|
C<後置デリファレンス> (postderef) 機能によっていくらか拡張されます。 |
413 |
|
|
この機能の詳細については、L<perlref/Postfix Dereference Syntax> を |
414 |
|
|
参照してください。 |
415 |
|
|
|
416 |
|
|
=head2 Auto-increment and Auto-decrement |
417 |
|
|
X<increment> X<auto-increment> X<++> X<decrement> X<auto-decrement> X<--> |
418 |
|
|
|
419 |
|
|
(インクリメントとデクリメント) |
420 |
|
|
|
421 |
|
|
=begin original |
422 |
|
|
|
423 |
|
|
C<"++"> and C<"--"> work as in C. That is, if placed before a variable, |
424 |
|
|
they increment or decrement the variable by one before returning the |
425 |
|
|
value, and if placed after, increment or decrement after returning the |
426 |
|
|
value. |
427 |
|
|
|
428 |
|
|
=end original |
429 |
|
|
|
430 |
|
|
C<"++"> と C<"--"> は、C の場合と同じように動作します。 |
431 |
|
|
つまり、変数の前に置かれれば、値を返す前に変数を 1 インクリメントまたは |
432 |
|
|
デクリメントし、後に置かれれば、値を返した後で変数を |
433 |
|
|
インクリメントまたはデクリメントします。 |
434 |
|
|
|
435 |
|
|
$i = 0; $j = 0; |
436 |
|
|
print $i++; # prints 0 |
437 |
|
|
print ++$j; # prints 1 |
438 |
|
|
|
439 |
|
|
=begin original |
440 |
|
|
|
441 |
|
|
Note that just as in C, Perl doesn't define B<when> the variable is |
442 |
|
|
incremented or decremented. You just know it will be done sometime |
443 |
|
|
before or after the value is returned. This also means that modifying |
444 |
|
|
a variable twice in the same statement will lead to undefined behavior. |
445 |
|
|
Avoid statements like: |
446 |
|
|
|
447 |
|
|
=end original |
448 |
|
|
|
449 |
|
|
C と同様、Perl は B<いつ> 変数がインクリメントまたはデクリメントされるかは |
450 |
|
|
定義されません。 |
451 |
|
|
値が返される前か後のどこかで行われる、ということだけがわかります。 |
452 |
|
|
これは、同じ文である変数を 2 回修正すると、振る舞いが未定義になることを |
453 |
|
|
意味します。 |
454 |
|
|
以下のような文は避けてください: |
455 |
|
|
|
456 |
|
|
$i = $i ++; |
457 |
|
|
print ++ $i + $i ++; |
458 |
|
|
|
459 |
|
|
=begin original |
460 |
|
|
|
461 |
|
|
Perl will not guarantee what the result of the above statements is. |
462 |
|
|
|
463 |
|
|
=end original |
464 |
|
|
|
465 |
|
|
Perl は上記の文の結果について保障しません。 |
466 |
|
|
|
467 |
|
|
=begin original |
468 |
|
|
|
469 |
|
|
The auto-increment operator has a little extra builtin magic to it. If |
470 |
|
|
you increment a variable that is numeric, or that has ever been used in |
471 |
|
|
a numeric context, you get a normal increment. If, however, the |
472 |
|
|
variable has been used in only string contexts since it was set, and |
473 |
|
|
has a value that is not the empty string and matches the pattern |
474 |
|
|
C</^[a-zA-Z]*[0-9]*\z/>, the increment is done as a string, preserving each |
475 |
|
|
character within its range, with carry: |
476 |
|
|
|
477 |
|
|
=end original |
478 |
|
|
|
479 |
|
|
インクリメント演算子には、ちょっと風変わりな機能が組み込まれています。 |
480 |
|
|
数値が入った変数や、数値の文脈で使われてきた変数を |
481 |
|
|
インクリメントする場合には、通常のインクリメントとして動作します。 |
482 |
|
|
しかし、その変数が設定されてからずっと文字列の文脈でしか使われていなくて、 |
483 |
|
|
空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする値を持っているときには、 |
484 |
|
|
個々の文字の範囲を保ちながら桁あげを行なって、文字列としてインクリメントが |
485 |
|
|
行なわれます (マジカルインクリメントと呼ばれます): |
486 |
|
|
|
487 |
|
|
print ++($foo = "99"); # prints "100" |
488 |
|
|
print ++($foo = "a0"); # prints "a1" |
489 |
|
|
print ++($foo = "Az"); # prints "Ba" |
490 |
|
|
print ++($foo = "zz"); # prints "aaa" |
491 |
|
|
|
492 |
|
|
=begin original |
493 |
|
|
|
494 |
|
|
C<undef> is always treated as numeric, and in particular is changed |
495 |
|
|
to C<0> before incrementing (so that a post-increment of an undef value |
496 |
|
|
will return C<0> rather than C<undef>). |
497 |
|
|
|
498 |
|
|
=end original |
499 |
|
|
|
500 |
|
|
C<undef> は常に数値として扱われ、特にインクリメントされる前には C<0> に |
501 |
|
|
変換されます(従って、undef のポストインクリメント値は C<undef> ではなく |
502 |
|
|
C<0> になります)。 |
503 |
|
|
|
504 |
|
|
=begin original |
505 |
|
|
|
506 |
|
|
The auto-decrement operator is not magical. |
507 |
|
|
|
508 |
|
|
=end original |
509 |
|
|
|
510 |
|
|
デクリメント演算子には、マジカルなものはありません。 |
511 |
|
|
|
512 |
|
|
=head2 Exponentiation |
513 |
|
|
X<**> X<exponentiation> X<power> |
514 |
|
|
|
515 |
|
|
(指数演算子) |
516 |
|
|
|
517 |
|
|
=begin original |
518 |
|
|
|
519 |
|
|
Binary C<"**"> is the exponentiation operator. It binds even more |
520 |
|
|
tightly than unary minus, so C<-2**4> is C<-(2**4)>, not C<(-2)**4>. |
521 |
|
|
(This is |
522 |
|
|
implemented using C's C<pow(3)> function, which actually works on doubles |
523 |
|
|
internally.) |
524 |
|
|
|
525 |
|
|
=end original |
526 |
|
|
|
527 |
|
|
二項演算子の C<"**"> は指数演算子です。 |
528 |
|
|
この演算子は、単項のマイナスよりも結合が強い演算子で、 |
529 |
|
|
C<-2**4> は C<(-2)**4> ではなく、C<-(2**4)> と解釈されます。 |
530 |
|
|
(これは C の C<pow(3)> を使って実装されていますので、 |
531 |
|
|
内部的には double で動作します。) |
532 |
|
|
|
533 |
|
|
=begin original |
534 |
|
|
|
535 |
|
|
Note that certain exponentiation expressions are ill-defined: |
536 |
|
|
these include C<0**0>, C<1**Inf>, and C<Inf**0>. Do not expect |
537 |
|
|
any particular results from these special cases, the results |
538 |
|
|
are platform-dependent. |
539 |
|
|
|
540 |
|
|
=end original |
541 |
|
|
|
542 |
argrath |
1.2 |
一部の指数表現は明確に定義されていません: |
543 |
|
|
これは C<0**0>, C<1**Inf>, C<Inf**0> などです。 |
544 |
|
|
これらの特殊な場合に関して特定の結果を想定しないでください; |
545 |
|
|
結果はプラットフォーム依存です。 |
546 |
argrath |
1.1 |
|
547 |
|
|
=head2 Symbolic Unary Operators |
548 |
|
|
X<unary operator> X<operator, unary> |
549 |
|
|
|
550 |
|
|
(単項演算子) |
551 |
|
|
|
552 |
|
|
=begin original |
553 |
|
|
|
554 |
|
|
Unary C<"!"> performs logical negation, that is, "not". See also C<not> for a lower |
555 |
|
|
precedence version of this. |
556 |
|
|
X<!> |
557 |
|
|
|
558 |
|
|
=end original |
559 |
|
|
|
560 |
|
|
単項演算子の C<"!"> は論理否定を行ないます; つまり「not」ということです。 |
561 |
|
|
この演算子の優先順位を低くしたものとして、C<not> が用意されています。 |
562 |
|
|
X<!> |
563 |
|
|
|
564 |
|
|
=begin original |
565 |
|
|
|
566 |
|
|
Unary C<"-"> performs arithmetic negation if the operand is numeric, |
567 |
|
|
including any string that looks like a number. If the operand is |
568 |
|
|
an identifier, a string consisting of a minus sign concatenated |
569 |
|
|
with the identifier is returned. Otherwise, if the string starts |
570 |
|
|
with a plus or minus, a string starting with the opposite sign is |
571 |
|
|
returned. One effect of these rules is that C<-bareword> is equivalent |
572 |
|
|
to the string C<"-bareword">. If, however, the string begins with a |
573 |
|
|
non-alphabetic character (excluding C<"+"> or C<"-">), Perl will attempt |
574 |
|
|
to convert |
575 |
|
|
the string to a numeric, and the arithmetic negation is performed. If the |
576 |
|
|
string cannot be cleanly converted to a numeric, Perl will give the warning |
577 |
|
|
B<Argument "the string" isn't numeric in negation (-) at ...>. |
578 |
|
|
X<-> X<negation, arithmetic> |
579 |
|
|
|
580 |
|
|
=end original |
581 |
|
|
|
582 |
|
|
単項演算子の C<"-"> は被演算子が数値または数値に見える文字列であれば、 |
583 |
|
|
算術否定を行ないます。 |
584 |
|
|
被演算子が識別子ならば、マイナス記号にその識別子をつなげた文字列が返されます。 |
585 |
|
|
これ以外で被演算子の最初の文字がプラスかマイナスのときには、 |
586 |
|
|
その記号を逆のものに置き換えた文字列を返します。 |
587 |
|
|
この規則の結果、C<-bareword> が文字列 C<"-bareword"> に等価となります。 |
588 |
|
|
しかし、文字列が英字以外(C<"+"> と C<"-"> を除く)で始まっていると、 |
589 |
|
|
Perl は文字列を数値に変換しようとし、それから算術否定が実行されます。 |
590 |
|
|
もし文字列が明確に数値に変換できない場合、Perl は |
591 |
|
|
B<Argument "the string" isn't numeric in negation (-) at ...> という |
592 |
|
|
警告を出します。 |
593 |
|
|
X<-> X<negation, arithmetic> |
594 |
|
|
|
595 |
|
|
=begin original |
596 |
|
|
|
597 |
|
|
Unary C<"~"> performs bitwise negation, that is, 1's complement. For |
598 |
|
|
example, S<C<0666 & ~027>> is 0640. (See also L<Integer Arithmetic> and |
599 |
|
|
L<Bitwise String Operators>.) Note that the width of the result is |
600 |
|
|
platform-dependent: C<~0> is 32 bits wide on a 32-bit platform, but 64 |
601 |
|
|
bits wide on a 64-bit platform, so if you are expecting a certain bit |
602 |
|
|
width, remember to use the C<"&"> operator to mask off the excess bits. |
603 |
|
|
X<~> X<negation, binary> |
604 |
|
|
|
605 |
|
|
=end original |
606 |
|
|
|
607 |
|
|
単項演算子の C<"~"> はビットごとの否定を行ないます; つまり、1 の補数を返します。 |
608 |
|
|
例えば、S<C<0666 & ~027>> は 0640 です。 |
609 |
|
|
(L<Integer Arithmetic> と L<Bitwise String Operators> も参照して下さい。) |
610 |
|
|
結果の幅はプラットホーム依存であることに注意してください: C<~0> は |
611 |
|
|
32 ビットプラットホームでは 32 ビット幅ですが、64 ビットプラットホームでは |
612 |
|
|
64 ビット幅なので、特定のビット幅を仮定する場合は、 |
613 |
|
|
余分なビットをマスクするために C<"&"> 演算子を使うことを忘れないでください。 |
614 |
|
|
X<~> X<negation, binary> |
615 |
|
|
|
616 |
|
|
=begin original |
617 |
|
|
|
618 |
|
|
When complementing strings, if all characters have ordinal values under |
619 |
|
|
256, then their complements will, also. But if they do not, all |
620 |
|
|
characters will be in either 32- or 64-bit complements, depending on your |
621 |
|
|
architecture. So for example, C<~"\x{3B1}"> is C<"\x{FFFF_FC4E}"> on |
622 |
|
|
32-bit machines and C<"\x{FFFF_FFFF_FFFF_FC4E}"> on 64-bit machines. |
623 |
|
|
|
624 |
|
|
=end original |
625 |
|
|
|
626 |
|
|
文字列の補数を計算するとき、全ての文字の値が 256 未満の場合は、その値の |
627 |
|
|
補数が計算されます。 |
628 |
|
|
そうでない場合は、全ての文字はアーキテクチャに依存しいて 32 ビットまたは |
629 |
|
|
64 ビットで補数が計算されます。 |
630 |
|
|
従って例えば、C<~"\x{3B1}"> は 32 ビットマシンでは C<"\x{FFFF_FC4E}"> に、 |
631 |
|
|
64 ビットマシンでは C<"\x{FFFF_FFFF_FFFF_FC4E}"> になります。 |
632 |
|
|
|
633 |
|
|
=begin original |
634 |
|
|
|
635 |
|
|
If the experimental "bitwise" feature is enabled via S<C<use feature |
636 |
|
|
'bitwise'>>, then unary C<"~"> always treats its argument as a number, and an |
637 |
|
|
alternate form of the operator, C<"~.">, always treats its argument as a |
638 |
|
|
string. So C<~0> and C<~"0"> will both give 2**32-1 on 32-bit platforms, |
639 |
|
|
whereas C<~.0> and C<~."0"> will both yield C<"\xff">. This feature |
640 |
|
|
produces a warning unless you use S<C<no warnings 'experimental::bitwise'>>. |
641 |
|
|
|
642 |
|
|
=end original |
643 |
|
|
|
644 |
argrath |
1.2 |
実験的な "bitwise" 機能が S<C<use feature 'bitwise'>> で有効にされると、 |
645 |
|
|
単項の C<"~"> は常にその引数を数値として扱い、その代替形式である |
646 |
|
|
C<"~."> はその引数を常に文字列として扱います。 |
647 |
|
|
従って 32 ビットプラットフォームでは C<~0> と C<~"0"> はどちらも |
648 |
|
|
2**32-1 を意味しますが、C<~.0> と C<~."0"> はどちらも C<"\xff"> になります。 |
649 |
|
|
S<C<no warnings 'experimental::bitwise'>> を使わない限り、この機能は警告を |
650 |
|
|
発生させます。 |
651 |
argrath |
1.1 |
|
652 |
|
|
=begin original |
653 |
|
|
|
654 |
|
|
Unary C<"+"> has no effect whatsoever, even on strings. It is useful |
655 |
|
|
syntactically for separating a function name from a parenthesized expression |
656 |
|
|
that would otherwise be interpreted as the complete list of function |
657 |
|
|
arguments. (See examples above under L<Terms and List Operators (Leftward)>.) |
658 |
|
|
X<+> |
659 |
|
|
|
660 |
|
|
=end original |
661 |
|
|
|
662 |
|
|
単項演算子の C<"+"> は、たとえ文字列に対して用いられた場合にも、何もしません。 |
663 |
|
|
関数名に続けて括弧付きの式を書く場合に、関数の引数リストと |
664 |
|
|
解釈されないようにするために用いることができます。 |
665 |
|
|
(下記 L<Terms and List Operators (Leftward)> の例を参照してください。) |
666 |
|
|
X<+> |
667 |
|
|
|
668 |
|
|
=begin original |
669 |
|
|
|
670 |
|
|
Unary C<"\"> creates a reference to whatever follows it. See L<perlreftut> |
671 |
|
|
and L<perlref>. Do not confuse this behavior with the behavior of |
672 |
|
|
backslash within a string, although both forms do convey the notion |
673 |
|
|
of protecting the next thing from interpolation. |
674 |
|
|
X<\> X<reference> X<backslash> |
675 |
|
|
|
676 |
|
|
=end original |
677 |
|
|
|
678 |
|
|
単項演算子の C<"\"> はその後に続くものへのリファレンスを生成します。 |
679 |
|
|
L<perlreftut> と L<perlref> を参照してください。 |
680 |
|
|
この用法も文字列中のバックスラッシュも、後に続くものが展開されるのを |
681 |
|
|
防ぐことになりますが、動作を混同しないでください。 |
682 |
|
|
X<\> X<reference> X<backslash> |
683 |
|
|
|
684 |
|
|
=head2 Binding Operators |
685 |
|
|
X<binding> X<operator, binding> X<=~> X<!~> |
686 |
|
|
|
687 |
|
|
(拘束演算子) |
688 |
|
|
|
689 |
|
|
=begin original |
690 |
|
|
|
691 |
|
|
Binary C<"=~"> binds a scalar expression to a pattern match. Certain operations |
692 |
|
|
search or modify the string C<$_> by default. This operator makes that kind |
693 |
|
|
of operation work on some other string. The right argument is a search |
694 |
|
|
pattern, substitution, or transliteration. The left argument is what is |
695 |
|
|
supposed to be searched, substituted, or transliterated instead of the default |
696 |
|
|
C<$_>. When used in scalar context, the return value generally indicates the |
697 |
|
|
success of the operation. The exceptions are substitution (C<s///>) |
698 |
|
|
and transliteration (C<y///>) with the C</r> (non-destructive) option, |
699 |
|
|
which cause the B<r>eturn value to be the result of the substitution. |
700 |
|
|
Behavior in list context depends on the particular operator. |
701 |
|
|
See L</"Regexp Quote-Like Operators"> for details and L<perlretut> for |
702 |
|
|
examples using these operators. |
703 |
|
|
|
704 |
|
|
=end original |
705 |
|
|
|
706 |
|
|
二項演算子の C<"=~"> は、スカラ式をパターンマッチに拘束します。 |
707 |
|
|
デフォルトで C<$_> の文字列を検索したり、変更したりする演算があります。 |
708 |
|
|
この演算子は、そのような演算を他の文字列に対して行なわせるようにするものです。 |
709 |
|
|
右引数は、検索パターン、置換、文字変換のいずれかです。 |
710 |
|
|
左引数は、デフォルトの C<$_> の代わりに検索、置換、文字変換の |
711 |
|
|
対象となるものです。 |
712 |
|
|
スカラコンテキストで使うと、返り値は一般的に演算の結果が成功したか否かです。 |
713 |
|
|
例外は、C</r> (非破壊) オプション付きの置換 (C<s///>) と |
714 |
|
|
文字変換 (C<y///>) です; この場合は変換した結果を返します。 |
715 |
|
|
リストコンテキストでの振る舞いは演算子に依存します。 |
716 |
|
|
詳しくは L</"Regexp Quote-Like Operators"> を、これらの演算子を使った |
717 |
|
|
例については L<perlretut> を参照して下さい。 |
718 |
|
|
|
719 |
|
|
=begin original |
720 |
|
|
|
721 |
|
|
If the right argument is an expression rather than a search pattern, |
722 |
|
|
substitution, or transliteration, it is interpreted as a search pattern at run |
723 |
|
|
time. Note that this means that its |
724 |
|
|
contents will be interpolated twice, so |
725 |
|
|
|
726 |
|
|
=end original |
727 |
|
|
|
728 |
|
|
右引数が検索パターン、置換、文字変換ではなく、式であれば、 |
729 |
|
|
それは実行時に決まる検索パターンと解釈されます。 |
730 |
|
|
これは、内容が 2 回展開されることを意味することに注意してください; |
731 |
|
|
つまり: |
732 |
|
|
|
733 |
|
|
'\\' =~ q'\\'; |
734 |
|
|
|
735 |
|
|
=begin original |
736 |
|
|
|
737 |
|
|
is not ok, as the regex engine will end up trying to compile the |
738 |
|
|
pattern C<\>, which it will consider a syntax error. |
739 |
|
|
|
740 |
|
|
=end original |
741 |
|
|
|
742 |
|
|
は正しくありません; 正規表現エンジンは最終的にパターン C<\> を |
743 |
|
|
コンパイルしようとして、これは文法エラーと考えるからです。 |
744 |
|
|
|
745 |
|
|
=begin original |
746 |
|
|
|
747 |
|
|
Binary C<"!~"> is just like C<"=~"> except the return value is negated in |
748 |
|
|
the logical sense. |
749 |
|
|
|
750 |
|
|
=end original |
751 |
|
|
|
752 |
|
|
二項演算子の C<"!~"> は、返される値が論理否定されることを除いて |
753 |
|
|
C<"=~"> と同じです。 |
754 |
|
|
|
755 |
|
|
=begin original |
756 |
|
|
|
757 |
|
|
Binary C<"!~"> with a non-destructive substitution (C<s///r>) or transliteration |
758 |
|
|
(C<y///r>) is a syntax error. |
759 |
|
|
|
760 |
|
|
=end original |
761 |
|
|
|
762 |
|
|
二項演算子の C<"!~"> を非破壊置換 (C<s///r>) や変換 (C<y///r>) で使うと |
763 |
|
|
文法エラーとなります。 |
764 |
|
|
|
765 |
|
|
=head2 Multiplicative Operators |
766 |
|
|
X<operator, multiplicative> |
767 |
|
|
|
768 |
|
|
(乗法演算子) |
769 |
|
|
|
770 |
|
|
=begin original |
771 |
|
|
|
772 |
|
|
Binary C<"*"> multiplies two numbers. |
773 |
|
|
X<*> |
774 |
|
|
|
775 |
|
|
=end original |
776 |
|
|
|
777 |
|
|
二項演算子の C<"*"> は 2 つの数値の積を返します。 |
778 |
|
|
X<*> |
779 |
|
|
|
780 |
|
|
=begin original |
781 |
|
|
|
782 |
|
|
Binary C<"/"> divides two numbers. |
783 |
|
|
X</> X<slash> |
784 |
|
|
|
785 |
|
|
=end original |
786 |
|
|
|
787 |
|
|
二項演算子の C<"/"> は 2 つの数値の商を返します。 |
788 |
|
|
X</> X<slash> |
789 |
|
|
|
790 |
|
|
=begin original |
791 |
|
|
|
792 |
|
|
Binary C<"%"> is the modulo operator, which computes the division |
793 |
|
|
remainder of its first argument with respect to its second argument. |
794 |
|
|
Given integer |
795 |
|
|
operands C<$m> and C<$n>: If C<$n> is positive, then S<C<$m % $n>> is |
796 |
|
|
C<$m> minus the largest multiple of C<$n> less than or equal to |
797 |
|
|
C<$m>. If C<$n> is negative, then S<C<$m % $n>> is C<$m> minus the |
798 |
|
|
smallest multiple of C<$n> that is not less than C<$m> (that is, the |
799 |
|
|
result will be less than or equal to zero). If the operands |
800 |
|
|
C<$m> and C<$n> are floating point values and the absolute value of |
801 |
|
|
C<$n> (that is C<abs($n)>) is less than S<C<(UV_MAX + 1)>>, only |
802 |
|
|
the integer portion of C<$m> and C<$n> will be used in the operation |
803 |
|
|
(Note: here C<UV_MAX> means the maximum of the unsigned integer type). |
804 |
|
|
If the absolute value of the right operand (C<abs($n)>) is greater than |
805 |
|
|
or equal to S<C<(UV_MAX + 1)>>, C<"%"> computes the floating-point remainder |
806 |
|
|
C<$r> in the equation S<C<($r = $m - $i*$n)>> where C<$i> is a certain |
807 |
|
|
integer that makes C<$r> have the same sign as the right operand |
808 |
|
|
C<$n> (B<not> as the left operand C<$m> like C function C<fmod()>) |
809 |
|
|
and the absolute value less than that of C<$n>. |
810 |
|
|
Note that when S<C<use integer>> is in scope, C<"%"> gives you direct access |
811 |
|
|
to the modulo operator as implemented by your C compiler. This |
812 |
|
|
operator is not as well defined for negative operands, but it will |
813 |
|
|
execute faster. |
814 |
|
|
X<%> X<remainder> X<modulo> X<mod> |
815 |
|
|
|
816 |
|
|
=end original |
817 |
|
|
|
818 |
|
|
二項演算子の C<"%"> は剰余演算子で、一つ目の引数を二つ目の引数で割ったときの |
819 |
|
|
余りを返します。 |
820 |
|
|
C<$m> と C<$n> の二つの整数の被演算子を取ったとすると: |
821 |
|
|
C<$n> が正の場合、S<C<$m % $n>> は、C<$m> から C<$m> 以下の最大の |
822 |
|
|
C<$n> の倍数を引いた値です。 |
823 |
|
|
C<$n> が負の場合、S<C<$m % $n>> は、C<$m> から C<$m> を下回らない |
824 |
|
|
最小の C<$n> の倍数を引いた値です(従って結果はゼロ以下になります)。 |
825 |
|
|
オペランド C<$m> と C<$n> が浮動小数点数で、C<$n> の絶対値 |
826 |
|
|
(つまり C<abs($n)>) が S<C<(UV_MAX + 1)>> より小さい場合、 |
827 |
|
|
C<$m> と C<$n> の整数部のみが操作で使われます |
828 |
|
|
(注意: ここで C<UV_MAX> は符号なし整数の最大値を意味します)。 |
829 |
|
|
右オペランドの絶対値 (C<abs($n)>) が S<C<(UV_MAX + 1)>> 以上の場合、 |
830 |
|
|
C<"%"> は、S<C<($r = $m - $i*$n)>> となる浮動小数点剰余 C<$r> を計算します; |
831 |
|
|
ここで C<$i> は、C<$r> が右オペランド C<$n> と同じ符号 (C の |
832 |
|
|
関数 C<fmod()> のように左オペランド C<$m> B<ではありません>) で、 |
833 |
|
|
絶対値が C<$n> より小さいものになるような、ある整数です。 |
834 |
|
|
S<C<use integer>> がスコープ内にある場合、 |
835 |
|
|
C<"%"> は C コンパイラで実装された剰余演算子を使います。 |
836 |
|
|
この演算子は被演算子が負の場合の挙動が不確実ですが、 |
837 |
|
|
より高速です。 |
838 |
|
|
X<%> X<remainder> X<modulo> X<mod> |
839 |
|
|
|
840 |
|
|
=begin original |
841 |
|
|
|
842 |
|
|
Binary C<"x"> is the repetition operator. In scalar context or if the left |
843 |
|
|
operand is not enclosed in parentheses, it returns a string consisting |
844 |
|
|
of the left operand repeated the number of times specified by the right |
845 |
|
|
operand. In list context, if the left operand is enclosed in |
846 |
|
|
parentheses or is a list formed by C<qw/I<STRING>/>, it repeats the list. |
847 |
|
|
If the right operand is zero or negative (raising a warning on |
848 |
|
|
negative), it returns an empty string |
849 |
|
|
or an empty list, depending on the context. |
850 |
|
|
X<x> |
851 |
|
|
|
852 |
|
|
=end original |
853 |
|
|
|
854 |
|
|
二項演算子の C<"x"> は繰り返し演算子です。 |
855 |
|
|
スカラコンテキストまたは左辺値が括弧で括られていない場合は、左被演算子を |
856 |
|
|
右被演算子に示す数だけ繰り返したもので構成される文字列を返します。 |
857 |
|
|
リストコンテキストでは、左被演算子が括弧で括られているか、C<qw/I<STRING>/> の |
858 |
|
|
形のリストの場合、リストを繰り返します。 |
859 |
|
|
右オペランドが 0 か負数の場合(負数の場合は警告が出ます)、 |
860 |
|
|
コンテキストによって空文字列か空リストを返します。 |
861 |
|
|
X<x> |
862 |
|
|
|
863 |
|
|
print '-' x 80; # print row of dashes |
864 |
|
|
|
865 |
|
|
print "\t" x ($tab/8), ' ' x ($tab%8); # tab over |
866 |
|
|
|
867 |
|
|
@ones = (1) x 80; # a list of 80 1's |
868 |
|
|
@ones = (5) x @ones; # set all elements to 5 |
869 |
|
|
|
870 |
|
|
=head2 Additive Operators |
871 |
|
|
X<operator, additive> |
872 |
|
|
|
873 |
|
|
(加法演算子) |
874 |
|
|
|
875 |
|
|
=begin original |
876 |
|
|
|
877 |
|
|
Binary C<"+"> returns the sum of two numbers. |
878 |
|
|
X<+> |
879 |
|
|
|
880 |
|
|
=end original |
881 |
|
|
|
882 |
|
|
二項演算子の C<"+"> は 2 つの数値の和を返します。 |
883 |
|
|
X<+> |
884 |
|
|
|
885 |
|
|
=begin original |
886 |
|
|
|
887 |
|
|
Binary C<"-"> returns the difference of two numbers. |
888 |
|
|
X<-> |
889 |
|
|
|
890 |
|
|
=end original |
891 |
|
|
|
892 |
|
|
二項演算子の C<"-"> は 2 つの数値の差を返します。 |
893 |
|
|
X<-> |
894 |
|
|
|
895 |
|
|
=begin original |
896 |
|
|
|
897 |
|
|
Binary C<"."> concatenates two strings. |
898 |
|
|
X<string, concatenation> X<concatenation> |
899 |
|
|
X<cat> X<concat> X<concatenate> X<.> |
900 |
|
|
|
901 |
|
|
=end original |
902 |
|
|
|
903 |
|
|
二項演算子の C<"."> は 2 つの文字列を連結します。 |
904 |
|
|
X<string, concatenation> X<concatenation> |
905 |
|
|
X<cat> X<concat> X<concatenate> X<.> |
906 |
|
|
|
907 |
|
|
=head2 Shift Operators |
908 |
|
|
X<shift operator> X<operator, shift> X<<< << >>> |
909 |
|
|
X<<< >> >>> X<right shift> X<left shift> X<bitwise shift> |
910 |
|
|
X<shl> X<shr> X<shift, right> X<shift, left> |
911 |
|
|
|
912 |
|
|
(シフト演算子) |
913 |
|
|
|
914 |
|
|
=begin original |
915 |
|
|
|
916 |
|
|
Binary C<<< "<<" >>> returns the value of its left argument shifted left by the |
917 |
|
|
number of bits specified by the right argument. Arguments should be |
918 |
|
|
integers. (See also L<Integer Arithmetic>.) |
919 |
|
|
|
920 |
|
|
=end original |
921 |
|
|
|
922 |
|
|
二項演算子の C<<< "<<" >>> は左引数の値を、右引数で示すビット数だけ、 |
923 |
|
|
左にシフトした値を返します。 |
924 |
|
|
引数は整数でなければなりません。 |
925 |
|
|
(L<Integer Arithmetic> も参照して下さい。) |
926 |
|
|
|
927 |
|
|
=begin original |
928 |
|
|
|
929 |
|
|
Binary C<<< ">>" >>> returns the value of its left argument shifted right by |
930 |
|
|
the number of bits specified by the right argument. Arguments should |
931 |
|
|
be integers. (See also L<Integer Arithmetic>.) |
932 |
|
|
|
933 |
|
|
=end original |
934 |
|
|
|
935 |
|
|
二項演算子の C<<< ">>" >>> は左引数の値を、右引数で示すビット数だけ、 |
936 |
|
|
右にシフトした値を返します。 |
937 |
|
|
引数は整数でなければなりません。 |
938 |
|
|
(L<Integer Arithmetic> も参照して下さい。) |
939 |
|
|
|
940 |
|
|
=begin original |
941 |
|
|
|
942 |
|
|
Note that both C<<< << >>> and C<<< >> >>> in Perl are implemented directly using |
943 |
|
|
C<<< << >>> and C<<< >> >>> in C. If S<C<use integer>> (see L<Integer Arithmetic>) is |
944 |
|
|
in force then signed C integers are used, else unsigned C integers are |
945 |
|
|
used, even for negative shiftees. Either way, the implementation isn't going to generate results |
946 |
|
|
larger than the size of the integer type Perl was built with (32 bits |
947 |
|
|
or 64 bits). |
948 |
|
|
|
949 |
|
|
=end original |
950 |
|
|
|
951 |
|
|
Perl での C<<< << >>> と C<<< >> >>> は C での C<<< << >>> と C<<< >> >>> を |
952 |
|
|
直接利用して実装されていることに注意してください。 |
953 |
|
|
S<C<use integer>> (L<Integer Arithmetic> を参照してください)が有効な場合、 |
954 |
|
|
C の符号付き整数が使われ、そうでない場合は(例え負のシフトでも) |
955 |
|
|
C の符号なし整数が使われます。 |
956 |
|
|
どちらの場合も、この実装は Perl がビルドされた整数型のサイズ(32 ビットか |
957 |
|
|
64 ビット)よりも大きい結果を生成することはありません。 |
958 |
|
|
|
959 |
|
|
=begin original |
960 |
|
|
|
961 |
|
|
The result of overflowing the range of the integers is undefined |
962 |
|
|
because it is undefined also in C. In other words, using 32-bit |
963 |
|
|
integers, S<C<< 1 << 32 >>> is undefined. Shifting by a negative number |
964 |
|
|
of bits is also undefined. |
965 |
|
|
|
966 |
|
|
=end original |
967 |
|
|
|
968 |
|
|
整数の範囲をオーバーフローした場合の結果は、C でも未定義なので、未定義です。 |
969 |
|
|
言い換えると、32 ビット整数を使っているとき、S<C<< 1 << 32 >>> は未定義です。 |
970 |
|
|
シフトするビット数として負の数を指定した場合も未定義です。 |
971 |
|
|
|
972 |
|
|
=begin original |
973 |
|
|
|
974 |
|
|
If you get tired of being subject to your platform's native integers, |
975 |
|
|
the S<C<use bigint>> pragma neatly sidesteps the issue altogether: |
976 |
|
|
|
977 |
|
|
=end original |
978 |
|
|
|
979 |
|
|
プラットフォームのネイティブな整数の影響に疲れたなら、S<C<use bigint>> |
980 |
|
|
プラグマは問題を完全にうまく回避します: |
981 |
|
|
|
982 |
|
|
print 20 << 20; # 20971520 |
983 |
|
|
print 20 << 40; # 5120 on 32-bit machines, |
984 |
|
|
# 21990232555520 on 64-bit machines |
985 |
|
|
use bigint; |
986 |
|
|
print 20 << 100; # 25353012004564588029934064107520 |
987 |
|
|
|
988 |
|
|
=head2 Named Unary Operators |
989 |
|
|
X<operator, named unary> |
990 |
|
|
|
991 |
|
|
(名前付き単項演算子) |
992 |
|
|
|
993 |
|
|
=begin original |
994 |
|
|
|
995 |
|
|
The various named unary operators are treated as functions with one |
996 |
|
|
argument, with optional parentheses. |
997 |
|
|
|
998 |
|
|
=end original |
999 |
|
|
|
1000 |
|
|
さまざまな名前付き単項演算子が、引数を 1 つ持ち、括弧が省略可能な、 |
1001 |
|
|
関数として扱われます。 |
1002 |
|
|
|
1003 |
|
|
=begin original |
1004 |
|
|
|
1005 |
|
|
If any list operator (C<print()>, etc.) or any unary operator (C<chdir()>, etc.) |
1006 |
|
|
is followed by a left parenthesis as the next token, the operator and |
1007 |
|
|
arguments within parentheses are taken to be of highest precedence, |
1008 |
|
|
just like a normal function call. For example, |
1009 |
|
|
because named unary operators are higher precedence than C<||>: |
1010 |
|
|
|
1011 |
|
|
=end original |
1012 |
|
|
|
1013 |
|
|
リスト演算子 (C<print()> など) や単項演算子 (C<chdir()> など) は、 |
1014 |
|
|
すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、 |
1015 |
|
|
通常の関数呼び出しのようにもっとも高い優先順位として扱われます。 |
1016 |
|
|
たとえば、名前つき単項演算子は C<||> より優先順位が高いので、 |
1017 |
|
|
以下のようになります: |
1018 |
|
|
|
1019 |
|
|
chdir $foo || die; # (chdir $foo) || die |
1020 |
|
|
chdir($foo) || die; # (chdir $foo) || die |
1021 |
|
|
chdir ($foo) || die; # (chdir $foo) || die |
1022 |
|
|
chdir +($foo) || die; # (chdir $foo) || die |
1023 |
|
|
|
1024 |
|
|
=begin original |
1025 |
|
|
|
1026 |
|
|
but, because C<"*"> is higher precedence than named operators: |
1027 |
|
|
|
1028 |
|
|
=end original |
1029 |
|
|
|
1030 |
|
|
しかし C<"*"> は名前つき演算子より優先順位が高いので、以下のようになります: |
1031 |
|
|
|
1032 |
|
|
chdir $foo * 20; # chdir ($foo * 20) |
1033 |
|
|
chdir($foo) * 20; # (chdir $foo) * 20 |
1034 |
|
|
chdir ($foo) * 20; # (chdir $foo) * 20 |
1035 |
|
|
chdir +($foo) * 20; # chdir ($foo * 20) |
1036 |
|
|
|
1037 |
|
|
rand 10 * 20; # rand (10 * 20) |
1038 |
|
|
rand(10) * 20; # (rand 10) * 20 |
1039 |
|
|
rand (10) * 20; # (rand 10) * 20 |
1040 |
|
|
rand +(10) * 20; # rand (10 * 20) |
1041 |
|
|
|
1042 |
|
|
=begin original |
1043 |
|
|
|
1044 |
|
|
Regarding precedence, the filetest operators, like C<-f>, C<-M>, etc. are |
1045 |
|
|
treated like named unary operators, but they don't follow this functional |
1046 |
|
|
parenthesis rule. That means, for example, that C<-f($file).".bak"> is |
1047 |
|
|
equivalent to S<C<-f "$file.bak">>. |
1048 |
|
|
X<-X> X<filetest> X<operator, filetest> |
1049 |
|
|
|
1050 |
|
|
=end original |
1051 |
|
|
|
1052 |
|
|
優先順位に関して、C<-f> や C<-M> のようなファイルテスト演算子は、名前付き |
1053 |
|
|
単項演算子として扱われますが、この関数のかっこルールは適用されません。 |
1054 |
|
|
これは、例えば C<-f($file).".bak"> は S<C<-f "$file.bak">> と等価であることを |
1055 |
|
|
意味します。 |
1056 |
|
|
X<-X> X<filetest> X<operator, filetest> |
1057 |
|
|
|
1058 |
|
|
=begin original |
1059 |
|
|
|
1060 |
|
|
See also L<"Terms and List Operators (Leftward)">. |
1061 |
|
|
|
1062 |
|
|
=end original |
1063 |
|
|
|
1064 |
|
|
L<"Terms and List Operators (Leftward)"> も参照して下さい。 |
1065 |
|
|
|
1066 |
|
|
=head2 Relational Operators |
1067 |
|
|
X<relational operator> X<operator, relational> |
1068 |
|
|
|
1069 |
|
|
(比較演算子) |
1070 |
|
|
|
1071 |
|
|
=begin original |
1072 |
|
|
|
1073 |
|
|
Perl operators that return true or false generally return values |
1074 |
|
|
that can be safely used as numbers. For example, the relational |
1075 |
|
|
operators in this section and the equality operators in the next |
1076 |
|
|
one return C<1> for true and a special version of the defined empty |
1077 |
|
|
string, C<"">, which counts as a zero but is exempt from warnings |
1078 |
|
|
about improper numeric conversions, just as S<C<"0 but true">> is. |
1079 |
|
|
|
1080 |
|
|
=end original |
1081 |
|
|
|
1082 |
|
|
真か偽の値を返す Perl 演算子は一般的に安全に数値として使える値を返します。 |
1083 |
|
|
例えば、この節の関係演算子と次の節の等価演算子は、真および、 |
1084 |
|
|
S<C<"0 but true">> と同様、ゼロとカウントされるけれども不適切な数値変換に |
1085 |
|
|
関する警告の出ない、定義された空文字列 C<""> の特別版に対して C<1> を |
1086 |
|
|
返します。 |
1087 |
|
|
|
1088 |
|
|
=begin original |
1089 |
|
|
|
1090 |
|
|
Binary C<< "<" >> returns true if the left argument is numerically less than |
1091 |
|
|
the right argument. |
1092 |
|
|
X<< < >> |
1093 |
|
|
|
1094 |
|
|
=end original |
1095 |
|
|
|
1096 |
|
|
二項演算子の C<< "<" >> は左引数が数値的に右引数よりも小さければ、 |
1097 |
|
|
真を返します。 |
1098 |
|
|
X<< < >> |
1099 |
|
|
|
1100 |
|
|
=begin original |
1101 |
|
|
|
1102 |
|
|
Binary C<< ">" >> returns true if the left argument is numerically greater |
1103 |
|
|
than the right argument. |
1104 |
|
|
X<< > >> |
1105 |
|
|
|
1106 |
|
|
=end original |
1107 |
|
|
|
1108 |
|
|
二項演算子の C<< ">" >> は左引数が数値的に右引数よりも大きければ、 |
1109 |
|
|
真を返します。 |
1110 |
|
|
X<< > >> |
1111 |
|
|
|
1112 |
|
|
=begin original |
1113 |
|
|
|
1114 |
|
|
Binary C<< "<=" >> returns true if the left argument is numerically less than |
1115 |
|
|
or equal to the right argument. |
1116 |
|
|
X<< <= >> |
1117 |
|
|
|
1118 |
|
|
=end original |
1119 |
|
|
|
1120 |
|
|
二項演算子の C<< "<=" >> は左引数が数値的に右引数よりも小さいか等しければ、 |
1121 |
|
|
真を返します。 |
1122 |
|
|
X<< <= >> |
1123 |
|
|
|
1124 |
|
|
=begin original |
1125 |
|
|
|
1126 |
|
|
Binary C<< ">=" >> returns true if the left argument is numerically greater |
1127 |
|
|
than or equal to the right argument. |
1128 |
|
|
X<< >= >> |
1129 |
|
|
|
1130 |
|
|
=end original |
1131 |
|
|
|
1132 |
|
|
二項演算子の C<< ">=" >> は左引数が数値的に右引数よりも大きいか等しければ、 |
1133 |
|
|
真を返します。 |
1134 |
|
|
X<< >= >> |
1135 |
|
|
|
1136 |
|
|
=begin original |
1137 |
|
|
|
1138 |
|
|
Binary C<"lt"> returns true if the left argument is stringwise less than |
1139 |
|
|
the right argument. |
1140 |
|
|
X<< lt >> |
1141 |
|
|
|
1142 |
|
|
=end original |
1143 |
|
|
|
1144 |
|
|
二項演算子の C<"lt"> は左引数が文字列的に右引数よりも小さければ、真を返します。 |
1145 |
|
|
X<< lt >> |
1146 |
|
|
|
1147 |
|
|
=begin original |
1148 |
|
|
|
1149 |
|
|
Binary C<"gt"> returns true if the left argument is stringwise greater |
1150 |
|
|
than the right argument. |
1151 |
|
|
X<< gt >> |
1152 |
|
|
|
1153 |
|
|
=end original |
1154 |
|
|
|
1155 |
|
|
二項演算子の C<"gt"> は左引数が文字列的に右引数よりも大きければ、真を返します。 |
1156 |
|
|
X<< gt >> |
1157 |
|
|
|
1158 |
|
|
=begin original |
1159 |
|
|
|
1160 |
|
|
Binary C<"le"> returns true if the left argument is stringwise less than |
1161 |
|
|
or equal to the right argument. |
1162 |
|
|
X<< le >> |
1163 |
|
|
|
1164 |
|
|
=end original |
1165 |
|
|
|
1166 |
|
|
二項演算子の C<"le"> は左引数が文字列的に右引数よりも小さいか等しければ、 |
1167 |
|
|
真を返します。 |
1168 |
|
|
X<< le >> |
1169 |
|
|
|
1170 |
|
|
=begin original |
1171 |
|
|
|
1172 |
|
|
Binary C<"ge"> returns true if the left argument is stringwise greater |
1173 |
|
|
than or equal to the right argument. |
1174 |
|
|
X<< ge >> |
1175 |
|
|
|
1176 |
|
|
=end original |
1177 |
|
|
|
1178 |
|
|
二項演算子の C<"ge"> は左引数が文字列的に右引数よりも大きいか等しければ、 |
1179 |
|
|
真を返します。 |
1180 |
|
|
X<< ge >> |
1181 |
|
|
|
1182 |
|
|
=head2 Equality Operators |
1183 |
|
|
X<equality> X<equal> X<equals> X<operator, equality> |
1184 |
|
|
|
1185 |
|
|
(等価演算子) |
1186 |
|
|
|
1187 |
|
|
=begin original |
1188 |
|
|
|
1189 |
|
|
Binary C<< "==" >> returns true if the left argument is numerically equal to |
1190 |
|
|
the right argument. |
1191 |
|
|
X<==> |
1192 |
|
|
|
1193 |
|
|
=end original |
1194 |
|
|
|
1195 |
|
|
二項演算子の C<< "==" >> は左引数が数値的に右引数と等しければ、真を返します。 |
1196 |
|
|
X<==> |
1197 |
|
|
|
1198 |
|
|
=begin original |
1199 |
|
|
|
1200 |
|
|
Binary C<< "!=" >> returns true if the left argument is numerically not equal |
1201 |
|
|
to the right argument. |
1202 |
|
|
X<!=> |
1203 |
|
|
|
1204 |
|
|
=end original |
1205 |
|
|
|
1206 |
|
|
二項演算子の C<< "!=" >> は左引数が数値的に右引数と等しくなければ、真を |
1207 |
|
|
返します。 |
1208 |
|
|
X<!=> |
1209 |
|
|
|
1210 |
|
|
=begin original |
1211 |
|
|
|
1212 |
|
|
Binary C<< "<=>" >> returns -1, 0, or 1 depending on whether the left |
1213 |
|
|
argument is numerically less than, equal to, or greater than the right |
1214 |
|
|
argument. If your platform supports C<NaN>'s (not-a-numbers) as numeric |
1215 |
|
|
values, using them with C<< "<=>" >> returns undef. C<NaN> is not |
1216 |
|
|
C<< "<" >>, C<< "==" >>, C<< ">" >>, C<< "<=" >> or C<< ">=" >> anything |
1217 |
|
|
(even C<NaN>), so those 5 return false. S<C<< NaN != NaN >>> returns |
1218 |
|
|
true, as does S<C<NaN !=> I<anything else>>. If your platform doesn't |
1219 |
|
|
support C<NaN>'s then C<NaN> is just a string with numeric value 0. |
1220 |
|
|
X<< <=> >> |
1221 |
|
|
X<spaceship> |
1222 |
|
|
|
1223 |
|
|
=end original |
1224 |
|
|
|
1225 |
|
|
二項演算子の C<< "<=>" >> は左引数が数値的に右引数より小さいか、等しいか、 |
1226 |
|
|
大きいかに従って、-1, 0, 1 を返します。 |
1227 |
|
|
数値として C<NaN> (非数) に対応しているプラットフォームでは、 |
1228 |
|
|
それに対して C<< "<=>" >> を使うと undef を返します。 |
1229 |
|
|
C<NaN> はどの値に対しても(C<NaN> に対してでさえも) C<< "<" >>, C<< "==" >>, |
1230 |
|
|
C<< ">" >>, C<< "<=" >>, C<< ">=" >> のいずれも成立しないので、これらは全て |
1231 |
|
|
偽となります。 |
1232 |
|
|
S<C<< NaN != NaN >>> は真を返しますが、 |
1233 |
|
|
S<C<NaN !=> I<その他のどの値でも>> です。 |
1234 |
|
|
C<NaN> に対応していないプラットフォームでは、C<NaN> は単に数としての値 0 を |
1235 |
|
|
持つ文字列です。 |
1236 |
|
|
X<< <=> >> X<spaceship> |
1237 |
|
|
|
1238 |
|
|
$ perl -le '$x = "NaN"; print "No NaN support here" if $x == $x' |
1239 |
|
|
$ perl -le '$x = "NaN"; print "NaN support here" if $x != $x' |
1240 |
|
|
|
1241 |
|
|
=begin original |
1242 |
|
|
|
1243 |
|
|
(Note that the L<bigint>, L<bigrat>, and L<bignum> pragmas all |
1244 |
|
|
support C<"NaN">.) |
1245 |
|
|
|
1246 |
|
|
=end original |
1247 |
|
|
|
1248 |
|
|
(L<bigint>, L<bigrat>, L<bignum> プラグマは全て C<"NaN"> に対応していることに |
1249 |
|
|
注意してください。) |
1250 |
|
|
|
1251 |
|
|
=begin original |
1252 |
|
|
|
1253 |
|
|
Binary C<"eq"> returns true if the left argument is stringwise equal to |
1254 |
|
|
the right argument. |
1255 |
|
|
X<eq> |
1256 |
|
|
|
1257 |
|
|
=end original |
1258 |
|
|
|
1259 |
|
|
二項演算子の C<"eq"> は左引数が文字列的に右引数と等しければ、真を返します。 |
1260 |
|
|
X<eq> |
1261 |
|
|
|
1262 |
|
|
=begin original |
1263 |
|
|
|
1264 |
|
|
Binary C<"ne"> returns true if the left argument is stringwise not equal |
1265 |
|
|
to the right argument. |
1266 |
|
|
X<ne> |
1267 |
|
|
|
1268 |
|
|
=end original |
1269 |
|
|
|
1270 |
|
|
二項演算子の C<"ne"> は左引数が文字列的に右引数と等しくなければ、真を |
1271 |
|
|
返します。 |
1272 |
|
|
X<ne> |
1273 |
|
|
|
1274 |
|
|
=begin original |
1275 |
|
|
|
1276 |
|
|
Binary C<"cmp"> returns -1, 0, or 1 depending on whether the left |
1277 |
|
|
argument is stringwise less than, equal to, or greater than the right |
1278 |
|
|
argument. |
1279 |
|
|
X<cmp> |
1280 |
|
|
|
1281 |
|
|
=end original |
1282 |
|
|
|
1283 |
|
|
二項演算子の C<"cmp"> は左引数が文字列的に右引数より小さいか、 |
1284 |
|
|
等しいか、大きいかに従って、-1, 0, 1 を返します。 |
1285 |
|
|
X<cmp> |
1286 |
|
|
|
1287 |
|
|
=begin original |
1288 |
|
|
|
1289 |
|
|
Binary C<"~~"> does a smartmatch between its arguments. Smart matching |
1290 |
|
|
is described in the next section. |
1291 |
|
|
X<~~> |
1292 |
|
|
|
1293 |
|
|
=end original |
1294 |
|
|
|
1295 |
|
|
二項演算子の C<"~~"> はその引数に対してスマートマッチングを行います。 |
1296 |
|
|
スマートマッチングについては次の節で述べられています。 |
1297 |
|
|
X<~~> |
1298 |
|
|
|
1299 |
|
|
=begin original |
1300 |
|
|
|
1301 |
|
|
C<"lt">, C<"le">, C<"ge">, C<"gt"> and C<"cmp"> use the collation (sort) |
1302 |
|
|
order specified by the current C<LC_COLLATE> locale if a S<C<use |
1303 |
|
|
locale>> form that includes collation is in effect. See L<perllocale>. |
1304 |
|
|
Do not mix these with Unicode, |
1305 |
|
|
only use them with legacy 8-bit locale encodings. |
1306 |
|
|
The standard C<L<Unicode::Collate>> and |
1307 |
|
|
C<L<Unicode::Collate::Locale>> modules offer much more powerful |
1308 |
|
|
solutions to collation issues. |
1309 |
|
|
|
1310 |
|
|
=end original |
1311 |
|
|
|
1312 |
|
|
C<"lt">, C<"le">, C<"ge">, C<"gt">, C<"cmp"> は照合を含む |
1313 |
|
|
S<C<use locale>> 型式が有効な場合は、現在の C<LC_COLLATE> ロケールで |
1314 |
|
|
指定された照合(ソート)順が使われます。 |
1315 |
|
|
L<perllocale> を参照して下さい。 |
1316 |
|
|
これらを Unicode と混ぜないでください; |
1317 |
|
|
伝統的な 8 ビットロケールコーディングでのみ使ってください。 |
1318 |
|
|
標準の C<L<Unicode::Collate>> と C<L<Unicode::Collate::Locale>> モジュールは、 |
1319 |
|
|
照合問題に関する遥かに強力な解決法を提供します。 |
1320 |
|
|
|
1321 |
|
|
=begin original |
1322 |
|
|
|
1323 |
|
|
For case-insensitive comparisions, look at the L<perlfunc/fc> case-folding |
1324 |
|
|
function, available in Perl v5.16 or later: |
1325 |
|
|
|
1326 |
|
|
=end original |
1327 |
|
|
|
1328 |
argrath |
1.2 |
大文字小文字を無視した比較に関しては、Perl v5.16 以降で利用可能な |
1329 |
|
|
L<perlfunc/fc> 大文字小文字畳み込み関数を参照してください: |
1330 |
argrath |
1.1 |
|
1331 |
|
|
if ( fc($x) eq fc($y) ) { ... } |
1332 |
|
|
|
1333 |
|
|
=head2 Smartmatch Operator |
1334 |
|
|
|
1335 |
|
|
(スマートマッチング演算子) |
1336 |
|
|
|
1337 |
|
|
=begin original |
1338 |
|
|
|
1339 |
|
|
First available in Perl 5.10.1 (the 5.10.0 version behaved differently), |
1340 |
|
|
binary C<~~> does a "smartmatch" between its arguments. This is mostly |
1341 |
|
|
used implicitly in the C<when> construct described in L<perlsyn>, although |
1342 |
|
|
not all C<when> clauses call the smartmatch operator. Unique among all of |
1343 |
|
|
Perl's operators, the smartmatch operator can recurse. The smartmatch |
1344 |
|
|
operator is L<experimental|perlpolicy/experimental> and its behavior is |
1345 |
|
|
subject to change. |
1346 |
|
|
|
1347 |
|
|
=end original |
1348 |
|
|
|
1349 |
|
|
Perl 5.10.1 で最初に現れた (5.10.0 版は異なった振る舞いでした)、2 項 C<~~> は |
1350 |
|
|
引数に対する「スマートマッチング」を行います。 |
1351 |
|
|
これはほとんど L<perlsyn> で記述されている C<when> 構文で暗黙に使われますが、 |
1352 |
|
|
C<when> 節だけがスマートマッチング演算子を呼び出すわけではありません。 |
1353 |
|
|
全ての Perl の演算子の中で唯一、スマートマッチング演算子は再帰できます。 |
1354 |
|
|
スマートマッチング演算子は L<実験的|perlpolicy/experimental> で、その振る舞いは |
1355 |
|
|
変更されることがあります。 |
1356 |
|
|
|
1357 |
|
|
=begin original |
1358 |
|
|
|
1359 |
|
|
It is also unique in that all other Perl operators impose a context |
1360 |
|
|
(usually string or numeric context) on their operands, autoconverting |
1361 |
|
|
those operands to those imposed contexts. In contrast, smartmatch |
1362 |
|
|
I<infers> contexts from the actual types of its operands and uses that |
1363 |
|
|
type information to select a suitable comparison mechanism. |
1364 |
|
|
|
1365 |
|
|
=end original |
1366 |
|
|
|
1367 |
|
|
また、その他全ての Perl 演算子はそのオペランドにコンテキスト(通常は |
1368 |
|
|
文字列または数値コンテキスト)を割り当てて、オペランドを割り当てた |
1369 |
|
|
コンテキストに自動変換します。 |
1370 |
|
|
一方、スマートマッチングはそのオペランドの実際の型からコンテキストを |
1371 |
|
|
I<推論> して、適切な比較機構を選択するためにその型情報を使います。 |
1372 |
|
|
|
1373 |
|
|
=begin original |
1374 |
|
|
|
1375 |
|
|
The C<~~> operator compares its operands "polymorphically", determining how |
1376 |
|
|
to compare them according to their actual types (numeric, string, array, |
1377 |
|
|
hash, etc.) Like the equality operators with which it shares the same |
1378 |
|
|
precedence, C<~~> returns 1 for true and C<""> for false. It is often best |
1379 |
|
|
read aloud as "in", "inside of", or "is contained in", because the left |
1380 |
|
|
operand is often looked for I<inside> the right operand. That makes the |
1381 |
|
|
order of the operands to the smartmatch operand often opposite that of |
1382 |
|
|
the regular match operator. In other words, the "smaller" thing is usually |
1383 |
|
|
placed in the left operand and the larger one in the right. |
1384 |
|
|
|
1385 |
|
|
=end original |
1386 |
|
|
|
1387 |
|
|
C<~~> 演算子はオペランドを「多態的に」比較します; どのように比較するかの |
1388 |
|
|
決定は、実際の型 (数値、文字列、配列、ハッシュなど) に基づきます。 |
1389 |
|
|
同じ優先順位を共有する等価演算子のように、 |
1390 |
|
|
C<~~> は真では 1 を、偽では C<""> を返します。 |
1391 |
|
|
これはしばしば "in", "inside of", "is contained in" と呼ぶのが最良です; |
1392 |
|
|
なぜなら左オペランドはしばしば右オペランドの I<内側> を探すからです。 |
1393 |
|
|
これにより、スマートマッチングオペランドへのオペランドの順序はしばしば |
1394 |
|
|
正規表現演算子のものと逆になります。 |
1395 |
|
|
言い換えると、「より小さい」ものが普通は左オペランドに置かれ、より |
1396 |
|
|
大きいものが右側に置かれます。 |
1397 |
|
|
|
1398 |
|
|
=begin original |
1399 |
|
|
|
1400 |
|
|
The behavior of a smartmatch depends on what type of things its arguments |
1401 |
|
|
are, as determined by the following table. The first row of the table |
1402 |
|
|
whose types apply determines the smartmatch behavior. Because what |
1403 |
|
|
actually happens is mostly determined by the type of the second operand, |
1404 |
|
|
the table is sorted on the right operand instead of on the left. |
1405 |
|
|
|
1406 |
|
|
=end original |
1407 |
|
|
|
1408 |
|
|
スマートマッチングの振る舞いは、次の表で決定されるように、引数がどんな |
1409 |
|
|
型かに依存します。 |
1410 |
|
|
型が適用される表の最初の行は、スマートマッチングの振る舞いを決定します。 |
1411 |
|
|
実際に何が起こるかはほとんどの場合 2 番目のオペランドの型で決定されるので、 |
1412 |
|
|
表は左ではなく右オペランドでソートされています。 |
1413 |
|
|
|
1414 |
|
|
=begin original |
1415 |
|
|
|
1416 |
|
|
Left Right Description and pseudocode |
1417 |
|
|
=============================================================== |
1418 |
|
|
Any undef check whether Any is undefined |
1419 |
|
|
like: !defined Any |
1420 |
|
|
|
1421 |
|
|
=end original |
1422 |
|
|
|
1423 |
|
|
左 右 説明と擬似コード |
1424 |
|
|
=============================================================== |
1425 |
|
|
Any undef Any が未定義かどうか調べる |
1426 |
|
|
like: !defined Any |
1427 |
|
|
|
1428 |
|
|
=begin original |
1429 |
|
|
|
1430 |
|
|
Any Object invoke ~~ overloading on Object, or die |
1431 |
|
|
|
1432 |
|
|
=end original |
1433 |
|
|
|
1434 |
|
|
Any Object Object に対する ~~ オーバーロードを起動するか die |
1435 |
|
|
|
1436 |
|
|
=begin original |
1437 |
|
|
|
1438 |
|
|
Right operand is an ARRAY: |
1439 |
|
|
|
1440 |
|
|
=end original |
1441 |
|
|
|
1442 |
|
|
右被演算子が 配列: |
1443 |
|
|
|
1444 |
|
|
=begin original |
1445 |
|
|
|
1446 |
|
|
Left Right Description and pseudocode |
1447 |
|
|
=============================================================== |
1448 |
|
|
ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2] |
1449 |
|
|
like: (ARRAY1[0] ~~ ARRAY2[0]) |
1450 |
|
|
&& (ARRAY1[1] ~~ ARRAY2[1]) && ... |
1451 |
|
|
HASH ARRAY any ARRAY elements exist as HASH keys |
1452 |
|
|
like: grep { exists HASH->{$_} } ARRAY |
1453 |
|
|
Regexp ARRAY any ARRAY elements pattern match Regexp |
1454 |
|
|
like: grep { /Regexp/ } ARRAY |
1455 |
|
|
undef ARRAY undef in ARRAY |
1456 |
|
|
like: grep { !defined } ARRAY |
1457 |
|
|
Any ARRAY smartmatch each ARRAY element[3] |
1458 |
|
|
like: grep { Any ~~ $_ } ARRAY |
1459 |
|
|
|
1460 |
|
|
=end original |
1461 |
|
|
|
1462 |
|
|
左 右 説明と擬似コード |
1463 |
|
|
=============================================================== |
1464 |
|
|
ARRAY1 ARRAY2 ARRAY1 と ARRAY2 の組の要素に対して再帰 [2] |
1465 |
|
|
like: (ARRAY1[0] ~~ ARRAY2[0]) |
1466 |
|
|
&& (ARRAY1[1] ~~ ARRAY2[1]) && ... |
1467 |
|
|
HASH ARRAY いずれかの ARRAY 要素が HASH キーに存在するか |
1468 |
|
|
like: grep { exists HASH->{$_} } ARRAY |
1469 |
|
|
Regexp ARRAY いずれかの ARRAY 要素が Regexp でマッチングするか |
1470 |
|
|
like: grep { /Regexp/ } ARRAY |
1471 |
|
|
undef ARRAY ARRAY 内の undef |
1472 |
|
|
like: grep { !defined } ARRAY |
1473 |
|
|
Any ARRAY それぞれの ARRAY 要素に対してスマートマッチング [3] |
1474 |
|
|
like: grep { Any ~~ $_ } ARRAY |
1475 |
|
|
|
1476 |
|
|
=begin original |
1477 |
|
|
|
1478 |
|
|
Right operand is a HASH: |
1479 |
|
|
|
1480 |
|
|
=end original |
1481 |
|
|
|
1482 |
|
|
右被演算子がハッシュ: |
1483 |
|
|
|
1484 |
|
|
=begin original |
1485 |
|
|
|
1486 |
|
|
Left Right Description and pseudocode |
1487 |
|
|
=============================================================== |
1488 |
|
|
HASH1 HASH2 all same keys in both HASHes |
1489 |
|
|
like: keys HASH1 == |
1490 |
|
|
grep { exists HASH2->{$_} } keys HASH1 |
1491 |
|
|
ARRAY HASH any ARRAY elements exist as HASH keys |
1492 |
|
|
like: grep { exists HASH->{$_} } ARRAY |
1493 |
|
|
Regexp HASH any HASH keys pattern match Regexp |
1494 |
|
|
like: grep { /Regexp/ } keys HASH |
1495 |
|
|
undef HASH always false (undef can't be a key) |
1496 |
|
|
like: 0 == 1 |
1497 |
|
|
Any HASH HASH key existence |
1498 |
|
|
like: exists HASH->{Any} |
1499 |
|
|
|
1500 |
|
|
=end original |
1501 |
|
|
|
1502 |
|
|
左 右 説明と擬似コード |
1503 |
|
|
=============================================================== |
1504 |
|
|
HASH1 HASH2 HASH1 と HASH2 両方が全て同じキー |
1505 |
|
|
like: keys HASH1 == |
1506 |
|
|
grep { exists HASH2->{$_} } keys HASH1 |
1507 |
|
|
ARRAY HASH いずれかの ARRAY 要素が HASH キーに存在するか |
1508 |
|
|
like: grep { exists HASH->{$_} } ARRAY |
1509 |
|
|
Regexp HASH いずれかの HASH キーが Regexp でマッチングするか |
1510 |
|
|
like: grep { /Regexp/ } keys HASH |
1511 |
|
|
undef HASH 常に偽 (undef はキーになれない) |
1512 |
|
|
like: 0 == 1 |
1513 |
|
|
Any HASH HASH キーが存在するか |
1514 |
|
|
like: exists HASH->{Any} |
1515 |
|
|
|
1516 |
|
|
=begin original |
1517 |
|
|
|
1518 |
|
|
Right operand is CODE: |
1519 |
|
|
|
1520 |
|
|
=end original |
1521 |
|
|
|
1522 |
|
|
右被演算子がコードリファレンス: |
1523 |
|
|
|
1524 |
|
|
=begin original |
1525 |
|
|
|
1526 |
|
|
Left Right Description and pseudocode |
1527 |
|
|
=============================================================== |
1528 |
|
|
ARRAY CODE sub returns true on all ARRAY elements[1] |
1529 |
|
|
like: !grep { !CODE->($_) } ARRAY |
1530 |
|
|
HASH CODE sub returns true on all HASH keys[1] |
1531 |
|
|
like: !grep { !CODE->($_) } keys HASH |
1532 |
|
|
Any CODE sub passed Any returns true |
1533 |
|
|
like: CODE->(Any) |
1534 |
|
|
|
1535 |
|
|
=end original |
1536 |
|
|
|
1537 |
|
|
左 右 説明と擬似コード |
1538 |
|
|
=============================================================== |
1539 |
|
|
ARRAY CODE 全ての ARRAY 要素に対してサブルーチンが真を返す [1] |
1540 |
|
|
like: !grep { !CODE->($_) } ARRAY |
1541 |
|
|
HASH CODE 全ての HASH キーに対してサブルーチンが真を返す [1] |
1542 |
|
|
like: !grep { !CODE->($_) } keys HASH |
1543 |
|
|
Any CODE サブルーチンに Any を渡して真を返す |
1544 |
|
|
like: CODE->(Any) |
1545 |
|
|
|
1546 |
|
|
=begin original |
1547 |
|
|
|
1548 |
|
|
Right operand is a Regexp: |
1549 |
|
|
|
1550 |
|
|
=end original |
1551 |
|
|
|
1552 |
|
|
右被演算子が正規表現: |
1553 |
|
|
|
1554 |
|
|
=begin original |
1555 |
|
|
|
1556 |
|
|
Left Right Description and pseudocode |
1557 |
|
|
=============================================================== |
1558 |
|
|
ARRAY Regexp any ARRAY elements match Regexp |
1559 |
|
|
like: grep { /Regexp/ } ARRAY |
1560 |
|
|
HASH Regexp any HASH keys match Regexp |
1561 |
|
|
like: grep { /Regexp/ } keys HASH |
1562 |
|
|
Any Regexp pattern match |
1563 |
|
|
like: Any =~ /Regexp/ |
1564 |
|
|
|
1565 |
|
|
=end original |
1566 |
|
|
|
1567 |
|
|
左 右 説明と擬似コード |
1568 |
|
|
=============================================================== |
1569 |
|
|
ARRAY Regexp いずれかの ARRAY 要素が Regexp にマッチングするか |
1570 |
|
|
like: grep { /Regexp/ } ARRAY |
1571 |
|
|
HASH Regexp いずれかの HASH キーが Regexp にマッチングするか |
1572 |
|
|
like: grep { /Regexp/ } keys HASH |
1573 |
|
|
Any Regexp パターンマッチング |
1574 |
|
|
like: Any =~ /Regexp/ |
1575 |
|
|
|
1576 |
|
|
=begin original |
1577 |
|
|
|
1578 |
|
|
Other: |
1579 |
|
|
|
1580 |
|
|
=end original |
1581 |
|
|
|
1582 |
|
|
その他: |
1583 |
|
|
|
1584 |
|
|
=begin original |
1585 |
|
|
|
1586 |
|
|
Left Right Description and pseudocode |
1587 |
|
|
=============================================================== |
1588 |
|
|
Object Any invoke ~~ overloading on Object, |
1589 |
|
|
or fall back to... |
1590 |
|
|
|
1591 |
|
|
=end original |
1592 |
|
|
|
1593 |
|
|
左 右 説明と擬似コード |
1594 |
|
|
=============================================================== |
1595 |
|
|
Object Any Object に対して ~~ のオーバーロードを起動、 |
1596 |
|
|
あるいは次にフォールバック… |
1597 |
|
|
|
1598 |
|
|
=begin original |
1599 |
|
|
|
1600 |
|
|
Any Num numeric equality |
1601 |
|
|
like: Any == Num |
1602 |
|
|
Num nummy[4] numeric equality |
1603 |
|
|
like: Num == nummy |
1604 |
|
|
undef Any check whether undefined |
1605 |
|
|
like: !defined(Any) |
1606 |
|
|
Any Any string equality |
1607 |
|
|
like: Any eq Any |
1608 |
|
|
|
1609 |
|
|
=end original |
1610 |
|
|
|
1611 |
|
|
Any Num 数値の等価性 |
1612 |
|
|
like: Any == Num |
1613 |
|
|
Num nummy[4] 数値の等価性 |
1614 |
|
|
like: Num == nummy |
1615 |
|
|
undef Any 未定義かどうかを調べる |
1616 |
|
|
like: !defined(Any) |
1617 |
|
|
Any Any 文字列の等価性 |
1618 |
|
|
like: Any eq Any |
1619 |
|
|
|
1620 |
|
|
=begin original |
1621 |
|
|
|
1622 |
|
|
Notes: |
1623 |
|
|
|
1624 |
|
|
=end original |
1625 |
|
|
|
1626 |
|
|
注意: |
1627 |
|
|
|
1628 |
|
|
=over |
1629 |
|
|
|
1630 |
|
|
=item 1. |
1631 |
|
|
Empty hashes or arrays match. |
1632 |
|
|
|
1633 |
|
|
(1. 空ハッシュや配列はマッチングします。) |
1634 |
|
|
|
1635 |
|
|
=item 2. |
1636 |
|
|
That is, each element smartmatches the element of the same index in the other array.[3] |
1637 |
|
|
|
1638 |
|
|
(2. つまり、それぞれの要素は他の配列の同じインデックスの要素とスマートマッチングします。[3]) |
1639 |
|
|
|
1640 |
|
|
=item 3. |
1641 |
|
|
If a circular reference is found, fall back to referential equality. |
1642 |
|
|
|
1643 |
|
|
(3. 循環参照が見つかると、参照の透過性にフォールバックします。) |
1644 |
|
|
|
1645 |
|
|
=item 4. |
1646 |
|
|
Either an actual number, or a string that looks like one. |
1647 |
|
|
|
1648 |
|
|
(4. 実際の数値か、数値に見える文字列のどちらかです。) |
1649 |
|
|
|
1650 |
|
|
=back |
1651 |
|
|
|
1652 |
|
|
=begin original |
1653 |
|
|
|
1654 |
|
|
The smartmatch implicitly dereferences any non-blessed hash or array |
1655 |
|
|
reference, so the C<I<HASH>> and C<I<ARRAY>> entries apply in those cases. |
1656 |
|
|
For blessed references, the C<I<Object>> entries apply. Smartmatches |
1657 |
|
|
involving hashes only consider hash keys, never hash values. |
1658 |
|
|
|
1659 |
|
|
=end original |
1660 |
|
|
|
1661 |
|
|
スマートマッチングは bless されていないハッシュや配列のリファレンスを暗黙に |
1662 |
|
|
デリファレンスするので、それらの場合では C<I<HASH>> と C<I<ARRAY>> の |
1663 |
|
|
エントリが適用されます。 |
1664 |
|
|
bless されたリファレンスでは、C<I<Object>> エントリが適用されます。 |
1665 |
|
|
ハッシュに関連するスマートマッチングはキーのみを考慮し、ハッシュの値は |
1666 |
|
|
考慮しません。 |
1667 |
|
|
|
1668 |
|
|
=begin original |
1669 |
|
|
|
1670 |
|
|
The "like" code entry is not always an exact rendition. For example, the |
1671 |
|
|
smartmatch operator short-circuits whenever possible, but C<grep> does |
1672 |
|
|
not. Also, C<grep> in scalar context returns the number of matches, but |
1673 |
|
|
C<~~> returns only true or false. |
1674 |
|
|
|
1675 |
|
|
=end original |
1676 |
|
|
|
1677 |
|
|
"like" コードエントリは常に正確な処理を行うわけではありません。 |
1678 |
|
|
例えば、スマートマッチング演算子は可能なら短絡しますが、C<grep> はしません。 |
1679 |
|
|
また、スカラコンテキストでは C<grep> はマッチングした数を返しますが、 |
1680 |
|
|
C<~~> は真か偽かのみを返します。 |
1681 |
|
|
|
1682 |
|
|
=begin original |
1683 |
|
|
|
1684 |
|
|
Unlike most operators, the smartmatch operator knows to treat C<undef> |
1685 |
|
|
specially: |
1686 |
|
|
|
1687 |
|
|
=end original |
1688 |
|
|
|
1689 |
|
|
ほとんどの演算子と異なり、スマートマッチング演算子は C<undef> を特別に |
1690 |
|
|
扱う方法を知っています: |
1691 |
|
|
|
1692 |
|
|
use v5.10.1; |
1693 |
|
|
@array = (1, 2, 3, undef, 4, 5); |
1694 |
|
|
say "some elements undefined" if undef ~~ @array; |
1695 |
|
|
|
1696 |
|
|
=begin original |
1697 |
|
|
|
1698 |
|
|
Each operand is considered in a modified scalar context, the modification |
1699 |
|
|
being that array and hash variables are passed by reference to the |
1700 |
|
|
operator, which implicitly dereferences them. Both elements |
1701 |
|
|
of each pair are the same: |
1702 |
|
|
|
1703 |
|
|
=end original |
1704 |
|
|
|
1705 |
|
|
それぞれのオペランドは修正されたスカラコンテキストと考えられます; |
1706 |
|
|
修正というのは、配列とハッシュの変数は演算子にリファレンスが渡され、 |
1707 |
|
|
暗黙にデリファレンスされます。 |
1708 |
|
|
それぞれの組のどちらの要素も同じです: |
1709 |
|
|
|
1710 |
|
|
use v5.10.1; |
1711 |
|
|
|
1712 |
|
|
my %hash = (red => 1, blue => 2, green => 3, |
1713 |
|
|
orange => 4, yellow => 5, purple => 6, |
1714 |
|
|
black => 7, grey => 8, white => 9); |
1715 |
|
|
|
1716 |
|
|
my @array = qw(red blue green); |
1717 |
|
|
|
1718 |
|
|
say "some array elements in hash keys" if @array ~~ %hash; |
1719 |
|
|
say "some array elements in hash keys" if \@array ~~ \%hash; |
1720 |
|
|
|
1721 |
|
|
say "red in array" if "red" ~~ @array; |
1722 |
|
|
say "red in array" if "red" ~~ \@array; |
1723 |
|
|
|
1724 |
|
|
say "some keys end in e" if /e$/ ~~ %hash; |
1725 |
|
|
say "some keys end in e" if /e$/ ~~ \%hash; |
1726 |
|
|
|
1727 |
|
|
=begin original |
1728 |
|
|
|
1729 |
|
|
Two arrays smartmatch if each element in the first array smartmatches |
1730 |
|
|
(that is, is "in") the corresponding element in the second array, |
1731 |
|
|
recursively. |
1732 |
|
|
|
1733 |
|
|
=end original |
1734 |
|
|
|
1735 |
|
|
二つの配列は、最初の配列のそれぞれの要素が、二つ目の配列の対応する要素に |
1736 |
|
|
(つまり "in") 再帰的にスマートマッチングするときに、スマートマッチングします。 |
1737 |
|
|
|
1738 |
|
|
use v5.10.1; |
1739 |
|
|
my @little = qw(red blue green); |
1740 |
|
|
my @bigger = ("red", "blue", [ "orange", "green" ] ); |
1741 |
|
|
if (@little ~~ @bigger) { # true! |
1742 |
|
|
say "little is contained in bigger"; |
1743 |
|
|
} |
1744 |
|
|
|
1745 |
|
|
=begin original |
1746 |
|
|
|
1747 |
|
|
Because the smartmatch operator recurses on nested arrays, this |
1748 |
|
|
will still report that "red" is in the array. |
1749 |
|
|
|
1750 |
|
|
=end original |
1751 |
|
|
|
1752 |
|
|
スマートマッチング演算子はネストした配列を再帰するので、これは "red" が |
1753 |
|
|
配列にいると報告するままです。 |
1754 |
|
|
|
1755 |
|
|
use v5.10.1; |
1756 |
|
|
my @array = qw(red blue green); |
1757 |
|
|
my $nested_array = [[[[[[[ @array ]]]]]]]; |
1758 |
|
|
say "red in array" if "red" ~~ $nested_array; |
1759 |
|
|
|
1760 |
|
|
=begin original |
1761 |
|
|
|
1762 |
|
|
If two arrays smartmatch each other, then they are deep |
1763 |
|
|
copies of each others' values, as this example reports: |
1764 |
|
|
|
1765 |
|
|
=end original |
1766 |
|
|
|
1767 |
|
|
二つの配列が互いにスマートマッチングすると、次の例が報告しているように、 |
1768 |
|
|
互いの値のディープコピーになります: |
1769 |
|
|
|
1770 |
|
|
use v5.12.0; |
1771 |
|
|
my @a = (0, 1, 2, [3, [4, 5], 6], 7); |
1772 |
|
|
my @b = (0, 1, 2, [3, [4, 5], 6], 7); |
1773 |
|
|
|
1774 |
|
|
if (@a ~~ @b && @b ~~ @a) { |
1775 |
|
|
say "a and b are deep copies of each other"; |
1776 |
|
|
} |
1777 |
|
|
elsif (@a ~~ @b) { |
1778 |
|
|
say "a smartmatches in b"; |
1779 |
|
|
} |
1780 |
|
|
elsif (@b ~~ @a) { |
1781 |
|
|
say "b smartmatches in a"; |
1782 |
|
|
} |
1783 |
|
|
else { |
1784 |
|
|
say "a and b don't smartmatch each other at all"; |
1785 |
|
|
} |
1786 |
|
|
|
1787 |
|
|
=begin original |
1788 |
|
|
|
1789 |
|
|
If you were to set S<C<$b[3] = 4>>, then instead of reporting that "a and b |
1790 |
|
|
are deep copies of each other", it now reports that C<"b smartmatches in a">. |
1791 |
|
|
That's because the corresponding position in C<@a> contains an array that |
1792 |
|
|
(eventually) has a 4 in it. |
1793 |
|
|
|
1794 |
|
|
=end original |
1795 |
|
|
|
1796 |
|
|
S<C<$b[3] = 4>> に設定すると、"a and b are deep copies of each other" と |
1797 |
|
|
報告されるのではなく、C<"b smartmatches in a"> と報告されます。 |
1798 |
|
|
これは、C<@a> の対応する位置には(最終的に)中に 4 がある配列を |
1799 |
|
|
含んでいるからです。 |
1800 |
|
|
|
1801 |
|
|
=begin original |
1802 |
|
|
|
1803 |
|
|
Smartmatching one hash against another reports whether both contain the |
1804 |
|
|
same keys, no more and no less. This could be used to see whether two |
1805 |
|
|
records have the same field names, without caring what values those fields |
1806 |
|
|
might have. For example: |
1807 |
|
|
|
1808 |
|
|
=end original |
1809 |
|
|
|
1810 |
|
|
あるハッシュを他のものとスマートマッチングすると、両方に同じキーが |
1811 |
|
|
含まれているかどうかを報告し、それ以上でもそれ以下でもありません。 |
1812 |
|
|
これは、値を気にせずに、二つのレコードが同じフィールド名を持っているか |
1813 |
|
|
どうかを見るのに使えるかもしれません。 |
1814 |
|
|
例えば: |
1815 |
|
|
|
1816 |
|
|
use v5.10.1; |
1817 |
|
|
sub make_dogtag { |
1818 |
|
|
state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 }; |
1819 |
|
|
|
1820 |
|
|
my ($class, $init_fields) = @_; |
1821 |
|
|
|
1822 |
|
|
die "Must supply (only) name, rank, and serial number" |
1823 |
|
|
unless $init_fields ~~ $REQUIRED_FIELDS; |
1824 |
|
|
|
1825 |
|
|
... |
1826 |
|
|
} |
1827 |
|
|
|
1828 |
|
|
=begin original |
1829 |
|
|
|
1830 |
|
|
or, if other non-required fields are allowed, use ARRAY ~~ HASH: |
1831 |
|
|
|
1832 |
|
|
=end original |
1833 |
|
|
|
1834 |
|
|
あるいは、もう片方に非必須のフィールドを認めるなら、ARRAY ~~ HASH を使います: |
1835 |
|
|
|
1836 |
|
|
use v5.10.1; |
1837 |
|
|
sub make_dogtag { |
1838 |
|
|
state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 }; |
1839 |
|
|
|
1840 |
|
|
my ($class, $init_fields) = @_; |
1841 |
|
|
|
1842 |
|
|
die "Must supply (at least) name, rank, and serial number" |
1843 |
|
|
unless [keys %{$init_fields}] ~~ $REQUIRED_FIELDS; |
1844 |
|
|
|
1845 |
|
|
... |
1846 |
|
|
} |
1847 |
|
|
|
1848 |
|
|
=begin original |
1849 |
|
|
|
1850 |
|
|
The smartmatch operator is most often used as the implicit operator of a |
1851 |
|
|
C<when> clause. See the section on "Switch Statements" in L<perlsyn>. |
1852 |
|
|
|
1853 |
|
|
=end original |
1854 |
|
|
|
1855 |
|
|
スマートマッチング演算子は C<when> 節の暗黙の演算子としてもっともよく |
1856 |
|
|
使われます。 |
1857 |
|
|
L<perlsyn> の "Switch Statements" の節を参照してください。 |
1858 |
|
|
|
1859 |
|
|
=head3 Smartmatching of Objects |
1860 |
|
|
|
1861 |
|
|
(オブジェクトのスマートマッチング) |
1862 |
|
|
|
1863 |
|
|
=begin original |
1864 |
|
|
|
1865 |
|
|
To avoid relying on an object's underlying representation, if the |
1866 |
|
|
smartmatch's right operand is an object that doesn't overload C<~~>, |
1867 |
|
|
it raises the exception "C<Smartmatching a non-overloaded object |
1868 |
|
|
breaks encapsulation>". That's because one has no business digging |
1869 |
|
|
around to see whether something is "in" an object. These are all |
1870 |
|
|
illegal on objects without a C<~~> overload: |
1871 |
|
|
|
1872 |
|
|
=end original |
1873 |
|
|
|
1874 |
|
|
オブジェクトの基となる表現に依存することを避けるために、スマートマッチングの |
1875 |
|
|
右オペランドが C<~~> をオーバーロードしないオブジェクトなら、例外 |
1876 |
|
|
"C<Smartmatching a non-overloaded object breaks encapsulation>" が発生します。 |
1877 |
|
|
これは、何かがオブジェクトに「含まれている」かどうかを知るために |
1878 |
|
|
調べる筋合いではないからです。 |
1879 |
|
|
次のものは C<~~> のオーバーロードがなければ全て不正です: |
1880 |
|
|
|
1881 |
|
|
%hash ~~ $object |
1882 |
|
|
42 ~~ $object |
1883 |
|
|
"fred" ~~ $object |
1884 |
|
|
|
1885 |
|
|
=begin original |
1886 |
|
|
|
1887 |
|
|
However, you can change the way an object is smartmatched by overloading |
1888 |
|
|
the C<~~> operator. This is allowed to |
1889 |
|
|
extend the usual smartmatch semantics. |
1890 |
|
|
For objects that do have an C<~~> overload, see L<overload>. |
1891 |
|
|
|
1892 |
|
|
=end original |
1893 |
|
|
|
1894 |
|
|
しかし、C<~~> 演算子をオーバーロードすることにオブジェクトが |
1895 |
|
|
スマートマッチングする方法を変更できます。 |
1896 |
|
|
これにより通常のスマートマッチングの意味論を拡張できます。 |
1897 |
|
|
C<~~> のオーバーロードを持つオブジェクトについては、L<overload> を |
1898 |
|
|
参照してください。 |
1899 |
|
|
|
1900 |
|
|
=begin original |
1901 |
|
|
|
1902 |
|
|
Using an object as the left operand is allowed, although not very useful. |
1903 |
|
|
Smartmatching rules take precedence over overloading, so even if the |
1904 |
|
|
object in the left operand has smartmatch overloading, this will be |
1905 |
|
|
ignored. A left operand that is a non-overloaded object falls back on a |
1906 |
|
|
string or numeric comparison of whatever the C<ref> operator returns. That |
1907 |
|
|
means that |
1908 |
|
|
|
1909 |
|
|
=end original |
1910 |
|
|
|
1911 |
|
|
左オペランドにオブジェクトを使うことは許されていますが、あまり |
1912 |
|
|
有用ではありません。 |
1913 |
|
|
スマートマッチングの規則はオーバーロードより優先順位が高いので、例え |
1914 |
|
|
左オペランドのオブジェクトがスマートマッチングのオーバーロードを |
1915 |
|
|
持っていても、無視されます。 |
1916 |
|
|
左オペランドがオーバーロードされていないオブジェクトなら、C<ref> 演算子が |
1917 |
|
|
返したものに従って、文字または数値比較にフォールバックします。 |
1918 |
|
|
これは、 |
1919 |
|
|
|
1920 |
|
|
$object ~~ X |
1921 |
|
|
|
1922 |
|
|
=begin original |
1923 |
|
|
|
1924 |
|
|
does I<not> invoke the overload method with C<I<X>> as an argument. |
1925 |
|
|
Instead the above table is consulted as normal, and based on the type of |
1926 |
|
|
C<I<X>>, overloading may or may not be invoked. For simple strings or |
1927 |
|
|
numbers, "in" becomes equivalent to this: |
1928 |
|
|
|
1929 |
|
|
=end original |
1930 |
|
|
|
1931 |
|
|
は C<I<X>> を引数としてオーバーロードメソッドを起動 I<しない> ということです。 |
1932 |
|
|
通常通り前述の表を見て C<I<X>> の型に依存するのではなく、オーバーロードは |
1933 |
|
|
起動されるかもしれませんし、されないかもしれません。 |
1934 |
|
|
単純な文字列や数値については、"in" は以下と等価になります: |
1935 |
|
|
|
1936 |
|
|
$object ~~ $number ref($object) == $number |
1937 |
|
|
$object ~~ $string ref($object) eq $string |
1938 |
|
|
|
1939 |
|
|
=begin original |
1940 |
|
|
|
1941 |
|
|
For example, this reports that the handle smells IOish |
1942 |
|
|
(but please don't really do this!): |
1943 |
|
|
|
1944 |
|
|
=end original |
1945 |
|
|
|
1946 |
|
|
例えば、これは "handle smells IOish" と報告します (しかし実際にこれを |
1947 |
|
|
しないでください!): |
1948 |
|
|
|
1949 |
|
|
use IO::Handle; |
1950 |
|
|
my $fh = IO::Handle->new(); |
1951 |
|
|
if ($fh ~~ /\bIO\b/) { |
1952 |
|
|
say "handle smells IOish"; |
1953 |
|
|
} |
1954 |
|
|
|
1955 |
|
|
=begin original |
1956 |
|
|
|
1957 |
|
|
That's because it treats C<$fh> as a string like |
1958 |
|
|
C<"IO::Handle=GLOB(0x8039e0)">, then pattern matches against that. |
1959 |
|
|
|
1960 |
|
|
=end original |
1961 |
|
|
|
1962 |
|
|
これは、C<$fh> を C<"IO::Handle=GLOB(0x8039e0)"> のような文字列として扱い、 |
1963 |
|
|
それからパターンはこれに対してマッチングするからです。 |
1964 |
|
|
|
1965 |
|
|
=head2 Bitwise And |
1966 |
|
|
X<operator, bitwise, and> X<bitwise and> X<&> |
1967 |
|
|
|
1968 |
|
|
(ビットごとの AND) |
1969 |
|
|
|
1970 |
|
|
=begin original |
1971 |
|
|
|
1972 |
|
|
Binary C<"&"> returns its operands ANDed together bit by bit. Although no |
1973 |
|
|
warning is currently raised, the result is not well defined when this operation |
1974 |
|
|
is performed on operands that aren't either numbers (see |
1975 |
|
|
L<Integer Arithmetic>) nor bitstrings (see L<Bitwise String Operators>). |
1976 |
|
|
|
1977 |
|
|
=end original |
1978 |
|
|
|
1979 |
|
|
二項演算子の C<"&"> は、両オペランドのビットごとに論理積をとって、 |
1980 |
|
|
その結果を返します。 |
1981 |
|
|
数値 (L<Integer Arithmetic>) 参照) でもビット文字列 |
1982 |
|
|
(L<Bitwise String Operators> 参照) でもないオペランドに対してこの演算を |
1983 |
|
|
実行した場合、現在のところ警告は出ませんが、結果は未定義です。 |
1984 |
|
|
|
1985 |
|
|
=begin original |
1986 |
|
|
|
1987 |
|
|
Note that C<"&"> has lower priority than relational operators, so for example |
1988 |
|
|
the parentheses are essential in a test like |
1989 |
|
|
|
1990 |
|
|
=end original |
1991 |
|
|
|
1992 |
|
|
C<"&"> は関係演算子より優先順位が低いので、例えば以下のようなテストでは、 |
1993 |
|
|
かっこが重要です: |
1994 |
|
|
|
1995 |
|
|
print "Even\n" if ($x & 1) == 0; |
1996 |
|
|
|
1997 |
|
|
=begin original |
1998 |
|
|
|
1999 |
|
|
If the experimental "bitwise" feature is enabled via S<C<use feature |
2000 |
|
|
'bitwise'>>, then this operator always treats its operand as numbers. This |
2001 |
|
|
feature produces a warning unless you also use C<S<no warnings |
2002 |
|
|
'experimental::bitwise'>>. |
2003 |
|
|
|
2004 |
|
|
=end original |
2005 |
|
|
|
2006 |
argrath |
1.2 |
S<C<use feature 'bitwise'>> によって実験的な "bitwise" 機能が |
2007 |
|
|
有効になっている場合、この演算子はオペランドを常に数値として扱います。 |
2008 |
|
|
この機能は、C<S<no warnings 'experimental::bitwise'>> と共に使っていない限り |
2009 |
|
|
警告が出力されます。 |
2010 |
argrath |
1.1 |
|
2011 |
|
|
=head2 Bitwise Or and Exclusive Or |
2012 |
|
|
X<operator, bitwise, or> X<bitwise or> X<|> X<operator, bitwise, xor> |
2013 |
|
|
X<bitwise xor> X<^> |
2014 |
|
|
|
2015 |
|
|
(ビットごとの OR と XOR) |
2016 |
|
|
|
2017 |
|
|
=begin original |
2018 |
|
|
|
2019 |
|
|
Binary C<"|"> returns its operands ORed together bit by bit. |
2020 |
|
|
|
2021 |
|
|
=end original |
2022 |
|
|
|
2023 |
|
|
二項演算子の C<"|"> は、両オペランドのビットごとに論理和をとって、 |
2024 |
|
|
その結果を返します。 |
2025 |
|
|
|
2026 |
|
|
=begin original |
2027 |
|
|
|
2028 |
|
|
Binary C<"^"> returns its operands XORed together bit by bit. |
2029 |
|
|
|
2030 |
|
|
=end original |
2031 |
|
|
|
2032 |
|
|
二項演算子の C<"^"> は、両オペランドのビットごとに排他論理和をとって、 |
2033 |
|
|
その結果を返します。 |
2034 |
|
|
|
2035 |
|
|
=begin original |
2036 |
|
|
|
2037 |
|
|
Although no warning is currently raised, the results are not well |
2038 |
|
|
defined when these operations are performed on operands that aren't either |
2039 |
|
|
numbers (see L<Integer Arithmetic>) nor bitstrings (see L<Bitwise String |
2040 |
|
|
Operators>). |
2041 |
|
|
|
2042 |
|
|
=end original |
2043 |
|
|
|
2044 |
|
|
数値 (L<Integer Arithmetic>) 参照) でもビット文字列 |
2045 |
|
|
(L<Bitwise String Operators> 参照) でもないオペランドに対してこれらの演算を |
2046 |
|
|
実行した場合、現在のところ警告は出ませんが、結果は未定義です。 |
2047 |
|
|
|
2048 |
|
|
=begin original |
2049 |
|
|
|
2050 |
|
|
Note that C<"|"> and C<"^"> have lower priority than relational operators, so |
2051 |
|
|
for example the parentheses are essential in a test like |
2052 |
|
|
|
2053 |
|
|
=end original |
2054 |
|
|
|
2055 |
|
|
C<"|"> と C<"^"> は関係演算子より優先順位が低いので、例えば以下のような |
2056 |
|
|
テストでは、かっこが重要です: |
2057 |
|
|
|
2058 |
|
|
print "false\n" if (8 | 2) != 10; |
2059 |
|
|
|
2060 |
|
|
=begin original |
2061 |
|
|
|
2062 |
|
|
If the experimental "bitwise" feature is enabled via S<C<use feature |
2063 |
|
|
'bitwise'>>, then this operator always treats its operand as numbers. This |
2064 |
|
|
feature produces a warning unless you also use S<C<no warnings |
2065 |
|
|
'experimental::bitwise'>>. |
2066 |
|
|
|
2067 |
|
|
=end original |
2068 |
|
|
|
2069 |
argrath |
1.2 |
S<C<use feature 'bitwise'>> によって実験的な "bitwise" 機能が |
2070 |
|
|
有効になっている場合、この演算子はオペランドを常に数値として扱います。 |
2071 |
|
|
この機能は、S<C<no warnings 'experimental::bitwise'>> と共に使っていない限り |
2072 |
|
|
警告が出力されます。 |
2073 |
argrath |
1.1 |
|
2074 |
|
|
=head2 C-style Logical And |
2075 |
|
|
X<&&> X<logical and> X<operator, logical, and> |
2076 |
|
|
|
2077 |
|
|
(C スタイルの論理積) |
2078 |
|
|
|
2079 |
|
|
=begin original |
2080 |
|
|
|
2081 |
|
|
Binary C<"&&"> performs a short-circuit logical AND operation. That is, |
2082 |
|
|
if the left operand is false, the right operand is not even evaluated. |
2083 |
|
|
Scalar or list context propagates down to the right operand if it |
2084 |
|
|
is evaluated. |
2085 |
|
|
|
2086 |
|
|
=end original |
2087 |
|
|
|
2088 |
|
|
二項演算子の C<"&&"> は、短絡の論理積演算を行ないます。 |
2089 |
|
|
つまり、左被演算子が偽であれば、右被演算子は評価さえ |
2090 |
|
|
行なわれないということです。 |
2091 |
|
|
評価される場合には、スカラーかリストかというコンテキストは、 |
2092 |
|
|
右被演算子にも及びます。 |
2093 |
|
|
|
2094 |
|
|
=head2 C-style Logical Or |
2095 |
|
|
X<||> X<operator, logical, or> |
2096 |
|
|
|
2097 |
|
|
(C スタイルの論理和) |
2098 |
|
|
|
2099 |
|
|
=begin original |
2100 |
|
|
|
2101 |
|
|
Binary C<"||"> performs a short-circuit logical OR operation. That is, |
2102 |
|
|
if the left operand is true, the right operand is not even evaluated. |
2103 |
|
|
Scalar or list context propagates down to the right operand if it |
2104 |
|
|
is evaluated. |
2105 |
|
|
|
2106 |
|
|
=end original |
2107 |
|
|
|
2108 |
|
|
二項演算子の C<"||"> は、短絡の論理和演算を行ないます。 |
2109 |
|
|
つまり、左被演算子が真であれば、右被演算子は評価さえ |
2110 |
|
|
行なわれないということです。 |
2111 |
|
|
評価される場合には、スカラーかリストかというコンテキストは、 |
2112 |
|
|
右被演算子にも及びます。 |
2113 |
|
|
|
2114 |
|
|
=head2 Logical Defined-Or |
2115 |
|
|
X<//> X<operator, logical, defined-or> |
2116 |
|
|
|
2117 |
|
|
(論理定義性和) |
2118 |
|
|
|
2119 |
|
|
=begin original |
2120 |
|
|
|
2121 |
|
|
Although it has no direct equivalent in C, Perl's C<//> operator is related |
2122 |
|
|
to its C-style "or". In fact, it's exactly the same as C<||>, except that it |
2123 |
|
|
tests the left hand side's definedness instead of its truth. Thus, |
2124 |
|
|
S<C<< EXPR1 // EXPR2 >>> returns the value of C<< EXPR1 >> if it's defined, |
2125 |
|
|
otherwise, the value of C<< EXPR2 >> is returned. |
2126 |
|
|
(C<< EXPR1 >> is evaluated in scalar context, C<< EXPR2 >> |
2127 |
|
|
in the context of C<< // >> itself). Usually, |
2128 |
|
|
this is the same result as S<C<< defined(EXPR1) ? EXPR1 : EXPR2 >>> (except that |
2129 |
|
|
the ternary-operator form can be used as a lvalue, while S<C<< EXPR1 // EXPR2 >>> |
2130 |
|
|
cannot). This is very useful for |
2131 |
|
|
providing default values for variables. If you actually want to test if |
2132 |
|
|
at least one of C<$x> and C<$y> is defined, use S<C<defined($x // $y)>>. |
2133 |
|
|
|
2134 |
|
|
=end original |
2135 |
|
|
|
2136 |
|
|
C では直接等価なものはありませんが、Perl の C<//> 演算子は C スタイル |
2137 |
|
|
論理和に関連しています。 |
2138 |
|
|
実際、左辺の真偽ではなく定義されているかを判定することを除けば |
2139 |
|
|
C<||> と同じです。 |
2140 |
|
|
従って S<C<< EXPR1 // EXPR2 >>> は、C<< EXPR1 >> が定義されていればその値を |
2141 |
|
|
返し、さもなければ、C<< EXPR2 >> の値を返します。 |
2142 |
|
|
(C<< EXPR1 >> はスカラコンテキストで、C<< EXPR2 >> は C<< // >> 自身の |
2143 |
|
|
コンテキストで評価されます。) |
2144 |
|
|
普通はこれは S<C<< defined(EXPR1) ? EXPR1 : EXPR2 >>> と同じ結果になり |
2145 |
|
|
完全に等価です (例外は 3 項演算子形式は左辺値として使えますが、 |
2146 |
|
|
S<C<< EXPR1 // EXPR2 >>> は使えません)。 |
2147 |
|
|
これは、変数に対するデフォルト値を設定するのにとても有用です。 |
2148 |
|
|
実際に、C<$x> と C<$y> の少なくとも片方が定義されているかを判定したいなら、 |
2149 |
|
|
S<C<defined($x // $y)>> を使ってください。 |
2150 |
|
|
|
2151 |
|
|
=begin original |
2152 |
|
|
|
2153 |
|
|
The C<||>, C<//> and C<&&> operators return the last value evaluated |
2154 |
|
|
(unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably |
2155 |
|
|
portable way to find out the home directory might be: |
2156 |
|
|
|
2157 |
|
|
=end original |
2158 |
|
|
|
2159 |
|
|
C<||>, C<//>, C<&&> 演算子は、(C のように 単に 0 や 1 を返すのではなく) |
2160 |
|
|
最後に評価された値を返します。 |
2161 |
|
|
これにより、かなり一般的に使えるホームディレクトリを探す方法は: |
2162 |
|
|
|
2163 |
|
|
$home = $ENV{HOME} |
2164 |
|
|
// $ENV{LOGDIR} |
2165 |
|
|
// (getpwuid($<))[7] |
2166 |
|
|
// die "You're homeless!\n"; |
2167 |
|
|
|
2168 |
|
|
=begin original |
2169 |
|
|
|
2170 |
|
|
In particular, this means that you shouldn't use this |
2171 |
|
|
for selecting between two aggregates for assignment: |
2172 |
|
|
|
2173 |
|
|
=end original |
2174 |
|
|
|
2175 |
|
|
特に、これは代入のために二つの集合を選択するためには |
2176 |
|
|
使うべきではないことを意味します。 |
2177 |
|
|
|
2178 |
|
|
=begin original |
2179 |
|
|
|
2180 |
|
|
@a = @b || @c; # this is wrong |
2181 |
|
|
@a = scalar(@b) || @c; # really meant this |
2182 |
|
|
@a = @b ? @b : @c; # this works fine, though |
2183 |
|
|
|
2184 |
|
|
=end original |
2185 |
|
|
|
2186 |
|
|
@a = @b || @c; # これは間違い |
2187 |
|
|
@a = scalar(@b) || @c; # 本当にしたいこと |
2188 |
|
|
@a = @b ? @b : @c; # しかしこれも動作する |
2189 |
|
|
|
2190 |
|
|
=begin original |
2191 |
|
|
|
2192 |
|
|
As alternatives to C<&&> and C<||> when used for |
2193 |
|
|
control flow, Perl provides the C<and> and C<or> operators (see below). |
2194 |
|
|
The short-circuit behavior is identical. The precedence of C<"and"> |
2195 |
|
|
and C<"or"> is much lower, however, so that you can safely use them after a |
2196 |
|
|
list operator without the need for parentheses: |
2197 |
|
|
|
2198 |
|
|
=end original |
2199 |
|
|
|
2200 |
|
|
Perl では、フロー制御に使う場合の C<&&> と C<||> の同義語として、 |
2201 |
|
|
C<and> 演算子と C<or> 演算子が用意されています (下記参照)。 |
2202 |
|
|
短絡の動作は全く同じです。 |
2203 |
|
|
しかし、C<"and"> と C<"or"> の優先順位はかなり低くしてあるので、引数に括弧を |
2204 |
|
|
使っていないリスト演算子のあとに続けて使う場合にも、 |
2205 |
|
|
安心して使うことができます: |
2206 |
|
|
|
2207 |
|
|
unlink "alpha", "beta", "gamma" |
2208 |
|
|
or gripe(), next LINE; |
2209 |
|
|
|
2210 |
|
|
=begin original |
2211 |
|
|
|
2212 |
|
|
With the C-style operators that would have been written like this: |
2213 |
|
|
|
2214 |
|
|
=end original |
2215 |
|
|
|
2216 |
|
|
C スタイルの演算子では以下のように書く必要があります。 |
2217 |
|
|
|
2218 |
|
|
unlink("alpha", "beta", "gamma") |
2219 |
|
|
|| (gripe(), next LINE); |
2220 |
|
|
|
2221 |
|
|
=begin original |
2222 |
|
|
|
2223 |
|
|
It would be even more readable to write that this way: |
2224 |
|
|
|
2225 |
|
|
=end original |
2226 |
|
|
|
2227 |
|
|
次のようにして書き込みをより読みやすくすることもできます: |
2228 |
|
|
|
2229 |
|
|
unless(unlink("alpha", "beta", "gamma")) { |
2230 |
|
|
gripe(); |
2231 |
|
|
next LINE; |
2232 |
|
|
} |
2233 |
|
|
|
2234 |
|
|
=begin original |
2235 |
|
|
|
2236 |
|
|
Using C<"or"> for assignment is unlikely to do what you want; see below. |
2237 |
|
|
|
2238 |
|
|
=end original |
2239 |
|
|
|
2240 |
|
|
代入で C<"or"> を使うと、したいことと違うことになります; 以下を |
2241 |
|
|
参照して下さい。 |
2242 |
|
|
|
2243 |
|
|
=head2 Range Operators |
2244 |
|
|
X<operator, range> X<range> X<..> X<...> |
2245 |
|
|
|
2246 |
|
|
(範囲演算子) |
2247 |
|
|
|
2248 |
|
|
=begin original |
2249 |
|
|
|
2250 |
|
|
Binary C<".."> is the range operator, which is really two different |
2251 |
|
|
operators depending on the context. In list context, it returns a |
2252 |
|
|
list of values counting (up by ones) from the left value to the right |
2253 |
|
|
value. If the left value is greater than the right value then it |
2254 |
|
|
returns the empty list. The range operator is useful for writing |
2255 |
|
|
S<C<foreach (1..10)>> loops and for doing slice operations on arrays. In |
2256 |
|
|
the current implementation, no temporary array is created when the |
2257 |
|
|
range operator is used as the expression in C<foreach> loops, but older |
2258 |
|
|
versions of Perl might burn a lot of memory when you write something |
2259 |
|
|
like this: |
2260 |
|
|
|
2261 |
|
|
=end original |
2262 |
|
|
|
2263 |
|
|
二項演算子の C<".."> は範囲演算子で、使われるコンテキストによって |
2264 |
|
|
異なる動作をする 2 つの演算子を合わせたものです。 |
2265 |
|
|
リストコンテキストでは、左の値から右の値まで (1 づつ昇順で) 数えあげた値から |
2266 |
|
|
なるリストを返します。 |
2267 |
|
|
左側の値が右側の値より大きい場合は、空リストを返します。 |
2268 |
|
|
範囲演算子は、S<C<foreach (1..10)>> のようなループを書くときや、 |
2269 |
|
|
配列のスライス演算を行なうときに便利です。 |
2270 |
|
|
現状の実装では、C<foreach> ループの式の中で範囲演算子を使っても |
2271 |
|
|
一時配列は作りませんが、古い Perl は以下のようなことを書くと、 |
2272 |
|
|
大量のメモリを消費することになります: |
2273 |
|
|
|
2274 |
|
|
for (1 .. 1_000_000) { |
2275 |
|
|
# code |
2276 |
|
|
} |
2277 |
|
|
|
2278 |
|
|
=begin original |
2279 |
|
|
|
2280 |
|
|
The range operator also works on strings, using the magical |
2281 |
|
|
auto-increment, see below. |
2282 |
|
|
|
2283 |
|
|
=end original |
2284 |
|
|
|
2285 |
|
|
範囲演算子は、マジカル自動インクリメントを使うことで文字列でも動作します; |
2286 |
|
|
以下を参照してください。 |
2287 |
|
|
|
2288 |
|
|
=begin original |
2289 |
|
|
|
2290 |
|
|
In scalar context, C<".."> returns a boolean value. The operator is |
2291 |
|
|
bistable, like a flip-flop, and emulates the line-range (comma) |
2292 |
|
|
operator of B<sed>, B<awk>, and various editors. Each C<".."> operator |
2293 |
|
|
maintains its own boolean state, even across calls to a subroutine |
2294 |
|
|
that contains it. It is false as long as its left operand is false. |
2295 |
|
|
Once the left operand is true, the range operator stays true until the |
2296 |
|
|
right operand is true, I<AFTER> which the range operator becomes false |
2297 |
|
|
again. It doesn't become false till the next time the range operator |
2298 |
|
|
is evaluated. It can test the right operand and become false on the |
2299 |
|
|
same evaluation it became true (as in B<awk>), but it still returns |
2300 |
|
|
true once. If you don't want it to test the right operand until the |
2301 |
|
|
next evaluation, as in B<sed>, just use three dots (C<"...">) instead of |
2302 |
|
|
two. In all other regards, C<"..."> behaves just like C<".."> does. |
2303 |
|
|
|
2304 |
|
|
=end original |
2305 |
|
|
|
2306 |
|
|
スカラコンテキストで使われたときには、C<".."> は真偽値を返します。 |
2307 |
|
|
この演算子は、フリップフロップのように 2 値安定で、 |
2308 |
|
|
B<sed> や B<awk> や多くのエディタでの行範囲 (コンマ) 演算子を |
2309 |
|
|
エミュレートするものとなります。 |
2310 |
|
|
各々の C<".."> 演算子は、例えそれを含むサブルーチンの呼び出しを |
2311 |
|
|
またいでも、それぞれに独立して自分の真偽状態を管理します。 |
2312 |
|
|
はじめは、左被演算子が偽である間、演算全体も偽となっています。 |
2313 |
|
|
いったん左被演算子が真になると、範囲演算子は、右被演算子が真である間、 |
2314 |
|
|
真を返すようになります; 範囲演算子が再び偽になった I<後> です。 |
2315 |
|
|
次に範囲演算子が評価されるまでは、偽とはなりません。 |
2316 |
|
|
(B<awk> でのように) 真となった、その評価の中で右被演算子をテストし、 |
2317 |
|
|
偽とすることができますが、1 度は真を返すことになります。 |
2318 |
|
|
B<sed> でのように、次に評価されるまで右被演算子をテストしたくなければ、 |
2319 |
|
|
2 個のドットの代わりに 3 つのドット (C<"...">) を使ってください。 |
2320 |
|
|
その他の点では、C<"..."> は C<".."> と同様に振舞います. |
2321 |
|
|
|
2322 |
|
|
=begin original |
2323 |
|
|
|
2324 |
|
|
The right operand is not evaluated while the operator is in the |
2325 |
|
|
"false" state, and the left operand is not evaluated while the |
2326 |
|
|
operator is in the "true" state. The precedence is a little lower |
2327 |
|
|
than || and &&. The value returned is either the empty string for |
2328 |
|
|
false, or a sequence number (beginning with 1) for true. The sequence |
2329 |
|
|
number is reset for each range encountered. The final sequence number |
2330 |
|
|
in a range has the string C<"E0"> appended to it, which doesn't affect |
2331 |
|
|
its numeric value, but gives you something to search for if you want |
2332 |
|
|
to exclude the endpoint. You can exclude the beginning point by |
2333 |
|
|
waiting for the sequence number to be greater than 1. |
2334 |
|
|
|
2335 |
|
|
=end original |
2336 |
|
|
|
2337 |
|
|
右被演算子は、演算子の状態が「偽」である間は評価されることがなく、 |
2338 |
|
|
左被演算子は、演算子の状態が「真」である間は評価されることがありません。 |
2339 |
|
|
優先順位は、|| と && の少し下です。 |
2340 |
|
|
偽としては空文字列が返され、 |
2341 |
|
|
真としては (1 から始まる) 通し番号が返されます。 |
2342 |
|
|
この通し番号は、新たに範囲が始まるごとにリセットされます。 |
2343 |
|
|
範囲の最後の通し番号には、文字列 C<"E0"> が末尾につけられます; これは、 |
2344 |
|
|
数値としては何の影響もありませんが、範囲の終わりで何か特別なことをしたい |
2345 |
|
|
場合に、目印として使うことができます。 |
2346 |
|
|
範囲の始まりを除きたい場合には、通し番号が 1 よりも大きくなるのを |
2347 |
|
|
待てばよいでしょう。 |
2348 |
|
|
|
2349 |
|
|
=begin original |
2350 |
|
|
|
2351 |
|
|
If either operand of scalar C<".."> is a constant expression, |
2352 |
|
|
that operand is considered true if it is equal (C<==>) to the current |
2353 |
|
|
input line number (the C<$.> variable). |
2354 |
|
|
|
2355 |
|
|
=end original |
2356 |
|
|
|
2357 |
|
|
スカラの C<".."> の被演算子が定数表現であるときは、その被演算子は暗黙に、 |
2358 |
|
|
現在の入力行番号(変数 C<$.>) と等しい(C<==>) 場合に真となります。 |
2359 |
|
|
|
2360 |
|
|
=begin original |
2361 |
|
|
|
2362 |
|
|
To be pedantic, the comparison is actually S<C<int(EXPR) == int(EXPR)>>, |
2363 |
|
|
but that is only an issue if you use a floating point expression; when |
2364 |
|
|
implicitly using C<$.> as described in the previous paragraph, the |
2365 |
|
|
comparison is S<C<int(EXPR) == int($.)>> which is only an issue when C<$.> |
2366 |
|
|
is set to a floating point value and you are not reading from a file. |
2367 |
|
|
Furthermore, S<C<"span" .. "spat">> or S<C<2.18 .. 3.14>> will not do what |
2368 |
|
|
you want in scalar context because each of the operands are evaluated |
2369 |
|
|
using their integer representation. |
2370 |
|
|
|
2371 |
|
|
=end original |
2372 |
|
|
|
2373 |
|
|
とても細かい話をすると、比較は実際には S<C<int(EXPR) == int(EXPR)>> ですが、 |
2374 |
|
|
これは浮動小数点数を使うときにだけ問題になります; 前の段落で記述したように |
2375 |
|
|
明示的に C<$.> を使ったとき、比較は S<C<int(EXPR) == int($.)>> となり、 |
2376 |
|
|
C<$.> に浮動小数点数がセットされ、ファイルから読み込みを行わない場合にのみ |
2377 |
|
|
問題になります。 |
2378 |
|
|
さらに、S<C<"span" .. "spat">> や S<C<2.18 .. 3.14>> は、それぞれの |
2379 |
|
|
オペランドが整数表現を使って評価されるため、スカラコンテキストでは |
2380 |
|
|
望みどおりの結果になりません。 |
2381 |
|
|
|
2382 |
|
|
=begin original |
2383 |
|
|
|
2384 |
|
|
Examples: |
2385 |
|
|
|
2386 |
|
|
=end original |
2387 |
|
|
|
2388 |
|
|
例: |
2389 |
|
|
|
2390 |
|
|
=begin original |
2391 |
|
|
|
2392 |
|
|
As a scalar operator: |
2393 |
|
|
|
2394 |
|
|
=end original |
2395 |
|
|
|
2396 |
|
|
スカラ演算子として: |
2397 |
|
|
|
2398 |
|
|
if (101 .. 200) { print; } # print 2nd hundred lines, short for |
2399 |
|
|
# if ($. == 101 .. $. == 200) { print; } |
2400 |
|
|
|
2401 |
|
|
next LINE if (1 .. /^$/); # skip header lines, short for |
2402 |
|
|
# next LINE if ($. == 1 .. /^$/); |
2403 |
|
|
# (typically in a loop labeled LINE) |
2404 |
|
|
|
2405 |
|
|
s/^/> / if (/^$/ .. eof()); # quote body |
2406 |
|
|
|
2407 |
|
|
# parse mail messages |
2408 |
|
|
while (<>) { |
2409 |
|
|
$in_header = 1 .. /^$/; |
2410 |
|
|
$in_body = /^$/ .. eof; |
2411 |
|
|
if ($in_header) { |
2412 |
|
|
# do something |
2413 |
|
|
} else { # in body |
2414 |
|
|
# do something else |
2415 |
|
|
} |
2416 |
|
|
} continue { |
2417 |
|
|
close ARGV if eof; # reset $. each file |
2418 |
|
|
} |
2419 |
|
|
|
2420 |
|
|
=begin original |
2421 |
|
|
|
2422 |
|
|
Here's a simple example to illustrate the difference between |
2423 |
|
|
the two range operators: |
2424 |
|
|
|
2425 |
|
|
=end original |
2426 |
|
|
|
2427 |
|
|
以下は二つの範囲演算子の違いを示す単純な例です: |
2428 |
|
|
|
2429 |
|
|
@lines = (" - Foo", |
2430 |
|
|
"01 - Bar", |
2431 |
|
|
"1 - Baz", |
2432 |
|
|
" - Quux"); |
2433 |
|
|
|
2434 |
|
|
foreach (@lines) { |
2435 |
|
|
if (/0/ .. /1/) { |
2436 |
|
|
print "$_\n"; |
2437 |
|
|
} |
2438 |
|
|
} |
2439 |
|
|
|
2440 |
|
|
=begin original |
2441 |
|
|
|
2442 |
|
|
This program will print only the line containing "Bar". If |
2443 |
|
|
the range operator is changed to C<...>, it will also print the |
2444 |
|
|
"Baz" line. |
2445 |
|
|
|
2446 |
|
|
=end original |
2447 |
|
|
|
2448 |
|
|
このプログラムは "Bar" を含む行だけを表示します。 |
2449 |
|
|
範囲演算子を C<...> に変更すると、"Baz" の行も表示します。 |
2450 |
|
|
|
2451 |
|
|
=begin original |
2452 |
|
|
|
2453 |
|
|
And now some examples as a list operator: |
2454 |
|
|
|
2455 |
|
|
=end original |
2456 |
|
|
|
2457 |
|
|
これはリスト演算子の例です: |
2458 |
|
|
|
2459 |
|
|
=begin original |
2460 |
|
|
|
2461 |
|
|
for (101 .. 200) { print } # print $_ 100 times |
2462 |
|
|
@foo = @foo[0 .. $#foo]; # an expensive no-op |
2463 |
|
|
@foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items |
2464 |
|
|
|
2465 |
|
|
=end original |
2466 |
|
|
|
2467 |
|
|
for (101 .. 200) { print } # $_ を 100 回出力 |
2468 |
|
|
@foo = @foo[0 .. $#foo]; # 高価な no-op |
2469 |
|
|
@foo = @foo[$#foo-4 .. $#foo]; # 末尾の 5 アイテムをスライス |
2470 |
|
|
|
2471 |
|
|
=begin original |
2472 |
|
|
|
2473 |
|
|
The range operator (in list context) makes use of the magical |
2474 |
|
|
auto-increment algorithm if the operands are strings. You |
2475 |
|
|
can say |
2476 |
|
|
|
2477 |
|
|
=end original |
2478 |
|
|
|
2479 |
|
|
(リストコンテキストでの) 範囲演算子は、被演算子が文字列であるときには、 |
2480 |
|
|
マジカルインクリメントの機能を使います。 |
2481 |
|
|
以下のように書くと: |
2482 |
|
|
|
2483 |
|
|
@alphabet = ("A" .. "Z"); |
2484 |
|
|
|
2485 |
|
|
=begin original |
2486 |
|
|
|
2487 |
|
|
to get all normal letters of the English alphabet, or |
2488 |
|
|
|
2489 |
|
|
=end original |
2490 |
|
|
|
2491 |
|
|
英語の大文字すべてを得られますし: |
2492 |
|
|
|
2493 |
|
|
$hexdigit = (0 .. 9, "a" .. "f")[$num & 15]; |
2494 |
|
|
|
2495 |
|
|
=begin original |
2496 |
|
|
|
2497 |
|
|
to get a hexadecimal digit, or |
2498 |
|
|
|
2499 |
|
|
=end original |
2500 |
|
|
|
2501 |
|
|
と書けば、16 進の数字が得られますし、 |
2502 |
|
|
|
2503 |
|
|
@z2 = ("01" .. "31"); |
2504 |
|
|
print $z2[$mday]; |
2505 |
|
|
|
2506 |
|
|
=begin original |
2507 |
|
|
|
2508 |
|
|
to get dates with leading zeros. |
2509 |
|
|
|
2510 |
|
|
=end original |
2511 |
|
|
|
2512 |
|
|
とすれば、0 付きの日付が得られます。 |
2513 |
|
|
|
2514 |
|
|
=begin original |
2515 |
|
|
|
2516 |
|
|
If the final value specified is not in the sequence that the magical |
2517 |
|
|
increment would produce, the sequence goes until the next value would |
2518 |
|
|
be longer than the final value specified. |
2519 |
|
|
|
2520 |
|
|
=end original |
2521 |
|
|
|
2522 |
|
|
マジカルインクリメントによって得られる値の中に指定した最終値に |
2523 |
|
|
ちょうど一致するものが見つからないような場合には、 |
2524 |
|
|
マジカルインクリメントによって得られる次の値の文字列長が、 |
2525 |
|
|
最終値として指定した値のものより長くなるまでインクリメントが続けられます。 |
2526 |
|
|
|
2527 |
|
|
=begin original |
2528 |
|
|
|
2529 |
|
|
If the initial value specified isn't part of a magical increment |
2530 |
|
|
sequence (that is, a non-empty string matching C</^[a-zA-Z]*[0-9]*\z/>), |
2531 |
|
|
only the initial value will be returned. So the following will only |
2532 |
|
|
return an alpha: |
2533 |
|
|
|
2534 |
|
|
=end original |
2535 |
|
|
|
2536 |
|
|
指定された初期値がマジカルインクリメント処理の一部でない場合 |
2537 |
|
|
(つまり、C</^[a-zA-Z]*[0-9]*\z/> にマッチングする、空でない文字列の場合)、 |
2538 |
|
|
初期値のみが返されます。 |
2539 |
|
|
従って、以下はαのみを返します: |
2540 |
|
|
|
2541 |
|
|
use charnames "greek"; |
2542 |
|
|
my @greek_small = ("\N{alpha}" .. "\N{omega}"); |
2543 |
|
|
|
2544 |
|
|
=begin original |
2545 |
|
|
|
2546 |
|
|
To get the 25 traditional lowercase Greek letters, including both sigmas, |
2547 |
|
|
you could use this instead: |
2548 |
|
|
|
2549 |
|
|
=end original |
2550 |
|
|
|
2551 |
|
|
両方のシグマを含む、25 文字の伝統的な小文字のギリシャ文字を得るためには、 |
2552 |
|
|
代わりに以下のようにしてください: |
2553 |
|
|
|
2554 |
|
|
use charnames "greek"; |
2555 |
|
|
my @greek_small = map { chr } ( ord("\N{alpha}") |
2556 |
|
|
.. |
2557 |
|
|
ord("\N{omega}") |
2558 |
|
|
); |
2559 |
|
|
|
2560 |
|
|
=begin original |
2561 |
|
|
|
2562 |
|
|
However, because there are I<many> other lowercase Greek characters than |
2563 |
|
|
just those, to match lowercase Greek characters in a regular expression, |
2564 |
|
|
you could use the pattern C</(?:(?=\p{Greek})\p{Lower})+/> (or the |
2565 |
|
|
L<experimental feature|perlrecharclass/Extended Bracketed Character |
2566 |
|
|
Classes> C<S</(?[ \p{Greek} & \p{Lower} ])+/>>). |
2567 |
|
|
|
2568 |
|
|
=end original |
2569 |
|
|
|
2570 |
|
|
しかし、小文字のギリシャ文字はここに書いたものよりも I<たくさん> あるので、 |
2571 |
|
|
正規表現で小文字のギリシャ文字にマッチングさせるためには、 |
2572 |
|
|
C</(?:(?=\p{Greek})\p{Lower})+/> というパターン (または |
2573 |
|
|
L<実験的機能|perlrecharclass/Extended Bracketed Character Classes> である |
2574 |
|
|
C<S</(?[ \p{Greek} & \p{Lower} ])+/>>) を使います。 |
2575 |
|
|
|
2576 |
|
|
=begin original |
2577 |
|
|
|
2578 |
|
|
Because each operand is evaluated in integer form, S<C<2.18 .. 3.14>> will |
2579 |
|
|
return two elements in list context. |
2580 |
|
|
|
2581 |
|
|
=end original |
2582 |
|
|
|
2583 |
|
|
それぞれのオペランドは整数の形で評価されるので、S<C<2.18 .. 3.14>> は |
2584 |
|
|
リストコンテキストでは二つの要素を返します。 |
2585 |
|
|
|
2586 |
|
|
=begin original |
2587 |
|
|
|
2588 |
|
|
@list = (2.18 .. 3.14); # same as @list = (2 .. 3); |
2589 |
|
|
|
2590 |
|
|
=end original |
2591 |
|
|
|
2592 |
|
|
@list = (2.18 .. 3.14); # @list = (2 .. 3); と同じ |
2593 |
|
|
|
2594 |
|
|
=head2 Conditional Operator |
2595 |
|
|
X<operator, conditional> X<operator, ternary> X<ternary> X<?:> |
2596 |
|
|
|
2597 |
|
|
(条件演算子) |
2598 |
|
|
|
2599 |
|
|
=begin original |
2600 |
|
|
|
2601 |
|
|
Ternary C<"?:"> is the conditional operator, just as in C. It works much |
2602 |
|
|
like an if-then-else. If the argument before the C<?> is true, the |
2603 |
|
|
argument before the C<:> is returned, otherwise the argument after the |
2604 |
|
|
C<:> is returned. For example: |
2605 |
|
|
|
2606 |
|
|
=end original |
2607 |
|
|
|
2608 |
|
|
三項演算子の C<"?:"> は、C の場合と同じ条件演算子です。 |
2609 |
|
|
これは、if-then-else のように働きます。 |
2610 |
|
|
C<"?"> の前の引数が真であれば C<":"> の前の引数が返されますが、 |
2611 |
|
|
真でなければ、C<":"> の後の引数が返されます。 |
2612 |
|
|
例えば: |
2613 |
|
|
|
2614 |
|
|
printf "I have %d dog%s.\n", $n, |
2615 |
|
|
($n == 1) ? "" : "s"; |
2616 |
|
|
|
2617 |
|
|
=begin original |
2618 |
|
|
|
2619 |
|
|
Scalar or list context propagates downward into the 2nd |
2620 |
|
|
or 3rd argument, whichever is selected. |
2621 |
|
|
|
2622 |
|
|
=end original |
2623 |
|
|
|
2624 |
|
|
スカラコンテキストかリストコンテキストかという状況は、 |
2625 |
|
|
選択された 2 番目もしくは 3 番目の引数にまで伝わります。 |
2626 |
|
|
|
2627 |
|
|
=begin original |
2628 |
|
|
|
2629 |
|
|
$x = $ok ? $y : $z; # get a scalar |
2630 |
|
|
@x = $ok ? @y : @z; # get an array |
2631 |
|
|
$x = $ok ? @y : @z; # oops, that's just a count! |
2632 |
|
|
|
2633 |
|
|
=end original |
2634 |
|
|
|
2635 |
|
|
$x = $ok ? $y : $z; # スカラを取る |
2636 |
|
|
@x = $ok ? @y : @z; # 配列を取る |
2637 |
|
|
$x = $ok ? @y : @z; # うわ、これは単なる数です! |
2638 |
|
|
|
2639 |
|
|
=begin original |
2640 |
|
|
|
2641 |
|
|
The operator may be assigned to if both the 2nd and 3rd arguments are |
2642 |
|
|
legal lvalues (meaning that you can assign to them): |
2643 |
|
|
|
2644 |
|
|
=end original |
2645 |
|
|
|
2646 |
|
|
2 番目と 3 番目の引数双方が左辺値 (代入可能ということ)であれば、 |
2647 |
|
|
この演算子に代入を行なうこともできます: |
2648 |
|
|
|
2649 |
|
|
($x_or_y ? $x : $y) = $z; |
2650 |
|
|
|
2651 |
|
|
=begin original |
2652 |
|
|
|
2653 |
|
|
Because this operator produces an assignable result, using assignments |
2654 |
|
|
without parentheses will get you in trouble. For example, this: |
2655 |
|
|
|
2656 |
|
|
=end original |
2657 |
|
|
|
2658 |
|
|
この演算子は代入可能な結果を生み出すので、 |
2659 |
|
|
括弧なしで代入を行うとおかしくなるかもしれません。 |
2660 |
|
|
例えばこれは: |
2661 |
|
|
|
2662 |
|
|
$x % 2 ? $x += 10 : $x += 2 |
2663 |
|
|
|
2664 |
|
|
=begin original |
2665 |
|
|
|
2666 |
|
|
Really means this: |
2667 |
|
|
|
2668 |
|
|
=end original |
2669 |
|
|
|
2670 |
|
|
以下を意味し: |
2671 |
|
|
|
2672 |
|
|
(($x % 2) ? ($x += 10) : $x) += 2 |
2673 |
|
|
|
2674 |
|
|
=begin original |
2675 |
|
|
|
2676 |
|
|
Rather than this: |
2677 |
|
|
|
2678 |
|
|
=end original |
2679 |
|
|
|
2680 |
|
|
以下のようにはなりません: |
2681 |
|
|
|
2682 |
|
|
($x % 2) ? ($x += 10) : ($x += 2) |
2683 |
|
|
|
2684 |
|
|
=begin original |
2685 |
|
|
|
2686 |
|
|
That should probably be written more simply as: |
2687 |
|
|
|
2688 |
|
|
=end original |
2689 |
|
|
|
2690 |
|
|
恐らく以下のようにもっと単純に書くべきでしょう: |
2691 |
|
|
|
2692 |
|
|
$x += ($x % 2) ? 10 : 2; |
2693 |
|
|
|
2694 |
|
|
=head2 Assignment Operators |
2695 |
|
|
X<assignment> X<operator, assignment> X<=> X<**=> X<+=> X<*=> X<&=> |
2696 |
|
|
X<<< <<= >>> X<&&=> X<-=> X</=> X<|=> X<<< >>= >>> X<||=> X<//=> X<.=> |
2697 |
|
|
X<%=> X<^=> X<x=> X<&.=> X<|.=> X<^.=> |
2698 |
|
|
|
2699 |
|
|
(代入演算子) |
2700 |
|
|
|
2701 |
|
|
=begin original |
2702 |
|
|
|
2703 |
|
|
C<"="> is the ordinary assignment operator. |
2704 |
|
|
|
2705 |
|
|
=end original |
2706 |
|
|
|
2707 |
|
|
C<"="> は通常の代入演算子です。 |
2708 |
|
|
|
2709 |
|
|
=begin original |
2710 |
|
|
|
2711 |
|
|
Assignment operators work as in C. That is, |
2712 |
|
|
|
2713 |
|
|
=end original |
2714 |
|
|
|
2715 |
|
|
代入演算子は C の場合と同様の働きをします。 |
2716 |
|
|
つまり、 |
2717 |
|
|
|
2718 |
|
|
$x += 2; |
2719 |
|
|
|
2720 |
|
|
=begin original |
2721 |
|
|
|
2722 |
|
|
is equivalent to |
2723 |
|
|
|
2724 |
|
|
=end original |
2725 |
|
|
|
2726 |
|
|
は以下と等価です: |
2727 |
|
|
|
2728 |
|
|
$x = $x + 2; |
2729 |
|
|
|
2730 |
|
|
=begin original |
2731 |
|
|
|
2732 |
|
|
although without duplicating any side effects that dereferencing the lvalue |
2733 |
|
|
might trigger, such as from C<tie()>. Other assignment operators work similarly. |
2734 |
|
|
The following are recognized: |
2735 |
|
|
|
2736 |
|
|
=end original |
2737 |
|
|
|
2738 |
|
|
しかし、C<tie()> のようなもので起こる左辺値の被参照による |
2739 |
|
|
副作用が 2 回起こることはありません。 |
2740 |
|
|
他の代入演算も同様に働きます。 |
2741 |
|
|
以下のものが認識されます: |
2742 |
|
|
|
2743 |
|
|
**= += *= &= &.= <<= &&= |
2744 |
|
|
-= /= |= |.= >>= ||= |
2745 |
|
|
.= %= ^= ^.= //= |
2746 |
|
|
x= |
2747 |
|
|
|
2748 |
|
|
=begin original |
2749 |
|
|
|
2750 |
|
|
Although these are grouped by family, they all have the precedence |
2751 |
|
|
of assignment. These combined assignment operators can only operate on |
2752 |
|
|
scalars, whereas the ordinary assignment operator can assign to arrays, |
2753 |
|
|
hashes, lists and even references. (See L<"Context"|perldata/Context> |
2754 |
|
|
and L<perldata/List value constructors>, and L<perlref/Assigning to |
2755 |
|
|
References>.) |
2756 |
|
|
|
2757 |
|
|
=end original |
2758 |
|
|
|
2759 |
|
|
グループ分けしてありますが、これらはいずれも代入演算子として |
2760 |
|
|
同じ優先順位となっています。 |
2761 |
peanutsjamjam |
1.3 |
これらの複合代入演算子はスカラとしてのみ動作しますが、一方 |
2762 |
argrath |
1.2 |
通常の代入演算子は配列、スカラ、リスト、リファレンスに代入できます。 |
2763 |
|
|
(L<"Context"|perldata/Context>, L<perldata/List value constructors>, |
2764 |
|
|
L<perlref/Assigning to References> を参照してください。) |
2765 |
argrath |
1.1 |
|
2766 |
|
|
=begin original |
2767 |
|
|
|
2768 |
|
|
Unlike in C, the scalar assignment operator produces a valid lvalue. |
2769 |
|
|
Modifying an assignment is equivalent to doing the assignment and |
2770 |
|
|
then modifying the variable that was assigned to. This is useful |
2771 |
|
|
for modifying a copy of something, like this: |
2772 |
|
|
|
2773 |
|
|
=end original |
2774 |
|
|
|
2775 |
|
|
C と違って、スカラ代入演算子は有効な左辺値を作り出します。 |
2776 |
|
|
代入を修正することは、代入を行なってから、その代入された変数を修正するのと |
2777 |
|
|
同じことになります。 |
2778 |
|
|
これは、以下のように何かのコピーを変更したいときに便利です: |
2779 |
|
|
|
2780 |
|
|
($tmp = $global) =~ tr/13579/24680/; |
2781 |
|
|
|
2782 |
|
|
=begin original |
2783 |
|
|
|
2784 |
|
|
Although as of 5.14, that can be also be accomplished this way: |
2785 |
|
|
|
2786 |
|
|
=end original |
2787 |
|
|
|
2788 |
|
|
しかし 5.14 現在、これは次のようにしてもできるようになりました: |
2789 |
|
|
|
2790 |
|
|
use v5.14; |
2791 |
|
|
$tmp = ($global =~ tr/13579/24680/r); |
2792 |
|
|
|
2793 |
|
|
=begin original |
2794 |
|
|
|
2795 |
|
|
Likewise, |
2796 |
|
|
|
2797 |
|
|
=end original |
2798 |
|
|
|
2799 |
|
|
同様に、 |
2800 |
|
|
|
2801 |
|
|
($x += 2) *= 3; |
2802 |
|
|
|
2803 |
|
|
=begin original |
2804 |
|
|
|
2805 |
|
|
is equivalent to |
2806 |
|
|
|
2807 |
|
|
=end original |
2808 |
|
|
|
2809 |
|
|
は以下と等価です: |
2810 |
|
|
|
2811 |
|
|
$x += 2; |
2812 |
|
|
$x *= 3; |
2813 |
|
|
|
2814 |
|
|
=begin original |
2815 |
|
|
|
2816 |
|
|
Similarly, a list assignment in list context produces the list of |
2817 |
|
|
lvalues assigned to, and a list assignment in scalar context returns |
2818 |
|
|
the number of elements produced by the expression on the right hand |
2819 |
|
|
side of the assignment. |
2820 |
|
|
|
2821 |
|
|
=end original |
2822 |
|
|
|
2823 |
|
|
同様に、リストコンテキストでのリストへの代入は代入可能な左辺値のリストとなり、 |
2824 |
|
|
スカラコンテキストでのリストへの代入は代入の右側の式で作成された |
2825 |
|
|
要素の数を返します。 |
2826 |
|
|
|
2827 |
|
|
=begin original |
2828 |
|
|
|
2829 |
|
|
The three dotted bitwise assignment operators (C<&.=> C<|.=> C<^.=>) are new in |
2830 |
|
|
Perl 5.22 and experimental. See L</Bitwise String Operators>. |
2831 |
|
|
|
2832 |
|
|
=end original |
2833 |
|
|
|
2834 |
argrath |
1.2 |
三つのドット付きビット単位代入演算子 (C<&.=> C<|.=> C<^.=>) は |
2835 |
|
|
Perl 5.22 からの新しいもので実験的なものです。 |
2836 |
|
|
L</Bitwise String Operators> を参照してください。 |
2837 |
argrath |
1.1 |
|
2838 |
|
|
=head2 Comma Operator |
2839 |
|
|
X<comma> X<operator, comma> X<,> |
2840 |
|
|
|
2841 |
|
|
(コンマ演算子) |
2842 |
|
|
|
2843 |
|
|
=begin original |
2844 |
|
|
|
2845 |
|
|
Binary C<","> is the comma operator. In scalar context it evaluates |
2846 |
|
|
its left argument, throws that value away, then evaluates its right |
2847 |
|
|
argument and returns that value. This is just like C's comma operator. |
2848 |
|
|
|
2849 |
|
|
=end original |
2850 |
|
|
|
2851 |
|
|
二項演算子の C<","> はコンマ演算子です。 |
2852 |
|
|
スカラコンテキストではその左引数を評価し、その値を捨てて、 |
2853 |
|
|
それから右引数を評価し、その値を返します。 |
2854 |
|
|
これはちょうど、C のコンマ演算子と同じです。 |
2855 |
|
|
|
2856 |
|
|
=begin original |
2857 |
|
|
|
2858 |
|
|
In list context, it's just the list argument separator, and inserts |
2859 |
|
|
both its arguments into the list. These arguments are also evaluated |
2860 |
|
|
from left to right. |
2861 |
|
|
|
2862 |
|
|
=end original |
2863 |
|
|
|
2864 |
|
|
リストコンテキストでは、これは単にリスト引数の区切り文字で、 |
2865 |
|
|
双方の引数をそのリストに挿入する働きがあります。 |
2866 |
|
|
これらの引数も左から右に評価されます。 |
2867 |
|
|
|
2868 |
|
|
=begin original |
2869 |
|
|
|
2870 |
|
|
The C<< => >> operator (sometimes pronounced "fat comma") is a synonym |
2871 |
|
|
for the comma except that it causes a |
2872 |
|
|
word on its left to be interpreted as a string if it begins with a letter |
2873 |
|
|
or underscore and is composed only of letters, digits and underscores. |
2874 |
|
|
This includes operands that might otherwise be interpreted as operators, |
2875 |
|
|
constants, single number v-strings or function calls. If in doubt about |
2876 |
|
|
this behavior, the left operand can be quoted explicitly. |
2877 |
|
|
|
2878 |
|
|
=end original |
2879 |
|
|
|
2880 |
|
|
C<< => >> 演算子(時々「ファットコンマ」と発音されます)はコンマ演算子の |
2881 |
|
|
同義語ですが、もし左側の単語が文字か下線で始まっていて、かつ文字、数字、 |
2882 |
|
|
下線でのみ構成されている場合、これを文字列として扱うという効果もあります。 |
2883 |
|
|
これには他の場所では演算子、定数、v-文字列、関数呼び出しとして扱われる |
2884 |
|
|
オペランドを含みます。 |
2885 |
|
|
この振る舞いについて迷うことがあるなら、左オペランドを明示的に |
2886 |
|
|
クォートすることも出来ます。 |
2887 |
|
|
|
2888 |
|
|
=begin original |
2889 |
|
|
|
2890 |
|
|
Otherwise, the C<< => >> operator behaves exactly as the comma operator |
2891 |
|
|
or list argument separator, according to context. |
2892 |
|
|
|
2893 |
|
|
=end original |
2894 |
|
|
|
2895 |
|
|
さもなければ、C<< => >> 演算子はコンテキストによって、 |
2896 |
|
|
カンマ演算子かリスト引数の区切り文字と全く同様に振る舞います。 |
2897 |
|
|
|
2898 |
|
|
=begin original |
2899 |
|
|
|
2900 |
|
|
For example: |
2901 |
|
|
|
2902 |
|
|
=end original |
2903 |
|
|
|
2904 |
|
|
例えば: |
2905 |
|
|
|
2906 |
|
|
use constant FOO => "something"; |
2907 |
|
|
|
2908 |
|
|
my %h = ( FOO => 23 ); |
2909 |
|
|
|
2910 |
|
|
=begin original |
2911 |
|
|
|
2912 |
|
|
is equivalent to: |
2913 |
|
|
|
2914 |
|
|
=end original |
2915 |
|
|
|
2916 |
|
|
は、以下と等価です: |
2917 |
|
|
|
2918 |
|
|
my %h = ("FOO", 23); |
2919 |
|
|
|
2920 |
|
|
=begin original |
2921 |
|
|
|
2922 |
|
|
It is I<NOT>: |
2923 |
|
|
|
2924 |
|
|
=end original |
2925 |
|
|
|
2926 |
|
|
これは I<違います>: |
2927 |
|
|
|
2928 |
|
|
my %h = ("something", 23); |
2929 |
|
|
|
2930 |
|
|
=begin original |
2931 |
|
|
|
2932 |
|
|
The C<< => >> operator is helpful in documenting the correspondence |
2933 |
|
|
between keys and values in hashes, and other paired elements in lists. |
2934 |
|
|
|
2935 |
|
|
=end original |
2936 |
|
|
|
2937 |
|
|
C<< => >> 演算子は、ハッシュのキーと値や、その他のリスト中の組となる |
2938 |
|
|
要素の関係を表現するのに便利です。 |
2939 |
|
|
|
2940 |
|
|
%hash = ( $key => $value ); |
2941 |
|
|
login( $username => $password ); |
2942 |
|
|
|
2943 |
|
|
=begin original |
2944 |
|
|
|
2945 |
|
|
The special quoting behavior ignores precedence, and hence may apply to |
2946 |
|
|
I<part> of the left operand: |
2947 |
|
|
|
2948 |
|
|
=end original |
2949 |
|
|
|
2950 |
|
|
特殊なクォートの振る舞いは優先順位を無視し、従って左オペランドの I<一部> に |
2951 |
|
|
適用されることがあります: |
2952 |
|
|
|
2953 |
|
|
print time.shift => "bbb"; |
2954 |
|
|
|
2955 |
|
|
=begin original |
2956 |
|
|
|
2957 |
|
|
That example prints something like C<"1314363215shiftbbb">, because the |
2958 |
|
|
C<< => >> implicitly quotes the C<shift> immediately on its left, ignoring |
2959 |
|
|
the fact that C<time.shift> is the entire left operand. |
2960 |
|
|
|
2961 |
|
|
=end original |
2962 |
|
|
|
2963 |
|
|
この例は C<"1314363215shiftbbb"> のようなものを表示します; なぜなら |
2964 |
|
|
C<< => >> は暗黙にすぐ左にある C<shift> をクォートし、 C<time.shift> 全体が |
2965 |
|
|
左オペランドであるという事実を無視するからです。 |
2966 |
|
|
|
2967 |
|
|
=head2 List Operators (Rightward) |
2968 |
|
|
X<operator, list, rightward> X<list operator> |
2969 |
|
|
|
2970 |
|
|
(リスト演算子 (右方向)) |
2971 |
|
|
|
2972 |
|
|
=begin original |
2973 |
|
|
|
2974 |
|
|
On the right side of a list operator, the comma has very low precedence, |
2975 |
|
|
such that it controls all comma-separated expressions found there. |
2976 |
|
|
The only operators with lower precedence are the logical operators |
2977 |
|
|
C<"and">, C<"or">, and C<"not">, which may be used to evaluate calls to list |
2978 |
|
|
operators without the need for parentheses: |
2979 |
|
|
|
2980 |
|
|
=end original |
2981 |
|
|
|
2982 |
|
|
リスト演算子の右側のものにとって、カンマはとても低い優先順位になります; |
2983 |
|
|
これによってコンマで区切った式をリスト演算子の引数として |
2984 |
|
|
置くことができます。 |
2985 |
|
|
これよりも優先順位が低いものは、論理演算子の C<"and">, C<"or">, |
2986 |
|
|
C<"not"> のみで、括弧を付けないリスト演算子の呼び出しを評価するために使えます: |
2987 |
|
|
|
2988 |
|
|
open HANDLE, "< :utf8", "filename" or die "Can't open: $!\n"; |
2989 |
|
|
|
2990 |
|
|
=begin original |
2991 |
|
|
|
2992 |
|
|
However, some people find that code harder to read than writing |
2993 |
|
|
it with parentheses: |
2994 |
|
|
|
2995 |
|
|
=end original |
2996 |
|
|
|
2997 |
|
|
しかし、かっこ付きで書くよりもコードが読みにくいという人もいます: |
2998 |
|
|
|
2999 |
|
|
open(HANDLE, "< :utf8", "filename") or die "Can't open: $!\n"; |
3000 |
|
|
|
3001 |
|
|
=begin original |
3002 |
|
|
|
3003 |
|
|
in which case you might as well just use the more customary C<"||"> operator: |
3004 |
|
|
|
3005 |
|
|
=end original |
3006 |
|
|
|
3007 |
|
|
この場合、より慣習的な C<"||"> 演算子も使えます: |
3008 |
|
|
|
3009 |
|
|
open(HANDLE, "< :utf8", "filename") || die "Can't open: $!\n"; |
3010 |
|
|
|
3011 |
|
|
=begin original |
3012 |
|
|
|
3013 |
|
|
See also discussion of list operators in L<Terms and List Operators (Leftward)>. |
3014 |
|
|
|
3015 |
|
|
=end original |
3016 |
|
|
|
3017 |
|
|
L<Terms and List Operators (Leftward)> のリスト演算子の議論も参照して下さい。 |
3018 |
|
|
|
3019 |
|
|
=head2 Logical Not |
3020 |
|
|
X<operator, logical, not> X<not> |
3021 |
|
|
|
3022 |
|
|
(論理否定) |
3023 |
|
|
|
3024 |
|
|
=begin original |
3025 |
|
|
|
3026 |
|
|
Unary C<"not"> returns the logical negation of the expression to its right. |
3027 |
|
|
It's the equivalent of C<"!"> except for the very low precedence. |
3028 |
|
|
|
3029 |
|
|
=end original |
3030 |
|
|
|
3031 |
|
|
単項演算子の C<"not"> は右側に来る式の否定を返します。 |
3032 |
|
|
これは、優先順位がずっと低いことを除いては C<"!"> と等価です。 |
3033 |
|
|
|
3034 |
|
|
=head2 Logical And |
3035 |
|
|
X<operator, logical, and> X<and> |
3036 |
|
|
|
3037 |
|
|
(論理積) |
3038 |
|
|
|
3039 |
|
|
=begin original |
3040 |
|
|
|
3041 |
|
|
Binary C<"and"> returns the logical conjunction of the two surrounding |
3042 |
|
|
expressions. It's equivalent to C<&&> except for the very low |
3043 |
|
|
precedence. This means that it short-circuits: the right |
3044 |
|
|
expression is evaluated only if the left expression is true. |
3045 |
|
|
|
3046 |
|
|
=end original |
3047 |
|
|
|
3048 |
|
|
二項演算子の C<"and"> は両側の式の論理積を返します。 |
3049 |
|
|
これは、優先順位がずっと低いことを除けば C<&&> と等価です。 |
3050 |
|
|
つまり、これも短絡演算を行ない、右側の式は左側の式が |
3051 |
|
|
「真」であった場合にのみ評価されます。 |
3052 |
|
|
|
3053 |
|
|
=head2 Logical or and Exclusive Or |
3054 |
|
|
X<operator, logical, or> X<operator, logical, xor> |
3055 |
|
|
X<operator, logical, exclusive or> |
3056 |
|
|
X<or> X<xor> |
3057 |
|
|
|
3058 |
|
|
(論理和と排他論理和) |
3059 |
|
|
|
3060 |
|
|
=begin original |
3061 |
|
|
|
3062 |
|
|
Binary C<"or"> returns the logical disjunction of the two surrounding |
3063 |
|
|
expressions. It's equivalent to C<||> except for the very low precedence. |
3064 |
|
|
This makes it useful for control flow: |
3065 |
|
|
|
3066 |
|
|
=end original |
3067 |
|
|
|
3068 |
|
|
二項演算子の C<"or"> は両側の式の論理和を返します。 |
3069 |
|
|
これは、優先順位がずっと低いことを除いて C<||> と等価です。 |
3070 |
|
|
これはフローを制御するのに有用です: |
3071 |
|
|
|
3072 |
|
|
print FH $data or die "Can't write to FH: $!"; |
3073 |
|
|
|
3074 |
|
|
=begin original |
3075 |
|
|
|
3076 |
|
|
This means that it short-circuits: the right expression is evaluated |
3077 |
|
|
only if the left expression is false. Due to its precedence, you must |
3078 |
|
|
be careful to avoid using it as replacement for the C<||> operator. |
3079 |
|
|
It usually works out better for flow control than in assignments: |
3080 |
|
|
|
3081 |
|
|
=end original |
3082 |
|
|
|
3083 |
|
|
つまり、これも短絡演算を行ない、右側の式は左側の式が |
3084 |
|
|
「偽」であった場合にのみ評価されます。 |
3085 |
|
|
優先度の関係で、これを C<||> 演算子の置き換えに使うのは慎重に |
3086 |
|
|
避けなければなりません。 |
3087 |
|
|
これは普通代入よりも、フローの制御でうまく動作します: |
3088 |
|
|
|
3089 |
|
|
=begin original |
3090 |
|
|
|
3091 |
|
|
$x = $y or $z; # bug: this is wrong |
3092 |
|
|
($x = $y) or $z; # really means this |
3093 |
|
|
$x = $y || $z; # better written this way |
3094 |
|
|
|
3095 |
|
|
=end original |
3096 |
|
|
|
3097 |
|
|
$x = $y or $z; # バグ: これは間違い |
3098 |
|
|
($x = $y) or $z; # 本当にしたいこと |
3099 |
|
|
$x = $y || $z; # こう書いた方がいい |
3100 |
|
|
|
3101 |
|
|
=begin original |
3102 |
|
|
|
3103 |
|
|
However, when it's a list-context assignment and you're trying to use |
3104 |
|
|
C<||> for control flow, you probably need C<"or"> so that the assignment |
3105 |
|
|
takes higher precedence. |
3106 |
|
|
|
3107 |
|
|
=end original |
3108 |
|
|
|
3109 |
|
|
しかし、代入がリストコンテキストの時に C<||> をフロー制御に使おうとする場合、 |
3110 |
|
|
代入により大きな優先順位を持たせるために C<"or"> が必要かもしれません。 |
3111 |
|
|
|
3112 |
|
|
=begin original |
3113 |
|
|
|
3114 |
|
|
@info = stat($file) || die; # oops, scalar sense of stat! |
3115 |
|
|
@info = stat($file) or die; # better, now @info gets its due |
3116 |
|
|
|
3117 |
|
|
=end original |
3118 |
|
|
|
3119 |
|
|
@info = stat($file) || die; # うわ、stat がスカラの意味だ! |
3120 |
|
|
@info = stat($file) or die; # よりよい; @info はその目的を果たす |
3121 |
|
|
|
3122 |
|
|
=begin original |
3123 |
|
|
|
3124 |
|
|
Then again, you could always use parentheses. |
3125 |
|
|
|
3126 |
|
|
=end original |
3127 |
|
|
|
3128 |
|
|
もちろん、常に括弧をつけてもよいです。 |
3129 |
|
|
|
3130 |
|
|
=begin original |
3131 |
|
|
|
3132 |
|
|
Binary C<"xor"> returns the exclusive-OR of the two surrounding expressions. |
3133 |
|
|
It cannot short-circuit (of course). |
3134 |
|
|
|
3135 |
|
|
=end original |
3136 |
|
|
|
3137 |
|
|
二項演算子の C<"xor"> は両側の式の排他論理和を返します。 |
3138 |
|
|
これは (もちろん) 短絡できません。 |
3139 |
|
|
|
3140 |
|
|
=begin original |
3141 |
|
|
|
3142 |
|
|
There is no low precedence operator for defined-OR. |
3143 |
|
|
|
3144 |
|
|
=end original |
3145 |
|
|
|
3146 |
|
|
定義性論理和の低優先順位版はありません。 |
3147 |
|
|
|
3148 |
|
|
=head2 C Operators Missing From Perl |
3149 |
|
|
X<operator, missing from perl> X<&> X<*> |
3150 |
|
|
X<typecasting> X<(TYPE)> |
3151 |
|
|
|
3152 |
|
|
(Perl にない C の演算子) |
3153 |
|
|
|
3154 |
|
|
=begin original |
3155 |
|
|
|
3156 |
|
|
Here is what C has that Perl doesn't: |
3157 |
|
|
|
3158 |
|
|
=end original |
3159 |
|
|
|
3160 |
|
|
C にあって Perl に無いものは以下の通りです: |
3161 |
|
|
|
3162 |
|
|
=over 8 |
3163 |
|
|
|
3164 |
|
|
=item unary & |
3165 |
|
|
|
3166 |
|
|
=begin original |
3167 |
|
|
|
3168 |
|
|
Address-of operator. (But see the C<"\"> operator for taking a reference.) |
3169 |
|
|
|
3170 |
|
|
=end original |
3171 |
|
|
|
3172 |
|
|
アドレス演算子。 |
3173 |
|
|
(しかし C<"\"> 演算子がリファレンスのために用いられます。) |
3174 |
|
|
|
3175 |
|
|
=item unary * |
3176 |
|
|
|
3177 |
|
|
=begin original |
3178 |
|
|
|
3179 |
|
|
Dereference-address operator. (Perl's prefix dereferencing |
3180 |
|
|
operators are typed: C<$>, C<@>, C<%>, and C<&>.) |
3181 |
|
|
|
3182 |
|
|
=end original |
3183 |
|
|
|
3184 |
|
|
被アドレス参照演算子。 |
3185 |
|
|
(Perl の被参照プリフィクス演算子が型づけを行ないます: |
3186 |
|
|
C<$>, C<@>, C<%>, C<&>。) |
3187 |
|
|
|
3188 |
|
|
=item (TYPE) |
3189 |
|
|
|
3190 |
|
|
=begin original |
3191 |
|
|
|
3192 |
|
|
Type-casting operator. |
3193 |
|
|
|
3194 |
|
|
=end original |
3195 |
|
|
|
3196 |
|
|
型のキャスト演算子。 |
3197 |
|
|
|
3198 |
|
|
=back |
3199 |
|
|
|
3200 |
|
|
=head2 Quote and Quote-like Operators |
3201 |
|
|
X<operator, quote> X<operator, quote-like> X<q> X<qq> X<qx> X<qw> X<m> |
3202 |
|
|
X<qr> X<s> X<tr> X<'> X<''> X<"> X<""> X<//> X<`> X<``> X<<< << >>> |
3203 |
|
|
X<escape sequence> X<escape> |
3204 |
|
|
|
3205 |
|
|
(クォートとクォート風の演算子) |
3206 |
|
|
|
3207 |
|
|
=begin original |
3208 |
|
|
|
3209 |
|
|
While we usually think of quotes as literal values, in Perl they |
3210 |
|
|
function as operators, providing various kinds of interpolating and |
3211 |
|
|
pattern matching capabilities. Perl provides customary quote characters |
3212 |
|
|
for these behaviors, but also provides a way for you to choose your |
3213 |
|
|
quote character for any of them. In the following table, a C<{}> represents |
3214 |
|
|
any pair of delimiters you choose. |
3215 |
|
|
|
3216 |
|
|
=end original |
3217 |
|
|
|
3218 |
|
|
クォートはリテラル値であると考えるのが普通ですが、Perl において、 |
3219 |
|
|
クォートは演算子として働き、さまざまな展開やパターンマッチの機能を |
3220 |
|
|
持っています。 |
3221 |
|
|
そのような動作をさせるのに、Perl は慣習的にクォート文字を使っていますが、 |
3222 |
|
|
どの種類のクォートも、自分でクォート文字を選べるようになっています。 |
3223 |
|
|
以下の表では、{} がその選んだ区切文字のペアを示しています。 |
3224 |
|
|
|
3225 |
|
|
=begin original |
3226 |
|
|
|
3227 |
|
|
Customary Generic Meaning Interpolates |
3228 |
|
|
'' q{} Literal no |
3229 |
|
|
"" qq{} Literal yes |
3230 |
|
|
`` qx{} Command yes* |
3231 |
|
|
qw{} Word list no |
3232 |
|
|
// m{} Pattern match yes* |
3233 |
|
|
qr{} Pattern yes* |
3234 |
|
|
s{}{} Substitution yes* |
3235 |
|
|
tr{}{} Transliteration no (but see below) |
3236 |
|
|
y{}{} Transliteration no (but see below) |
3237 |
|
|
<<EOF here-doc yes* |
3238 |
|
|
|
3239 |
|
|
=end original |
3240 |
|
|
|
3241 |
|
|
通常記法 汎用記法 意味 展開 |
3242 |
|
|
================================================= |
3243 |
|
|
'' q{} リテラル 不可 |
3244 |
|
|
"" qq{} リテラル 可 |
3245 |
|
|
`` qx{} コマンド 可 * |
3246 |
|
|
qw{} 単語リスト 不可 |
3247 |
|
|
// m{} パターンマッチ 可 * |
3248 |
|
|
qr{} パターン 可 * |
3249 |
|
|
s{}{} 置換 可 * |
3250 |
|
|
tr{}{} 変換 不可 (但し以下を参照のこと) |
3251 |
|
|
y{}{} 変換 不可 (但し以下を参照のこと) |
3252 |
|
|
<<EOF ヒアドキュメント 可 * |
3253 |
|
|
|
3254 |
|
|
=begin original |
3255 |
|
|
|
3256 |
|
|
* unless the delimiter is ''. |
3257 |
|
|
|
3258 |
|
|
=end original |
3259 |
|
|
|
3260 |
|
|
* '' がデリミタでない場合のみ |
3261 |
|
|
|
3262 |
|
|
=begin original |
3263 |
|
|
|
3264 |
|
|
Non-bracketing delimiters use the same character fore and aft, but the four |
3265 |
|
|
sorts of ASCII brackets (round, angle, square, curly) all nest, which means |
3266 |
|
|
that |
3267 |
|
|
|
3268 |
|
|
=end original |
3269 |
|
|
|
3270 |
|
|
選んだ区切文字が括弧の類でない場合には、前後の文字として同一のものを |
3271 |
|
|
使いますが、4 つの ASCII のかっこ ((), <>, [], {}) の場合にはネストできます; |
3272 |
|
|
つまり、以下のものは、 |
3273 |
|
|
|
3274 |
|
|
q{foo{bar}baz} |
3275 |
|
|
|
3276 |
|
|
=begin original |
3277 |
|
|
|
3278 |
|
|
is the same as |
3279 |
|
|
|
3280 |
|
|
=end original |
3281 |
|
|
|
3282 |
|
|
以下と同じです。 |
3283 |
|
|
|
3284 |
|
|
'foo{bar}baz' |
3285 |
|
|
|
3286 |
|
|
=begin original |
3287 |
|
|
|
3288 |
|
|
Note, however, that this does not always work for quoting Perl code: |
3289 |
|
|
|
3290 |
|
|
=end original |
3291 |
|
|
|
3292 |
|
|
しかし、以下のコードはクォートされた Perl コードでは |
3293 |
|
|
いつも正しく動くわけではないことに注意してください: |
3294 |
|
|
|
3295 |
|
|
$s = q{ if($x eq "}") ... }; # WRONG |
3296 |
|
|
|
3297 |
|
|
=begin original |
3298 |
|
|
|
3299 |
|
|
is a syntax error. The C<L<Text::Balanced>> module (standard as of v5.8, |
3300 |
|
|
and from CPAN before then) is able to do this properly. |
3301 |
|
|
|
3302 |
|
|
=end original |
3303 |
|
|
|
3304 |
|
|
これは文法エラーとなります。 |
3305 |
|
|
C<L<Text::Balanced>> モジュール(Perl 5.8 からは標準配布、それ以前は CPAN に |
3306 |
|
|
あります)はこれを適切に行います。 |
3307 |
|
|
|
3308 |
|
|
=begin original |
3309 |
|
|
|
3310 |
|
|
There can be whitespace between the operator and the quoting |
3311 |
|
|
characters, except when C<#> is being used as the quoting character. |
3312 |
|
|
C<q#foo#> is parsed as the string C<foo>, while S<C<q #foo#>> is the |
3313 |
|
|
operator C<q> followed by a comment. Its argument will be taken |
3314 |
|
|
from the next line. This allows you to write: |
3315 |
|
|
|
3316 |
|
|
=end original |
3317 |
|
|
|
3318 |
|
|
演算子とクォート文字の間に空白を置くことも出来ます; ただし、C<#> を |
3319 |
|
|
クォート文字として使う場合は例外です。 |
3320 |
|
|
C<q#foo#> は文字列 C<foo> としてパースされますが、 |
3321 |
|
|
S<C<q #foo#>> は C<q> 演算子の後にコメントがあるとみなされます。 |
3322 |
|
|
この引数は次の行から取られます。つまり、以下のように書けます: |
3323 |
|
|
|
3324 |
|
|
=begin original |
3325 |
|
|
|
3326 |
|
|
s {foo} # Replace foo |
3327 |
|
|
{bar} # with bar. |
3328 |
|
|
|
3329 |
|
|
=end original |
3330 |
|
|
|
3331 |
|
|
s {foo} # foo を |
3332 |
|
|
{bar} # bar で置き換える |
3333 |
|
|
|
3334 |
|
|
=begin original |
3335 |
|
|
|
3336 |
|
|
The following escape sequences are available in constructs that interpolate, |
3337 |
|
|
and in transliterations: |
3338 |
|
|
X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}> |
3339 |
|
|
X<\o{}> |
3340 |
|
|
|
3341 |
|
|
=end original |
3342 |
|
|
|
3343 |
|
|
以下のエスケープシーケンスが展開と文字変換の構文で利用可能です: |
3344 |
|
|
X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}> |
3345 |
|
|
X<\o{}> |
3346 |
|
|
|
3347 |
|
|
=begin original |
3348 |
|
|
|
3349 |
|
|
Sequence Note Description |
3350 |
|
|
\t tab (HT, TAB) |
3351 |
|
|
\n newline (NL) |
3352 |
|
|
\r return (CR) |
3353 |
|
|
\f form feed (FF) |
3354 |
|
|
\b backspace (BS) |
3355 |
|
|
\a alarm (bell) (BEL) |
3356 |
|
|
\e escape (ESC) |
3357 |
|
|
\x{263A} [1,8] hex char (example: SMILEY) |
3358 |
|
|
\x1b [2,8] restricted range hex char (example: ESC) |
3359 |
|
|
\N{name} [3] named Unicode character or character sequence |
3360 |
|
|
\N{U+263D} [4,8] Unicode character (example: FIRST QUARTER MOON) |
3361 |
|
|
\c[ [5] control char (example: chr(27)) |
3362 |
|
|
\o{23072} [6,8] octal char (example: SMILEY) |
3363 |
|
|
\033 [7,8] restricted range octal char (example: ESC) |
3364 |
|
|
|
3365 |
|
|
=end original |
3366 |
|
|
|
3367 |
|
|
シーケンス 注意 説明 |
3368 |
|
|
\t タブ (HT, TAB) |
3369 |
|
|
\n 改行 (NL) |
3370 |
|
|
\r 復帰 (CR) |
3371 |
|
|
\f 改ページ (FF) |
3372 |
|
|
\b バックスペース (BS) |
3373 |
|
|
\a アラーム (BEL) |
3374 |
|
|
\e エスケープ (ESC) |
3375 |
|
|
\x{263a} [1,8] 16 進文字 (例: SMILEY) |
3376 |
|
|
\x1b [2,8] 範囲制限された 16 進数で表した文字 (例: ESC) |
3377 |
|
|
\N{name} [3] 名前つき Unicode 文字または文字シーケンス |
3378 |
|
|
\N{U+263D} [4,8] Unicode 文字 (例: FIRST QUARTER MOON) |
3379 |
|
|
\c[ [5] 制御文字 (例: chr(27)) |
3380 |
|
|
\o{23072} [6,8] 8 進文字 (例: SMILEY) |
3381 |
|
|
\033 [7,8] 範囲制限された 8 進文字 (例: ESC) |
3382 |
|
|
|
3383 |
|
|
=over 4 |
3384 |
|
|
|
3385 |
|
|
=item [1] |
3386 |
|
|
|
3387 |
|
|
=begin original |
3388 |
|
|
|
3389 |
|
|
The result is the character specified by the hexadecimal number between |
3390 |
|
|
the braces. See L</[8]> below for details on which character. |
3391 |
|
|
|
3392 |
|
|
=end original |
3393 |
|
|
|
3394 |
|
|
結果は中かっこで囲まれた 16 進数で指定された文字です。 |
3395 |
|
|
その文字に関する詳細については以下の L</[8]> を参照してください。 |
3396 |
|
|
|
3397 |
|
|
=begin original |
3398 |
|
|
|
3399 |
|
|
Only hexadecimal digits are valid between the braces. If an invalid |
3400 |
|
|
character is encountered, a warning will be issued and the invalid |
3401 |
|
|
character and all subsequent characters (valid or invalid) within the |
3402 |
|
|
braces will be discarded. |
3403 |
|
|
|
3404 |
|
|
=end original |
3405 |
|
|
|
3406 |
|
|
中かっこの中には 16 進数字のみが妥当です。 |
3407 |
|
|
不正な文字に遭遇すると、警告が発生し、中かっこの内側の不正な文字と |
3408 |
|
|
それ以降の文字(妥当でも不正でも)は捨てられます。 |
3409 |
|
|
|
3410 |
|
|
=begin original |
3411 |
|
|
|
3412 |
|
|
If there are no valid digits between the braces, the generated character is |
3413 |
|
|
the NULL character (C<\x{00}>). However, an explicit empty brace (C<\x{}>) |
3414 |
|
|
will not cause a warning (currently). |
3415 |
|
|
|
3416 |
|
|
=end original |
3417 |
|
|
|
3418 |
|
|
中かっこの中に妥当な文字がなければ、生成される文字は NULL 文字 |
3419 |
|
|
(C<\x{00}>) です。 |
3420 |
|
|
しかし、明示的な空の中かっこ (C<\x{}>) は(今のところ)警告を出しません。 |
3421 |
|
|
|
3422 |
|
|
=item [2] |
3423 |
|
|
|
3424 |
|
|
=begin original |
3425 |
|
|
|
3426 |
|
|
The result is the character specified by the hexadecimal number in the range |
3427 |
|
|
0x00 to 0xFF. See L</[8]> below for details on which character. |
3428 |
|
|
|
3429 |
|
|
=end original |
3430 |
|
|
|
3431 |
|
|
結果は 0x00 から 0xFF の範囲の 16 進数で指定された文字です。 |
3432 |
|
|
その文字に関する詳細については以下の L</[8]> を参照してください。 |
3433 |
|
|
|
3434 |
|
|
=begin original |
3435 |
|
|
|
3436 |
|
|
Only hexadecimal digits are valid following C<\x>. When C<\x> is followed |
3437 |
|
|
by fewer than two valid digits, any valid digits will be zero-padded. This |
3438 |
|
|
means that C<\x7> will be interpreted as C<\x07>, and a lone C<"\x"> will be |
3439 |
|
|
interpreted as C<\x00>. Except at the end of a string, having fewer than |
3440 |
|
|
two valid digits will result in a warning. Note that although the warning |
3441 |
|
|
says the illegal character is ignored, it is only ignored as part of the |
3442 |
|
|
escape and will still be used as the subsequent character in the string. |
3443 |
|
|
For example: |
3444 |
|
|
|
3445 |
|
|
=end original |
3446 |
|
|
|
3447 |
|
|
C<\x> に引き続くのは 16 進数字のみが妥当です。 |
3448 |
|
|
C<\x> に引き続く妥当な数字が 2 桁ない場合、妥当な数字はゼロで |
3449 |
|
|
パッディングされます。 |
3450 |
|
|
これは、C<\x7> は C<\x07> と解釈され、単独の C<"\x"> は C<\x00> と |
3451 |
|
|
解釈されるということです。 |
3452 |
|
|
文字列の末尾を例外として、妥当な数字が 2 桁ない場合は警告が発生します。 |
3453 |
|
|
警告は不正な文字が無視されると言うにも関わらず、エスケープの一部のみが |
3454 |
|
|
無視され、文字列中の引き続く文字は使われるままであることに注意してください。 |
3455 |
|
|
例えば: |
3456 |
|
|
|
3457 |
|
|
Original Result Warns? |
3458 |
|
|
"\x7" "\x07" no |
3459 |
|
|
"\x" "\x00" no |
3460 |
|
|
"\x7q" "\x07q" yes |
3461 |
|
|
"\xq" "\x00q" yes |
3462 |
|
|
|
3463 |
|
|
=item [3] |
3464 |
|
|
|
3465 |
|
|
=begin original |
3466 |
|
|
|
3467 |
|
|
The result is the Unicode character or character sequence given by I<name>. |
3468 |
|
|
See L<charnames>. |
3469 |
|
|
|
3470 |
|
|
=end original |
3471 |
|
|
|
3472 |
|
|
結果は I<name> で指定される Unicode 文字または文字の並びです。 |
3473 |
|
|
L<charnames> を参照してください。 |
3474 |
|
|
|
3475 |
|
|
=item [4] |
3476 |
|
|
|
3477 |
|
|
=begin original |
3478 |
|
|
|
3479 |
|
|
S<C<\N{U+I<hexadecimal number>}>> means the Unicode character whose Unicode code |
3480 |
|
|
point is I<hexadecimal number>. |
3481 |
|
|
|
3482 |
|
|
=end original |
3483 |
|
|
|
3484 |
|
|
S<C<\N{U+I<hexadecimal number>}>> は、Unicode 符号位置が |
3485 |
|
|
I<hexadecimal number> の Unicode 文字を意味します。 |
3486 |
|
|
|
3487 |
|
|
=item [5] |
3488 |
|
|
|
3489 |
|
|
=begin original |
3490 |
|
|
|
3491 |
|
|
The character following C<\c> is mapped to some other character as shown in the |
3492 |
|
|
table: |
3493 |
|
|
|
3494 |
|
|
=end original |
3495 |
|
|
|
3496 |
|
|
C<\c> に引き続く文字は以下の表に示すように他の文字にマッピングされます: |
3497 |
|
|
|
3498 |
|
|
Sequence Value |
3499 |
|
|
\c@ chr(0) |
3500 |
|
|
\cA chr(1) |
3501 |
|
|
\ca chr(1) |
3502 |
|
|
\cB chr(2) |
3503 |
|
|
\cb chr(2) |
3504 |
|
|
... |
3505 |
|
|
\cZ chr(26) |
3506 |
|
|
\cz chr(26) |
3507 |
|
|
\c[ chr(27) |
3508 |
|
|
# See below for chr(28) |
3509 |
|
|
\c] chr(29) |
3510 |
|
|
\c^ chr(30) |
3511 |
|
|
\c_ chr(31) |
3512 |
|
|
\c? chr(127) # (on ASCII platforms; see below for link to |
3513 |
|
|
# EBCDIC discussion) |
3514 |
|
|
|
3515 |
|
|
=begin original |
3516 |
|
|
|
3517 |
|
|
In other words, it's the character whose code point has had 64 xor'd with |
3518 |
|
|
its uppercase. C<\c?> is DELETE on ASCII platforms because |
3519 |
|
|
S<C<ord("?") ^ 64>> is 127, and |
3520 |
|
|
C<\c@> is NULL because the ord of C<"@"> is 64, so xor'ing 64 itself produces 0. |
3521 |
|
|
|
3522 |
|
|
=end original |
3523 |
|
|
|
3524 |
|
|
言い換えると、符号位置を 64 で xor して大文字にした文字です。 |
3525 |
|
|
S<C<ord("?") ^ 64>> は 127 なので C<\c?> は ASCII プラットフォームでは |
3526 |
|
|
DELETE で、C<"@"> は 64 のために |
3527 |
|
|
64 で xor すると 0 になるので C<\c@> は NUL です。 |
3528 |
|
|
|
3529 |
|
|
=begin original |
3530 |
|
|
|
3531 |
|
|
Also, C<\c\I<X>> yields S<C< chr(28) . "I<X>">> for any I<X>, but cannot come at the |
3532 |
|
|
end of a string, because the backslash would be parsed as escaping the end |
3533 |
|
|
quote. |
3534 |
|
|
|
3535 |
|
|
=end original |
3536 |
|
|
|
3537 |
|
|
また、C<\c\I<X>> は任意の I<X> について S<C< chr(28) . "I<X>">> となりますが、 |
3538 |
|
|
文字列の末尾には来ません; 逆スラッシュは末尾のクォートをエスケープするように |
3539 |
|
|
パースされるからです。 |
3540 |
|
|
|
3541 |
|
|
=begin original |
3542 |
|
|
|
3543 |
|
|
On ASCII platforms, the resulting characters from the list above are the |
3544 |
|
|
complete set of ASCII controls. This isn't the case on EBCDIC platforms; see |
3545 |
|
|
L<perlebcdic/OPERATOR DIFFERENCES> for a full discussion of the |
3546 |
|
|
differences between these for ASCII versus EBCDIC platforms. |
3547 |
|
|
|
3548 |
|
|
=end original |
3549 |
|
|
|
3550 |
|
|
ASCII プラットフォームでは、上述の一覧からの結果の文字は ASCII 制御文字の |
3551 |
|
|
完全な集合です。 |
3552 |
|
|
これは EBCDIC プラットフォームには当てはまりません; これに関する |
3553 |
|
|
ASCII プラットフォームと EBCDIC プラットフォームとの違いの完全な議論については |
3554 |
|
|
L<perlebcdic/OPERATOR DIFFERENCES> を参照してください。 |
3555 |
|
|
|
3556 |
|
|
=begin original |
3557 |
|
|
|
3558 |
|
|
Use of any other character following the C<"c"> besides those listed above is |
3559 |
|
|
discouraged, and as of Perl v5.20, the only characters actually allowed |
3560 |
|
|
are the printable ASCII ones, minus the left brace C<"{">. What happens |
3561 |
|
|
for any of the allowed other characters is that the value is derived by |
3562 |
|
|
xor'ing with the seventh bit, which is 64, and a warning raised if |
3563 |
|
|
enabled. Using the non-allowed characters generates a fatal error. |
3564 |
|
|
|
3565 |
|
|
=end original |
3566 |
|
|
|
3567 |
|
|
C<"c"> に引き続いて上述した以外の文字を使うことは非推奨であり、 |
3568 |
|
|
as of Perl v5.20, the only characters actually allowed |
3569 |
|
|
are the printable ASCII ones, minus the left brace C<"{">. |
3570 |
|
|
許されている他の文字を置いたときに起こることは、値が第 7 ビット; |
3571 |
|
|
つまり 64 で xor を取ったものになり、 |
3572 |
argrath |
1.2 |
有効の場合は警告が発生します。 |
3573 |
|
|
許されていない文字を使うと致命的エラーが発生します。 |
3574 |
argrath |
1.1 |
|
3575 |
|
|
=begin original |
3576 |
|
|
|
3577 |
|
|
To get platform independent controls, you can use C<\N{...}>. |
3578 |
|
|
|
3579 |
|
|
=end original |
3580 |
|
|
|
3581 |
|
|
プラットフォーム非依存の制御文字を得るには、C<\N{...}> を使ってください。 |
3582 |
|
|
|
3583 |
|
|
=item [6] |
3584 |
|
|
|
3585 |
|
|
=begin original |
3586 |
|
|
|
3587 |
|
|
The result is the character specified by the octal number between the braces. |
3588 |
|
|
See L</[8]> below for details on which character. |
3589 |
|
|
|
3590 |
|
|
=end original |
3591 |
|
|
|
3592 |
|
|
結果は中かっこで囲まれた 8 進数で指定された文字です。 |
3593 |
|
|
その文字に関する詳細については以下の L</[8]> を参照してください。 |
3594 |
|
|
|
3595 |
|
|
=begin original |
3596 |
|
|
|
3597 |
|
|
If a character that isn't an octal digit is encountered, a warning is raised, |
3598 |
|
|
and the value is based on the octal digits before it, discarding it and all |
3599 |
|
|
following characters up to the closing brace. It is a fatal error if there are |
3600 |
|
|
no octal digits at all. |
3601 |
|
|
|
3602 |
|
|
=end original |
3603 |
|
|
|
3604 |
|
|
8 進数でない文字に遭遇すると、警告が発生し、値はそこまでの 8 進数字を |
3605 |
|
|
基として、それ以降閉じ中かっこまでの全ての文字を捨てます。 |
3606 |
|
|
8 進数字がまったくないと致命的エラーになります。 |
3607 |
|
|
|
3608 |
|
|
=item [7] |
3609 |
|
|
|
3610 |
|
|
=begin original |
3611 |
|
|
|
3612 |
|
|
The result is the character specified by the three-digit octal number in the |
3613 |
|
|
range 000 to 777 (but best to not use above 077, see next paragraph). See |
3614 |
|
|
L</[8]> below for details on which character. |
3615 |
|
|
|
3616 |
|
|
=end original |
3617 |
|
|
|
3618 |
|
|
結果は範囲 000 から 777 までの 3 桁の 8 進数で指定される文字です |
3619 |
|
|
(しかし 077 より上は使わないのが最良です; 次の段落を参照してください)。 |
3620 |
|
|
その文字に関する詳細については以下の L</[8]> を参照してください。 |
3621 |
|
|
|
3622 |
|
|
=begin original |
3623 |
|
|
|
3624 |
|
|
Some contexts allow 2 or even 1 digit, but any usage without exactly |
3625 |
|
|
three digits, the first being a zero, may give unintended results. (For |
3626 |
|
|
example, in a regular expression it may be confused with a backreference; |
3627 |
|
|
see L<perlrebackslash/Octal escapes>.) Starting in Perl 5.14, you may |
3628 |
|
|
use C<\o{}> instead, which avoids all these problems. Otherwise, it is best to |
3629 |
|
|
use this construct only for ordinals C<\077> and below, remembering to pad to |
3630 |
|
|
the left with zeros to make three digits. For larger ordinals, either use |
3631 |
|
|
C<\o{}>, or convert to something else, such as to hex and use C<\N{U+}> |
3632 |
|
|
(which is portable between platforms with different character sets) or |
3633 |
|
|
C<\x{}> instead. |
3634 |
|
|
|
3635 |
|
|
=end original |
3636 |
|
|
|
3637 |
|
|
一部のコンテキストでは 2 桁や、1 桁ですら許されますが、正確に 3 桁かつ |
3638 |
|
|
先頭が 0、以外の使い方は意図していない結果をもたらすかもしれません。 |
3639 |
|
|
(例えば、正規表現中では後方参照で混乱するかもしれません; |
3640 |
|
|
L<perlrebackslash/Octal escapes> を参照してください。) |
3641 |
|
|
Perl 5.14 から、代わりに C<\o{}> を使えます; これはこれらすべての問題を |
3642 |
|
|
避けられます。 |
3643 |
|
|
さもなければ、この構文を値 C<\077> 以下でのみ使用するのが最良です; |
3644 |
|
|
3 桁にするために左側にゼロをパッディングするのを忘れないでください。 |
3645 |
|
|
より大きな値では、C<\o{}> を使うか、代わりに 16 進数にして |
3646 |
|
|
C<\N{U+}> |
3647 |
argrath |
1.2 |
(これは異なった文字集合のプラットフォーム間で移植性があります) を使うか、 |
3648 |
|
|
または C<\x{}> を |
3649 |
argrath |
1.1 |
使うような、他のものに変換してください。 |
3650 |
|
|
|
3651 |
|
|
=item [8] |
3652 |
|
|
|
3653 |
|
|
=begin original |
3654 |
|
|
|
3655 |
|
|
Several constructs above specify a character by a number. That number |
3656 |
|
|
gives the character's position in the character set encoding (indexed from 0). |
3657 |
|
|
This is called synonymously its ordinal, code position, or code point. Perl |
3658 |
|
|
works on platforms that have a native encoding currently of either ASCII/Latin1 |
3659 |
|
|
or EBCDIC, each of which allow specification of 256 characters. In general, if |
3660 |
|
|
the number is 255 (0xFF, 0377) or below, Perl interprets this in the platform's |
3661 |
|
|
native encoding. If the number is 256 (0x100, 0400) or above, Perl interprets |
3662 |
|
|
it as a Unicode code point and the result is the corresponding Unicode |
3663 |
|
|
character. For example C<\x{50}> and C<\o{120}> both are the number 80 in |
3664 |
|
|
decimal, which is less than 256, so the number is interpreted in the native |
3665 |
|
|
character set encoding. In ASCII the character in the 80th position (indexed |
3666 |
|
|
from 0) is the letter C<"P">, and in EBCDIC it is the ampersand symbol C<"&">. |
3667 |
|
|
C<\x{100}> and C<\o{400}> are both 256 in decimal, so the number is interpreted |
3668 |
|
|
as a Unicode code point no matter what the native encoding is. The name of the |
3669 |
|
|
character in the 256th position (indexed by 0) in Unicode is |
3670 |
|
|
C<LATIN CAPITAL LETTER A WITH MACRON>. |
3671 |
|
|
|
3672 |
|
|
=end original |
3673 |
|
|
|
3674 |
|
|
上述のいくつかの構造は文字を数値で指定しています。 |
3675 |
|
|
この値は文字集合エンコーディングで (0 から始めた) 文字の位置を指定します。 |
3676 |
|
|
これは同意語として序数(ordinal)、コード位置(code position)、 |
3677 |
|
|
符号位置(code point)と呼ばれます。 |
3678 |
|
|
Perl は現在のところ、それぞれ 256 文字を定義している ASCII/Latin1 または |
3679 |
|
|
EBCDIC のどちらかのネイティブエンコーディングを持つプラットフォームで |
3680 |
|
|
動作します。 |
3681 |
|
|
一般的に、数値が 255 (0xFF, 0377) 以下なら、Perl はこれをプラットフォームの |
3682 |
|
|
ネイティブエンコーディングと解釈します。 |
3683 |
|
|
数値が 256 (0x100, 0400) 以上なら、Perl はこれを Unicode 符号位置と解釈し、 |
3684 |
|
|
結果は対応する Unicode 文字となります。 |
3685 |
|
|
例えば C<\x{50}> と C<\o{120}> はどちらも 10 進数では 80 で、これは 256 より |
3686 |
|
|
小さいので、数値はネイティブ文字集合エンコーディングとして解釈されます。 |
3687 |
|
|
ASCII では (0 から始めて) 80 番目の位置の文字は C<"P"> で、EBCDIC では |
3688 |
|
|
アンパサンド記号 C<"&"> です。 |
3689 |
|
|
C<\x{100}> と C<\o{400}> はどちらも 10 進数では 256 なので、数値は |
3690 |
|
|
ネイティブエンコーディングが何かに関わらず Unicode 符号位置として |
3691 |
|
|
解釈されます。 |
3692 |
|
|
Unicode での (0 から始めて) 256 番目の位置の文字の名前は |
3693 |
|
|
C<LATIN CAPITAL LETTER A WITH MACRON> です。 |
3694 |
|
|
|
3695 |
|
|
=begin original |
3696 |
|
|
|
3697 |
|
|
There are a couple of exceptions to the above rule. S<C<\N{U+I<hex number>}>> is |
3698 |
|
|
always interpreted as a Unicode code point, so that C<\N{U+0050}> is C<"P"> even |
3699 |
|
|
on EBCDIC platforms. And if C<S<L<use encoding|encoding>>> is in effect, the |
3700 |
|
|
number is considered to be in that encoding, and is translated from that into |
3701 |
|
|
the platform's native encoding if there is a corresponding native character; |
3702 |
|
|
otherwise to Unicode. |
3703 |
|
|
|
3704 |
|
|
=end original |
3705 |
|
|
|
3706 |
|
|
上述の規則には二つの例外があります。 |
3707 |
|
|
S<C<\N{U+I<hex number>}>> は常に Unicode 符号位置として解釈されるので、 |
3708 |
|
|
C<\N{U+0050}> は EBCDIC プラットフォームでも C<"P"> です。 |
3709 |
|
|
そして C<S<L<use encoding|encoding>>> が有効なら、数値はその |
3710 |
|
|
エンコーディングであると考えられ、対応するネイティブな文字があるなら |
3711 |
|
|
プラットフォームのネイティブなエンコーディングに変換されます; さもなければ |
3712 |
|
|
Unicode です。 |
3713 |
|
|
|
3714 |
|
|
=back |
3715 |
|
|
|
3716 |
|
|
=begin original |
3717 |
|
|
|
3718 |
|
|
B<NOTE>: Unlike C and other languages, Perl has no C<\v> escape sequence for |
3719 |
|
|
the vertical tab (VT, which is 11 in both ASCII and EBCDIC), but you may |
3720 |
|
|
use C<\N{VT}>, C<\ck>, C<\N{U+0b}>, or C<\x0b>. (C<\v> |
3721 |
|
|
does have meaning in regular expression patterns in Perl, see L<perlre>.) |
3722 |
|
|
|
3723 |
|
|
=end original |
3724 |
|
|
|
3725 |
|
|
B<注意>: C やその他の言語と違って、Perl は垂直タブ (VT - ASCII と EBCDIC の |
3726 |
|
|
両方で 11) のための \v エスケープシーケンスはありませんが、C<\N{VT}>, C<\ck>, |
3727 |
|
|
C<\N{U+0b}>, C<\x0b> が使えます。 |
3728 |
|
|
(C<\v> は Perl の正規表現パターンでは意味があります; L<perlre> を |
3729 |
|
|
参照してください。) |
3730 |
|
|
|
3731 |
|
|
=begin original |
3732 |
|
|
|
3733 |
|
|
The following escape sequences are available in constructs that interpolate, |
3734 |
|
|
but not in transliterations. |
3735 |
|
|
X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> X<\F> |
3736 |
|
|
|
3737 |
|
|
=end original |
3738 |
|
|
|
3739 |
|
|
以下のエスケープシーケンスが展開と文字変換の構文で利用可能です。 |
3740 |
|
|
X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> |
3741 |
|
|
X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> X<\F> |
3742 |
|
|
|
3743 |
|
|
=begin original |
3744 |
|
|
|
3745 |
|
|
\l lowercase next character only |
3746 |
|
|
\u titlecase (not uppercase!) next character only |
3747 |
|
|
\L lowercase all characters till \E or end of string |
3748 |
|
|
\U uppercase all characters till \E or end of string |
3749 |
|
|
\F foldcase all characters till \E or end of string |
3750 |
|
|
\Q quote (disable) pattern metacharacters till \E or |
3751 |
|
|
end of string |
3752 |
|
|
\E end either case modification or quoted section |
3753 |
|
|
(whichever was last seen) |
3754 |
|
|
|
3755 |
|
|
=end original |
3756 |
|
|
|
3757 |
|
|
\l 次の文字だけを小文字にする |
3758 |
|
|
\u 次の文字だけをタイトル文字(大文字ではありません!)にする |
3759 |
|
|
\L \E か文字列の末尾まで小文字にする |
3760 |
|
|
\U \E か文字列の末尾まで大文字にする |
3761 |
|
|
\F \E か文字列の末尾まで畳み込み文字にする |
3762 |
|
|
\Q \E か文字列の末尾までパターンメタ文字をクォート(無効化)する |
3763 |
|
|
\E 大文字小文字変換かクォート部分(どちらか最後に現れたもの)を |
3764 |
|
|
終了させる |
3765 |
|
|
|
3766 |
|
|
=begin original |
3767 |
|
|
|
3768 |
|
|
See L<perlfunc/quotemeta> for the exact definition of characters that |
3769 |
|
|
are quoted by C<\Q>. |
3770 |
|
|
|
3771 |
|
|
=end original |
3772 |
|
|
|
3773 |
|
|
C<\Q> でクォートされる文字の正確な定義については L<perlfunc/quotemeta> を |
3774 |
|
|
参照してください。 |
3775 |
|
|
|
3776 |
|
|
=begin original |
3777 |
|
|
|
3778 |
|
|
C<\L>, C<\U>, C<\F>, and C<\Q> can stack, in which case you need one |
3779 |
|
|
C<\E> for each. For example: |
3780 |
|
|
|
3781 |
|
|
=end original |
3782 |
|
|
|
3783 |
|
|
C<\L>, C<\U>, C<\F>, C<\Q> はスタックできます; この場合それぞれに対して |
3784 |
|
|
C<\E> が必要です。 |
3785 |
|
|
例えば: |
3786 |
|
|
|
3787 |
|
|
say"This \Qquoting \ubusiness \Uhere isn't quite\E done yet,\E is it?"; |
3788 |
|
|
This quoting\ Business\ HERE\ ISN\'T\ QUITE\ done\ yet\, is it? |
3789 |
|
|
|
3790 |
|
|
=begin original |
3791 |
|
|
|
3792 |
|
|
If a S<C<use locale>> form that includes C<LC_CTYPE> is in effect (see |
3793 |
|
|
L<perllocale>), the case map used by C<\l>, C<\L>, C<\u>, and C<\U> is |
3794 |
|
|
taken from the current locale. If Unicode (for example, C<\N{}> or code |
3795 |
|
|
points of 0x100 or beyond) is being used, the case map used by C<\l>, |
3796 |
|
|
C<\L>, C<\u>, and C<\U> is as defined by Unicode. That means that |
3797 |
|
|
case-mapping a single character can sometimes produce a sequence of |
3798 |
|
|
several characters. |
3799 |
|
|
Under S<C<use locale>>, C<\F> produces the same results as C<\L> |
3800 |
|
|
for all locales but a UTF-8 one, where it instead uses the Unicode |
3801 |
|
|
definition. |
3802 |
|
|
|
3803 |
|
|
=end original |
3804 |
|
|
|
3805 |
|
|
C<LC_CTYPE> を含む S<C<use locale>> 型式が有効の場合(L<perllocale> を |
3806 |
|
|
参照して下さい)、C<\l>, C<\L>, C<\u>, C<\U> で使われる |
3807 |
|
|
大文字小文字テーブルは現在のロケールのものが使われます。 |
3808 |
|
|
(例えば、C<\N{}> や、0x100 以上の符号位置の) Unicode が |
3809 |
|
|
使われている場合、C<\l>, C<\L>, C<\u>, C<\U> で使われる大文字小文字 |
3810 |
|
|
テーブルは Unicode で定義されているものになります。 |
3811 |
|
|
これは、単一の文字の大文字小文字マッピングは複数の文字の並びを生成することが |
3812 |
|
|
あるということです。 |
3813 |
|
|
S<C<use locale>> の基では、C<\F> は、Unicode の定義が使われる UTF-8 以外の |
3814 |
|
|
全てのロケールにおいて、C<\L> と同じ結果を生成します。 |
3815 |
|
|
|
3816 |
|
|
=begin original |
3817 |
|
|
|
3818 |
|
|
All systems use the virtual C<"\n"> to represent a line terminator, |
3819 |
|
|
called a "newline". There is no such thing as an unvarying, physical |
3820 |
|
|
newline character. It is only an illusion that the operating system, |
3821 |
|
|
device drivers, C libraries, and Perl all conspire to preserve. Not all |
3822 |
|
|
systems read C<"\r"> as ASCII CR and C<"\n"> as ASCII LF. For example, |
3823 |
|
|
on the ancient Macs (pre-MacOS X) of yesteryear, these used to be reversed, |
3824 |
|
|
and on systems without a line terminator, |
3825 |
|
|
printing C<"\n"> might emit no actual data. In general, use C<"\n"> when |
3826 |
|
|
you mean a "newline" for your system, but use the literal ASCII when you |
3827 |
|
|
need an exact character. For example, most networking protocols expect |
3828 |
|
|
and prefer a CR+LF (C<"\015\012"> or C<"\cM\cJ">) for line terminators, |
3829 |
|
|
and although they often accept just C<"\012">, they seldom tolerate just |
3830 |
|
|
C<"\015">. If you get in the habit of using C<"\n"> for networking, |
3831 |
|
|
you may be burned some day. |
3832 |
|
|
X<newline> X<line terminator> X<eol> X<end of line> |
3833 |
|
|
X<\n> X<\r> X<\r\n> |
3834 |
|
|
|
3835 |
|
|
=end original |
3836 |
|
|
|
3837 |
|
|
全てのシステムでは "newline" と呼ばれる行端末子を表現するために |
3838 |
|
|
仮想的な C<"\n"> が用いられます。 |
3839 |
|
|
普遍の、物理的な "newline" 文字と言うものはありません。 |
3840 |
|
|
オペレーティングシステム、デバイスドライバ、C ライブラリ、Perl が全て |
3841 |
|
|
協力して保存しようとすると言うのは単なる幻想です。 |
3842 |
|
|
全てのシステムで C<"\r"> を ASCII CR として、また C<"\n"> を |
3843 |
|
|
ASCII LF として読み込むわけではありません。 |
3844 |
|
|
例えば昔の Mac (MacOS X 以前)ではこれらは保存され、行端末子のない |
3845 |
|
|
システムでは、C<"\n"> を print しても実際のデータは何も出力しません。 |
3846 |
|
|
一般に、システムで "newline" を意味したいときには C<"\n"> を使いますが、 |
3847 |
|
|
正確な文字が必要な場合はリテラルな ASCII を使います。 |
3848 |
|
|
例えば、ほとんどのネットワークプロトコルでは行端末子として |
3849 |
|
|
CR+LF (C<"\015\012"> または C<"\cM\cJ">) を予想し、また好みますが、 |
3850 |
|
|
しばしば C<"\012"> だけでも許容し、さらに時々は C<"\015"> だけでも認めます。 |
3851 |
|
|
もしネットワーク関係で C<"\n"> を使う習慣がついていると、 |
3852 |
|
|
いつか痛い目を見ることになるでしょう。 |
3853 |
|
|
X<newline> X<line terminator> X<eol> X<end of line> |
3854 |
|
|
X<\n> X<\r> X<\r\n> |
3855 |
|
|
|
3856 |
|
|
=begin original |
3857 |
|
|
|
3858 |
|
|
For constructs that do interpolate, variables beginning with "C<$>" |
3859 |
|
|
or "C<@>" are interpolated. Subscripted variables such as C<$a[3]> or |
3860 |
|
|
C<< $href->{key}[0] >> are also interpolated, as are array and hash slices. |
3861 |
|
|
But method calls such as C<< $obj->meth >> are not. |
3862 |
|
|
|
3863 |
|
|
=end original |
3864 |
|
|
|
3865 |
|
|
展開が行なわれる構文では、"C<$>" や "C<@>" で始まる変数が展開されます。 |
3866 |
|
|
C<$a[3]> や C<< $href->{key}[0] >> のような添え字付き変数もまた |
3867 |
|
|
配列やハッシュのスライスのように展開されます。 |
3868 |
|
|
しかし、C<< $obj->meth >> のようなメソッド呼び出しは展開されません。 |
3869 |
|
|
|
3870 |
|
|
=begin original |
3871 |
|
|
|
3872 |
|
|
Interpolating an array or slice interpolates the elements in order, |
3873 |
|
|
separated by the value of C<$">, so is equivalent to interpolating |
3874 |
|
|
S<C<join $", @array>>. "Punctuation" arrays such as C<@*> are usually |
3875 |
|
|
interpolated only if the name is enclosed in braces C<@{*}>, but the |
3876 |
|
|
arrays C<@_>, C<@+>, and C<@-> are interpolated even without braces. |
3877 |
|
|
|
3878 |
|
|
=end original |
3879 |
|
|
|
3880 |
|
|
配列やスライスの展開は、要素を順番に、C<$"> の値で分割して展開されるので、 |
3881 |
|
|
S<C<join $", @array>> の展開と等価です。 |
3882 |
|
|
C<@*> のような「句読点」配列は普通は名前が C<@{*}> のように中かっこで |
3883 |
|
|
囲われている場合にのみ展開されますが、配列 C<@_>, C<@+>, C<@-> は |
3884 |
|
|
中かっこなしでも展開されます。 |
3885 |
|
|
|
3886 |
|
|
=begin original |
3887 |
|
|
|
3888 |
|
|
For double-quoted strings, the quoting from C<\Q> is applied after |
3889 |
|
|