| 1 |
#============================================================= -*-Perl-*- |
| 2 |
# |
| 3 |
# Template::Plugin::Datafile |
| 4 |
# |
| 5 |
# DESCRIPTION |
| 6 |
# Template Toolkit Plugin which reads a datafile and constructs a |
| 7 |
# list object containing hashes representing records in the file. |
| 8 |
# |
| 9 |
# AUTHOR |
| 10 |
# Andy Wardley <[email protected]> |
| 11 |
# |
| 12 |
# COPYRIGHT |
| 13 |
# Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. |
| 14 |
# |
| 15 |
# This module is free software; you can redistribute it and/or |
| 16 |
# modify it under the same terms as Perl itself. |
| 17 |
# |
| 18 |
#============================================================================ |
| 19 |
|
| 20 |
package Template::Plugin::Datafile; |
| 21 |
|
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
use base 'Template::Plugin'; |
| 25 |
|
| 26 |
our $VERSION = 2.72; |
| 27 |
|
| 28 |
sub new { |
| 29 |
my ($class, $context, $filename, $params) = @_; |
| 30 |
my ($delim, $line, @fields, @data, @results); |
| 31 |
my $self = [ ]; |
| 32 |
local *FD; |
| 33 |
local $/ = "\n"; |
| 34 |
|
| 35 |
$params ||= { }; |
| 36 |
$delim = $params->{'delim'} || ':'; |
| 37 |
$delim = quotemeta($delim); |
| 38 |
|
| 39 |
return $class->fail("No filename specified") |
| 40 |
unless $filename; |
| 41 |
|
| 42 |
open(FD, $filename) |
| 43 |
|| return $class->fail("$filename: $!"); |
| 44 |
|
| 45 |
# first line of file should contain field definitions |
| 46 |
while (! $line || $line =~ /^#/) { |
| 47 |
$line = <FD>; |
| 48 |
chomp $line; |
| 49 |
$line =~ s/\r$//; |
| 50 |
} |
| 51 |
|
| 52 |
(@fields = split(/\s*$delim\s*/, $line)) |
| 53 |
|| return $class->fail("first line of file must contain field names"); |
| 54 |
|
| 55 |
# read each line of the file |
| 56 |
while (<FD>) { |
| 57 |
chomp; |
| 58 |
s/\r$//; |
| 59 |
|
| 60 |
# ignore comments and blank lines |
| 61 |
next if /^#/ || /^\s*$/; |
| 62 |
|
| 63 |
# split line into fields |
| 64 |
@data = split(/\s*$delim\s*/); |
| 65 |
|
| 66 |
# create hash record to represent data |
| 67 |
my %record; |
| 68 |
@record{ @fields } = @data; |
| 69 |
|
| 70 |
push(@$self, \%record); |
| 71 |
} |
| 72 |
|
| 73 |
# return $self; |
| 74 |
bless $self, $class; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
sub as_list { |
| 79 |
return $_[0]; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
1; |
| 84 |
|
| 85 |
__END__ |
| 86 |
|
| 87 |
=head1 NAME |
| 88 |
|
| 89 |
Template::Plugin::Datafile - Plugin to construct records from a simple data file |
| 90 |
|
| 91 |
=head1 SYNOPSIS |
| 92 |
|
| 93 |
[% USE mydata = datafile('/path/to/datafile') %] |
| 94 |
[% USE mydata = datafile('/path/to/datafile', delim = '|') %] |
| 95 |
|
| 96 |
[% FOREACH record = mydata %] |
| 97 |
[% record.this %] [% record.that %] |
| 98 |
[% END %] |
| 99 |
|
| 100 |
=head1 DESCRIPTION |
| 101 |
|
| 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, |
| 104 |
from a data file. |
| 105 |
|
| 106 |
[% USE datafile(filename) %] |
| 107 |
|
| 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 |
| 111 |
delimiter character. |
| 112 |
|
| 113 |
[% USE userlist = datafile('/path/to/file/users') %] |
| 114 |
[% USE things = datafile('items', delim = '|') %] |
| 115 |
|
| 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. |
| 120 |
|
| 121 |
id : name : email : tel |
| 122 |
abw : Andy Wardley : [email protected] : 555-1234 |
| 123 |
sam : Simon Matthews : [email protected] : 555-9876 |
| 124 |
|
| 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. |
| 129 |
|
| 130 |
[% FOREACH user = userlist %] |
| 131 |
[% user.id %]: [% user.name %] |
| 132 |
[% END %] |
| 133 |
|
| 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<#>'. |
| 137 |
|
| 138 |
=head1 BUGS |
| 139 |
|
| 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. |
| 142 |
|
| 143 |
=head1 AUTHOR |
| 144 |
|
| 145 |
Andy Wardley E<lt>[email protected]<gt> L<http://wardley.org/> |
| 146 |
|
| 147 |
=head1 COPYRIGHT |
| 148 |
|
| 149 |
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. |
| 150 |
|
| 151 |
This module is free software; you can redistribute it and/or |
| 152 |
modify it under the same terms as Perl itself. |
| 153 |
|
| 154 |
=head1 SEE ALSO |
| 155 |
|
| 156 |
L<Template::Plugin> |
| 157 |
|
| 158 |
=cut |
| 159 |
|
| 160 |
# Local Variables: |
| 161 |
# mode: perl |
| 162 |
# perl-indent-level: 4 |
| 163 |
# indent-tabs-mode: nil |
| 164 |
# End: |
| 165 |
# |
| 166 |
# vim: expandtab shiftwidth=4: |