Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

perl -Mutf8 -MMath::Trig -E 'use constant π => pi; say π'
3.14159265358979
perl -Mutf8 -MMath::Trig=:pi -C -E 'use constant π² => pi2; say π²'
Unrecognized character \x{b2}; marked by <-- HERE after constant π<-- HERE near column 15 at -e line 1.
perl -Mutf8 -MMath::Trig=:pi -C -E 'use constant q(π²) => pi2; say π²'
Constant name 'π²' has invalid characters at -e line 1. BEGIN failed--compilation aborted at -e line 1.

Replies are listed 'Best First'.
Re: Unicode constant
by haukex (Archbishop) on Nov 07, 2019 at 07:39 UTC

    From Identifier parsing:

    If working under the effect of the use utf8; pragma, the following rules apply:
    / (?[ ( \p{Word} & \p{XID_Start} ) + [_] ]) (?[ ( \p{Word} & \p{XID_Continue} ) ]) * /x

    And looking at the Unicode Character Properties of U+00B2 SUPERSCRIPT TWO, it's neither XID_Start nor XID_Continue. Although I'm not an expert on Unicode identifiers, this seems like it explains it.

      Basically, identifiers can't contain symbols except "_". They can't contain "²" any more than "!". (Punctuation variables excepted.)

Re: Unicode constant
by Anonymous Monk on Nov 08, 2019 at 00:49 UTC
    Yes, the Perl parser's rules for identifiers considerably predates the entire concept of UTF8 ... and, for whatever it is worth, even today most programming languages are the same.
      Still pretty impressive, Perl is, TIMTOWTDI:
      perl -Mutf8 -MMath::Trig=:pi -C -E 'use constant π2 => pi2; say π2'
      6.28318530717959