Skip to content

Instantly share code, notes, and snippets.

@peter-leonov
Last active August 29, 2015 14:22
Show Gist options
  • Save peter-leonov/37445d1c1904dd5ad75c to your computer and use it in GitHub Desktop.
Save peter-leonov/37445d1c1904dd5ad75c to your computer and use it in GitHub Desktop.
tiny aka small aka simple aka plain perl template "engine"
#!/usr/bin/perl
# use: echo "hello <%= $USER %> <% if (true) { %>abc<%>%>" | perl template.pl > out.txt
for $key (keys %ENV) { ${$key} = $ENV{$key} }
$template = join("", <>);
$template =~ s/<%=/<% print /g;
@code = @text = split(/<%(.+?)%>/s, $template);
for (my $i = 0; $i <= $#code; $i+=2) { @code[$i] = "; print \@text[$i];" }
eval join(";", @code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment