Why does // have lower priority than equality in perl?

Why //has a lower priority than ==(at least) perl 5.010?

For example, this

use 5.010;
my $may_be_undefined = 1;
my $is_equal_to_two = ($may_be_undefined//0 == 2);
say $is_equal_to_two;

prints (for me) a very unexpected result.

+5
source share
1 answer

This is due to the category of operators falling under //as well ==.

==is an "equality operator", although it //belongs to the category of "logical C-style operators".

As an example; &&is in the same category as, and //both of the statements below are equivalent when it comes to operator priority. Can this facilitate understanding?

  print "hello world" if $may_be_undefined && 0 == 2;
  print "hello world" if $may_be_undefined // 0 == 2;

C- Defined-or (//)

C, Perl// C- . , , ||, , , .

, $a//$b ($ a) || $b ( , $a, ($ a)) , ($ a)? $a: $b ( , lvalue, $a//$b ).

. , $a $b, ($ a//$b).

||,// && ( C || & &, 0 1).


+13

All Articles