in reply to Converting to boolean
There is also a variation on yours; $x &&= 1; and there is the corresponding pair on or; $x = $x || 0; $x ||= 0;. Update: Also trinary, $x = $x ? 1 : 0; and all sorts of variations on that, which have the advantage that truth and falsity are what you say they are.</Update>
None of the basic three are equivalent. Here is a comparison:
Expr | Value | Result | Value | Result |
$x = $x && 1; | true | 1 | false | unchanged |
$x = $x || 0; | true | unchanged | false | 0 |
$x = !! $x; | true | 1 | false | '' |
But see diotalevi's fine reply for what !! actually returns. Context is everything! |
But perl generally limps along just fine without any boolean type at all. See What is truth? (Curiosity corner).
After Compline,
Zaxo
|
---|