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:

   1  
   2  sub parse_template() {
   3      my($file, %context) = @_;
   4      my($buffer) = "";
   5      open(FILE, $file) || die("Cannot open template " . $file);
   6      while ($line = <FILE>) {
   7          $line =~ s/\$(\w+)/$context{$1}/g;
   8          $buffer .= $line;
   9      }
  10      return $buffer;
  11  }


To use it, put values in the context map:

   1  
   2  %context = (
   3      "var1" => "value1",
   4      "var2" => "value2",
   5  );
   6  $out = &parse_template("template.txt", %context);
   7  print $out;
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS