D
Dietrich
Challenge:
I have variables in a configuration file that read ${profile} or
${altdatabase}, etc. They are also the pattern that I am using for a
substitution, but the challenge is that they get interpolated more
(resolved/dereferenced) than I would like. I have found a way around
them, but that means removing the use strict pragma and -w option in
the magic cookie ( top line of of a shell or perl script).
I initially had substitution problems. The pattern on the left was
never found, as it resolved what $string_match would be and that was
not a found regexp. This is what I mean by the title, Over
Interpolation of Variables in a Substitution.
My method to get the script to work is to add an extra $ to the
substitution code as follows, in order to prevent the over
interpolation:
29 s/$$string_match/$resolved_value/;
When use the apply the -w option to the Perl interpreter, the script
still works, but it gives an message to the xterminal as follows:
chd07 dev3cst $ timex populate_invars.esp-new ESP_CST_invars
Use of uninitialized value in regexp compilation at
../populate_invars.esp-new line 29, <INVARS_READ> line 15.
Use of uninitialized value in regexp compilation at
../populate_invars.esp-new line 29, <INVARS_READ> line 18.
real 0.23
user 0.17
sys 0.06
Here shows exactly what I want to happen with the output ( the
INVARS_NW file handle as shown in the code below).
chd07 dev3cst $ diff $OLDPWD/ESP_CST_invars $OLDPWD/ESP_CST_invarsnw
15c15
< DB_MAIN_NAME = '${profile}'
---
< DB_ARCHIVE_NAME = '${altdatabase}'
---
So when I utilize the use strict pragma, the output totally is not
what I want.
chd07 dev3cst $ timex populate_invars.esp-new ESP_CST_invars
Can't use string ("${profile}") as a SCALAR ref while "strict refs" in
use at ./populate_invars.esp-new line 30, <INVARS_READ> line 15.
real 0.20
user 0.16
sys 0.04
The Code:
This is the entire script. This version does not have the -w option
being used, nor the use strict pragma:
1 #! /opt/perl/bin/perl
2
3 # Dietrich Martin
4 # July 15, 2004
5 #- A test script for esp_put changes.
6
7 use Env;
8 use English;
9
10 # $invars_file will be passed from esp_put.
11 my $invars_file=$ARGV[0];
12 my $invars_filenw=$invars_file."nw";
13
14 open (INVARS_NW, ">${CBRCONFIGPATH}/ESP_NEW/$invars_filenw")
|| die "Can't open $invars_filenw: $OS_ERROR";
15
16 open (INVARS_READ, "${CBRCONFIGPATH}/ESP_NEW/$invars_file")
|| die "Can't open $invars_file: $OS_ERROR";
17 while (<INVARS_READ>)
18 {
19 # When there is the ${'.*'} regexp, perform substitution
to record the environmental value.
20 if ($ARG =~ /\${(.*)}/)
21 # Look for the ${} pattern as in ${profile}.
22 {
23 my $string_match = $MATCH;
24
25 my $resolved_value = `echo $string_match`;
26
27 chomp ($resolved_value);
28
29 s/$$string_match/$resolved_value/;
30
31 print INVARS_NW $ARG;
32 }
33 else
34 # In case there is no ${'.*'} regexp, copy the record
from input anyway (comments & blank lines).
35 {
36 print INVARS_NW $ARG;
37 }
38 }
39
40
41 close (INVARS_READ) || die "Can't close $invars_file:
$OS_ERROR";
42 close (INVARS_NW) || die "Can't close $invars_filenw:
$OS_ERROR";
Goal:
To be able to do this same variable substitution, while using the use
strict pragma and -w option to the interpreter.
Reference:
comp.lang.perl
File Handle Reading Blues: Rereading a File Handle for Input
Thank you for your time.
I have variables in a configuration file that read ${profile} or
${altdatabase}, etc. They are also the pattern that I am using for a
substitution, but the challenge is that they get interpolated more
(resolved/dereferenced) than I would like. I have found a way around
them, but that means removing the use strict pragma and -w option in
the magic cookie ( top line of of a shell or perl script).
I initially had substitution problems. The pattern on the left was
never found, as it resolved what $string_match would be and that was
not a found regexp. This is what I mean by the title, Over
Interpolation of Variables in a Substitution.
My method to get the script to work is to add an extra $ to the
substitution code as follows, in order to prevent the over
interpolation:
29 s/$$string_match/$resolved_value/;
When use the apply the -w option to the Perl interpreter, the script
still works, but it gives an message to the xterminal as follows:
chd07 dev3cst $ timex populate_invars.esp-new ESP_CST_invars
Use of uninitialized value in regexp compilation at
../populate_invars.esp-new line 29, <INVARS_READ> line 15.
Use of uninitialized value in regexp compilation at
../populate_invars.esp-new line 29, <INVARS_READ> line 18.
real 0.23
user 0.17
sys 0.06
Here shows exactly what I want to happen with the output ( the
INVARS_NW file handle as shown in the code below).
chd07 dev3cst $ diff $OLDPWD/ESP_CST_invars $OLDPWD/ESP_CST_invarsnw
15c15
< DB_MAIN_NAME = '${profile}'
---
18c18DB_MAIN_NAME = 'Nixon'
< DB_ARCHIVE_NAME = '${altdatabase}'
---
DB_ARCHIVE_NAME = 'cbrde29'
So when I utilize the use strict pragma, the output totally is not
what I want.
chd07 dev3cst $ timex populate_invars.esp-new ESP_CST_invars
Can't use string ("${profile}") as a SCALAR ref while "strict refs" in
use at ./populate_invars.esp-new line 30, <INVARS_READ> line 15.
real 0.20
user 0.16
sys 0.04
The Code:
This is the entire script. This version does not have the -w option
being used, nor the use strict pragma:
1 #! /opt/perl/bin/perl
2
3 # Dietrich Martin
4 # July 15, 2004
5 #- A test script for esp_put changes.
6
7 use Env;
8 use English;
9
10 # $invars_file will be passed from esp_put.
11 my $invars_file=$ARGV[0];
12 my $invars_filenw=$invars_file."nw";
13
14 open (INVARS_NW, ">${CBRCONFIGPATH}/ESP_NEW/$invars_filenw")
|| die "Can't open $invars_filenw: $OS_ERROR";
15
16 open (INVARS_READ, "${CBRCONFIGPATH}/ESP_NEW/$invars_file")
|| die "Can't open $invars_file: $OS_ERROR";
17 while (<INVARS_READ>)
18 {
19 # When there is the ${'.*'} regexp, perform substitution
to record the environmental value.
20 if ($ARG =~ /\${(.*)}/)
21 # Look for the ${} pattern as in ${profile}.
22 {
23 my $string_match = $MATCH;
24
25 my $resolved_value = `echo $string_match`;
26
27 chomp ($resolved_value);
28
29 s/$$string_match/$resolved_value/;
30
31 print INVARS_NW $ARG;
32 }
33 else
34 # In case there is no ${'.*'} regexp, copy the record
from input anyway (comments & blank lines).
35 {
36 print INVARS_NW $ARG;
37 }
38 }
39
40
41 close (INVARS_READ) || die "Can't close $invars_file:
$OS_ERROR";
42 close (INVARS_NW) || die "Can't close $invars_filenw:
$OS_ERROR";
Goal:
To be able to do this same variable substitution, while using the use
strict pragma and -w option to the interpreter.
Reference:
comp.lang.perl
File Handle Reading Blues: Rereading a File Handle for Input
Thank you for your time.