Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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.

Comment on syntax error with constant
Select or Download Code
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
    notation.

    Liz

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

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

      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

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.

    artist

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://280516]
Approved by Corion
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (15)
As of 2014-09-17 15:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (89 votes), past polls