Dear Monks,
I am new two Perl. That is the reason I would like to kindly ask you for some help or ideas.
Simply puth the goal, I would like to achieve is described as:
I have two files to work on, Config and Template. Based on them a third one should be generated.
The first one (Config) includes different sections.
The second one should also includes the same sections, with different information.
I am using some "delimeter" line which checks, for a new, section, coming in the first file. Each new sections is saved, as the keys of a hash, and their respective section's following info, as the values for the respective key. Then we go through the second file. When an occuring matching section definition is also found in the second one, we first copy, the contents of the Config section from the Config file (the first file), and then we append to it the contents of the second one (Template).
I would like to be able to define, some options, such as the directory, where the the particular section's file, should be created if the section's file, should be created at all, etc. It would be perfect if the order of definition, does not matter.
So I would like to have in the Config file, on for e.g. on the same line, as the section starting delimeter definition line, also information, for example looking as:
.SECTION ( ABOVEALL ) (targetdir=<'some relative path, w.r.t the dir at which the script has been called, or real path>, buildOn = <yes/no OR 0/1 >, etc options)
some information from the .SECTION (aboveall) following
.SECTION (Someothersection) (someothertargetdir= < >, and so on)
Maybe it is nice idea, to extract, the info, from the Config, and then somehow, to check, from this line, for matching keys from the default %modifiers hash. If no options are defined for the particular line, then just use the defaults. Problems, which I am currently seeing is that, in the Template file, the delimeter doesnot match anymore, due to the existance of the options on the same line.
the example code, which I was thinking of and having now is:
#!/usr/bin/env perl
my $Configuration = 'Config.txt';
my $MakeTemplate = 'MakeTemplate.txt';
my $mgen = "MakefileGeneration";
# read the config file to an assoc array
open(CONFIG, $Configuration) || die "$mgen> Couldn't open the Configur
+ation file!\n";
my %config = ();
my $curSection = '';
my $delimName = '\.SECTION\s*\(\s*(\w+)\s*\)\s*(\(\s*\w+\s*=\s*<\s*[a-
+zA-Z\/\\\.:]+\s*>\s*\))';
my %modifiers = ();
#my %modifiers = {'targetdir', '.', 'mod1', ''} some #defaults, such a
+s current directory and so on.
my $curSectionOpt;
my $something;
while(<CONFIG>) {
if (/$delimName/) {
$curSection = $1;
$modifiers{$curSection} = $2;
}
else{
$config{$curSection} .= $_;
}
}
close CONFIG;
my @mytypes = ('DEFAULT','BEH'); # just an example
open(TARGETMAKEFILE,">");
open(MAKETEMPLATE, $MakeTemplate) || die "$mgen> Couldn't open the Mak
+efile Template file!\n";
my $copyOn = 0;
while(//g){
grep(print,keys(%modifiers));
# print("\n\n");
grep(print,values(%modifiers));
# print("\n\n\n");
}
while(<MAKETEMPLATE>) {
if (/$delimName/){
my $section = $1;
$copyOn = grep(/$section/i,@mytypes);
print TARGETMAKEFILE $config{$section} if $copyOn;
} elsif ($copyOn) {
print TARGETMAKEFILE;
}
}
close MAKETEMPLATE;