Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Substituting tokens into configuration values

by dbush (Deacon)
on Mar 20, 2003 at 17:42 UTC ( [id://244661]=note: print w/replies, xml ) Need Help??


in reply to Substituting tokens into configuration values

Many thanks (and a ++ tomorrow) to zby, crouchingpenguin, dakkar, robartes, Jenda and hiseldl for their insightful replies.

The solution I have come up with is to use the tie functionality within Config::IniFiles and a slightly modified version of robartes regular expression approach. The while loop handles nested tokens (unlikely as they are in my case).

The following is a code snippet of the approach I intend to use. Comments welcome.

#perl -w use strict; use warnings; use Config::IniFiles; my ($appname, $cmdline, $priority, $szProcess); my %config; tie %config, 'Config::IniFiles', (-file => 'service.ini', -nocase => 1 +); my @processesToStart = map { $config{'service'}{$_} } grep { /^start[0-9]+/ } keys %{$config{'service'}}; foreach $szProcess (@processesToStart) { #Break process down into it's parts ($appname, $cmdline, $priority) = split /;/, $szProcess; #Process cmdline while ($cmdline =~ s/%([^%]+)-([^%]+)%/$config{lc $1}{lc $2}/eg) {} #Log and run it already print $cmdline, "\n"; } __END__ Text of service.ini: [GENERAL] scheduledb=e:\users\dbush\schedule.db [SERVICE] debug=1 interval=5 start1=browser.exe;browser;NORMAL_PRIORITY_CLASS start2=schedule.exe;schedule "localhost:%general-scheduledb%";NORMAL_P +RIORITY_CLASS Output: browser schedule "localhost:e:\users\dbush\schedule.gdb"

The other modules mentioned look interesting (esp. Win32::Daemon::Simple) but I'm afraid I'm up against a bit of a deadline. I also didn't mention that the configuration file must be a standard ini as I share it with some C programs.

Thanks again,
Dom.

Update: Corrected output typo.

Replies are listed 'Best First'.
Re: Re: Substituting tokens into configuration values
by Jenda (Abbot) on Mar 20, 2003 at 21:34 UTC

    I would suggest one little change to the regexp:

    while ($cmdline =~ s/%([^%-]+)-([^%]+)%/$config{lc $1}{lc $2}/eg)
    This way it should be quicker since it does not force Perl to backtrack after it slurps up the whole general-scheduledb.

    Of course it's not entirely equivalent. If your INI contained %foo-bar-baz%, then your regexp would match it like %(foo-bar)-(baz)% and mine as %(foo)-(bar-baz)%. I don't think it matters.

    Jenda

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://244661]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found