juan reyero

catloc — cat with localization

Very simple script that reads a file and writes it out doing replacements. Used instead of real templating mechanisms like the venerable Template Toolkit for simple tasks.

#!/usr/bin/perl -w

sub usage
{
    print "Usage:\ncatloc var=value,othervar=othevalue[,*] file\n";
    print "  Cats file replacing [%var%] with value for all var occurrences.\n";
    if (@_) {
        print "Error:\n";
        print @_,"\n";
    }
    exit(-1);
}

sub read_file
{
    my $f=shift;
    open(FI,$f);
    my @lines=<FI>;
    my $c=join("",@lines);
    close(FI);
    return $c;
}

@ARGV == 2 || usage("  incorrect number of arguments");
my $vars = $ARGV[0];
my $file = $ARGV[1];
-r $file || usage("  $file does not look like a readable file");

my $content = read_file($file);

my @vars  = split(/\,/, $vars);

foreach my $var (@vars) {
    my ($name, $value) = split(/=/, $var);
    $content =~ s/\[\%${name}\%\]/$value/g;
}

print $content;
Juan Reyero Barcelona, 2009-11-07
 

blog comments powered by Disqus