Hello Grimy,
This is a nice idea, but unfortunately, as long as the regex contains metacharacters, it can’t be made to work, because $1 will never contain those metacharacters:
#! perl
use strict;
use warnings;
use Data::Dump;
my $re = 'aaa\s+bbb';
my %dic = ($re => 'new');
my $s = 'aaa bbb';
$s =~ s/($re)/$dic{$1}/;
print "\$1 = $1\n";
print "\$s = $s\n";
print "\%dic = ";
dd \%dic;
Output:
19:34 >perl 1011_SoPW.pl
Use of uninitialized value within %dic in substitution iterator at 101
+1_SoPW.pl line 10.
$1 = aaa bbb
$s =
%dic = { "aaa\\s+bbb" => "new" }
19:34 >
Sorry, :-(
|