Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Operator precedence of unary plus (Bug or Feature)

by LanX (Saint)
on Feb 04, 2012 at 23:40 UTC ( [id://951878]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

DB<130> $q=1 DB<131> print $q + 4*5 21 DB<132> print $q +4*5 # what ??? DB<133> use warnings; print $q +4*5 print() on unopened filehandle 1 at (eval 67)[/usr/share/perl/5.10/per +l5db.pl:638] line 2. ... DB<134> use warnings; print $q+4*5 21 DB<135> use warnings; print $q+ 4*5 21

Is there any parsing rule which makes the second expression act like as if $q was meant to be a filehandle?

Bug or feature?

Cheers Rolf

Replies are listed 'Best First'.
Re: Operator precedence of unary plus (Bug or Feature)
by ikegami (Patriarch) on Feb 04, 2012 at 23:57 UTC

    There is no unary plus operator. The "+" is part of the number literal, as defined in perldata.

    (If it was a unary plus operator, the behaviour would require a two token lookahead, and that's very unlikely.)

      And now, I shall prove myself at least partially wrong:

      >perl -wE"my $x=4; my $y=5; say $x + 6" 10 >perl -wE"my $x=4; my $y=5; say $x +6" say() on unopened filehandle 4 at -e line 1. >perl -wE"my $x=4; my $y=5; say $x +$y" say() on unopened filehandle 4 at -e line 1.
Re: Operator precedence of unary plus (Bug or Feature)
by JavaFan (Canon) on Feb 05, 2012 at 10:32 UTC
    Perl has to apply heuristics, opting to guess what the programmer intended, instead of having a defined parsing rule, and sticking to it.

    Since it's common for programmers to put spaces on both sides of binary operators, and also common to not have a space between a unary operator and its argument, Perl chooses to parse print $q + 4*5 as if the + were a binary operator, and print $q +4*5 as if the + were a unary operator.

      I think this special parsing is restricted to print and say.

      The plus in this snippet: "5   +6" will in most other cases be interpreted as a binary operator, no matter how many whitespaces.

      Cheers Rolf

        I think this special parsing is restricted to print and say.
        Perl does not have to apply heuristics if there's only one way of parsing it. In most cases, the only way to parse the plus sign will either be as binary operator, or as a unary operator.
        The plus in this snippet: "5 +6" will in most other cases be interpreted as a binary operator, no matter how many whitespaces.
        package Foo; sub bar {say "[@_]"} package main; bar Foo + 6;
        This will print [Foo 6] no matter how many whitespace you use around the +. This actually surprised me, I would have expected Perl to apply the same heuristics as the OP encountered.
Re: Operator precedence of unary plus (Bug or Feature)
by Khen1950fx (Canon) on Feb 05, 2012 at 04:42 UTC
    As I understand it, unary plus predecedence here needs to be defined with parentheses; hence to get 21 using the correct precedence:
    #!/usr/bin/perl -l use strict; use warnings; print my $q = 1; print $q + +(4*5); print $q+4*5; print $q+ 4*5;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-19 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found