/
perlop.pod
7714 lines (5483 loc) · 243 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 euc-jp
=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 are evaluated before
others. For example, in S<C<2 + 4 * 5>>, the multiplication has higher
precedence so S<C<4 * 5>> is evaluated first yielding S<C<2 + 20 ==
22>> and not S<C<6 * 5 == 30>>.
=end original
I<演算子の優先順位> とは、他の演算子より先に評価される演算子があると
いうことです。
例えば、S<C<2 + 4 * 5>> の場合、乗算が高い優先順位を持っているので、
S<C<4 * 5>> が先に評価され、結果は S<C<6 * 5 == 30>> ではなく、
S<C<2 + 20 == 22>> となります。
=begin original
I<Operator associativity> defines what happens if a sequence of the
same operators is used one after another: whether the evaluator will
evaluate the left operations first, or the right first. For example, in
S<C<8 - 4 - 2>>, subtraction is left associative so Perl evaluates the
expression left to right. S<C<8 - 4>> is evaluated first making the
expression S<C<4 - 2 == 2>> and not S<C<8 - 2 == 6>>.
=end original
I<演算子の結合性> は、同じ演算子が連続して現れた場合に何が起こるかを
定義します: 評価器が左側を先に評価するか右側を先に評価するかです。
例えば、S<C<8 - 4 - 2>> の場合、減算は左結合なので、Perl は式を左から右に
評価します。
S<C<8 - 4>> が先に評価されるので、S<C<8 - 2 == 6>> ではなく
S<C<4 - 2 == 2>> となります。
=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 < > <= >= lt gt le ge
nonassoc == != <=> 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 ~~
左結合 &
左結合 | ^
左結合 &&
左結合 || //
非結合 .. ...
右結合 ?:
右結合 = += -= *= など; goto last next redo dump
左結合 , =>
非結合 リスト演算子 (右方向に対して)
右結合 not
左結合 and
左結合 or xor
=begin original
In the following sections, these operators are covered in precedence order.
=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 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 experimental 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 C<not> for a lower
precedence version of this.
X<!>
=end original
単項演算子の C<"!"> は論理否定を行ないます; つまり「not」ということです。
この演算子の優先順位を低くしたものとして、C<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
When complementing strings, if all characters have ordinal values under
256, then their complements will, also. But if they do not, all
characters will be in either 32- or 64-bit complements, depending on your
architecture. So for example, C<~"\x{3B1}"> is C<"\x{FFFF_FC4E}"> on
32-bit machines and C<"\x{FFFF_FFFF_FFFF_FC4E}"> on 64-bit machines.
=end original
文字列の補数を計算するとき、全ての文字の値が 256 未満の場合は、その値の
補数が計算されます。
そうでない場合は、全ての文字はアーキテクチャに依存しいて 32 ビットまたは
64 ビットで補数が計算されます。
従って例えば、C<~"\x{3B1}"> は 32 ビットマシンでは C<"\x{FFFF_FC4E}"> に、
64 ビットマシンでは C<"\x{FFFF_FFFF_FFFF_FC4E}"> になります。
=begin original
If the experimental "bitwise" feature is enabled via S<C<use feature
'bitwise'>>, 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">. This feature
produces a warning unless you use S<C<no warnings 'experimental::bitwise'>>.
=end original
実験的な "bitwise" 機能が S<C<use feature 'bitwise'>> で有効にされると、
単項の C<"~"> は常にその引数を数値として扱い、その代替形式である
C<"~."> はその引数を常に文字列として扱います。
従って 32 ビットプラットフォームでは C<~0> と C<~"0"> はどちらも
2**32-1 を意味しますが、C<~.0> と C<~."0"> はどちらも C<"\xff"> になります。
S<C<no warnings '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 a reference to whatever follows it. 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
C<$n> (B<not> as the left operand C<$m> like C function C<fmod()>)
and the absolute value less than that of C<$n>.
Note that when S<C<use integer>> is in scope, C<"%"> gives you direct access
to the modulo operator as implemented by your C compiler. This
operator is not as well defined for negative operands, but it will
execute faster.
X<%> X<remainder> X<modulo> X<mod>
=end original
二項演算子の C<"%"> は剰余演算子で、一つ目の引数を二つ目の引数で割ったときの
余りを返します。
C<$m> と C<$n> の二つの整数の被演算子を取ったとすると:
C<$n> が正の場合、S<C<$m % $n>> は、C<$m> から C<$m> 以下の最大の
C<$n> の倍数を引いた値です。
C<$n> が負の場合、S<C<$m % $n>> は、C<$m> から C<$m> を下回らない
最小の C<$n> の倍数を引いた値です(従って結果はゼロ以下になります)。
オペランド C<$m> と C<$n> が浮動小数点数で、C<$n> の絶対値
(つまり C<abs($n)>) が S<C<(UV_MAX + 1)>> より小さい場合、
C<$m> と C<$n> の整数部のみが操作で使われます
(注意: ここで C<UV_MAX> は符号なし整数の最大値を意味します)。
右オペランドの絶対値 (C<abs($n)>) が S<C<(UV_MAX + 1)>> 以上の場合、
C<"%"> は、S<C<($r = $m - $i*$n)>> となる浮動小数点剰余 C<$r> を計算します;
ここで C<$i> は、C<$r> が右オペランド C<$n> と同じ符号 (C の
関数 C<fmod()> のように左オペランド C<$m> B<ではありません>) で、
絶対値が C<$n> より小さいものになるような、ある整数です。
S<C<use integer>> がスコープ内にある場合、
C<"%"> は C コンパイラで実装された剰余演算子を使います。
この演算子は被演算子が負の場合の挙動が不確実ですが、
より高速です。
X<%> X<remainder> X<modulo> X<mod>
=begin original
Binary C<"x"> is the repetition operator. In scalar context or if the left
operand is not enclosed in parentheses, it returns a string consisting
of the left operand repeated the number of times specified by the right
operand. In list context, if the left operand is enclosed in
parentheses or is a list formed by C<qw/I<STRING>/>, it repeats the list.
If the right operand is zero or negative (raising a warning on
negative), it returns an empty string
or an empty list, depending on the context.
X<x>
=end original
二項演算子の C<"x"> は繰り返し演算子です。
スカラコンテキストまたは左辺値が括弧で括られていない場合は、左被演算子を
右被演算子に示す数だけ繰り返したもので構成される文字列を返します。
リストコンテキストでは、左被演算子が括弧で括られているか、C<qw/I<STRING>/> の
形のリストの場合、リストを繰り返します。
右オペランドが 0 か負数の場合(負数の場合は警告が出ます)、
コンテキストによって空文字列か空リストを返します。
X<x>
print '-' x 80; # print row of dashes
print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
@ones = (1) x 80; # a list of 80 1's
@ones = (5) x @ones; # set all elements to 5
=head2 Additive Operators
X<operator, additive>
(加法演算子)
=begin original
Binary C<"+"> returns the sum of two numbers.
X<+>
=end original
二項演算子の C<"+"> は 2 つの数値の和を返します。
X<+>
=begin original
Binary C<"-"> returns the difference of two numbers.
X<->
=end original
二項演算子の C<"-"> は 2 つの数値の差を返します。
X<->
=begin original
Binary C<"."> concatenates two strings.
X<string, concatenation> X<concatenation>
X<cat> X<concat> X<concatenate> X<.>
=end original
二項演算子の C<"."> は 2 つの文字列を連結します。
X<string, concatenation> X<concatenation>
X<cat> X<concat> X<concatenate> X<.>
=head2 Shift Operators
X<shift operator> X<operator, shift> X<<< << >>>
X<<< >> >>> X<right shift> X<left shift> X<bitwise shift>
X<shl> X<shr> X<shift, right> X<shift, left>
(シフト演算子)
=begin original
Binary C<<< "<<" >>> returns the value of its left argument shifted left by the
number of bits specified by the right argument. Arguments should be
integers. (See also L<Integer Arithmetic>.)
=end original
二項演算子の C<<< "<<" >>> は左引数の値を、右引数で示すビット数だけ、
左にシフトした値を返します。
引数は整数でなければなりません。
(L<Integer Arithmetic> も参照して下さい。)
=begin original
Binary C<<< ">>" >>> returns the value of its left argument shifted right by
the number of bits specified by the right argument. Arguments should
be integers. (See also L<Integer Arithmetic>.)
=end original
二項演算子の C<<< ">>" >>> は左引数の値を、右引数で示すビット数だけ、
右にシフトした値を返します。
引数は整数でなければなりません。
(L<Integer Arithmetic> も参照して下さい。)
=begin original
Note that both C<<< << >>> and C<<< >> >>> in Perl are implemented directly using
C<<< << >>> and C<<< >> >>> in C. If S<C<use integer>> (see L<Integer Arithmetic>) is
in force then signed C integers are used, else unsigned C integers are
used, even for negative shiftees. Either way, the implementation isn't going to generate results
larger than the size of the integer type Perl was built with (32 bits
or 64 bits).
=end original
Perl での C<<< << >>> と C<<< >> >>> は C での C<<< << >>> と C<<< >> >>> を
直接利用して実装されていることに注意してください。
S<C<use integer>> (L<Integer Arithmetic> を参照してください)が有効な場合、
C の符号付き整数が使われ、そうでない場合は(例え負のシフトでも)
C の符号なし整数が使われます。
どちらの場合も、この実装は Perl がビルドされた整数型のサイズ(32 ビットか
64 ビット)よりも大きい結果を生成することはありません。
=begin original
The result of overflowing the range of the integers is undefined
because it is undefined also in C. In other words, using 32-bit
integers, S<C<< 1 << 32 >>> is undefined. Shifting by a negative number
of bits is also undefined.
=end original
整数の範囲をオーバーフローした場合の結果は、C でも未定義なので、未定義です。
言い換えると、32 ビット整数を使っているとき、S<C<< 1 << 32 >>> は未定義です。
シフトするビット数として負の数を指定した場合も未定義です。
=begin original
If you get tired of being subject to your platform's native integers,
the S<C<use bigint>> pragma neatly sidesteps the issue altogether:
=end original
プラットフォームのネイティブな整数の影響に疲れたなら、S<C<use bigint>>
プラグマは問題を完全にうまく回避します:
print 20 << 20; # 20971520
print 20 << 40; # 5120 on 32-bit machines,
# 21990232555520 on 64-bit machines
use bigint;
print 20 << 100; # 25353012004564588029934064107520
=head2 Named Unary Operators
X<operator, named unary>
(名前付き単項演算子)
=begin original
The various named unary operators are treated as functions with one
argument, with optional parentheses.
=end original
さまざまな名前付き単項演算子が、引数を 1 つ持ち、括弧が省略可能な、