that is definitely not the best (nor easiest) way to do
templating. there are (too) many templating modules already out there.
Alright, I'll bite. The template looks like any other template
language:
{if device.vendor() is "cisco"}
show conf {extract /^interface (\S+)\.(\d+)/ as interfaces, units}
{loop interfaces as interface}
show interfaces {$interface} controller
{end}
{end}
I'm not in the office, but this is a simplified version of the
compiled template:
if ($device->vendor() eq "cisco") {
my @response = $stream->send("show conf");
my @interfaces;
my @units;
for (@response) {
next unless /^interface (\S+)\.(\d+)/;
push(@interfaces, $1);
push(@units, $2);
}
for my $interface (@interfaces) {
my @response = $stream->send("show interfaces $interface
controller");
}
}
(I skipped the included error handling, locking for parallelization
via threads, and some more.) This took less than a day to fully
implement.
Yes, some template languages are somewhat flexible and allow you to
register your own functions, but that's really not good enough. Also,
I believe that parsers and lexers do not really gain you a lot when
parsing this. The code isn't open sourced yet (it will be soon), but I
bet it will be hard to implement this with significantly less LOC
(approx. 350 currently), even by introducing new dependencies. Either
way, the time it took to write this is probably even less than the
time it would take to research any other template language.
-Samuel