in reply to Re: REGEX: muliple search and replace
in thread REGEX: muliple search and replace

I use a variation on this that allows for both recursive expansion and late binding.
$vars{foo} = sub { int(rand(1) ? "bar" : "baz" };
$vars{bar} = "vi";
$vars{baz} = "emacs";

print expand_template("<!--foo--> rules!");

sub expand_template {
    my $template = shift;
    $template =~ s/<!--(\w+)-->/expand_template(expand_var($1))/ge;
    return $template;
}

sub expand_var {
    my $name = shift;
    return "" if not exists $vars{$name};
    return &{$vars{$name}}() if ref $vars{$name} eq "CODE";
    return $vars{$name};
}
  • Comment on RE: Re: REGEX: muliple search and replace