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

Whats the correct way of using constants in a large multi-script and package environment? I have a large list of constants in a seperate file like this:

use constant VERSION => "1.5.7"; ....

To use them in my web scripts (webscript.cgi) so I would do this:

#!/usr/bin/perl use strict; BEGIN { require "../constants.pl"; }; print VERSION;

Now I am writing a package to use within the website and would like it to have access to the constants as well. When I require them in this package I get errors saying:

Bareword "VERSION" not allowed while strict subs in use at ./webscript +.cgi

Does anyone have a better suggestion to approaching using constants in this way?

Replies are listed 'Best First'.
Re: Using constants in multiple scripts
by ELISHEVA (Prior) on Mar 05, 2009 at 10:07 UTC

    I think you are going to need to provide more information. I've tried to reproduce the warning two different ways and cannot:

    use strict; use warnings; use constant VERSION => "1.5.7"; print VERSION; print "\n";

    prints "1.5.7", without warnings.

    Moving the statement to an include file like this:

    use strict; use warnings; package Foo; use constant VERSION => "1.5.7"; return 1;

    and including it with BEGIN {...} does the same thing: print "1.5.7" with no warnings.

    What version of Perl are you using? What does your include file look like? And what are you actually doing? The warning you are seeing would indicate that the place where you are using "VERSION" is a place where Perl is expecting to see a subroutine or code reference rather than a string or other constant value. From perldiag:

    # Bareword "%s" not allowed while "strict subs" in use
    (F) With "strict subs" in use, a bareword is only allowed as a subroutine identifier, in curly brackets or to the left of the "=>" symbol. Perhaps you need to predeclare a subroutine?

    You may also find this post Re: Bareword "FILE" not allowed while "strict subs" helpful.

    Best, beth

Re: Using constants in multiple scripts
by rovf (Priest) on Mar 05, 2009 at 11:51 UTC

    You mention that the error occurs when you started to use a package, but you didn't show any package declaration in your code. A first guess would be that the constant is defined in one package, but used in another one, and that you did not import it.

    Does anyone have a better suggestion to approaching using constants in this way?

    "Better" is often a matter of taste. Here is how I use constants in my current project:

    Instead of using use constant, I have installed the Readonly module from CPAN. I declare a constant in one package like this:

    package Glogg; Readonly our $myconst => "goofy";
    and refer to it from another package like this:
    package Drull; print("$Glogg::myconst\n");
    Of course I could also export it if I don't want to refer to it via the package name.

    -- 
    Ronald Fischer <ynnor@mm.st>