Suddenly I found similar error with print:
use warnings;
use strict;
my $A = 0;
print $A +1 == 0 ? "A\n" : "B\n";
Can't use string ("0") as a symbol ref while "strict refs" in use at <
+...> line 5.
| [reply] [d/l] [select] |
Well, I said I found your first case confusing, I think this one is worse, it took me far too much time to understand what you meant to do. But the main difference is that the previous example at least was a plain old syntax error, caught at compile time. This one is valid syntax:
use strict;
use warnings;
open my $A, '>', 'out.txt';
print $A -1 == 0 ? "A\n" : "B\n";
This makes it even trickier as it might be seen too late. And this means you can't say that you can guess that an operator is not unary when there is a scalar on its left, because perl actually allows this kind of constructs.
BTW, since the ternary operator makes your example even more confusing (I find its priority obvious but non intuitive, which means I get it right but need to think about it), it can be reduced to:
print $A +1;
Edit: reduced even further from $A +1 +2 to $A +1 | [reply] [d/l] [select] |
Thanks.
But it's strange:
open my $A, '>', 'out.txt';
print $A - 1 == 0 ? "A\n" : "B\n"; # prints 'B' to STDOUT
print STDERR - 1 == 0 ? "A\n" : "B\n"; # prints 'B' to STDERR
| [reply] [d/l] |