in reply to syntax error with constant

Probably because -k looks too much like one of the -X filetest operators (eg. -s -e -d etc.).

If you use upper case for your constant names as is generally advised, the parser has an additional clue which it uses to work out the right way to parse the statement and does so, albeit that it issues a friendly warning in the process.

use constant K => 2; print 1 - K; -1 print 1 -K; Ambiguous use of -K resolved as -&K() at ... -1

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: syntax error with constant
by liz (Monsignor) on Aug 03, 2003 at 22:42 UTC
    ...the parser has an additional clue which it uses to work out the right way to parse the statement...

    I think the parser is reallly straightforward in this respect: known -X filetest operators will give the error, unknown ones will give the warning. Nothing to do with upper case of lower case: -a works but gives the warning even though lowercase, whereas -T will give the compile error.

    perldoc -f -X lists the following uppercase filetest operators:

     -T  File is an ASCII text file (heuristic guess).
     -B  File is a "binary" file (opposite of -T).
    
     -M  Script start time minus file modification time, in days.
     -A  Same for access time.
     -C  Same for inode change time (Unix, may differ for other platforms)
    
    so don't call your constants T, B, M, A or C ;-).

    Liz

      But why does it happen with multi-character constant names?

      DB<1> use constant foo => 1 DB<2> print 1-foo Ambiguous use of -foo resolved as -&foo() at (eval 15)[/usr/lib/perl5/ +5.8.0/perl5db.pl:17] line 2. eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $ +^D = $^D | $DB::db_stop; print 1-foo; ;' called at /usr/lib/perl5/5.8.0/perl5db.pl line 17 DB::eval called at /usr/lib/perl5/5.8.0/perl5db.pl line 1323 DB::DB called at -e line 1 0

      Same thing happens with FOO.

      I don't encounter this problem, because I put spaces between terms and operators. Insert invisible 'nyah nyah' here

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

        I think the issue that is resolved has to do with my original assessment:

        What I think happens is that the parser gets confused with trying to handle the:

        -key => value
        notation.

        The warning does not occur when you have "-foo()" because the parentheses indicated a subroutine call. However, without it, "-foo" could still be a key specification. So the parser must take that into account after parsing -foo. Then it hits the ";" and it can resolve the issue. Although in this case strictly speaking, I don't see an ambiguity. So the warning seems superfluous to me.

        I guess a p5per with more perl internals knowledge should speak up ;-)

        Liz

        Huh, that's interesting. I get the same thing if I use quux as a constant, even though -q isn't a filetest operator. And this contradicts the documentation given by perldoc -f -X:
        Note that "-s/a/b/" does not do a negated substitution. Saying "-exp($foo)" still works as expected, however--only single letters following a minus are interpreted as file tests.