in reply to Re: Operator precedence of unary plus (Bug or Feature)
in thread Operator precedence of unary plus (Bug or Feature)

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

Replies are listed 'Best First'.
Re^3: Operator precedence of unary plus (Bug or Feature)
by JavaFan (Canon) on Feb 05, 2012 at 17:50 UTC
    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.
      >This actually surprised me, I would have expected Perl to apply the same heuristics as the OP encountered.

      I remember discussing here that "print FILEHANDLE" has a special magic different from normal "indirect object" syntax.

      but the parsing of bar Foo +6; is not really as dangerous as print $x +6 is.

      If Foo was a constant, any association to the package Foo would be overwritten.

      DB<140> package Foo; sub bar { print __PACKAGE__."::bar => [@_]\n"} DB<141> package main; sub bar { print __PACKAGE__."::bar => [@_]\n"} DB<142> bar Foo+6; Foo::bar => [Foo 6] DB<143> use constant "Foo" =>5 DB<144> bar Foo+6; main::bar => [11]

      But in the OP $q doesn't hold a filehandle.

      UPDATE: Maybe the whole confusion started, when variables for filehandles were introduced.

      Cheers Rolf

        is not really as dangerous as print $x +6 is.
        The latter is dangerous? Say $x is a number, and it gets interpreted as a filehandle. You get a runtime exception. OTOH, suppose you write print $x + 6, intending to print 6 to the filehandle $x -- what now happens is that it prints a large number to the default filehandle. Annoying, yes. Dangerous? Doubtful. Despite it being a heuristic, the outcome is deterministic. Any test that executes the code path this code is in will reveal this.
        Maybe the whole confusion started, when variables for filehandles were introduced.
        You probably mean "since we can use references to filehandles instead of filehandles"? That has been possible since 5.000. (Before that, Perl did not have references).

        But it was only that filehandle references autovivified that they became popular (5.6.0).