in reply to Interesting unary - oddity

One reason for this is to allow for hyphen-prefixed hash keys. $hash{-key} has a key called "-key", not the negative of "key".

On the other hand, $hash{+key} is seen as an expression.


Dave

Replies are listed 'Best First'.
Re^2: Interesting unary - oddity
by RMGir (Prior) on Aug 27, 2004 at 16:21 UTC
    Hmm, that means -bareword makes sense, which I don't disagree with.

    My problem is with -"-key" being "+key". That's harder to justify, isn't it?


    Mike
      One possible explanation is that this implementation is mathematically equivalent to stripping the minus sign, but doesn't require changing the length of the string, which is somewhat faster. In C, switching the sign requires a single character write, while removing it requires making a new copy of the string that's one character shorter; even in Perl using substr, it's somewhat faster:
      Benchmark: timing 100000 iterations of switchsign, unsign... switchsign: 4 wallclock secs ( 1.37 usr + 0.00 sys = 1.37 CPU) @ 72992.70/s (n=100000) unsign: 4 wallclock secs ( 1.51 usr + 0.00 sys = 1.51 CPU) @ 66225.17/s (n=100000)