| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 |
|
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
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
27
|
|
|
23
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
0
|
|
|
|
1
|
|
|
|
|
31
|
|
|
24
|
1
|
|
|
1
|
|
3
|
use base 'Template::Plugin'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
277
|
|
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = 2.72; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub new { |
|
29
|
3
|
|
|
3
|
1
|
5
|
my ($class, $context, $filename, $params) = @_; |
|
30
|
3
|
|
|
|
|
4
|
my ($delim, $line, @fields, @data, @results); |
|
31
|
3
|
|
|
|
|
4
|
my $self = [ ]; |
|
32
|
3
|
|
|
|
|
6
|
local *FD; |
|
33
|
3
|
|
|
|
|
13
|
local $/ = "\n"; |
|
34
|
|
|
|
|
|
|
|
|
35
|
3
|
|
100
|
|
|
9
|
$params ||= { }; |
|
36
|
3
|
|
100
|
|
|
7
|
$delim = $params->{'delim'} || ':'; |
|
37
|
3
|
|
|
|
|
5
|
$delim = quotemeta($delim); |
|
38
|
|
|
|
|
|
|
|
|
39
|
3
|
50
|
|
|
|
6
|
return $class->fail("No filename specified") |
|
40
|
|
|
|
|
|
|
unless $filename; |
|
41
|
|
|
|
|
|
|
|
|
42
|
3
|
50
|
|
|
|
135
|
open(FD, $filename) |
|
43
|
|
|
|
|
|
|
|| return $class->fail("$filename: $!"); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# first line of file should contain field definitions |
|
46
|
3
|
|
100
|
|
|
8
|
while (! $line || $line =~ /^#/) { |
|
47
|
6
|
|
|
|
|
42
|
$line = ; |
|
48
|
6
|
|
|
|
|
9
|
chomp $line; |
|
49
|
6
|
|
|
|
|
32
|
$line =~ s/\r$//; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
3
|
50
|
|
|
|
75
|
(@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
|
3
|
|
|
|
|
8
|
while () { |
|
57
|
10
|
|
|
|
|
7
|
chomp; |
|
58
|
10
|
|
|
|
|
11
|
s/\r$//; |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# ignore comments and blank lines |
|
61
|
10
|
100
|
66
|
|
|
35
|
next if /^#/ || /^\s*$/; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# split line into fields |
|
64
|
9
|
|
|
|
|
52
|
@data = split(/\s*$delim\s*/); |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# create hash record to represent data |
|
67
|
9
|
|
|
|
|
10
|
my %record; |
|
68
|
9
|
|
|
|
|
18
|
@record{ @fields } = @data; |
|
69
|
|
|
|
|
|
|
|
|
70
|
9
|
|
|
|
|
32
|
push(@$self, \%record); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# return $self; |
|
74
|
3
|
|
|
|
|
46
|
bless $self, $class; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub as_list { |
|
79
|
2
|
|
|
2
|
0
|
3
|
return $_[0]; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
__END__ |