Namespaces and require

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a fairly large suite of Perl scripts, and a configuration file
called which has some "configuration parameters" such as
where things are in the file tree, names of databases and tables and so
on. The intention is that this small file can be edited manually if needed.

When a stand-alone script is run it executes

require "" ;

which pulls in the definitions. This worked beautifully until I decided
that one new feature was best implemented as an object.

I therefore set up a package, let's call it dbadmin, which provides
methods to create an object, do something with it and finally output it
as HTML for display. This also works perfectly.

The problem is that if I require the configuration in main namespace it
is invisible in dbadmin and vice versa. If I put in two require
statements the clever behaviour of require means that only one does

At the moment I am getting at the definitions by writing things like
$Main::blivit, but is there a better, cleaner way of doing this?

Or in other works can I create a variable which is visible in all

Re: Namespaces and require

Quoted text here. Click to load it
Quoted text here. Click to load it

No, you can't. You have several options here:

    1. Refer to your config variables as $::blivit. It's a little
    clunky, but not too long, and has the advantage of making them stand

    2. Convert the config file into a real module, and 'use' it instead.
    Then you can export variables with Exporter. Something like

        package DBAdmin::Config;

        use base qw/Exporter/;
        our @EXPORT = qw(

        our $blivit = "...";

    3. If you think that's too much gunk in the config file, you can
    create a tiny module which requires the actual config file, and then
    exports its variables. Everywhere else you use the module instead of
    requiring the config file.

    4. You can manually import variables with

        package dbadmin;

        *blivit = $main::blivit;

    Not something you want to do a lot of, but it will work if you just
    need a few variables in a few places. If you want to avoid 'strict'
    failures, you will need to either declare the variables with 'our',
    or import them in a BEGIN block while in a different package, like

        package dbadmin;

        BEGIN {
            package main;
            *dbadmin::blivit = $blivit;
    (This is in fact exactly what Exporter does for you.)

    5. If you think listing all those variable names is too much work (I
    would :) ) you can move them into a hash and just export that:

        package DBAdmin::Config;

        use base qw/Exporter/;
        our @EXPORT = qw/%Config/;

        our %Config = (
            blivit => "...",

    You will need to change $blivit to $Config, of course. You
    may also want to apply Hash::Util::lock_hash to the hash after it's
    been populated, so you get typo checking on the keys and readonly


Re: Namespaces and require

Quoted text here. Click to load it

I would use a different name for the hash: Even %config is better, because
my mind automatically assumes %Config is the hash exported by Config:

perldoc Config


(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW: /

Re: Namespaces and require

Ben Morrow wrote:

Quoted text here. Click to load it

This is broadly what I have now.

Quoted text here. Click to load it

I really like this one. It's so clean and tidy that I'll probably
convert everything to it in version two.

Thanks very much for your help.

Site Timeline