in reply to Uninitialized scalar inside a scalar?
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.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Uninitialized scalar inside a scalar?
by n00bsauce (Initiate) on Jan 07, 2009 at 22:06 UTC | |
by kyle (Abbot) on Jan 07, 2009 at 22:15 UTC | |
by jethro (Monsignor) on Jan 07, 2009 at 22:44 UTC | |
by gwadej (Chaplain) on Jan 07, 2009 at 23:18 UTC |