in reply to Use of the => operator

Well... the => operator forces any "word" (bareword, really) to be interpretted as a string. Now, the definition of what exactly makes a "word" may be slightly different from place to place, but arguably, something consisting purely of numeric characters is not a "word".

Of course, what might otherwise be thought of as a "word" in other contexts in perl would be something like: /^[a-zA-Z_]\w*$/... but the fact that a leading dash is an acceptible part of a "word" with respect to the => greater operator... who can really say?

------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: Use of the => operator
by ysth (Canon) on Aug 09, 2004 at 18:47 UTC
    The leading dash isn't recognized as part of a bareword by =>, it is actually an alternative method for auto-quoting a bareword. Compare:
    $ perl -we'use strict; @_ = (-foo, "bar")' $ perl -we'use strict; @_ = (foo, "bar")' Bareword "foo" not allowed while "strict subs" in use at -e line 1.
      Absolutely correct! Wow, I'd somehow completely missed that. From perlop:
      Unary "-" performs arithmetic negation if the operand is numeric. If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned. One effect of these rules is that -bareword is equivalent to "-bareword".
      Which leads to this funness:
      [me@host]$ perl -le 'use strict; print -foo; print -"-foo"; print -"+f +oo";' -foo +foo -foo [me@host]$
      Just when you think there's nothing new to learn.
      ------------ :Wq Not an editor command: Wq
      That is really neat - I'd assumed -bareword was just a convention and that => was responsible for the quoting. Is there a reason that - and => aren't mentioned in the "Quotes and Quote-like Operators" section of perlop?
        Becayse they're not "Quotes or Quote-like Operators". The "Comma Operator" gets its own heading and the minus is listed under "Symbolic Unary Operators".