I am attempting to use Perl's Regular Expression Substitution with evaluation to help make some config files more dynamic during a Clearcase -> Git migration. The Clearcase system is highly dependent on the /vob/ directory, but we need to make this more dynamic for our Jenkins builds to be happy. I'm trying to reduce the likelihood that I break the Clearcase builds while migrating over.
I have a configuration file that is a text file with a path per line:
/vob/config/file1
/vob/config/file2
/vob/config/file3
This configuration does some additional stuff with those configuration files. The orchestration of that "stuff" is managed by a Perl script. I would like to have a few environment variables ("VOB_FOO") that I can override when I run the script.
I'm a complete novice with Perl, so what I thought was to use the Perl environment variable syntax, do a regex on it and evaluate the substitution results in-line as I'm processing the file.
I want my new config file to have explicit $ENV{'VOB_FOO'} entries in the file, so the file would become:
$ENV{'VOB_FOO'}/config/file1 -> /home/me/foo/config/file1
$ENV{'VOB_FOO'}/config/file2 -> /home/me/foo/config/file2
$ENV{'VOB_FOO'}/config/file3 -> /home/me/foo/config/file3
And the resulting regular expression substitution+evaluation would turn into (if VOB_FOO=/home/me/foo) :
$ENV{'VOB_FOO'}/config/file1 -> /home/me/foo/config/file1
$ENV{'VOB_FOO'}/config/file2 -> /home/me/foo/config/file2
$ENV{'VOB_FOO'}/config/file3 -> /home/me/foo/config/file3
My regular expression matches fine and it appears the substitution is working, but the evaluation part of the substitution is not, and I could use some assistance here. I get a successful match, but the substitution comes off as:
$ENV{'VOB_FOO'}/config/file1 -> $ENV('VOB_FOO'}/config/file1
$ENV{'VOB_FOO'}/config/file2 -> $ENV('VOB_FOO'}/config/file2
$ENV{'VOB_FOO'}/config/file3 -> $ENV('VOB_FOO'}/config/file3
Is there any caveat to this evaluation or some way I can make this work correctly? Here is my code:
## See if we need to substitute an environment variable (e.g., is there a $ENV{} anywhere?)
## s - substitute through regular expressions (s/foo/bar/e)
## e modifier evaluates replacement as perl statement
{
use re 'debugcolor';
# this is for debugging only - I want to substitute
# grab the $ENV('VOB') string from the file and substitute
# I may have multiple environment variables that I have to
# contend with.
my $vob = $ENV{'VOB'};
print $vob;
print "\n";
my $regexp = qr/(\$ENV\{[\'][\w]*[\']\})/;
if( $second =~ m/$regexp/ )
{
print "Found the regexp; attempting substitution.\n";
$second =~ s/$regexp/$1/e;
}
else
{
print $regexp + "\n";
print $second + "\n";
print "Did not find the regexp\n";
}
}
I am also open for critique or suggestions on a better way to do this - I'm not tied to this approach or code while I'm working through making this happen.