| 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 ||= {}
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
| |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | $delim = $params->{'delim'} || ':'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params->{'delim'} || ':'
MC/DC100%1/1
$params->{'delim'} || ':'
$params->{'delim'}
Truth table100%2/2
A
A= $params->{'delim'}
| |||||||||||||||||||||||||||||
| 37 | 5 | $delim = quotemeta($delim); | |||||||||||||||||||||||||||
| 38 | 5 | ▶ | $encoding = defined $params->{'encoding'} ? ':encoding('.$params->{'encoding'}.')' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 5 | ▶ | return $class->error("No filename specified") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | unless $filename; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | ▶ | open(FD, '<'.$encoding, $filename) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | || return $class->error("$filename: $!"); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # first line of file should contain field definitions | ||||||||||||||||||||||||||||
| 47 | 5 | ▶ | while (! $line || $line =~ /^#/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $line or $line =~ /^#/
MC/DC100%2/2
not $line or $line =~ /^#/
not $line
$line =~ /^#/
Truth table100%3/3
not(A) or B
A= not $lineB= $line =~ /^#/
| |||||||||||||||||||||||||||||
| 48 | 11 | $line = <FD>; | |||||||||||||||||||||||||||
| 49 | 11 | ▶ | last unless defined $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | unless defined $line; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | (@fields = split(/\s*$delim\s*/, $line)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | 10 | chomp; | |||||||||||||||||||||||||||
| 63 | 10 | s/\r$//; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # ignore comments and blank lines | ||||||||||||||||||||||||||||
| 66 | 10 | ▶ | next if /^#/ || /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/^#/ or /^\s*$/
MC/DC50%1/2
/^#/ or /^\s*$/
/^#/
/^\s*$/
Truth table66%2/3
A or B
A= /^#/B= /^\s*$/
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||