Serialise/Simple.pm

filter: Statementstmt 90.0% 54 / 60 Branchbran 57.1% 16 / 28 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 75.2% 85 / 113 SCAR 31.4
CC
19
Cov
78%
to_string
9.7
_warn_if_has_sep
5.3
_get_line_sep
4.1
CRAP 23.0
« Pronounce/Pronouncer.pm ↑ Summary Tag/Tagger.pm »
1 package WordLists::Serialise::Simple;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use WordLists::Common qw (@sDefaultAttList @sDefiningAttlist);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use WordLists::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 our $VERSION = $WordLists::Base::VERSION;
8
9 sub new
10 {
11 2 ▶ my ($class, $args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
12 2 my $self = {
13 'field_sep' => "\t",
14 'default_attlist'=> \@sDefaultAttList,
15 'header_marker' => '#*',
16 'line_sep' => \$/,
17 };
18 2 $self->{$_} = $args->{$_} foreach grep { defined $args->{$_}; }(qw(field_sep attlist default_attlist header_marker));
19 2 bless $self, $class;
20 }
21 sub _get_line_sep
22 {
23 3 ▶ my $self = shift;
Subroutine _get_line_sep: called CC=3 SCAR=14.2
24 3 ▶ if (ref $self->{'line_sep'} eq ref \'')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'line_sep'} eq ref \'') { } 3 0 3
elsif (not ref $self->{'line_sep'}) { } 0 0 0
25 {
26 3 return ${$self->{'line_sep'}};
27 }
28 elsif (!ref $self->{'line_sep'})
29 {
30 0 return $self->{'line_sep'};
31 }
32 else
33 {
34 0 return $/;
35 }
36 }
37 sub header_line_to_string
38 {
39 3 ▶ my ($self, $args) = @_;
Subroutine header_line_to_string: called CC=3 SCAR=12.0
Pod undocumented
40 my $sLine = $self->{'header_marker'} . join (
41 defined $args->{'field_sep'} ? $args->{'field_sep'} : $self->{'field_sep'},
42 3 ▶ defined $args->{'attlist'} ? @{$args->{'attlist'}} : @{$self->{'default_attlist'}}
Branch: 2 branches
BranchTrueFalseTotal
defined $args->{'field_sep'} ? : 0 3 3
defined $args->{'attlist'} ? : 0 3 3
43 );
44 3 return $sLine;
45 }
46 sub to_string
47 {
48 4 ▶ my ($self, $structure, $args) = @_;
Subroutine to_string: called CC=8 SCAR=22.7
Pod undocumented
49
50 4 ▶ if (ref $structure eq ref {})
Branch: 2 branches
BranchTrueFalseTotal
if (ref $structure eq ref {}) { } 1 3 4
elsif (ref $structure eq ref []) { } 3 0 3
51 {
52 1 return $self->hashref_to_string($structure, $args);
53 }
54 elsif (ref $structure eq ref [])
55 {
56 3 ▶ $args->{line_sep} = $self->_get_line_sep() unless defined $args->{line_sep};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"line_sep"} 3 0 3
57 3 my $sOut ='';
58 3 ▶ $sOut .= $self->header_line_to_string($args) . $args->{line_sep} unless $args->{no_header} or $self->{no_header};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"no_header"} or $self->{"no_header"} 3 0 3
Condition33%1/3
$args->{"no_header"} or $self->{"no_header"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$args->{"no_header"} or $self->{"no_header"}
$args->{"no_header"} $self->{"no_header"}
Truth table33%1/3
A or B
A= $args->{"no_header"}B= $self->{"no_header"}
ABresult
1X1
011
000
59 3 foreach (@{$structure})
60 {
61 5 ▶ if (ref $_ eq ref {})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq ref {}) { } 5 0 5
62 {
63 5 $sOut .= $self->hashref_to_string($_, $args);
64 }
65 else
66 {
67 0 warn "Cannot serialise - expected a HASH ref, got ". ref $_;
68 }
69 5 $sOut .= $args->{line_sep};
70 }
71 3 return $sOut;
72 }
73
74 0 return undef;
75 }
76 sub _warn_if_has_sep
77 {
78 24 ▶ my ($self, $test) = @_;
Subroutine _warn_if_has_sep: called CC=5 SCAR=16.7
79 24 ▶ if (defined ($test) and ref($test) eq ref '')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $test and ref $test eq ref "") 12 12 24
Condition66%2/3
defined $test and ref $test eq ref ""
!ll&&!rl&&r
12012
MC/DC50%1/2
defined $test and ref $test eq ref ""
defined $test ref $test eq ref ""
Truth table66%2/3
A and B
A= defined $testB= ref $test eq ref ""
ABresult
0X0
100
111
80 {
81 12 ▶ warn "Value contains field separator" if $test =~ /$self->{'field_sep'}/;
Branch: 1 branch
BranchTrueFalseTotal
if $test =~ /$self->{"field_sep"}/ 0 12 12
82 12 ▶ warn "Value contains line separator" if $test =~ /$self->{'line_sep'}/;
Branch: 1 branch
BranchTrueFalseTotal
if $test =~ /$self->{"line_sep"}/ 0 12 12
83 }
84 }
85 sub hashref_to_string
86 {
87 6 ▶ my ($self, $structure, $args) = @_;
Subroutine hashref_to_string: called CC=3 SCAR=11.1
Pod undocumented
88 6 ▶ my @sAttlist = defined $args->{'attlist'} ? @{$args->{'attlist'}} : @{$self->{'default_attlist'}};
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'attlist'} ? : 0 6 6
89 6 my $s = '';
90 $s .= join (
91 $self->{'field_sep'},
92 map {
93 6 $self->_warn_if_has_sep($structure->{$_});
94 24 ▶ defined $structure->{$_} ? $structure->{$_} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $structure->{$_} ? : 12 12 24
95 } @sAttlist
96 );
97 6 return $s;
98 }
99 7 1;
100
101
102 =pod
103
104 =head1 NAME
105
106 WordLists::Serialise::Simple
107
108 =head1 SYNOPSIS
109
110
111 =head1 DESCRIPTION
112
113 This is a simple serialiser for CSV/TSV files. It doesn't do any quoted values or anything like that - the delimiter must simply never occur in the text.
114
115 =head1 OPTIONS
116
117 On creation, a hashref may be passed with configuration options.
118
119 =head1 BUGS
120
121 Please use the Github issues tracker.
122
123 =head1 LICENSE
124
125 Copyright 2011-2012 © Cambridge University Press. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
126
127 =cut
« Pronounce/Pronouncer.pm ↑ Summary Tag/Tagger.pm »