Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Programmer's notebook: config files

Reply
Thread Tools

Programmer's notebook: config files

 
 
Irving Kimura
Guest
Posts: n/a
 
      01-18-2004



I'm relatively new to Perl, and I'm still learning the basic patterns
of good Perl programming. Here's one I'm having to deal with right
now: config files.

Often programs/scripts can have so many system-dependent options
that it is not practical for them to be specified command-line
flags. In these cases, a common strategy is to use configuration
files that the program reads at start-up. What's your favorite
way to handle system-dependent constants?

One straightforward approach is to define a simple syntax for the
configuration file (e.g. separate lines consisting of VAR=val pairs,
plus comments and blank lines), and having one's program parse this
file to get the configuration parameters to use.

But as I learn more about Perl, I see that another possibility is
to have a file (e.g. My_Module/Constants.pm) with format such as
this:

package My_Module::Constants;
use constant VAR1 => var1;
use constant VAR2 => var2;
# etc.

and then put the line

use My_Module::Constants;

at the beginning of every script that needs these configuration
variables. Then, the rest of the code can use these configuration
variables like this: my $foobar = My_Module::Constants::VAR1.
One advantage this approach has over the previous one is that it
gets perl to parse the config file for us.

This last technique looks to me like the way to go, at least for
programs that are expected to pay attention to no more than one
configuration file (as opposed to those that must deal with end-user
customizations), but I'm still too green with Perl, and I wonder
what pitfalls this approach may have, and whether there is a better
way to accomplish this task in Perl (yeah, I know about TMTOWTDI).

Thanks for your comments.

Irv
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      01-18-2004
"Irving Kimura" <> wrote in message
news:bueq2k$m2a$...
>

[snipped discussion about config files]
> ...
> But as I learn more about Perl, I see that another possibility is
> to have a file (e.g. My_Module/Constants.pm) with format such as
> this:
> ...
> and then put the line
>
> use My_Module::Constants;
>
> at the beginning of every script that needs these configuration
> variables. Then, the rest of the code can use these configuration
> variables like this: my $foobar = My_Module::Constants::VAR1.
> One advantage this approach has over the previous one is that it
> gets perl to parse the config file for us.


it all depends, of course.
it depends on whether the config is changed/set at install time
or frequently.
it depends on whether the config should be user-editable
it depends on whether the scripts using it are all in one location,
or if they are scattered all over, and maybe run by different users.

you have a few possibilities:
local configuration file, parsed by program/module
global configuration file (/etc or something)
require conf.pl
use MyModule::Conf

the module is pleasant if it needs to be used by various scripts,
say cgi, admin and cron scripts, all run under different environments,
and by different users. then there is no problem finding the config,
the 'use' command finds it for you.
the downside is that it is slightly more trouble if you need to edit it.
in that case you might have the module contain the location of the
editable conffile.

gnari




 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-18-2004
Irving Kimura <> wrote:

> use constant VAR1 => var1;

^^^
^^^

That is a strange choice of name for something that is not variable.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-19-2004
Irving Kimura <> wrote in comp.lang.perl.misc:
>
>
>
> I'm relatively new to Perl, and I'm still learning the basic patterns
> of good Perl programming. Here's one I'm having to deal with right
> now: config files.
>
> Often programs/scripts can have so many system-dependent options
> that it is not practical for them to be specified command-line
> flags. In these cases, a common strategy is to use configuration
> files that the program reads at start-up. What's your favorite
> way to handle system-dependent constants?
>
> One straightforward approach is to define a simple syntax for the
> configuration file (e.g. separate lines consisting of VAR=val pairs,
> plus comments and blank lines), and having one's program parse this
> file to get the configuration parameters to use.


There are a few modules on CPAN that do that. A colleague of mine
has been using Config::IniFiles to configure a print server. It
has supported 2500+ lines of configuration data with lots of changes
over the last few years, but there are other modules.

> But as I learn more about Perl, I see that another possibility is
> to have a file (e.g. My_Module/Constants.pm) with format such as
> this:
>
> package My_Module::Constants;
> use constant VAR1 => var1;
> use constant VAR2 => var2;
> # etc.
>
> and then put the line
>
> use My_Module::Constants;
>
> at the beginning of every script that needs these configuration
> variables. Then, the rest of the code can use these configuration
> variables like this: my $foobar = My_Module::Constants::VAR1.
> One advantage this approach has over the previous one is that it
> gets perl to parse the config file for us.


[advantages of this approach]

That's always an alternative. There are also downsides.

Maintainers of the configuration must understand some Perl to do their
job. They also must be trusted since they can make the final job do
*anything*.

It also blurs the distinction between code and data. There will always
be a temptation to put a little routine or two in the configuration
file because it's convenient. If you don't exercise strict discipline,
you end up with stuff in the configuration that doesn't belong there.

Anno
 
Reply With Quote
 
Irving Kimura
Guest
Posts: n/a
 
      01-19-2004
In <bugh9g$62n$> (Anno Siegel) writes:

>Maintainers of the configuration [...]
>must be trusted since they can make the final job do
>*anything*.


I see. But wouldn't this concern apply to any easily editable
script?

>It also blurs the distinction between code and data. There will always
>be a temptation to put a little routine or two in the configuration
>file because it's convenient. If you don't exercise strict discipline,
>you end up with stuff in the configuration that doesn't belong there.


Good point.

This is very useful feedback. Thank you very much.

Irv
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-19-2004
Irving Kimura <> wrote in comp.lang.perl.misc:
> In <bugh9g$62n$>
> (Anno Siegel) writes:
>
> >Maintainers of the configuration [...]
> >must be trusted since they can make the final job do
> >*anything*.

>
> I see. But wouldn't this concern apply to any easily editable
> script?


Yes, but unlike a config file, normal scripts don't *have* to be easily
editable.

Anno
 
Reply With Quote
 
bill
Guest
Posts: n/a
 
      01-19-2004
In <bugjt7$8go$> Irving Kimura <> writes:

>In <bugh9g$62n$> (Anno Siegel) writes:


>>Maintainers of the configuration [...]
>>must be trusted since they can make the final job do
>>*anything*.


>I see. But wouldn't this concern apply to any easily editable
>script?


Just run the rest of your script through an obfuscator.

bill

 
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
encrypting config file that aren't web config files... Ollie Riches ASP .Net 1 12-04-2008 04:59 PM
dll config and web.config and Label Expressions (binding label text to dll config settings) CSharpner ASP .Net 0 04-09-2007 09:00 PM
Windows Application App.config Files vs. web.config Nick Johnson ASP .Net Web Services 0 03-12-2007 07:21 PM
Default Config Files or Basic Config Steve Marshall Cisco 2 11-05-2003 10:25 PM
Config files in .war files? Miguel De Anda Java 2 08-15-2003 08:14 AM



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