perl: storing regex in array variables trouble

hi
this is an example of code:

use strict;
use warnings;
open FILE, "/tmp/result_2";
my $regex="\\[INFO\\] Starting program ver. (.*)";
my $res="Program started, version <$1> - OK.\n";
while (<FILE>) {
if ($_ =~ /($regex)/) {
   print "$res";
    }
}
close FILE;

This finds $regex and print out the $res, but "$1" doesn't work. I've tried <\$1> as well and other variations. How to make it work? :slight_smile:

With a string in double quotes, the $1 gets interpolated at the time you define the string. Try single quotes and eval print $res;

in this redaction:

use strict;
use warnings;
open FILE, "/tmp/result_2";
my $regex="\\[INFO\\] Starting program ver. (.*)";
my $res='Program started, version <$1> - OK.\n';
while (<FILE>) {
if ($_ =~ /$regex/) {
   eval print $res;
    }
}
close FILE;

doesn't work as well :frowning:

I guess you need to eval the matching as well.