Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I believe my problem is a general problem. I did a search, but found no answers.

Description:

There is a HTML file to be processed. I want to substitute a pattern with another one. There are many patterns to be processed, but I do not know how many. I came up a structure so that I can easily add a new pattern.

The structure looks like:

$file= '<A NAME="TOC124" HREF="cvs.html#SEC124">A.8 commit--Check file +s into the repository</A>'; $c->{Anchor}{from}='<A NAME="(.*?)" HREF=".*?\#(.*?)">(.*?)</A>'; $c->{Anchor}{to}='$3 |$2| *$1*'; foreach $item (keys %$c) { $from=$c->{$item}{from}; $to=$c->{$item}{to}; $file=~s/$from/$to/sg; } print "$file\n";

There is hash reference $c where I plan to add more pattern as long as necessary. The main body foreach process each of the patterns.

The expected result is:
A.8 commit--Check files into the repository |SEC124| *TOC124*

The result of my program is:
$3 |$2| *$1*

Here the problem is that $1,$2 and $3 is not evaluated. This is reasonable as I coded like this. My question is how can I make them evaluated ?

Thanks for any of your suggestions!

Replies are listed 'Best First'.
Danger Will Robinson! False Laziness!
by skyknight (Hermit) on Jul 30, 2003 at 17:13 UTC

    Get a real templating engine. You're reinventing the wheel, and you're almost certain to reinvent a worse one, as this is actually a very messy problem. Do yourself a favor and look up something like Embperl or Mason.

Re: how to evaluate a variable in a string?
by fglock (Vicar) on Jul 30, 2003 at 17:23 UTC

    This works, but it is certainly not the best way to do it:

    $file= '<A NAME="TOC124" HREF="cvs.html#SEC124">A.8 commit--Check file +s into the repository</A>'; $c->{Anchor}{from}='<A NAME="(.*?)" HREF=".*?\#(.*?)">(.*?)</A>'; $c->{Anchor}{to}='$3 |$2| *$1*'; foreach $item (keys %$c) { $from=$c->{$item}{from}; $to=$c->{$item}{to}; eval "\$file=~s/\$from/$to/sg"; } print "$file\n"; # A.8 commit--Check files into the repository |SEC124| *TOC124*
Re: how to evaluate a variable in a string?
by sauoq (Abbot) on Jul 30, 2003 at 17:34 UTC
    My question is how can I make them evaluated ?

    The problem is this line of code:

    $c->{Anchor}{to}='$3 |$2| *$1*';
    Use double quotes to get variable interpolation:
    $c->{Anchor}{to} = "$3 |$2|*$1*";

    My mistake. I didn't fully understand the question.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: how to evaluate a variable in a string?
by chunlou (Curate) on Jul 30, 2003 at 22:01 UTC
    Similar to fglock's first answer except for the variation:
    $file=~s/$from/eval'"'.$to.'"'/sge;
Re: how to evaluate a variable in a string?
by Kageneko (Scribe) on Jul 30, 2003 at 18:28 UTC
    In the line you are doing the assignment:
    $c->{Anchor}{to}='$3 |$2| *$1*';
    You are using single quotes. If you use double quotes:
    $c->{Anchor}{to}="$3 |$2| *$1*";
    then it should be properly interpolated. Barring that, you can also use eval (as noted above) to interpolate the string.
Re: how to evaluate a variable in a string?
by fglock (Vicar) on Jul 30, 2003 at 20:19 UTC

    Yet another way to do it. This is not exactly what you asked, but it may help (tested):

    $file= '<A NAME="TOC124" HREF="cvs.html#SEC124">A.8 commit--Check file +s into the repository</A>'; $c->{Anchor}{replace}= sub { $_[0] =~ s{<A NAME="(.*?)" HREF=".*?\#(.*?)">(.*?)</A>} {$3 |$2| *$1*} }; foreach $item (keys %$c) { $c->{$item}{replace}( $file ); } print "$file\n";