1 #============================================================= -*-Perl-*-
3 # Template::Plugin::Datafile
6 # Template Toolkit Plugin which reads a datafile and constructs a
7 # list object containing hashes representing records in the file.
10 # Andy Wardley <abw@wardley.org>
13 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
18 #============================================================================
20 package Template::Plugin::Datafile;
24 use base 'Template::Plugin';
29 my ($class, $context, $filename, $params) = @_;
30 my ($delim, $line, @fields, @data, @results);
36 $delim = $params->{'delim'} || ':';
37 $delim = quotemeta($delim);
39 return $class->fail("No filename specified")
43 || return $class->fail("$filename: $!");
45 # first line of file should contain field definitions
46 while (! $line || $line =~ /^#/) {
52 (@fields = split(/\s*$delim\s*/, $line))
53 || return $class->fail("first line of file must contain field names");
55 # read each line of the file
60 # ignore comments and blank lines
61 next if /^#/ || /^\s*$/;
63 # split line into fields
64 @data = split(/\s*$delim\s*/);
66 # create hash record to represent data
68 @record{ @fields } = @data;
70 push(@$self, \%record);
89 Template::Plugin::Datafile - Plugin to construct records from a simple data file
93 [% USE mydata = datafile('/path/to/datafile') %]
94 [% USE mydata = datafile('/path/to/datafile', delim = '|') %]
96 [% FOREACH record = mydata %]
97 [% record.this %] [% record.that %]
102 This plugin provides a simple facility to construct a list of hash
103 references, each of which represents a data record of known structure,
106 [% USE datafile(filename) %]
108 A absolute filename must be specified (for this initial implementation at
109 least - in a future version it might also use the C<INCLUDE_PATH>). An
110 optional C<delim> parameter may also be provided to specify an alternate
113 [% USE userlist = datafile('/path/to/file/users') %]
114 [% USE things = datafile('items', delim = '|') %]
116 The format of the file is intentionally simple. The first line
117 defines the field names, delimited by colons with optional surrounding
118 whitespace. Subsequent lines then defines records containing data
119 items, also delimited by colons. e.g.
121 id : name : email : tel
122 abw : Andy Wardley : abw@tt2.org : 555-1234
123 sam : Simon Matthews : sam@tt2.org : 555-9876
125 Each line is read, split into composite fields, and then used to
126 initialise a hash array containing the field names as relevant keys.
127 The plugin returns a blessed list reference containing the hash
128 references in the order as defined in the file.
130 [% FOREACH user = userlist %]
131 [% user.id %]: [% user.name %]
134 The first line of the file B<must> contain the field definitions.
135 After the first line, blank lines will be ignored, along with comment
136 line which start with a 'C<#>'.
140 Should handle file names relative to C<INCLUDE_PATH>.
141 Doesn't permit use of 'C<:>' in a field. Some escaping mechanism is required.
145 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
149 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
151 This module is free software; you can redistribute it and/or
152 modify it under the same terms as Perl itself.
162 # perl-indent-level: 4
163 # indent-tabs-mode: nil
166 # vim: expandtab shiftwidth=4: