Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

config package file

by martymart (Deacon)
on Nov 19, 2004 at 10:18 UTC ( [id://408970]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

I want like to put a load of config variables/arrays/hashes into one handy package configs.pm for access by a number of scripts. Here's how I would do it:
configs.pm a file containing useful info
package configs; use Exporter; use warnings; use strict; our @ISA = ("Exporter"); our @EXPORT = qw( %instarray ); our %instarray = (aa => "aakeyword", bb=> "bbkeyword", cc => "cckeyword", dd => "ddkeyword", ee => "eekeyword", ff => "ffkeyword", ); 1;
config.pl an example of a file that uses the above package:
#!usr/bin/perl -w use strict; use warnings; use configs; foreach my $tag (keys %instarray){ print "$tag - $instarray{$tag} \n"; }
Are there other (better?) ways to achieve this, any problems with using "our" in this way?
Thanks in advance,
Martymart

Replies are listed 'Best First'.
Re: config package file
by davorg (Chancellor) on Nov 19, 2004 at 10:41 UTC
    Are there other (better?) ways to achieve this, any problems with using "our" in this way?

    No, that seems a pretty standard way to do it.

    A couple of small points with your code tho':

    • Names in all lower case are usually saved for "pragmatic" modules - i.e. those that change the way the program executes (like strict and warnings). You'd probably be better advised to call it YourAppName::Config.
    • Your shebang line looks broken. Shouldn't it be /usr/bin/perl (with a leading '/')?
    • If you have "use warnings" then you no longer need '-w'.
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: config package file
by Mutant (Priest) on Nov 19, 2004 at 12:02 UTC

    I'd prefer to use @EXPORT_OK, so I can see where the %instarray has come from. It's usually preferable to be explicit.

    Alternatively, to add another degree of maintainability, but at the cost of more typing, you could export nothing and access everything through the full package name. That way when you're getting values out of your hash (or whatever), you know exactly where they've been set. This sort of thing is a huge benefit when you're reading someone else's code - otherwise you end up scanning the whole file trying to figure out where they set a variable.

Re: config package file
by bobf (Monsignor) on Nov 19, 2004 at 23:23 UTC

    If all you're trying to do is create a module to hold globals, then what you're doing certainly works (definitely use @EXPORT_OK instead of @EXPORT, though). Otherwise, you may want to consider Config::Vars instead of creating your own module. I've never used it, but it looks like it could be helpful (depending on how fancy you want to get).

    If the config values are intented to be accessible by users, you might want to use a more user-friendly flat-file format so they aren't poking around the code in your configs.pm module. There are a bunch of config modules on CPAN under the Config namespace. I use Config::General, but I also considered Config::IniFiles and Config::Simple.

    HTH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://408970]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-09-18 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (24 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.