Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Best practice for configuration variables stored in external file

by tunafish (Beadle)
on Sep 06, 2013 at 06:58 UTC ( #1052657=perlquestion: print w/replies, xml ) Need Help??
tunafish has asked for the wisdom of the Perl Monks concerning the following question:

So I was wondering what the best practice is for storing constants in an external file. I have scripts that I wrote before I heard of "use strict". The scripts share a configuration file, conf.pl, that defines things like database name, database user and password, paths to various folders, etc. Currently, the format is:

$constant_name = "constant_value";

I know this is wrong. What's the best way to do this?

Replies are listed 'Best First'.
Re: Best practice for configuration variables stored in external file
by tobyink (Abbot) on Sep 06, 2013 at 07:01 UTC

    Config::Tiny is a reasonably good solution.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Best practice for configuration variables stored in external file
by McA (Priest) on Sep 06, 2013 at 07:20 UTC

    Hi,

    I think your solution is not completely wrong. Put your constants into one or more perl modules an use (not import) them where you need it without polluting the namespace. Why shall a textfile which gets parsed by a module be better than a textfile (.pm) which gets parsed by perl itself?

    The question is: Do you need the ability to reconfigure at runtime (Constants are constant, aren't they)? Are the config constants bundled in a way, so that these constants belong to a component of your program. But this is the same problem as cutting your program into meaningful pieces (modules).

    Opinions from the illuminated monks appreciated.

    Best regards
    McA

      Constants belong into the code. Configuration options don't.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        I think then that I'm talking about "constants", not "configuration variables". They are not, at any point, edited from within the script.

        Would "use Module" then be the best way to handle this?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052657]
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-07-16 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (330 votes). Check out past polls.

    Notices?