3 # Reads one or more template files and runs it through Text::Template
5 # It is assumed that this scripts is called with -Mconfigdata, a module
6 # that holds configuration data in %config
11 # Because we know that Text::Template isn't a core Perl module, we use
12 # a fallback in case it's not installed on the system
14 use File::Spec::Functions;
15 use lib catdir(dirname(__FILE__));
16 use with_fallback qw(Text::Template);
18 # We actually expect to get the following hash tables from configdata:
24 # We just do a minimal test to see that we got what we expected.
25 # $config{target} must exist as an absolute minimum.
26 die "You must run this script with -Mconfigdata\n" if !exists($config{target});
28 # Helper functions for the templates #################################
30 # It might be practical to quotify some strings and have them protected
31 # from possible harm. These functions primarly quote things that might
32 # be interpreted wrongly by a perl eval.
35 # This adds quotes (") around the given string, and escapes any $, @, \,
36 # " and ' by prepending a \ to them.
39 $s =~ s/([\$\@\\"'])/\\$1/g;
44 # For each defined element in LIST (i.e. elements that aren't undef), have
45 # it quotified with 'quotofy1'
56 # Error reporter #####################################################
58 # The error reporter uses %lines to figure out exactly which file the
59 # error happened and at what line. Not that the line number may be
60 # the start of a perl snippet rather than the exact line where it
61 # happened. Nothing we can do about that here.
66 my $filename = "<STDIN>";
68 foreach (sort keys %lines) {
69 $filename = $lines{$_};
70 last if ($_ > $args{lineno});
73 print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
77 # Template reading ###################################################
79 # Read in all the templates into $text, while keeping track of each
80 # file and its size in lines, to try to help report errors with the
81 # correct file name and line number.
83 my $prev_linecount = 0;
86 ? join("", map { my $x = Text::Template::_load_text($_);
87 my $linecount = $x =~ tr/\n//;
88 $prev_linecount = ($linecount += $prev_linecount);
89 $lines{$linecount} = $_;
93 # Engage! ############################################################
95 # Load the full template (combination of files) into Text::Template
96 # and fill it up with our data. Output goes directly to STDOUT
98 my $template = Text::Template->new(TYPE => 'STRING', SOURCE => $text );
99 $template->fill_in(OUTPUT => \*STDOUT,
100 HASH => { config => \%config,
102 quotify1 => \"ify1,
103 quotify_l => \"ify_l },
104 DELIMITERS => [ "{-", "-}" ],