Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Minimal, %ENV based templating tool

by blindluke (Hermit)
on Nov 08, 2015 at 14:12 UTC ( [id://1147203]=CUFP: print w/replies, xml ) Need Help??

There is a nice, minimal templating tool out there, called skel. Its author, Scott Vokes, describes it as a "tiny command-line skeleton/snippet thing", and it's probably accurate.

Given a template file name, it expands all the occurences of #{VARIABLE} with the value obtained from the %ENV.

The tool has proven quite useful to me, mostly due to the way it provides a lot of useful features while still allowing you to use it in the most primitive way imaginable.

Inspired by the simplicity of it, I tried rewriting the most basic version of the idea in Perl, adding the one (the only?) feature I was missing - asking for variable values when the ENV var is unset.

#!/usr/bin/perl use v5.14; my %cfg = ( tmpl_path => $ENV{HOME}.'/.skel-closet/', opening_tag => '#{', closing_tag => '}', ); sub prompt { my $msg = shift; local $| = 1; local $\; warn "$msg \n"; my $ans = <STDIN>; defined $ans ? chomp $ans : warn "\n"; return $ans; } sub filled_line { my $line = shift; my $pattern = qr/$cfg{opening_tag}(.+?)$cfg{closing_tag}/; $line =~ s/$pattern/$ENV{$1} ? $ENV{$1} : prompt("$1 unset. Value: +") /eg; return $line; } my $filename = $cfg{tmpl_path}.shift; open (my $fh, '<', $filename) or die ("Could not open file: $filename"); my @output; while (<$fh>) { push @output, filled_line($_); } close ($fh); print for @output;

The use is straightforward. Here's an example:

$ cat ~/.skel-closet/test This is the value of HOME: #{HOME} This is the value of EDITOR: #{EDITOR} This is the value of FOO: #{FOO} $ ./pskel.pl test > out.txt FOO unset. Value: foovalue $ cat out.txt This is the value of HOME: /home/blj This is the value of EDITOR: emacs This is the value of FOO: foovalue

- Luke

Replies are listed 'Best First'.
Re: Minimal, %ENV based templating tool
by u65 (Chaplain) on Nov 09, 2015 at 12:13 UTC

    Nice improvement, Luke. You should submit a pull request.

      Thank you.

      The skel tool is written in C and, on top of the simple core idea, it offers a lot of small additional features, like lowercase/uppercase conversion and support for default values. My script could hardly be considered an improvement over the original - but it could complement it, when you need just the core functionality and the one feature I've added.

      I could try to port this feature to the original, as I would be happy to contribute to a tool I use, but I'm not sure if my C skills are up to it. But I will still fork it and try, if time allows.

      Just for completeness sake, here is my initial version with just the core functionality:

      #!/usr/bin/perl use v5.14; my %cfg = ( tmpl_path => $ENV{HOME}.'/.skel-closet/', opening_tag => '#{', closing_tag => '}', ); sub filled_line { my $line = shift; my $pattern = qr/$cfg{opening_tag}(.+?)$cfg{closing_tag}/; $line =~ s/$pattern/$ENV{$1}/g; return $line; } my $filename = $cfg{tmpl_path}.shift; open (my $fh, '<', $filename) or die ("Could not open file: $filename"); print filled_line($_) while (<$fh>); close ($fh);

      Not much use for it over the version I posted earlier, but it shows the one thing that still bugs me. The sub is called filled_line, because I knew I wanted a nice sounding noun for the print filled_line expression. With the switch to buffering in the second version, this nice sound of the penultimate line was unfortunately lost - I could not push filled_line($_) to @output, because push does not work that way.

      Someone might say that this is completely irrelevant, but the way I see it, if I did not care about the way those lines sound, I would not be so attached to Perl.

      - Luke

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1147203]
Approved by Athanasius
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2025-12-13 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (93 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.