lib/Template/Plugin/Datafile.pm

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