Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Anthony Eden http://allthings.mp/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Simple Perl Templating

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 = <FILE>) {
        $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 = &parse_template("template.txt", %context);
print $out;
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS