Skip to content

Instantly share code, notes, and snippets.

@scottw
Created July 9, 2018 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottw/435d47e1a03f83b2cd8698cf5dd1d008 to your computer and use it in GitHub Desktop.
Save scottw/435d47e1a03f83b2cd8698cf5dd1d008 to your computer and use it in GitHub Desktop.
An example of a no-dependency template in Perl
#!/usr/bin/env perl
use strict;
use warnings;
# hydrate: poor man's templating
#
# usage: hydrate key1=value1 key2=value2 ... path/to/file.in > path/to/file.out
#
# substitutes all strings '{{key1}}' in file.in with 'value1'
my $file = pop @ARGV;
my %kv = map { split /=/ } @ARGV;
open my $fh, "<", $file
or die "Unable to open '$file': $!\n";
while (my $line = <$fh>) {
$line =~ s/\{\{(\S+?)\}\}/$kv{$1}/g;
print $line;
}
close $fh;
{
"host": "{{mysql_host}}",
"username": "{{mysql_user}}",
"password": "{{mysql_pass}}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment