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

It's now Nth time that I run into this problem; however, this time I have enough resolve to learn more on how to avoid it in the future.

When I use both 'strict' and 'diagnostics' here's what I get whenever i attempt to execute my script:

-----------------------
Name "CONFIG::foo" used only once: possible typo at foo_script.pl line 181 (#1)

(W) Typographical errors often show up as unique variable names. If you had a good reason for having a unique name, then just mention it again somehow to suppress the message. The use vars pragma is provided for just this purpose.
-----------------------


Consider $CONFIG::foo being one of a few variables inside my CONFIG package used inside the main script. A few of the variables defined inside the CONFIG package are not used more than a single time. Consequently this causes a problem with perl as mentioned above.

According to the 'diagnostics' comment, one way to resolve this would be to "mention it again somehow..." (where 'it' is the variable). So, just how do I do it best? The whole idea of 'mentioning' a variable an extra time in order to avoid warnings is a little hard for me to grasp. I realize that one way (out of hundreds..) of doing this would be:
my $temp = $CONFIG::foo;
The question I have though is that it doesn't look at all normal to do it this way. I also tried playing with this:
use vars qw($CONFIG::foo);
However, to no surprise, it wouldn't let me 'redeclare' variables belonging to an 'alien' package (and therefore already 'defined'). Finally, could such a warning really attribute to poor code/logic design? Is it really so wrong and sinful to have a variable (mainly in a configuration package) to be used only once in the main code?
Please help with whatever may come to your mind ;-).

"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Replies are listed 'Best First'.
Re: Unique variable names & 'strict vars'
by maverick (Curate) on Dec 20, 2001 at 05:10 UTC
    There are several different config file readers / writers on CPAN. Personally I use Config::General for config files instead of config packages.

    HTH

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Re: Unique variable names & 'strict vars'
by VSarkiss (Monsignor) on Dec 20, 2001 at 05:13 UTC

    You can still do the use vars trick, you just have to change packages when you do it:

    package CONFIG; use vars '$foo'; package main;
    Substitute whatever package you started from for main.

    The most radical approach I've seen for this is in Slashcode, which sets up a WARN handler that eats any message starting with the string "Possible typo". TIMTOWTDI, although that's a little overkill for my taste....

    HTH

      Or, incidentally, this works too:
      #!/usr/local/bin/perl -w use strict; { package CONFIG; use vars qw($foo); } ....
      Putting 'package ' inside a a block eliminates the need to return back to the main package via 'package main' call. ;-)

      "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
(shockme) Re: Unique variable names & 'strict vars'
by shockme (Chaplain) on Dec 20, 2001 at 05:35 UTC
    Is it really so wrong and sinful to have a variable (mainly in a configuration package) to be used only once in the main code?

    No, it's not wrong at all. Actually, it's more common than naught. Think of this message as the small price we pay to avoid spelling errors.

    my $temp = $CONFIG::foo;

    Unless you use $temp in more than one spot, you'll not avoid the message. Think about it.

    This issue has been addressed a multitude of times. Check out this link and go from there. Good luck with it.

    If things get any worse, I'll have to ask you to stop helping me.