Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I want to declare a constant value throughout the whole program. How to do that?

2005-10-05 Retitled from "Constant" by Corion, as per Monastery guidelines
Original title: 'Constant'

Replies are listed 'Best First'.
Re: How can I declare constants?
by Corion (Patriarch) on Oct 05, 2005 at 11:16 UTC

    If you don't mind that it can be changed, you can simply declare your constants as follows:

    # Constants used throughout the program: my $number_of_pages = 3;

    If you are really paranoid and don't want your constants to be changeable, use the constant pragma:

    use constant NUMBER_OF_PAGES => 3;
      Thank you. I used this pragma already but had the problems mentioned in the bugs section (brewordsm, sub calls). Does there exist any other alternative (no my or our declaration)?
        You can decalare a sub routine...
        use strict; use warnings; sub PI () {3.14} print PI;
        still not sure what you've got against 'my' though
        ---
        my name's not Keith, and I'm not reasonable.

        No. The only bareword-"problem" in the documentation also has a solution mentioned with it. Did you try that solution? What part of that solution did not work for you? Can you show some code that demonstrates how you followed the documentation but still got the wrong behaviour?

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can I declare constants?
by Perl Mouse (Chaplain) on Oct 05, 2005 at 11:35 UTC
    Beside the already mentioned constant pragma, there's also the Readonly module. The advantage of Readonly is that you can mark interpolatable variable readonly (that is, constant), instead of using subroutines.
    use Readonly; Readonly my $foo => "bar"; $foo .= "baz"; __END__ Modification of a read-only value attempted at ...
    Perl --((8:>*

      Or just do it the old-fashioned way if all you need is constant scalars and you don't care if they are globals. Since, I find I often want my constants to be global and I rarely need a constant array or hash, this works very well for me. And on those rare occassions when I do have a need for a constant array (or hash), I never need to interpolate them... so use constant generally works just fine in those cases.

      *PI = \3.14159; *SLOGAN = \q(Don't tread on me!);
      -sauoq
      "My two cents aren't worth a dime.";
      
Re: How can I declare constants?
by Delusional (Beadle) on Oct 05, 2005 at 11:50 UTC
    Based on the question, I would assume your looking for the C/C++ #define method for Perl. You can find an explaination of converting C/C++ #define methods to Perl here.

    Simply put, the #define used in C/C++ does not exist in Perl, atleast not in the same way. You will need to use use constant to define a fixed value. You would need it, idealy, in your Perl script head, or in a use command (in the script head), like use myheadderfile; where myheadderfile is your #define's in Perl methods with .pm for the file extension (verses .h or .hpp).

    In regards to using my, in most situations my should be use, there are, however, situations where my causes the script to fail or causes incorrect output (few, but they do exist). Based on what you asked for, they should be fine, and should be used.
Re: How can I declare constants?
by kprasanna_79 (Hermit) on Oct 05, 2005 at 11:31 UTC
    Hi, You can use something like this.This is a sample.
    use constant USER => qw(email first_name last_name login mobile_phone phone );
    -Prasanna.K