/
perlop.pod
8762 lines (6260 loc) · 322 KB
/
perlop.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=encoding utf8
=head1 NAME
X<operator>
=begin original
perlop - Perl operators and precedence
=end original
perlop - Perl の演算子と優先順位
=head1 DESCRIPTION
=begin original
In Perl, the operator determines what operation is performed,
independent of the type of the operands. For example S<C<$x + $y>>
is always a numeric addition, and if C<$x> or C<$y> do not contain
numbers, an attempt is made to convert them to numbers first.
=end original
Perl では、演算子はどんな演算を行うかをオペランドの型と独立して決定します。
例えば S<C<$x + $y>> は常に数値加算で、C<$x> や C<$y> に数値でないものが
含まれている場合、まずそれらを数値に変換しようとします。
=begin original
This is in contrast to many other dynamic languages, where the
operation is determined by the type of the first argument. It also
means that Perl has two versions of some operators, one for numeric
and one for string comparison. For example S<C<$x == $y>> compares
two numbers for equality, and S<C<$x eq $y>> compares two strings.
=end original
これは、最初の引数の型によって演算が決定されるその他の多くの動的言語と
対照的です。
これはまた、数値比較用と文字列比較用の 2 種類の演算子があるということです。
例えば S<C<$x == $y>> は二つの数値の等価性を比較し、S<C<$x eq $y>> は二つの
文字列を比較します。
=begin original
There are a few exceptions though: C<x> can be either string
repetition or list repetition, depending on the type of the left
operand, and C<&>, C<|>, C<^> and C<~> can be either string or numeric bit
operations.
=end original
しかし、いくつかの例外があります: C<x> は左オペランドの型によって、
文字列の繰り返しの場合とリストの繰り返しの場合があります; また
C<&>, C<|>, C<^>, C<~> は文字列としてのビット演算と数値としてのビット演算の
場合があります。
=head2 Operator Precedence and Associativity
X<operator, precedence> X<precedence> X<associativity>
(演算子の優先順位と結合性)
=begin original
Operator precedence and associativity work in Perl more or less like
they do in mathematics.
=end original
Perl での演算子の優先順位と結合性は多かれ少なかれ数学のものと似ています。
=begin original
I<Operator precedence> means some operators group more tightly than others.
For example, in C<2 + 4 * 5>, the multiplication has higher precedence, so C<4
* 5> is grouped together as the right-hand operand of the addition, rather
than C<2 + 4> being grouped together as the left-hand operand of the
multiplication. It is as if the expression were written C<2 + (4 * 5)>, not
C<(2 + 4) * 5>. So the expression yields C<2 + 20 == 22>, rather than
C<6 * 5 == 30>.
=end original
I<演算子の優先順位> とは、他の演算子グループよりもしっかりと
結びついている演算子グループがあるということです。
例えば、C<2 + 4 * 5> の場合、乗算が高い優先順位を持っているので、
C<2 + 4> が乗算の左オペランドとしてまとめられるのではなく、
C<4 * 5> が加法の右オペランドとしてまとめられます。
これは、C<(2 + 4) * 5> ではなく、C<2 + (4 * 5)> と
書かれたかのようなものです。
従って、この式は C<6 * 5 == 30> ではなく C<2 + 20 == 22> になります。
=begin original
I<Operator associativity> defines what happens if a sequence of the same
operators is used one after another:
usually that they will be grouped at the left
or the right. For example, in C<9 - 3 - 2>, subtraction is left associative,
so C<9 - 3> is grouped together as the left-hand operand of the second
subtraction, rather than C<3 - 2> being grouped together as the right-hand
operand of the first subtraction. It is as if the expression were written
C<(9 - 3) - 2>, not C<9 - (3 - 2)>. So the expression yields C<6 - 2 == 4>,
rather than C<9 - 1 == 8>.
=end original
I<演算子の結合性> は、同じ演算子が連続して現れた場合に何が起こるかを
定義します: 通常はそれらが左側と結びつくか右側と結びつくかです。
例えば C<9 - 3 - 2>で、減法は左結合なので、
C<3 - 2> が最初の減法の右オペランドとしてまとめられるのではなく、
C<9 - 3> が 2 番目の減法の左オペランドとしてまとめられます。
これは、C<9 - (3 - 2)> ではなく C<(9 - 3) - 2> と書かれたかのようなものです。
従って、この式は C<9 - 1 == 8> ではなく C<6 - 2 == 4> になります。
=begin original
For simple operators that evaluate all their operands and then combine the
values in some way, precedence and associativity (and parentheses) imply some
ordering requirements on those combining operations. For example, in C<2 + 4 *
5>, the grouping implied by precedence means that the multiplication of 4 and
5 must be performed before the addition of 2 and 20, simply because the result
of that multiplication is required as one of the operands of the addition. But
the order of operations is not fully determined by this: in C<2 * 2 + 4 * 5>
both multiplications must be performed before the addition, but the grouping
does not say anything about the order in which the two multiplications are
performed. In fact Perl has a general rule that the operands of an operator
are evaluated in left-to-right order. A few operators such as C<&&=> have
special evaluation rules that can result in an operand not being evaluated at
all; in general, the top-level operator in an expression has control of
operand evaluation.
=end original
全てのオペランドを評価してから何らかの形で値を結合する
単純な演算子の場合、優先順位と結合性(とかっこ)はそれらの結合操作で
ある種の順序要求を暗示します。
例えば C<2 + 4 * 5> では、
優先順位が暗示するグループ化によって、
4 と 5 の乗算は
2 と 20 の加算の前に行われる必要があります;
単にこの乗法の結果が加法のオペランドの一つとして必要だからです。
しかし、演算の順序はこれによって完全に決定されるわけではありません:
C<2 * 2 + 4 * 5> では、両方の乗算は加算の前に行われなければなりませんが、
グループ化は二つの乗算が行われる順序については何も言っていません。
実際の所、Perl には、演算子のオペランドは左から右の順で
評価されるという一般的な規則があります。
C<&&=> のようないくつかの演算子は、
全く評価されないオペランドとなる特別な評価規則を持ちます;
一般的に、式の中の最上位の演算子がオペランド評価を制御します。
=begin original
Some comparison operators, as their associativity, I<chain> with some
operators of the same precedence (but never with operators of different
precedence). This chaining means that each comparison is performed
on the two arguments surrounding it, with each interior argument taking
part in two comparisons, and the comparison results are implicitly ANDed.
Thus S<C<"$x E<lt> $y E<lt>= $z">> behaves exactly like S<C<"$x E<lt>
$y && $y E<lt>= $z">>, assuming that C<"$y"> is as simple a scalar as
it looks. The ANDing short-circuits just like C<"&&"> does, stopping
the sequence of comparisons as soon as one yields false.
=end original
一部の比較演算子は、それらの優先順位と、
同じ優先順位を持つ一部の演算子と I<連鎖> させることができます
(異なる優先順位を持つ演算子とはできません)。
連鎖というのは、それぞれの比較はそれらの周りの二つの引数に対して
行われ、それぞれの内部引数は二つの比較の一部となり、比較結果は
暗黙に AND されるということです。
従って S<C<"$x E<lt> $y E<lt>= $z">> は、C<"$y"> が見た目単純な
スカラだと仮定すると、
S<C<"$x E<lt> $y && $y E<lt>= $z">> と正確に同じように振る舞います。
AND の短絡は C<"&&"> と同様に行われ、一つが偽となった時点で一連の連鎖は
停止します。
=begin original
In a chained comparison, each argument expression is evaluated at most
once, even if it takes part in two comparisons, but the result of the
evaluation is fetched for each comparison. (It is not evaluated
at all if the short-circuiting means that it's not required for any
comparisons.) This matters if the computation of an interior argument
is expensive or non-deterministic. For example,
=end original
連鎖比較において、それぞれの引数式は、例え二つの比較の一部となったとしても、
評価されるのは最大 1 回ですが、評価の結果はそれぞれの比較毎に取得されます。
(短絡によって比較が必要でない場合は、まったく評価されません。)
これは、内側の引数の計算が高価だったり非決定的だったりする場合に
問題になります。
例えば:
if($x < expensive_sub() <= $z) { ...
=begin original
is not entirely like
=end original
これは全体的に次のようなものではなく:
if($x < expensive_sub() && expensive_sub() <= $z) { ...
=begin original
but instead closer to
=end original
しかし次のものに近いです:
my $tmp = expensive_sub();
if($x < $tmp && $tmp <= $z) { ...
=begin original
in that the subroutine is only called once. However, it's not exactly
like this latter code either, because the chained comparison doesn't
actually involve any temporary variable (named or otherwise): there is
no assignment. This doesn't make much difference where the expression
is a call to an ordinary subroutine, but matters more with an lvalue
subroutine, or if the argument expression yields some unusual kind of
scalar by other means. For example, if the argument expression yields
a tied scalar, then the expression is evaluated to produce that scalar
at most once, but the value of that scalar may be fetched up to twice,
once for each comparison in which it is actually used.
=end original
ここでサブルーチンは 1 回だけ呼び出されます。
しかし、正確に後者のコードのようなものでもありません; なぜなら
連鎖比較は実際には(名前付きかどうかにかかわらず)一時変数を使わないからです;
代入はありません。
これは、式が通常のサブルーチンとして呼び出されるときにはあまり違いは
ありませんが、左辺値サブルーチンの呼び出しの場合や、引数式が
他の手段によって何らかの普通でない種類のスカラになった場合には
より問題になります。
例えば、引数式が tie されたスカラになった場合、式はスカラを作るために
最大 1 回評価されますが、スカラの値は、実際に使われる比較毎に 1 回、
合計 2 回読み込まれます。
=begin original
In this example, the expression is evaluated only once, and the tied
scalar (the result of the expression) is fetched for each comparison that
uses it.
=end original
この例で、式は 1 回だけ評価され、tie されたスカラ (式の結果) は、
これら使われる比較毎に取得されます。
if ($x < $tied_scalar < $z) { ...
=begin original
In the next example, the expression is evaluated only once, and the tied
scalar is fetched once as part of the operation within the expression.
The result of that operation is fetched for each comparison, which
normally doesn't matter unless that expression result is also magical due
to operator overloading.
=end original
次の例では、式は 1 回だけ評価され、 tie されたスカラは式の中の演算の
一部として 1 回だけ取得されます。
この演算の結果は比較毎に取得されます;
通常これは、式の結果も演算子オーバーロードによってマジカルでない限り、
関係ありません。
if ($x < $tied_scalar + 42 < $z) { ...
=begin original
Some operators are instead non-associative, meaning that it is a syntax
error to use a sequence of those operators of the same precedence.
For example, S<C<"$x .. $y .. $z">> is an error.
=end original
一部の演算子は非結合です; 同じ優先順位の演算子の並びを使うと
文法エラーになります。
例えば、S<C<"$x .. $y .. $z">> はエラーです。
=begin original
Perl operators have the following associativity and precedence,
listed from highest precedence to lowest. Operators borrowed from
C keep the same precedence relationship with each other, even where
C's precedence is slightly screwy. (This makes learning Perl easier
for C folks.) With very few exceptions, these all operate on scalar
values only, not array values.
=end original
Perl の演算子には、以下のような結合性と優先順位 (高い優先順位から
低いものへ並べている) があります。
C から持ってきた演算子の優先順位は、C での優先順位が多少おかしくても、
そのままにしてあります。
(これによって、C を使っている方が Perl に移りやすくなっています。)
ごく僅かな例外を別として、全ての演算子はスカラ値のみを持ち、
配列値を持ちません。
=begin original
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ ~. \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc isa
chained < > <= >= lt gt le ge
chain/na == != eq ne <=> cmp ~~
left & &.
left | |. ^ ^.
left &&
left || //
nonassoc .. ...
right ?:
right = += -= *= etc. goto last next redo dump
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor
=end original
左結合 項 リスト演算子 (左方向に対して)
左結合 ->
非結合 ++ --
右結合 **
右結合 ! ~ ~. \ 単項の+ 単項の-
左結合 =~ !~
左結合 * / % x
左結合 + - .
左結合 << >>
非結合 名前付き単項演算子
連鎖 < > <= >= lt gt le ge
連鎖/なし == != eq ne <=> cmp ~~
非結合 isa
左結合 & &.
左結合 | |. ^ ^.
左結合 &&
左結合 || //
非結合 .. ...
右結合 ?:
右結合 = += -= *= など; goto last next redo dump
左結合 , =>
非結合 リスト演算子 (右方向に対して)
右結合 not
左結合 and
左結合 or xor
=begin original
In the following sections, these operators are covered in detail, in the
same order in which they appear in the table above.
=end original
以下の章では、前述の表の順に、これらの演算子に関して詳述します。
=begin original
Many operators can be overloaded for objects. See L<overload>.
=end original
多くの演算子はオブジェクトでオーバーロードできます。
L<overload> を参照して下さい。
=head2 Terms and List Operators (Leftward)
X<list operator> X<operator, list> X<term>
(項とリスト演算子 (左方向))
=begin original
A TERM has the highest precedence in Perl. They include variables,
quote and quote-like operators, any expression in parentheses,
and any function whose arguments are parenthesized. Actually, there
aren't really functions in this sense, just list operators and unary
operators behaving as functions because you put parentheses around
the arguments. These are all documented in L<perlfunc>.
=end original
「項」は Perl でもっとも優先順位が高いものです。
これには、変数、クォートとクォート的な演算子、括弧で括った任意の式、
引数を括弧で括った任意の関数が含まれます。
実際には、この意味では本当の関数はなく、リスト演算子と関数のように働く
単項演算子が、引数を括弧で括るためそのように見えます。
これらはすべて L<perlfunc> に記述しています。
=begin original
If any list operator (C<print()>, etc.) or any unary operator (C<chdir()>, etc.)
is followed by a left parenthesis as the next token, the operator and
arguments within parentheses are taken to be of highest precedence,
just like a normal function call.
=end original
リスト演算子 (C<print()> など) や単項演算子 (C<chdir()> など) は、
すべて次のトークンとして開き括弧が続くと、その演算子と括弧内の引数は、
通常の関数呼び出しのようにもっとも高い優先順位として扱われます。
=begin original
In the absence of parentheses, the precedence of list operators such as
C<print>, C<sort>, or C<chmod> is either very high or very low depending on
whether you are looking at the left side or the right side of the operator.
For example, in
=end original
括弧が無い場合には、C<print>、C<sort>、C<chmod> のようなリスト演算子の
優先順位は、演算子の左側をからすると非常に高く、右側からすると
非常に低く見えます。たとえば、
@ary = (1, 3, sort 4, 2);
print @ary; # prints 1324
=begin original
the commas on the right of the C<sort> are evaluated before the C<sort>,
but the commas on the left are evaluated after. In other words,
list operators tend to gobble up all arguments that follow, and
then act like a simple TERM with regard to the preceding expression.
Be careful with parentheses:
=end original
では、C<sort> の右のコンマは C<sort> よりも前に評価されますが、左側のコンマは
後から評価されます。
言い方を変えると、リスト演算子は自分の後にある引数をすべて使って処理を行ない、
その結果を自分の前の式に対する「項」であるかのように見せるということです。
ただし、括弧には気を付けないといけません:
=begin original
# These evaluate exit before doing the print:
print($foo, exit); # Obviously not what you want.
print $foo, exit; # Nor is this.
=end original
# 以下は print を行なう前に exit を評価します:
print($foo, exit); # 明らかにやりたいことではないでしょう。
print $foo, exit; # これでもない。
=begin original
# These do the print before evaluating exit:
(print $foo), exit; # This is what you want.
print($foo), exit; # Or this.
print ($foo), exit; # Or even this.
=end original
# 以下は exit を評価する前に print を行ないます:
(print $foo), exit; # これがしたかった。
print($foo), exit; # これでもいい。
print ($foo), exit; # これも OK。
=begin original
Also note that
=end original
また、
print ($foo & 255) + 1, "\n";
=begin original
probably doesn't do what you expect at first glance. The parentheses
enclose the argument list for C<print> which is evaluated (printing
the result of S<C<$foo & 255>>). Then one is added to the return value
of C<print> (usually 1). The result is something like this:
=end original
の動作を一目見ただけで判断するのは、難しいでしょう。
かっこは C<print> のために評価される引数リストを囲っています
(S<C<$foo & 255>> の結果が表示されます)。
それから C<print> の返り値 (通常は 1) に 1 が加えられます。
結果は以下のようになります:
=begin original
1 + 1, "\n"; # Obviously not what you meant.
=end original
1 + 1, "\n"; # 明らかにやりたいことではないでしょう。
=begin original
To do what you meant properly, you must write:
=end original
意味したいことを適切に行うには、以下のように書く必要があります:
print(($foo & 255) + 1, "\n");
=begin original
See L</Named Unary Operators> for more discussion of this.
=end original
詳しくは、L</Named Unary Operators> を参照してください。
=begin original
Also parsed as terms are the S<C<do {}>> and S<C<eval {}>> constructs, as
well as subroutine and method calls, and the anonymous
constructors C<[]> and C<{}>.
=end original
この他に「項」として解析されるものには、S<C<do {}>> や S<C<eval {}>> の
構成、サブルーティンやメソッドの呼び出し、無名のコンストラクタ
C<[]> と C<{}> があります。
=begin original
See also L</Quote and Quote-like Operators> toward the end of this section,
as well as L</"I/O Operators">.
=end original
後の方の L</Quote and Quote-like Operators> や
L</"I/O Operators"> も参照してください。
=head2 The Arrow Operator
X<arrow> X<dereference> X<< -> >>
(矢印演算子)
=begin original
"C<< -> >>" is an infix dereference operator, just as it is in C
and C++. If the right side is either a C<[...]>, C<{...}>, or a
C<(...)> subscript, then the left side must be either a hard or
symbolic reference to an array, a hash, or a subroutine respectively.
(Or technically speaking, a location capable of holding a hard
reference, if it's an array or hash reference being used for
assignment.) See L<perlreftut> and L<perlref>.
=end original
C や C++ と同じように "C<< -> >>" は中置の被参照演算子です。
右側が C<[...]>, C<{...}>, C<(...)> のいずれかの形の添字であれば、左側は配列、
ハッシュ、サブルーチンへのハードリファレンスか
シンボリックリファレンスでなければなりません。
(あるいは技術的には、配列またはハードリファレンスが代入可能であれば
ハードリファレンスを保持できる場所です。)
L<perlreftut> と L<perlref> を参照してください。
=begin original
Otherwise, the right side is a method name or a simple scalar
variable containing either the method name or a subroutine reference,
and (if it is a method name) the left side must be either an object (a
blessed reference) or a class name (that is, a package name). See
L<perlobj>.
=end original
そうでなければ、右側はメソッド名かサブルーチンのリファレンスを持った
単純スカラ変数で、
(メソッド名なら) 左側はオブジェクト (bless されたリファレンス) か
クラス名でなければなりません。
L<perlobj> を参照してください。
=begin original
The dereferencing cases (as opposed to method-calling cases) are
somewhat extended by the C<postderef> feature. For the
details of that feature, consult L<perlref/Postfix Dereference Syntax>.
=end original
(メソッド呼び出しの場合ではなく) デリファレンスの場合、
C<後置デリファレンス> (postderef) 機能によっていくらか拡張されます。
この機能の詳細については、L<perlref/Postfix Dereference Syntax> を
参照してください。
=head2 Auto-increment and Auto-decrement
X<increment> X<auto-increment> X<++> X<decrement> X<auto-decrement> X<-->
(インクリメントとデクリメント)
=begin original
C<"++"> and C<"--"> work as in C. That is, if placed before a variable,
they increment or decrement the variable by one before returning the
value, and if placed after, increment or decrement after returning the
value.
=end original
C<"++"> と C<"--"> は、C の場合と同じように動作します。
つまり、変数の前に置かれれば、値を返す前に変数を 1 インクリメントまたは
デクリメントし、後に置かれれば、値を返した後で変数を
インクリメントまたはデクリメントします。
$i = 0; $j = 0;
print $i++; # prints 0
print ++$j; # prints 1
=begin original
Note that just as in C, Perl doesn't define B<when> the variable is
incremented or decremented. You just know it will be done sometime
before or after the value is returned. This also means that modifying
a variable twice in the same statement will lead to undefined behavior.
Avoid statements like:
=end original
C と同様、Perl は B<いつ> 変数がインクリメントまたはデクリメントされるかは
定義されません。
値が返される前か後のどこかで行われる、ということだけがわかります。
これは、同じ文である変数を 2 回修正すると、振る舞いが未定義になることを
意味します。
以下のような文は避けてください:
$i = $i ++;
print ++ $i + $i ++;
=begin original
Perl will not guarantee what the result of the above statements is.
=end original
Perl は上記の文の結果について保障しません。
=begin original
The auto-increment operator has a little extra builtin magic to it. If
you increment a variable that is numeric, or that has ever been used in
a numeric context, you get a normal increment. If, however, the
variable has been used in only string contexts since it was set, and
has a value that is not the empty string and matches the pattern
C</^[a-zA-Z]*[0-9]*\z/>, the increment is done as a string, preserving each
character within its range, with carry:
=end original
インクリメント演算子には、ちょっと風変わりな機能が組み込まれています。
数値が入った変数や、数値の文脈で使われてきた変数を
インクリメントする場合には、通常のインクリメントとして動作します。
しかし、その変数が設定されてからずっと文字列の文脈でしか使われていなくて、
空文字列でなく、 C</^[a-zA-Z]*[0-9]*\z/> にマッチする値を持っているときには、
個々の文字の範囲を保ちながら桁あげを行なって、文字列としてインクリメントが
行なわれます (マジカルインクリメントと呼ばれます):
print ++($foo = "99"); # prints "100"
print ++($foo = "a0"); # prints "a1"
print ++($foo = "Az"); # prints "Ba"
print ++($foo = "zz"); # prints "aaa"
=begin original
C<undef> is always treated as numeric, and in particular is changed
to C<0> before incrementing (so that a post-increment of an undef value
will return C<0> rather than C<undef>).
=end original
C<undef> は常に数値として扱われ、特にインクリメントされる前には C<0> に
変換されます(従って、undef のポストインクリメント値は C<undef> ではなく
C<0> になります)。
=begin original
The auto-decrement operator is not magical.
=end original
デクリメント演算子には、マジカルなものはありません。
=head2 Exponentiation
X<**> X<exponentiation> X<power>
(指数演算子)
=begin original
Binary C<"**"> is the exponentiation operator. It binds even more
tightly than unary minus, so C<-2**4> is C<-(2**4)>, not C<(-2)**4>.
(This is
implemented using C's C<pow(3)> function, which actually works on doubles
internally.)
=end original
二項演算子の C<"**"> は指数演算子です。
この演算子は、単項のマイナスよりも結合が強い演算子で、
C<-2**4> は C<(-2)**4> ではなく、C<-(2**4)> と解釈されます。
(これは C の C<pow(3)> を使って実装されていますので、
内部的には double で動作します。)
=begin original
Note that certain exponentiation expressions are ill-defined:
these include C<0**0>, C<1**Inf>, and C<Inf**0>. Do not expect
any particular results from these special cases, the results
are platform-dependent.
=end original
一部の指数表現は明確に定義されていません:
これは C<0**0>, C<1**Inf>, C<Inf**0> などです。
これらの特殊な場合に関して特定の結果を想定しないでください;
結果はプラットフォーム依存です。
=head2 Symbolic Unary Operators
X<unary operator> X<operator, unary>
(単項演算子)
=begin original
Unary C<"!"> performs logical negation, that is, "not". See also
L<C<not>|/Logical Not> for a lower precedence version of this.
X<!>
=end original
単項演算子の C<"!"> は論理否定を行ないます; つまり「not」ということです。
この演算子の優先順位を低くしたものとして、
L<C<not>|/Logical Not> が用意されています。
X<!>
=begin original
Unary C<"-"> performs arithmetic negation if the operand is numeric,
including any string that looks like a number. If the operand is
an identifier, a string consisting of a minus sign concatenated
with the identifier is returned. Otherwise, if the string starts
with a plus or minus, a string starting with the opposite sign is
returned. One effect of these rules is that C<-bareword> is equivalent
to the string C<"-bareword">. If, however, the string begins with a
non-alphabetic character (excluding C<"+"> or C<"-">), Perl will attempt
to convert
the string to a numeric, and the arithmetic negation is performed. If the
string cannot be cleanly converted to a numeric, Perl will give the warning
B<Argument "the string" isn't numeric in negation (-) at ...>.
X<-> X<negation, arithmetic>
=end original
単項演算子の C<"-"> は被演算子が数値または数値に見える文字列であれば、
算術否定を行ないます。
被演算子が識別子ならば、マイナス記号にその識別子をつなげた文字列が返されます。
これ以外で被演算子の最初の文字がプラスかマイナスのときには、
その記号を逆のものに置き換えた文字列を返します。
この規則の結果、C<-bareword> が文字列 C<"-bareword"> に等価となります。
しかし、文字列が英字以外(C<"+"> と C<"-"> を除く)で始まっていると、
Perl は文字列を数値に変換しようとし、それから算術否定が実行されます。
もし文字列が明確に数値に変換できない場合、Perl は
B<Argument "the string" isn't numeric in negation (-) at ...> という
警告を出します。
X<-> X<negation, arithmetic>
=begin original
Unary C<"~"> performs bitwise negation, that is, 1's complement. For
example, S<C<0666 & ~027>> is 0640. (See also L</Integer Arithmetic> and
L</Bitwise String Operators>.) Note that the width of the result is
platform-dependent: C<~0> is 32 bits wide on a 32-bit platform, but 64
bits wide on a 64-bit platform, so if you are expecting a certain bit
width, remember to use the C<"&"> operator to mask off the excess bits.
X<~> X<negation, binary>
=end original
単項演算子の C<"~"> はビットごとの否定を行ないます; つまり、1 の補数を返します。
例えば、S<C<0666 & ~027>> は 0640 です。
(L</Integer Arithmetic> と L</Bitwise String Operators> も参照して下さい。)
結果の幅はプラットホーム依存であることに注意してください: C<~0> は
32 ビットプラットホームでは 32 ビット幅ですが、64 ビットプラットホームでは
64 ビット幅なので、特定のビット幅を仮定する場合は、
余分なビットをマスクするために C<"&"> 演算子を使うことを忘れないでください。
X<~> X<negation, binary>
=begin original
Starting in Perl 5.28, it is a fatal error to try to complement a string
containing a character with an ordinal value above 255.
=end original
Perl 5.28 から、値が 255 を超える文字を含む文字列の
補数を求めようとすると致命的エラーになります。
=begin original
If the "bitwise" feature is enabled via S<C<use
feature 'bitwise'>> or C<use v5.28>, then unary
C<"~"> always treats its argument as a number, and an
alternate form of the operator, C<"~.">, always treats its argument as a
string. So C<~0> and C<~"0"> will both give 2**32-1 on 32-bit platforms,
whereas C<~.0> and C<~."0"> will both yield C<"\xff">. Until Perl 5.28,
this feature produced a warning in the C<"experimental::bitwise"> category.
=end original
"bitwise" 機能が S<C<use feature 'bitwise'>> か C<use v5.28> で有効にされると、
単項の C<"~"> は常にその引数を数値として扱い、その代替形式である
C<"~."> はその引数を常に文字列として扱います。
従って 32 ビットプラットフォームでは C<~0> と C<~"0"> はどちらも
2**32-1 を意味しますが、C<~.0> と C<~."0"> はどちらも C<"\xff"> になります。
Perl 5.28 まで、この機能は C<"experimental::bitwise"> カテゴリの警告を
発生させていました。
=begin original
Unary C<"+"> has no effect whatsoever, even on strings. It is useful
syntactically for separating a function name from a parenthesized expression
that would otherwise be interpreted as the complete list of function
arguments. (See examples above under L</Terms and List Operators (Leftward)>.)
X<+>
=end original
単項演算子の C<"+"> は、たとえ文字列に対して用いられた場合にも、何もしません。
関数名に続けて括弧付きの式を書く場合に、関数の引数リストと
解釈されないようにするために用いることができます。
(下記 L</Terms and List Operators (Leftward)> の例を参照してください。)
X<+>
=begin original
Unary C<"\"> creates references. If its operand is a single sigilled
thing, it creates a reference to that object. If its operand is a
parenthesised list, then it creates references to the things mentioned
in the list. Otherwise it puts its operand in list context, and creates
a list of references to the scalars in the list provided by the operand.
See L<perlreftut>
and L<perlref>. Do not confuse this behavior with the behavior of
backslash within a string, although both forms do convey the notion
of protecting the next thing from interpolation.
X<\> X<reference> X<backslash>
=end original
単項演算子の C<"\"> はリファレンスを生成します。
オペランドが一つの印付きのものの場合、それへのリファレンスを作ります。
オペランドがかっこでくくられたリストの場合、
リストで言及されているものへのリファレンスを作ります。
さもなければオペランドをリストコンテキストとし、
オペランドによって提供されたリストのスカラへのリファレンスのリストを作ります。
L<perlreftut> と L<perlref> を参照してください。
この用法も文字列中のバックスラッシュも、後に続くものが展開されるのを
防ぐことになりますが、動作を混同しないでください。
X<\> X<reference> X<backslash>
=head2 Binding Operators
X<binding> X<operator, binding> X<=~> X<!~>
(拘束演算子)
=begin original
Binary C<"=~"> binds a scalar expression to a pattern match. Certain operations
search or modify the string C<$_> by default. This operator makes that kind
of operation work on some other string. The right argument is a search
pattern, substitution, or transliteration. The left argument is what is
supposed to be searched, substituted, or transliterated instead of the default
C<$_>. When used in scalar context, the return value generally indicates the
success of the operation. The exceptions are substitution (C<s///>)
and transliteration (C<y///>) with the C</r> (non-destructive) option,
which cause the B<r>eturn value to be the result of the substitution.
Behavior in list context depends on the particular operator.
See L</"Regexp Quote-Like Operators"> for details and L<perlretut> for
examples using these operators.
=end original
二項演算子の C<"=~"> は、スカラ式をパターンマッチに拘束します。
デフォルトで C<$_> の文字列を検索したり、変更したりする演算があります。
この演算子は、そのような演算を他の文字列に対して行なわせるようにするものです。
右引数は、検索パターン、置換、文字変換のいずれかです。
左引数は、デフォルトの C<$_> の代わりに検索、置換、文字変換の
対象となるものです。
スカラコンテキストで使うと、返り値は一般的に演算の結果が成功したか否かです。
例外は、C</r> (非破壊) オプション付きの置換 (C<s///>) と
文字変換 (C<y///>) です; この場合は変換した結果を返します。
リストコンテキストでの振る舞いは演算子に依存します。
詳しくは L</"Regexp Quote-Like Operators"> を、これらの演算子を使った
例については L<perlretut> を参照して下さい。
=begin original
If the right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search pattern at run
time. Note that this means that its
contents will be interpolated twice, so
=end original
右引数が検索パターン、置換、文字変換ではなく、式であれば、
それは実行時に決まる検索パターンと解釈されます。
これは、内容が 2 回展開されることを意味することに注意してください;
つまり:
'\\' =~ q'\\';
=begin original
is not ok, as the regex engine will end up trying to compile the
pattern C<\>, which it will consider a syntax error.
=end original
は正しくありません; 正規表現エンジンは最終的にパターン C<\> を
コンパイルしようとして、これは文法エラーと考えるからです。
=begin original
Binary C<"!~"> is just like C<"=~"> except the return value is negated in
the logical sense.
=end original
二項演算子の C<"!~"> は、返される値が論理否定されることを除いて
C<"=~"> と同じです。
=begin original
Binary C<"!~"> with a non-destructive substitution (C<s///r>) or transliteration
(C<y///r>) is a syntax error.
=end original
二項演算子の C<"!~"> を非破壊置換 (C<s///r>) や変換 (C<y///r>) で使うと
文法エラーとなります。
=head2 Multiplicative Operators
X<operator, multiplicative>
(乗法演算子)
=begin original
Binary C<"*"> multiplies two numbers.
X<*>
=end original
二項演算子の C<"*"> は 2 つの数値の積を返します。
X<*>
=begin original
Binary C<"/"> divides two numbers.
X</> X<slash>
=end original
二項演算子の C<"/"> は 2 つの数値の商を返します。
X</> X<slash>
=begin original
Binary C<"%"> is the modulo operator, which computes the division
remainder of its first argument with respect to its second argument.
Given integer
operands C<$m> and C<$n>: If C<$n> is positive, then S<C<$m % $n>> is
C<$m> minus the largest multiple of C<$n> less than or equal to
C<$m>. If C<$n> is negative, then S<C<$m % $n>> is C<$m> minus the
smallest multiple of C<$n> that is not less than C<$m> (that is, the
result will be less than or equal to zero). If the operands
C<$m> and C<$n> are floating point values and the absolute value of
C<$n> (that is C<abs($n)>) is less than S<C<(UV_MAX + 1)>>, only
the integer portion of C<$m> and C<$n> will be used in the operation
(Note: here C<UV_MAX> means the maximum of the unsigned integer type).
If the absolute value of the right operand (C<abs($n)>) is greater than
or equal to S<C<(UV_MAX + 1)>>, C<"%"> computes the floating-point remainder
C<$r> in the equation S<C<($r = $m - $i*$n)>> where C<$i> is a certain
integer that makes C<$r> have the same sign as the right operand