Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

syntax error with constant

by chunlou (Curate)
on Aug 03, 2003 at 21:48 UTC ( #280516=perlquestion: print w/replies, xml ) Need Help??
chunlou has asked for the wisdom of the Perl Monks concerning the following question:

Perhaps a duh newbie question, but why will
use constant k => 2; print 1-k; print 1-k(); print 1 -k();
cause syntax error, whereas
use constant k => 2; print 1-&k; print 1- k(); print 1- k;
won't? Thanks.

Replies are listed 'Best First'.
Re: syntax error with constant
by BrowserUk (Pope) on Aug 03, 2003 at 22:19 UTC

    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.

      ...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 ;-).


        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/] line 2. eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $ +^D = $^D | $DB::db_stop; print 1-foo; ;' called at /usr/lib/perl5/5.8.0/ line 17 DB::eval called at /usr/lib/perl5/5.8.0/ 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


Re: syntax error with constant
by liz (Monsignor) on Aug 03, 2003 at 22:17 UTC
    The problem is not with use constant:

    sub k{} print 1-k; print 1 -k(); print 1 -k();
    gives the same error. This happens from at least 5.6.0 until 5.8.1-RC4.

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

    -key => value


    Update: -k  File has sticky bit set. is of course a much better explanation. Thanks to BrowserUk!

Re: syntax error with constant
by artist (Parson) on Aug 04, 2003 at 23:04 UTC
    Not the answer to your question, rather than the approach which won't give such question :-),

    Why name your constants like a,b,k or even K, Q etc.. have it some meaningful name like KING or QUEEN :-)

    Said that, nothing wrong against your curiosity.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://280516]
Approved by Corion
Front-paged by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-06-21 05:26 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.