in reply to Re^2: Variable substitute and capturing
in thread Variable substitute and capturing

It did work .. i'm guessing cause $search is from qr// ..
use strict; use warnings; my $s = "foobar"; my $search=qr/(foo)(bar)/; my $replace="\$1-\$2"; my $perl = "\$s =~ s/$search/$replace/"; warn $perl; eval $perl; print $s; __OUTPUT__ $s =~ s/(?-xism:(foo)(bar))/$1-$2/ at -e line 1. foo-bar
I guess escaping $search is better though so that each eval call is dynamic on both ends (search & replace)..

Also, i had tried the other way, but got an output of "0" instead of "foo-bar":
perl -le '$s = foobar; $search=qr/(foo)(bar)/; $replace="\$1-\$2"; $s +=~ s/$search/eval $replace/e; print $s'

Replies are listed 'Best First'.
Re^4: Variable substitute and capturing
by ikegami (Patriarch) on Jan 16, 2006 at 17:08 UTC
    You're right. Even without qr//. I've updated my post.
    # qr & no eval $s = qr/\\/; $t = "\\\\"; $t =~ s/$s/!/; print("$t\n"); # qr & eval $s = qr/\\/; $t = "\\\\"; eval "\$t =~ s/$s/!/"; print("$t\n"); # qr & no stringification $s = qr/\\/; $t = "\\\\"; eval "\$t =~ s/\$s/!/"; print("$t\n"); print("\n"); # qq & no eval $s = "\\\\"; $t = "\\\\"; $t =~ s/$s/!/; print("$t\n"); # qq & eval $s = "\\\\"; $t = "\\\\"; eval "\$t =~ s/$s/!/"; print("$t\n"); # qq & no stringification $s = "\\\\"; $t = "\\\\"; eval "\$t =~ s/\$s/!/"; print("$t\n");

    The regexp still gets stringified and recompiled, though.

    This all goes to prove the value of a template system.