in reply to symbolic references in reg exp
See the difference? You were matching "$title", then trying to substitute that with the value of the variable "$$title"--but that's not what you wanted. You want to just match the word part of the variable name ("title"), then substitute the whole thing with the value of "$title".$template_file_string =~ s/\$(\w+)/${$1}/g;
*However*, don't do it this way--there are better ways. One way is to put your template values into a hash, with the keys being the template variable names, and the values being the values that you want to substitute for the template variables; then use a regex to do the substitution.
Another (very) good option would be to use Text::Template.my %vars = ( title => 'Foo Bar' ); my $template = q(<h1>$title</h1>); $template =~ s/\$(\w+)/$vars{$1}/eg;
This last is probably the most robust and extensible approach, so check it out. (Note that you have to slightly modify your template so that your variables are in '{}'.)use Text::Template; my %vars = ( title => "Foo Bar" ); my $t = new Text::Template(TYPE => 'STRING', SOURCE => q(<h1>{$title}< +/h1>)) or die "Couldn't construct template: $Text::Template::ERROR"; my $text = $t->fill_in(HASH => \%vars);
|
|---|