B
bernd
Hi folks,
I have two perl progs, both expected to do the same, eval()ing a
pattern substitution expression using variables for the substitution
pattern and the replacement (to be used in a subroutine later on).
The first prog uses 'use strict' and lexical variables and looks like
this:
#!/usr/bin/perl -w
use strict;
my $old = 'test this one';
my $pat = 'this';
my $repl = 'that';
my $mods = '';
my $new;
eval('($new = $old) =~ s/$pat/$repl/ee . $mods');
print $new . "\n";
($mods can take a string containing one or more modifiers (like 'g')
to the s/// operator).
The output is
Use of uninitialized value in substitution iterator at (eval 1) line
1.
test one
In the second version I dispensed with 'use strict' and lexical
variables:
#!/usr/bin/perl -w
$old = 'test this one';
$pat = 'this';
$repl = 'that';
$mods = '';
eval('($new = $old) =~ s/$pat/$repl/ee . $mods');
print $new . "\n";
And the output is as expected:
Name "main::repl" used only once: possible typo at ./subst_nostrict.pl
line 5.
Name "main::new" used only once: possible typo at ./subst_nostrict.pl
line 10.
Name "main:ld" used only once: possible typo at ./subst_nostrict.pl
line 3.
Name "main:at" used only once: possible typo at ./subst_nostrict.pl
line 4.
Name "main::mods" used only once: possible typo at ./subst_nostrict.pl
line 6.
Unquoted string "that" may clash with future reserved word at (eval 2)
line 2.
test that one
Unfortunately, in the project in which the eval-stuff should be
integrated I have to use 'strict' and lexical scoping.
Can somebody explain the differences in the output and how I could
reach the desired output 'test that one'?
TIA
Bernd
I have two perl progs, both expected to do the same, eval()ing a
pattern substitution expression using variables for the substitution
pattern and the replacement (to be used in a subroutine later on).
The first prog uses 'use strict' and lexical variables and looks like
this:
#!/usr/bin/perl -w
use strict;
my $old = 'test this one';
my $pat = 'this';
my $repl = 'that';
my $mods = '';
my $new;
eval('($new = $old) =~ s/$pat/$repl/ee . $mods');
print $new . "\n";
($mods can take a string containing one or more modifiers (like 'g')
to the s/// operator).
The output is
Use of uninitialized value in substitution iterator at (eval 1) line
1.
test one
In the second version I dispensed with 'use strict' and lexical
variables:
#!/usr/bin/perl -w
$old = 'test this one';
$pat = 'this';
$repl = 'that';
$mods = '';
eval('($new = $old) =~ s/$pat/$repl/ee . $mods');
print $new . "\n";
And the output is as expected:
Name "main::repl" used only once: possible typo at ./subst_nostrict.pl
line 5.
Name "main::new" used only once: possible typo at ./subst_nostrict.pl
line 10.
Name "main:ld" used only once: possible typo at ./subst_nostrict.pl
line 3.
Name "main:at" used only once: possible typo at ./subst_nostrict.pl
line 4.
Name "main::mods" used only once: possible typo at ./subst_nostrict.pl
line 6.
Unquoted string "that" may clash with future reserved word at (eval 2)
line 2.
test that one
Unfortunately, in the project in which the eval-stuff should be
integrated I have to use 'strict' and lexical scoping.
Can somebody explain the differences in the output and how I could
reach the desired output 'test that one'?
TIA
Bernd