| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$args->{"no_header"} or $self->{"no_header"}
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"}
| |||||||||||||||||||||||||||||
| 59 | 3 | foreach (@{$structure}) | |||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 5 | ▶ | if (ref $_ eq ref {}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $test and ref $test eq ref ""
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 ""
| |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 12 | ▶ | warn "Value contains field separator" if $test =~ /$self->{'field_sep'}/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 12 | ▶ | warn "Value contains line separator" if $test =~ /$self->{'line_sep'}/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||