I
Irving Kimura
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
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