The answer is fairly simple: perl's ternary operator is assignable
In particular, your code really means this:
(test() ? $int += 1 : $int) = 0;
You may recall that the result of an assignment is the variable being assigned to; this means that the following happens:
- test() evaluates to true
- $int += 1 is evaluated, incrementing $int to 6
- The result of the assignment, $int, is assigned the value 0
This code will work how you want:
test() ? ($int++) : ($int = 0);
Or this:
$int += test() ? 1 : -$int; #If you want to be cute