in reply to Re: Illegal octal digit error
in thread Illegal octal digit error

This is surprising given the description of '=>' in perlop:

The "=>" operator is a synonym for the comma, but forces any word (consisting entirely of word characters) to its left to be interpreted as a string (as of 5.001). This includes words that might otherwise be considered a constant or function call.

And perlre defines a word character (\w) as:

A "\w" matches a single alphanumeric character (an alphabetic character, or a decimal digit) or "_"

Yet any collection of word characters that can be interpreted as a numeric constant is, and the result stringified. This includes octal, hex and some scientific notation (without period: e.g. 12e3).

Am I misreading perlsyn or does it say that 07, 0666, 0x123 and 12e3 or even 08, 0abc or even 0xyz should all be forced to be interpreted as strings rather than being interpreted as numbers or compile time errors?

Replies are listed 'Best First'.
Re^3: Illegal octal digit error
by GrandFather (Saint) on Jun 06, 2009 at 12:51 UTC

    Toward the end of the Comma Operator section in perlop it says:

    If the argument on the left is not a word, it is first interpreted as an expression, and then the string value of that is used.

    which is consistent with observed behaviour.


    True laziness is hard work

      In which case a word must be something other than a sequence of "word characters". Is word defined somewhere?

      update: I'm guessing a word must begin with an alphabetic character or an underscore so that anything beginning with a digit is not a word.

      update: Looking at perl source (toke.c) it appears that anything beginning with a digit is scanned as a number while anything starting with an underscore or alphabetic character other than 'v' or 'x' is scanned as... there are quite a few possibilities here, one of which is "a word before a => operator". There is some additional testing done for 'v' and 'x'. I don't follow it all, but suspect 'v' might be the start of a version string, handled somewhat differently, and 'x' followed by a digit might be a repeat operator, depending on context. Otherwise 'v' and 'x' are handled the same as all the other alphabetics.

      In conclusion: it appears that a word is not any sequence of word characters, but only those that begin with underscore or an alphabetic character. Anything beginning with a digit is a number, not a word, and is not forced to be a string when on the left of '=>'.

Re^3: Illegal octal digit error
by ikegami (Patriarch) on Jun 06, 2009 at 18:08 UTC

    The LHS of => must be a bareword for the quoting to occur. perldata defines a bareword as "a word that has no other interpretation in the grammar".

    The thing is, a bareword "will be treated as if it were a quoted string." That means => does nothing except prevent the default behaviour from being "outlawed" using use strict 'subs';.

    I don't know what the wording should be, but the docs for => need fixing. Submit a patch!

      It sounds like you have fallen victim to the fact that perldata gives an overly narrow defintion for "bareword" while also using the term "bareword" the much broader way that I tend to.

      The thing is, a bareword "will be treated as if it were a quoted string." That means => does nothing except prevent the default behaviour from being "outlawed" using use strict 'subs';.

      To be clear, => does much more than prevent strict.pm from firing. => actually does some dramatic syntax transformation:

      time, # time() time=> # 'time' s,this,that, # A substitution s=>this=>that=> # Three strings die if 0; # No-op die if , 0; # Syntax error die if => 0; # die "if",0; q=<0==1 # '<0'=1 q=>0==1 # 'q',0==1

      - tye        

        Thanks. I realized as much after reading the related post you made yesterday. (In fact, I thought mine and yours were part of the same thread.)
      -key and +key are treated differently
Re^3: Illegal octal digit error
by JavaFan (Canon) on Jun 06, 2009 at 16:51 UTC
    That wording is incorrect. "=>" quotes anything on the left that looks like a valid (non-punctuation, user-definable) identifier name. And identifiers cannot start with a digit.
    $ perl -Mstrict -e 'print foo => "\n"' foo $ perl -Mstrict -e 'print foo::bar => "\n"' foo::bar $ perl -Mstrict -e 'print foo:bar => "\n"' syntax error at -e line 1, near "foo:" Execution of -e aborted due to compilation errors. $ perl -Mstrict -e 'print 0foo => "\n"' syntax error at -e line 1, near "0foo" Execution of -e aborted due to compilation errors. $ perl -Mstrict -e "print foo'bar => qq'\n'" foo::bar $ perl -Mstrict -e "print foo''bar => qq'\n'" Bad name after foo' at -e line 1. $ perl -Mstrict -e "print foo'baz'bar => qq'\n'" foo::baz::bar $