Perl-operators-precedence-example

提供:Dev Guides
移動先:案内検索

Perl演算子の優先順位の例

次の表に、優先順位の高いものから低いものまで、すべての演算子を示します。

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.
left    , =>
nonassoc    list operators (rightward)
right   not
left    and
left    or xor

次の例を試して、Perlのすべてのperl演算子の優先順位を理解してください。 test.plファイルに次のPerlプログラムをコピーして貼り付け、このプログラムを実行します。

#!/usr/local/bin/perl

$a = 20;
$b = 10;
$c = 15;
$d = 5;
$e;

print "Value of \$a  = $a, \$b = $b, \$c = $c and \$d = $d\n";

$e = ($a + $b) *$c/$d;
print "Value of (\$a + \$b)* \$c/\$d is  = $e\n";

$e = (($a + $b) *$c )/$d;
print "Value of ((\$a + \$b)* \$c)/\$d is  = $e\n";

$e = ($a + $b) *($c/$d);
print "Value of (\$a + \$b)* (\$c/\$d ) is  = $e\n";

$e = $a + ($b *$c )/$d;
print "Value of \$a + (\$b* \$c )/\$d is  = $e\n";

上記のコードが実行されると、次の結果が生成されます-

Value of $a = 20, $b = 10, $c = 15 and $d = 5
Value of ($a + $b) *$c/$d is = 90
Value of (($a + $b)* $c)/$d is = 90
Value of ($a + $b) *($c/$d ) is = 90
Value of $a + ($b* $c )/$d is = 50