lib/Template/Plugin/Datafile.pm

filter: Statementstmt 100% 37 / 37 Branchbran 71.4% 10 / 14 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 90.5% 67 / 74 CC 12 SCAR 22.4
CC
12
Cov
92%
new
12.2
BEGIN
1.0
BEGIN
1.0
CRAP 9.4
« blib/lib/Template/Config.pm ↑ Summary blib/lib/Template/Plugin/Scalar.pm »
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 <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 1996-2022 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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $VERSION = '3.100';
27  
28 sub new {
29 3 ▶ my ($class, $context, $filename, $params) = @_;
Subroutine new: called CC=12 SCAR=25.0
Pod documented
30 3 my ($delim, $encoding, $line, @fields, @data, @results);
31 3 my $self = [ ];
32 3 local *FD;
33 3 local $/ = "\n";
34  
35 3 ▶ $params ||= { };
Condition100%2/2
$params ||= {}
l!l
21
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
36 3 ▶ $delim = $params->{'delim'} || ':';
Condition100%2/2
$params->{'delim'} || ':'
l!l
21
MC/DC100%1/1
$params->{'delim'} || ':'
$params->{'delim'}
Truth table100%2/2
A
A= $params->{'delim'}
Aresult
11
00
37 3 $delim = quotemeta($delim);
38 3 ▶ $encoding = defined $params->{'encoding'} ? ':encoding('.$params->{'encoding'}.')' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'encoding'} ? : 0 3 3
39  
40 3 ▶ return $class->error("No filename specified")
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 3 3
41 unless $filename;
42  
43 3 ▶ open(FD, '<'.$encoding, $filename)
Branch: 1 branch
BranchTrueFalseTotal
open FD, '<' . $encoding, $filename or return $class->error("${filename}: $!") 0 3 3
44 || return $class->error("$filename: $!");
45  
46 # first line of file should contain field definitions
47 3 ▶ while (! $line || $line =~ /^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line or $line =~ /^#/) 6 3 9
Condition100%3/3
not $line or $line =~ /^#/
l!l&&r!l&&!r
333
MC/DC100%2/2
not $line or $line =~ /^#/
not $line $line =~ /^#/
Truth table100%3/3
not(A) or B
A= not $lineB= $line =~ /^#/
ABresult
1X1
011
000
48 6 $line = <FD>;
49 6 chomp $line;
50 6 $line =~ s/\r$//;
51 }
52  
53 3 ▶ (@fields = split(/\s*$delim\s*/, $line))
Branch: 1 branch
BranchTrueFalseTotal
@fields = split(/\s*$delim\s*/, $line, 0) or return $class->error("first line of file must contain field names") 0 3 3
54 || return $class->error("first line of file must contain field names");
55  
56 # read each line of the file
57 3 ▶ while (<FD>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FD)) 10 3 13
58 10 chomp;
59 10 s/\r$//;
60  
61 # ignore comments and blank lines
62 10 ▶ next if /^#/ || /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or /^\s*$/ 1 9 10
Condition66%2/3
/^#/ or /^\s*$/
l!l&&r!l&&!r
109
MC/DC50%1/2
/^#/ or /^\s*$/
/^#/ /^\s*$/
Truth table66%2/3
A or B
A= /^#/B= /^\s*$/
ABresult
1X1
011
000
63  
64 # split line into fields
65 9 @data = split(/\s*$delim\s*/);
66  
67 # create hash record to represent data
68 9 my %record;
69 9 @record{ @fields } = @data;
70  
71 9 push(@$self, \%record);
72 }
73  
74 # return $self;
75 3 bless $self, $class;
76 }
77  
78  
79 sub as_list {
80 2 ▶ return $_[0];
Subroutine as_list: called CC=1 SCAR=0.0
Pod undocumented
81 }
82  
83  
84 1 1;
85  
86 __END__
« blib/lib/Template/Config.pm ↑ Summary blib/lib/Template/Plugin/Scalar.pm »