perl5ever has asked for the wisdom of the Perl Monks concerning the following question:
I'd like it to print 'capture 1 is 123', but it prints 'capture 1 is $1'. It's treating $to as a string literal and ignoring the occurrence of $1 in the string.sub foo { my ($text, $to) = @_; $text =~ s{(\d+)}{$to}; $text; } print foo("123", 'capture 1 is $1');
I have since figured out this:
sub bar { my ($text, $to) = @_; $text =~ s{(\d+)}{qq{qq{$to}}}ee; $text; } print bar("456", 'capture 1 is $1'); # prints capture 1 is 456
Is there any other way to get $1, $2, etc. interpolated without resorting to using the /ee modifier?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Interpolate $1 in: s{...}{$to}
by ikegami (Patriarch) on Jun 24, 2010 at 22:06 UTC | |
|
Re: Interpolate $1 in: s{...}{$to}
by MidLifeXis (Monsignor) on Jun 25, 2010 at 13:02 UTC |