Configuring a machine in a big development team for developer, function, system test or production debugging is done here by using different configuration files. But managing a dozen files is a pain. The Idea is to generate them on the fly with a set of Variables collected by a little menu or from the environment.
This is my first outline of that theme in Perl (and my first try in this forum). Maybe you can use it or improve it.
Given a template-file which some (sample-)contents:
PROD_RT = $(RUNTIME)
# other variant
PROD_RT = ~/prod/$(ENVIRONMENT)/$(VERSION)/bin
PLAN_ID = $(DATABASE)
This will replace it
#!perl.exe -w replace for real OSs
use strict;
use warnings;
my $debug = 1;
sub get_value # used in substitution
{
my ($ref_values, $key) = @_;
print " look for $key ?\n" if $debug;
my $result = ( exists $ref_values->{$key} ) # if exists ...
? $ref_values->{$key} # return Value
: "*ERR*" # else return *ERR*
}
sub generate_file
{
my ($templ_file, $ref_inivars, $target_file) = @_;
open IN, "<$templ_file" or return -1;
open OUT, ">$target_file" or return -2;
while ( my $line = <IN> )
{
$line=~s/\$\((\w+)\)/&get_value($ref_inivars,$1)/ge;
print OUT $line;
}
return 0;
}
#------------------
# Use the Code
generate_file ( "./template/inifile.ini",
{ %ENV, # just to demonstrate
'RUNTIME' => "~/prod/test/v1.2/bin",
'VERSION' => "v1.2",
'ENVIRONMENT' => "test"
'DATABASE' => "L63DTST",
},
"result.ini" );
Use a dialog to collect the options for the Environment
(Database, Test or Production etc) give the collected variables requested files into the Routine and generate the configuration files. Then use the selected environment.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.