in reply to variable interpolation in regexps
Maybe the following code will illustrate it all a little better.my $thing = 'THING'; my $regexp = "some$thing"; # now $regexp is 'someTHING'
#!/usr/bin/perl -W use strict; my $thing1 = 'THING'; my $thing2 = '$thing'; sub test { my $regex = shift; local $_ = 'some thing someTHING some$thing'; printf " %15s =~ %s", $regex, $_; print " == TRUE , matched [", $&, "]" if /$regex/; print "\n"; } test ( 'some$thing1' ); test ( "some$thing1" ); test ( 'some$thing2' ); test ( "some$thing2" ); test ( quotemeta 'some$thing1' ); test ( quotemeta "some$thing1" ); test ( quotemeta 'some$thing2' ); test ( quotemeta "some$thing2" );
|
|---|