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.

Replies are listed 'Best First'.
•Re: Generate Configuration files
by merlyn (Sage) on Aug 27, 2002 at 17:27 UTC
      I share your opinion about reinventing, and reuse of existing code. And that was, why I did look at several Template Modules, including the Template-Toolkit. The latter looked for my problem as if shooting at sparrows with canons (literal translation of a german saying).
      I preferred the 30 line solution over the installation of a new, some hundred kByte Moduleset at the customer's site. All constraints are known from our former OS/2 Rexx implementation.

      ... and at least it is not meant as a CPAN module, just a code-snippet. (Should I have posted it in discussions?)

      Nevertheless, I did not know your article before, it is a good example. Other projects require different solutions.

      Thank You for Your comment

      Ahh, Yet Another Templating Solution.

      Speaking of which, one possibility to take into account could be our fellow Rhandom's "new" Template::Alloy about which there's been some talk recently: what seems nice about it is that it supports several different templating schemes... and if I understand correctly, it should do so in a very efficient manner too. Thus it would allow one to pick the "best" from each...