Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: map sentence with subtraction interpreted as with negative number

by Discipulus (Canon)
on Nov 25, 2015 at 12:25 UTC ( [id://1148599]=note: print w/replies, xml ) Need Help??


in reply to map sentence with subtraction interpreted as with negative number

as already said in the thread you mentioned, you need to disambiguate your statement
perl -e "print map +$_ -1, 1;" 0
L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re: map sentence with subtraction interpreted as with negative number
  • Download Code

Replies are listed 'Best First'.
Re^2: map sentence with subtraction interpreted as with negative number
by rsFalse (Chaplain) on Nov 25, 2015 at 13:49 UTC
    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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1148599]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found