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

I am wondering the best way to supress an error. The specific error is:
Name "common::auth::admin" used only once: possible typo at Name "common::auth::user" used only once: possible typo at
I have a package file with this chunk of code. Notice I changed use vars qw($user $admin); to  our ($user, $admin);. Is that better?
package common::auth; our ($user, $admin); #use vars qw($user $admin); $user = $ENV{"REMOTE_USER"}; $admin = "admin"; # Set to the Administrators username
In my main program file.
#!/usr/bin/perl -w use strict; require "common.sub"; $username = "common::auth::user"; $admin = "common::auth::admin";
I don't see any sense in using the full package name for those variables throught my program. Thanks

Replies are listed 'Best First'.
Re: suppress an error
by BazB (Priest) on May 01, 2002 at 21:17 UTC

    Do something with $username and $admin.

    Perl is warning you that you don't appear to be using those values, and that because of that something might be wrong.

    Read perldoc -f our , perldoc vars and perldoc -f my to decide which one you need.

    Note that the vars documentation has this note:

    NOTE: The functionality provided by this pragma has been superseded by "our" declarations
    The docs are your friend.

    Update: I think I missed the obvious. Shouldn't it read as follows?

    $username = $common::auth::user; $admin = $common::auth::admin;
    Surely there is a nicer way of doing this? A small sub that returns the value you're after, perhaps?
      I actually use both variables throughout the script. The error occurs because I use $common::auth::user and $common::auth::admin only once to define the value of $username and $admin.
Re: supress an error
by Necos (Friar) on May 01, 2002 at 21:58 UTC
    I have a couple of questions.

    1.) Why are you defining variables in one package to export to another, when it is just as easy to define them in the program? You can always use Exporter to help you.

    Sample: (untested)

    package COMMON::AUTH; use strict; use Exporter; @COMMON::AUTH::ISA = qw(Exporter); our ($user,$admin); @EXPORT = qw($user $admin); $user = $ENV{'REMOTE_USER'}; $admin = 'admin'; 1;
    2.) In the earlier code sample, COMMON::AUTH is designed to be used as a module (with use or require). You might want to rethink your design. Why put "global" variables into a perl (.pl) file? Why not just define them in every script; or better yet, make small subs in your COMMON::AUTH module that return specific data. For example:
    package COMMON::AUTH; use strict; use Exporter; @COMMON::AUTH::ISA = qw(Exporter); @EXPORT = qw(user admin); sub user { return $ENV{'REMOTE_USER'}; #Make sure to untaint this data just i +n case } sub admin { return 'admin'; } 1;
    The cool thing about using subs to return this (fairly trivial) data is that you can do some other processing (for example, untainting) before you return.

    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    4650 W. Olympic Blvd.
    Los Angeles, CA 90019
    323-937-3210 ext. 224
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"
Re: supress an error
by boo_radley (Parson) on May 01, 2002 at 21:40 UTC
    I don't see any sense in using the full package name for those variables throught my program
    You could try exporting them.
    Or, since the values for these two items seems to be fairly constant, you could define them as constants, too.
      I will definatly look into that option. They are constant. Very constant.
Re: supress an error
by blakem (Monsignor) on May 02, 2002 at 08:16 UTC
    You can always lexically supress the warning if you're using 5.6 or later... In the following code $cat will trigger a warning but $dog won't.
    #!/usr/bin/perl use warnings; use strict; $main::cat = 'garfield'; { no warnings 'once'; $main::dog = 'spot'; }

    -Blake