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

Discussion in 'Perl Misc' started by Greg Ercolano, Mar 6, 2006.

  1. 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..
     
    Greg Ercolano, Mar 6, 2006
    #1
    1. Advertising

  2. Re: 'require'ing globals without 'used only once' complaints from 'perl -w'?

    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.
     
    it_says_BALLS_on_your_forehead, Mar 6, 2006
    #2
    1. Advertising

  3. Greg Ercolano

    thrill5 Guest

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

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

    Scott

    "Greg Ercolano" <> wrote in message
    news:uKLOf.1084$...
    > 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..
    >
    >
    >
     
    thrill5, Mar 6, 2006
    #3
  4. Re: 'require'ing globals without 'used only once' complaints from'perl -w'?

    [ 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
     
    Gunnar Hjalmarsson, Mar 6, 2006
    #4
  5. Re: 'require'ing globals without 'used only once' complaints from'perl -w'?

    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
     
    Gunnar Hjalmarsson, Mar 6, 2006
    #5
  6. Re: 'require'ing globals without 'used only once' complaints from'perl -w'?

    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.
     
    Greg Ercolano, Mar 6, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S2VubmV0aCBQ?=

    To whom should I address complaints?

    =?Utf-8?B?S2VubmV0aCBQ?=, Jan 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    361
    =?Utf-8?B?S2VubmV0aCBQ?=
    Jan 20, 2005
  2. Richard

    Complaints to

    Richard, Jul 22, 2004, in forum: C++
    Replies:
    0
    Views:
    1,766
    Richard
    Jul 22, 2004
  3. Richard

    Complaints to

    Richard, Jul 22, 2004, in forum: C Programming
    Replies:
    0
    Views:
    358
    Richard
    Jul 22, 2004
  4. Dan Jacobson

    perl -ws and "used only once" warnings

    Dan Jacobson, Oct 28, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    176
    Brian McCauley
    Oct 28, 2003
  5. Mike Ellwood
    Replies:
    0
    Views:
    208
    Mike Ellwood
    Jan 29, 2004
Loading...

Share This Page