in reply to How to apply modifiers to a substitution via variable?
Grandfather's answered your first question already.
In answer to your second, you can use eval for this as well, but make sure you put apostrophes (') around the $1 in the assignment, rather than quotes ("), or you'll get an error. Here's the code:
#!/usr/bin/perl use strict; use warnings; my $string = "aaaaabbbbaaaaaa"; my $replace = '$1'; eval "\$string =~ s/a+(b+)a+/$replace/g"; print $string;
bbbb
Note that you need to use quotes (") around the eval statement so that $1 will interpolate, but (as Grandfather did) you still have to escape $string with the backslash "\" in the same line (\$string), so it won't interpolate.
|
|---|