Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

used only once warning

by monktim (Friar)
on Sep 03, 2003 at 14:31 UTC ( [id://288648]=perlquestion: print w/replies, xml ) Need Help??

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

I've been getting the following warnings:
Name "CFG::SERVER" used only once: possible typo at x.pl line 69.<br> Name "CFG::DB" used only once: possible typo at x.pl line 2097.<br> Name "CFG::USER" used only once: possible typo at ad.pl line 69.<br>
The code runs fine but the warnings are bothersome. Should I just suppress the warning or am I doing something terribly wrong?

Below is a snippet, the CFG variables are in a package in cfg.pl that uses packages.
use strict; use warnings; require "cfg.pl"; require "db.pl"; my $dB = new DB($CFG'SERVER, $CFG'DB, $CFG'USER, 'DBPASSWD);
Thanks in advance.

Replies are listed 'Best First'.
Re: used only once warning
by Ovid (Cardinal) on Sep 03, 2003 at 14:59 UTC

    You're using an old syntax there. I recommend that you read perldoc perlmod in a modern perl. It will help you would quite a bit.

    As for the warnings, those variables are probably used somewhere in your "cfg.pl" program. The warning occurs because you have required that module (which happens are run time) and when Perl parses your CFG variables, it doesn't see (yet) that they've been used elsewhere. Hence the warning. There are several ways to get around this. One of the easier to implement and understand ways: (switching the old single quote package name delimiter to double colons):

    use strict; use warnings; require "cfg.pl"; require "db.pl"; my $db; { no warnings 'once'; $db = new DB($CFG::SERVER, $CFG::DB, $CFG::USER, $CFG::DBPASSWD) };

    (I assumed, possibly incorrectly, that the password was also from that file and you just typoed it.)

    What I would recommend to deal with the is to convert your .pl files to proper modules and then use them. The syntax would then look something like:

    use strict; use warnings; use Cfg; use Db; my $db = new DB($CFG::SERVER, $CFG::DB, $CFG::USER, $CFG::DBPASSWD);

    There are a few other things to look at, but I think this is a good start.

    Cheers,
    Ovid

    New address of my CGI Course.

Re: used only once warning
by Abigail-II (Bishop) on Sep 03, 2003 at 15:06 UTC
    First of all, why are you using the ancient style name space separator, '? We have been using :: for almost a decade now.

    Having said that, your program contains the mentioned variables once. Since it's possible in Perl to use a variable without declaring it, a typo in a variable name is still a valid program. Therefore, at compile time, Perl issues a warning if it sees a variable that's used only once - it's a strong indication the programmer made a typo.

    I presume that either cfg.pl or db.pl set the variables?

    Perhaps the best way to deal with this is to get rid of the perl4 style of coding, and use perl5 style of coding. Use use to include the other files, and export (using Exporter) the variables to the calling program.

    Other alternatives: mention the variables elsewhere in your file, or turn the specific warning off using lexical warnings.

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://288648]
Approved by ybiC
Front-paged by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found