Eyck has asked for the wisdom of the Perl Monks concerning the following question:
What is the best way for config file to contain perl code, for example stub routines, right now I'm usually slurping config file via required
#... require "/etc/myname.cfg"; ..
I tried config file modules but they're usually not flexible enough, and require way more code and awareness then simple require... and such simple method work great for specifying values, arrays, hashes etc... but what about subs?
Back to the point, I would like to specify something as a callback, or generally would like to allow defining perl code in config... my current solution looks like that:
And then I just eval lines from Actions array.# /etc/myscript.cfg $somehash = { 'Somestuff'=> { #... }, 'Someotherstuff'=> { #.... 'Actions'=> [ "print \"hello world\";", ] }, }
This looks awkward in configfile, because of all this quoting required, and editors won't hilight this correctly, I'm having problems accessing variables from such eval'd code, etc,
to sum this up - code specified in configfile looks extremely ugly and unclear.
Are there better ways to do that?
UPDATE: Based on feedback I updated my method to something like this:
package Scriptname; require "/etc/scriptname.cfg"
This one is a little easier on the eyes, instead of eval I usepackage Scriptname; $somehash=> { 'Something'=> { #.. stuff .. 'Sub' => sub { print "Hello world, "; print "here is my local var: $var\n"; }; } };
my $sub=$somehash->{'Something'}->{'Sub'}; &$sub() if defined($sub);
Still not perfect, but it's a serious improvement over the mess I used to create... thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What's the best way to put perl code in config file?
by dave_the_m (Monsignor) on Sep 21, 2004 at 10:51 UTC | |
|
•Re: What's the best way to put perl code in config file?
by merlyn (Sage) on Sep 21, 2004 at 10:52 UTC | |
by gothic_mallard (Pilgrim) on Sep 21, 2004 at 11:03 UTC | |
by Eyck (Priest) on Sep 21, 2004 at 10:55 UTC | |
by halley (Prior) on Sep 21, 2004 at 13:44 UTC | |
by SpanishInquisition (Pilgrim) on Sep 21, 2004 at 14:55 UTC | |
|
Re: What's the best way to put perl code in config file?
by gothic_mallard (Pilgrim) on Sep 21, 2004 at 10:54 UTC | |
|
Re: What's the best way to put perl code in config file?
by TedPride (Priest) on Sep 21, 2004 at 11:20 UTC |