in reply to Re: Problem with constant pragma and some hash definitions
in thread Problem with constant pragma and some hash definitions

Many thanks sauoq for your rapid answer. Just one follow up question if I may; is there any downside in doing this? In the real bit of code there will be 30-40 values at a maximum.

Regards,
Dom.

  • Comment on Re: Re: Problem with constant pragma and some hash definitions

Replies are listed 'Best First'.
Re: Re: Re: Problem with constant pragma and some hash definitions
by Jenda (Abbot) on Jul 18, 2003 at 20:39 UTC

    Actually there is. As soon as you add that ampersand the constants cease to be constants. And instead of inlining the value during compile-time, Perl actually calls the functions each time. Therefore I'd rather add an empty pair of braces:

    %hash = ( FOO() => 'FOO description, ...)
    Try to run these three and see the difference:
    perl -MO=Deparse -e "use constant FOO => 5; print FOO,qq{\n};" perl -MO=Deparse -e "use constant FOO => 5; print &FOO,qq{\n};" perl -MO=Deparse -e "use constant FOO => 5; print FOO(),qq{\n};"

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      there is another option you left out, which makes use of the unary "+" operator.

      perl -MO=Deparse -e "use constant FOO => 5; print +FOO,qq{\n};"

      which parses correctly. this syntax is idiomatic, and is the method i would prefer.

      ~Particle *accelerates*

Re: Re: Re: Problem with constant pragma and some hash definitions
by sauoq (Abbot) on Jul 18, 2003 at 18:46 UTC
    Just one follow up question if I may; is there any downside in doing this?

    A downside to using the ampersand notation? Not except for needing to keep track of where you need one and where you don't. (I actually think of that as a downside to using constant.)

    Update: "Actually, there is." Jenda is right.

    -sauoq
    "My two cents aren't worth a dime.";