in reply to Re: map sentence with subtraction interpreted as with negative number
in thread map sentence with subtraction interpreted as with negative number

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.

Replies are listed 'Best First'.
Re^3: map sentence with subtraction interpreted as with negative number
by Eily (Monsignor) on Nov 25, 2015 at 14:37 UTC

    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

      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
        As someone said, 'indirect objects are pretty wicked'. They're a great source of bugs. You probably shouldn't torture the Perl's parser so hard in this spot, since it's especially fragile here.