You know why single quotes don't work; variables don't get interpolated in single-quoted strings.
Double quotes don't work because the value of the string which interpolates $1 gets evaluated at the point of assignment, which occurs before the regular expression match.
You need to delay the evaluation of $1 until the point at which it contains a value you want to interpolate into a string. One way to do that is to define $outputString within the if block. Another approach is to use an anonymous function.
use 5.010; my $make_output_string = sub { "this is a $_[0]" }; ... if ($sample_data =~ m/$search_string/) { say $make_output_string->($1); }
(You don't have to use 5.10 to make this work, but I really like say.)
In reply to Re: Uninitialized scalar inside a scalar?
by chromatic
in thread Uninitialized scalar inside a scalar?
by n00bsauce
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |