File Coverage

blib/lib/WordLists/Serialise/Simple.pm
Criterion Covered Total %
statement 52 58 89.6
branch 16 28 57.1
condition 3 6 50.0
subroutine 11 11 100.0
pod 0 4 0.0
total 82 107 76.6


line stmt bran cond sub pod time code
1             package WordLists::Serialise::Simple;
2 7     7   1147 use strict;
  7         13  
  7         250  
3 7     7   36 use warnings;
  7         13  
  7         212  
4 7     7   35 use IO::File;
  7         32  
  7         1200  
5 7     7   39 use WordLists::Common qw (@sDefaultAttList @sDefiningAttlist);
  7         11  
  7         677  
6 7     7   38 use WordLists::Base;
  7         12  
  7         4473  
7             our $VERSION = $WordLists::Base::VERSION;
8            
9             sub new
10             {
11 2     2 0 14 my ($class, $args) = @_;
12 2         12 my $self = {
13             'field_sep' => "\t",
14             'default_attlist'=> \@sDefaultAttList,
15             'header_marker' => '#*',
16             'line_sep' => \$/,
17             };
18 2         6 $self->{$_} = $args->{$_} foreach grep { defined $args->{$_}; }(qw(field_sep attlist default_attlist header_marker));
  8         18  
19 2         13 bless $self, $class;
20             }
21             sub _get_line_sep
22             {
23 3     3   6 my $self = shift;
24 3 50       17 if (ref $self->{'line_sep'} eq ref \'')
    0          
25             {
26 3         5 return ${$self->{'line_sep'}};
  3         13  
27             }
28             elsif (!ref $self->{'line_sep'})
29             {
30 0         0 return $self->{'line_sep'};
31             }
32             else
33             {
34 0         0 return $/;
35             }
36             }
37             sub header_line_to_string
38             {
39 3     3 0 5 my ($self, $args) = @_;
40 0         0 my $sLine = $self->{'header_marker'} . join (
41             defined $args->{'field_sep'} ? $args->{'field_sep'} : $self->{'field_sep'},
42 3 50       17 defined $args->{'attlist'} ? @{$args->{'attlist'}} : @{$self->{'default_attlist'}}
  3 50       10  
43             );
44 3         10 return $sLine;
45             }
46             sub to_string
47             {
48 4     4 0 3693 my ($self, $structure, $args) = @_;
49            
50 4 100       25 if (ref $structure eq ref {})
    50          
51             {
52 1         4 return $self->hashref_to_string($structure, $args);
53             }
54             elsif (ref $structure eq ref [])
55             {
56 3 50       18 $args->{line_sep} = $self->_get_line_sep() unless defined $args->{line_sep};
57 3         6 my $sOut ='';
58 3 50 33     28 $sOut .= $self->header_line_to_string($args) . $args->{line_sep} unless $args->{no_header} or $self->{no_header};
59 3         5 foreach (@{$structure})
  3         9  
60             {
61 5 50       16 if (ref $_ eq ref {})
62             {
63 5         16 $sOut .= $self->hashref_to_string($_, $args);
64             }
65             else
66             {
67 0         0 warn "Cannot serialise - expected a HASH ref, got ". ref $_;
68             }
69 5         18 $sOut .= $args->{line_sep};
70             }
71 3         521 return $sOut;
72             }
73            
74 0         0 return undef;
75             }
76             sub _warn_if_has_sep
77             {
78 24     24   38 my ($self, $test) = @_;
79 24 100 66     88 if (defined ($test) and ref($test) eq ref '')
80             {
81 12 50       59 warn "Value contains field separator" if $test =~ /$self->{'field_sep'}/;
82 12 50       64 warn "Value contains line separator" if $test =~ /$self->{'line_sep'}/;
83             }
84             }
85             sub hashref_to_string
86             {
87 6     6 0 9 my ($self, $structure, $args) = @_;
88 6 50       12 my @sAttlist = defined $args->{'attlist'} ? @{$args->{'attlist'}} : @{$self->{'default_attlist'}};
  0         0  
  6         26  
89 6         10 my $s = '';
90 24         62 $s .= join (
91             $self->{'field_sep'},
92             map {
93 6         11 $self->_warn_if_has_sep($structure->{$_});
94 24 100       81 defined $structure->{$_} ? $structure->{$_} : '';
95             } @sAttlist
96             );
97 6         21 return $s;
98             }
99             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