I can't understand what does the perl substitution return after applied. I have the following snippet
my $w = "-foo";
my $v = $w =~ s/-//?0:1;
print "---- $v\n";
If $w
is -foo
, this prints ---- 0
, but if $w
is foo
, this prints ---- 1
Why does it behave like that?
You have reversed the logic in your code
my $v = $w =~ s/-// ? 0 : 1;
As Gilles notes in the comments, this is the operation of the ternary/conditional operator:
condition ? caseTrue : caseFalse
The conditional operator will have its condition evaluate to "true" when the pattern matches, and the value for true that you have put in is 0
. So you have reversed the logic:
"Return 0 for true, and 1 for false".
The true result of the operator can be demonstrated if you simply remove the ternary, then it will print 1.