| 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 ||= {}
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
| |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | $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 | 3 | $delim = quotemeta($delim); | |||||||||||||||||||||||||||
| 38 | 3 | ▶ | $encoding = defined $params->{'encoding'} ? ':encoding('.$params->{'encoding'}.')' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | return $class->error("No filename specified") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | unless $filename; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | open(FD, '<'.$encoding, $filename) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | || return $class->error("$filename: $!"); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # first line of file should contain field definitions | ||||||||||||||||||||||||||||
| 47 | 3 | ▶ | 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 | 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | 10 | chomp; | |||||||||||||||||||||||||||
| 59 | 10 | s/\r$//; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # ignore comments and blank lines | ||||||||||||||||||||||||||||
| 62 | 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*$/
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||