Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Simple Perl Templating

DZone's Guide to

Simple Perl Templating

·
Free Resource
Useful when you can't install modules because your host doesn't allow it:


sub parse_template() {
    my($file, %context) = @_;
    my($buffer) = "";
    open(FILE, $file) || die("Cannot open template " . $file);
    while ($line = 
  
   ) {
        $line =~ s/\$(\w+)/$context{$1}/g;
        $buffer .= $line;
    }
    return $buffer;
}

  

To use it, put values in the context map:


%context = (
    "var1" => "value1",
    "var2" => "value2",
);
$out = ∥se_template("template.txt", %context);
print $out;
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}