0

here is a code snippet:

$regex_match = "(.*).txt";
$replacement = "\$1.pdf";
my $new_replacement = ""; #???
my $new_regex_match = quotemeta ($regex_match); #???
$from = "test.txt";
(my $to = $from) =~ s{$regex_match}{$replacement}g;
print "From " . $from . " To " . $to  . "\n";

Output : From test.txt To $1.pdf

I believe the $1 is not being interpolated. What needs to change? What does new_replacement and/or new_regex_match be?

Paul
  • 608
  • 1
  • 9
  • 23

1 Answers1

2

You need s///ee to force evaluation.

my $regex_match = '(.*)\.txt';
my $replacement = '$1.".pdf"';

while (<>) {
    s/$regex_match/$replacement/ee;
    print;
}
codnodder
  • 1,674
  • 9
  • 10
  • $replacement = '$1.".pdf"'; What does $1. do with ee option? I ask because I tested this as well $replacement = '$1".pdf"'; and received errors. Your solution did work! Thx. Just not seeing why. – Paul Dec 18 '13 at 17:57
  • @Paul ysth has an [excellent explanation](http://stackoverflow.com/a/392717/176646) in the question I linked this as a dupe of. – ThisSuitIsBlackNot Dec 18 '13 at 18:04
  • Sorry did not see the dup comment prior to my answer. It is a good explanation. @Paul The short answer is that you are evaluating Perl code, so you want this: `$1 . ".pid"`. Yours expands to `$1 ".pdf"`, which is missing the concatenation operator. – codnodder Dec 18 '13 at 18:20
  • So it is a string concat on second eval? wow... – Paul Dec 18 '13 at 18:22