in reply to managing constants created with constant.pm

You are using the constant.pm pragmatic incorrectly, and constants don't do what you think they do.

Correct syntax is,

use constant SCROLL_X => 1; use constant SCROLL_Y => 2; use constant FLOW_X => 3; use constant FLOW_Y => 4;
which produces constant subs of the form sub SCROLL_X () { 1;}. The prototype is important there, because it is a hint to the perl compiler that it may optimize away the sub call by replacing it with the constant value. There is no runtime reminant of the sub or its name.

Update: As Abigail-II notes, recent constant.pm can take a hashref as argument. The syntax

use canstant { SCROLL_X => 1, SCROLL_Y => 2, FLOW_X => 3, FLOW_Y => 4, };
is fine if older versions need not be supported.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: managing constants created with constant.pm
by Abigail-II (Bishop) on Jul 14, 2003 at 08:39 UTC
    The OP was using constants correctly. She's just using a modern version of Perl.

    Abigail

      In 5.8.1, $ perl -e'use constant { FOO = 1, BAR = 2}; print FOO,BAR' does not compile. With stringy commas, the combined form does work,

      $ perl -e'use constant { FOO => 1, BAR => 2}; print FOO,BAR' 12$
      '1' and '3' are not legal names for subs.

      After Compline,
      Zaxo

        You are right about the stringification (I missed that), but your post suggested the hashref as only argument was incorrect, and that you need a use constant for each constant used.

        Abigail

      Abigail, I'm male!! :))))))))
      And folks, please forget about the syntax!! Everything I code with constants work( as you all pointed out), and thanks :))))
      The question still remains unanswered! Any ideas?? I know how to solve the problem. I just have a feeling that it is not the perl way!
      Ogla
        Hi,

        Abigail, I'm male!! :))))))))

        Am I they only monk who couldn't help but laugh at the irony here. :-)

        cheers

        thinker
        oh, by the way, I didn't like the line "... constants don't do what you think they do" Please be more specific.
        And in a 4000 line OO environment, I think, they exactly do what I expect them to do. OK, you can check ming SWF library, and maybe see my point of view.
        http://ming.sourceforge.net
        The constants are used as "FLAGS". This is very neat in my opinion. And one should have way to manage these flags. That's all I'm asking.