Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Namespaces and require

Reply
Thread Tools

Namespaces and require

 
 
Robert Billing
Guest
Posts: n/a
 
      03-26-2009
I have a fairly large suite of Perl scripts, and a configuration file
called db_config.pl 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 "db_config.pl" ;

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

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
namespaces?
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-27-2009
Ben Morrow <> wrote in
news:avit96-:

> 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 => "...",
> ...,
> );


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

Sinan

--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
 
Robert Billing
Guest
Posts: n/a
 
      03-27-2009
Ben Morrow wrote:
> Quoth Robert Billing <>:


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

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


This is broadly what I have now.

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


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.
 
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
[Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com Python 56 12-27-2012 01:25 PM
Namespaces in Vs2005 and ASP.net 2.0 Tariq ASP .Net 1 11-29-2005 07:14 PM
require namespaces Trans Ruby 2 09-29-2005 05:55 AM
Namespaces and Naming conventions Floppy Jellopy ASP .Net 4 07-21-2005 01:36 PM
@Import Syntax and Importing Namespaces in global.asax file D. Shane Fowlkes ASP .Net 1 01-13-2004 02:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57