in reply to qr and substitution
use strict; use warnings; my $string = "This is a test."; my $re = "s/test/TEST/i"; eval "if( \$string =~ $re ) { print qq/Yep\n/; }"; warn $@ if $@; print $string, "\n";
qr// allows you to pass the meat of a regular expression around compiled within a scalar variable. qr// doesn't allow for the regexp quotish operators to be embedded within (as in qr[s///].... that doesn't work), because it is not intended to contain any kind of code except for the meat of a regexp. If you want to pass a piece of actual code around as though it were a string, the way to execute the string is to eval it.
As Aristotle pointed out, you always have to think carefully when you pass quotes or quote-like operators within a quoted string. But if you ask how to do a tricky thing, you get a tricky solution. ;) eval is never for the faint of heart. As Aristotle points out, it's safer and cleaner to pass a code ref rather than stringified code.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: qr and substitution
by Aristotle (Chancellor) on Oct 13, 2003 at 19:04 UTC |