Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: Operator precedence of unary plus (Bug or Feature)

by LanX (Saint)
on Feb 05, 2012 at 18:22 UTC ( [id://951969]=note: print w/replies, xml ) Need Help??


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

>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

Replies are listed 'Best First'.
Re^5: Operator precedence of unary plus (Bug or Feature)
by JavaFan (Canon) on Feb 05, 2012 at 20:29 UTC
    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.
Re^5: Operator precedence of unary plus (Bug or Feature)
by JavaFan (Canon) on Feb 05, 2012 at 20:22 UTC
    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).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found