use HTML::Template; my $tmpl = HTML::Template->new( scalarref => \do { local $/; }, die_on_bad_params => 0, loop_context_vars => 1, filter => sub { my $s=shift; $$s=~s/%~([^~]+)~%//g; }, ); for my $key($tmpl->param) { next unless $key =~ /^SET_VAR_([^_]+)_(.+)$/i; $tmpl->param($1=>$2); } print $tmpl->output; __DATA__ %~SET_VAR_p1_http://somelong.net/path~% %~SET_VAR_p2_http://someother.net/otherpath~% %~p1~%/foo.html %~p1~%/bar.html %~p2~%/baz.html %~p2~%/qux.html