Reputation: 917
I would like to read a matching pattern from a config file using Config::INifiles. I.e. everything is contained in variables.
Matching works fine:
my $user = "CN=vpn,ou=test";
my $pattern = 'CN=(.*),ou=test';
if ($user =~ /$pattern/) {
print "match\n";
print $1;
};
But now I would also like to access the first match using $1
, which I would also like to read from the config file.
Is there a way to achieve this?
my $user = "CN=vpn,ou=test";
my $pattern = 'CN=(.*),ou=test';
my $m = "\\$1";
if ($user =~ /$pattern/) {
print "match\n";
print $m;
};
Upvotes: 0
Views: 83
Reputation: 18378
Probably this might help you:
my $user = 'CN=vpn,ou=test';
my $pattern = 'CN=(.*),ou=test';
my $m = 1; # number of capturing group
if (my @c = $user =~ /$pattern/) {
print "match\n";
print $c[$m-1];
};
Upvotes: 2
Reputation: 385506
You can view $m
as Perl code to be executed, or as a template. The latter is the much better approach. You can use String::Substitution's interpolate_match_vars
as your template processor.
use String::Substitution qw( interpolate_match_vars last_match_vars );
my $interpolated = interpolate_match_vars($m, last_match_vars());
Upvotes: 0