in reply to Re: Operator Precedence (unary nagation and exponentiation)
in thread Operator Precedence (unary negation and exponentiation)

It must be a parsing error, I guess: perl -e '$v=-2; print $v**2' correctly produces 4. So my guess is that the parsing in your example is simply throwing away the **2 component.

Why is another question...

--
Tommy
Too stupid to live.
Too stubborn to die.

Replies are listed 'Best First'.
Re: Operator Precedence (unary nagation and exponentiation)
by Abigail-II (Bishop) on Jan 14, 2003 at 11:20 UTC
    Use -w!
    $ perl -wle 'print (-2)**2' print (...) interpreted as function at -e line 1. Useless use of exponentiation (**) in void context at -e line 1. -2

    Just like any other function, if the function name is followed by a left paren, it's taken as the start of the argument list. print (-2)**2 is parsed as (print (-2)) ** 2. This is even documented. From perldoc print:

    Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to termi- nate the arguments to the print--interpose a "+" or put parentheses around all the arguments.

    Abigail

      Good catch, I should have used -w... sigh

      Arien mumbles something about how anybody can forget to put in switches now and then. ( -f ;-)

      — Arien

Re: Re: Re: Operator Precedence (unary nagation and exponentiation)
by BrowserUk (Patriarch) on Jan 14, 2003 at 11:07 UTC

    Try perl -e '$v = 2; print -$v ** 2;'

    This the use of unary negation that is the subject of the thread.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

      But if you look at the problem that Arien posed, the answer doesn't just have the incorrect sign, it's also got the wrong magnitude.

      --
      Tommy
      Too stupid to live.
      Too stubborn to die.