Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > 'require'ing globals without 'used only once' complaints from 'perl-w'?

Reply
Thread Tools

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

 
 
Greg Ercolano
Guest
Posts: n/a
 
      03-06-2006
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..



 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your_forehead
Guest
Posts: n/a
 
      03-06-2006

Greg Ercolano wrote:
> 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.

 
Reply With Quote
 
 
 
 
thrill5
Guest
Posts: n/a
 
      03-06-2006
Add "no warnings('once');" to the beginning of config.pl. This will turn
off the "used only once" warnings.

Scott

"Greg Ercolano" <(E-Mail Removed)> wrote in message
news:uKLOf.1084$(E-Mail Removed)...
> 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..
>
>
>



 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-06-2006
[ Please apply a sensible quoting style. Do not top post! ]

thrill5 wrote:
> Greg Ercolano wrote:
>> 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.


<snip>

>> 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.

>
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-06-2006
Greg Ercolano wrote:
> 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.


<snip>

> 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' }

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Greg Ercolano
Guest
Posts: n/a
 
      03-06-2006
Gunnar Hjalmarsson wrote:
> .. renaming config.pl to config.pm and use()ing it would be enough.


Wow, that worked!
Easy and clean solution -- very much thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exposing globals as read-only Ark Khasin C Programming 15 03-11-2008 02:33 AM
RE: Complaints about lack of depth in MOC 2297 =?Utf-8?B?bWF0dGI=?= MCSE 0 06-17-2005 06:00 AM
To whom should I address complaints? =?Utf-8?B?S2VubmV0aCBQ?= ASP .Net 2 01-20-2005 02:15 PM
Firefox: Some newbie complaints Arthur J. O'Dwyer Firefox 13 07-27-2004 06:12 PM
Newsgroup complaints? TBamaGirl Computer Support 10 09-28-2003 07:43 PM



Advertisments