'require'ing globals without 'used only once' complaints from 'perl-w'?

G

Greg Ercolano

I'm trying to figure out a way to load global variables using 'require'
with 'perl -w' enabled, but without getting "<varname> used only once" errors.

The only "solution" I've seen is to make sure all globals are referenced
more than once, but that gets hard to manage when you scale up the project.

My situation is a project of cgi-bin scripts, where there are several scripts
all wanting to load global settings from a single 'config.pl' file.

My current way of doing things is to make a 'config.pl' file that has the
global settings, ie:

$G::bgcolor = "#ff0000";
$G::cgibin = "/cgi-bin/MyApp/";
# ..etc..
1;

..and then all the actual scripts load these settings with 'require', eg:

#!/usr/bin/perl -w
use strict;
require "config.pl";
...

..that being in each of the potentially many script files that make up
the project.

Trouble is, if a script only refers to a global once, a "used only once"
error is printed, once per variable, which clogs the httpd daemon's error
logs with errors, one per variable.

The easy solution is to turn off the -w flag, but often it's useful to
leave it on during production to catch runtime errors.

From a developer's point of view, it's a real PITA to manually ensure
having no-op references for each global in /each/ script, eg:

#!/usr/bin/perl -w
use strict;
require "config.pl";
$G::bgcolor = $G::bgcolor; # silence 'used once' errors
$G::cgibin = $G::cgibin; # ""

..that becomes unmanageable fast.

Is there a 'right way' to do this without getting too 'hacky',
and without disabling -w? Maybe making the globals into a package?

Code examples welcome..
 
I

it_says_BALLS_on_your_forehead

Greg said:
I'm trying to figure out a way to load global variables using 'require'
with 'perl -w' enabled, but without getting "<varname> used only once" errors.

in your require script, you can write a subroutine that returns a
hashref that has all the values you need. although really, you don't
need a require at all. alternatively, just read in a plain text config
file to populate a hash.
 
T

thrill5

Add "no warnings('once');" to the beginning of config.pl. This will turn
off the "used only once" warnings.

Scott
 
G

Gunnar Hjalmarsson

[ Please apply a sensible quoting style. Do not top post! ]
Add "no warnings('once');" to the beginning of config.pl. This will
turn off the "used only once" warnings.

Did you try that?

Since the warnings pragma is lexically scoped, doing as you suggest
won't make a difference.
 
G

Gunnar Hjalmarsson

Greg said:
I'm trying to figure out a way to load global variables using
'require' with 'perl -w' enabled, but without getting "<varname> used
only once" errors.

Is there a 'right way' to do this without getting too 'hacky', and
without disabling -w? Maybe making the globals into a package?

The warning shows up because config.pl is loaded only at run time.
Consequently, renaming config.pl to config.pm and use()ing it would be
enough. A BEGIN block is another possibility:

BEGIN { require 'config.pl' }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top