| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Spreadsheet::Wright; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 89134 | use 5.010; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 81 |  | 
| 4 | 2 |  |  | 2 |  | 14 | use strict; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 68 |  | 
| 5 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 8 |  | 
|  | 2 |  |  |  |  | 91 |  | 
| 6 | 2 |  |  | 2 |  | 10 | no warnings qw( uninitialized numeric ); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 129 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | BEGIN { | 
| 9 | 2 |  |  | 2 |  | 5 | $Spreadsheet::Wright::VERSION   = '0.105'; | 
| 10 | 2 |  |  |  |  | 37 | $Spreadsheet::Wright::AUTHORITY = 'cpan:TOBYINK'; | 
| 11 |  |  |  |  |  |  | } | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 2 |  |  | 2 |  | 9 | use Carp; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 194 |  | 
| 14 | 2 |  |  | 2 |  | 2162 | use IO::File; | 
|  | 2 |  |  |  |  | 61411 |  | 
|  | 2 |  |  |  |  | 2215 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub new | 
| 17 |  |  |  |  |  |  | { | 
| 18 | 1 |  |  | 1 | 1 | 170 | my ($class, %args) = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 1 |  | 50 |  |  | 7 | my $format   = $args{'format'} // 'auto'; | 
| 21 | 1 |  | 33 |  |  | 5 | my $filename = $args{'file'}   // $args{'filename'}; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 1 | 50 |  |  |  | 7 | if (lc $format eq 'auto') | 
| 24 |  |  |  |  |  |  | { | 
| 25 | 1 | 50 |  |  |  | 13 | $format = ($filename =~ /\.([^\.]+)$/) ? lc($1) : 'auto'; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 1 |  |  |  |  | 11 | my $implementation = { | 
| 29 |  |  |  |  |  |  | auto    => 'CSV', | 
| 30 |  |  |  |  |  |  | csv     => 'CSV', | 
| 31 |  |  |  |  |  |  | excel   => 'Excel', | 
| 32 |  |  |  |  |  |  | html    => 'HTML', | 
| 33 |  |  |  |  |  |  | json    => 'JSON', | 
| 34 |  |  |  |  |  |  | ods     => 'OpenDocument', | 
| 35 |  |  |  |  |  |  | odsxml  => 'OpenDocumentXML', | 
| 36 |  |  |  |  |  |  | text    => 'CSV', | 
| 37 |  |  |  |  |  |  | txt     => 'CSV', | 
| 38 |  |  |  |  |  |  | xhtml   => 'XHTML', | 
| 39 |  |  |  |  |  |  | xls     => 'Excel', | 
| 40 |  |  |  |  |  |  | xml     => 'OpenDocumentXML', | 
| 41 |  |  |  |  |  |  | }->{lc $format}; | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | my $self = eval | 
| 44 | 1 |  |  |  |  | 4 | { | 
| 45 | 1 | 50 |  |  |  | 3 | croak "Format $format is not supported" unless $implementation; | 
| 46 | 1 |  |  |  |  | 4 | $implementation = join '::', (__PACKAGE__, $implementation); | 
| 47 | 1 |  |  | 1 |  | 86 | eval "use $implementation;"; | 
|  | 1 |  |  |  |  | 656 |  | 
|  | 1 |  |  |  |  | 24 |  | 
|  | 1 |  |  |  |  | 19 |  | 
| 48 | 1 | 50 |  |  |  | 5 | die $@ if $@; | 
| 49 | 1 |  |  |  |  | 6 | return $implementation->new(%args); | 
| 50 |  |  |  |  |  |  | }; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 1 | 50 | 33 |  |  | 8 | if ($self and !$@) | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | { | 
| 54 | 1 |  |  |  |  | 5 | return $self; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | elsif ($args{'failsafe'}) | 
| 57 |  |  |  |  |  |  | { | 
| 58 | 0 |  |  |  |  | 0 | $implementation = join '::', (__PACKAGE__, 'CSV'); | 
| 59 | 0 |  |  |  |  | 0 | eval "use $implementation;"; | 
| 60 | 0 | 0 |  |  |  | 0 | die $@ if $@; | 
| 61 | 0 |  |  |  |  | 0 | return $implementation->new(%args); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  | elsif ($@) | 
| 64 |  |  |  |  |  |  | { | 
| 65 | 0 |  |  |  |  | 0 | die $@; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | else | 
| 68 |  |  |  |  |  |  | { | 
| 69 | 0 |  |  |  |  | 0 | croak "Could not instantiate spreadsheet!\n"; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub DESTROY | 
| 74 |  |  |  |  |  |  | { | 
| 75 | 1 |  |  | 1 |  | 1206 | my $self = shift; | 
| 76 | 1 |  |  |  |  | 5 | $self->close; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub error | 
| 80 |  |  |  |  |  |  | { | 
| 81 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 82 | 0 |  |  |  |  | 0 | return $self->{'_ERROR'}; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub _open | 
| 86 |  |  |  |  |  |  | { | 
| 87 | 3 |  |  | 3 |  | 3 | my $self=shift; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 3 | 50 |  |  |  | 7 | $self->{'_CLOSED'} && croak "Can't reuse a closed spreadsheet"; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 3 |  |  |  |  | 3 | my $fh = $self->{'_FH'}; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 3 | 100 |  |  |  | 7 | if(!$fh) | 
| 94 |  |  |  |  |  |  | { | 
| 95 | 1 | 50 |  |  |  | 4 | my $filename = $self->{'_FILENAME'} or return; | 
| 96 | 1 |  |  |  |  | 8 | $fh = IO::File->new; | 
| 97 | 1 | 50 |  |  |  | 41 | $fh->open($filename,"w") | 
| 98 |  |  |  |  |  |  | or croak "Can't open file $filename for writing: $!"; | 
| 99 | 1 |  |  |  |  | 112 | $self->{'_FH'}=$fh; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 3 |  |  |  |  | 7 | return $self->_prepare; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub _prepare | 
| 106 |  |  |  |  |  |  | { | 
| 107 | 0 |  |  | 0 |  | 0 | return $_[0]; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub addrow | 
| 111 |  |  |  |  |  |  | { | 
| 112 | 3 |  |  | 3 | 1 | 10 | my $self = shift; | 
| 113 | 3 | 50 |  |  |  | 9 | $self->_open() or return; | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 3 |  |  |  |  | 4 | my @cells; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 3 |  |  |  |  | 6 | foreach my $item (@_) | 
| 118 |  |  |  |  |  |  | { | 
| 119 | 6 | 50 |  |  |  | 10 | if (ref $item eq 'HASH') | 
| 120 |  |  |  |  |  |  | { | 
| 121 | 0 | 0 |  |  |  | 0 | if (ref $item->{content} eq 'ARRAY') | 
| 122 |  |  |  |  |  |  | { | 
| 123 | 0 |  |  |  |  | 0 | foreach my $i (@{ $item->{'content'} }) | 
|  | 0 |  |  |  |  | 0 |  | 
| 124 |  |  |  |  |  |  | { | 
| 125 | 0 |  |  |  |  | 0 | my %newitem = %$item; | 
| 126 | 0 |  |  |  |  | 0 | $newitem{'content'} = $i; | 
| 127 | 0 |  |  |  |  | 0 | push @cells, \%newitem; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | else | 
| 131 |  |  |  |  |  |  | { | 
| 132 | 0 |  |  |  |  | 0 | push @cells, $item; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  | else | 
| 136 |  |  |  |  |  |  | { | 
| 137 | 6 |  |  |  |  | 16 | push @cells, { content => $item }; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 3 |  |  |  |  | 9 | return $self->_add_prepared_row(@cells); | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | sub _add_prepared_row | 
| 145 |  |  |  |  |  |  | { | 
| 146 | 0 |  |  | 0 |  | 0 | return $_[0]; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | sub addrows | 
| 150 |  |  |  |  |  |  | { | 
| 151 | 1 |  |  | 1 | 1 | 10 | my $self = shift; | 
| 152 | 1 |  |  |  |  | 2 | foreach my $row (@_) | 
| 153 |  |  |  |  |  |  | { | 
| 154 | 2 | 50 |  |  |  | 4 | if (ref $row eq 'ARRAY') | 
|  |  | 0 |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | { | 
| 156 | 2 |  |  |  |  | 5 | $self->addrow(@$row); | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  | elsif (!ref $row) | 
| 159 |  |  |  |  |  |  | { | 
| 160 | 0 |  |  |  |  | 0 | $self->addsheet($row); | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  | else | 
| 163 |  |  |  |  |  |  | { | 
| 164 | 0 |  |  |  |  | 0 | carp "Could not add row."; | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  | } | 
| 167 | 1 |  |  |  |  | 3 | return $self; | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | sub addsheet | 
| 171 |  |  |  |  |  |  | { | 
| 172 | 0 |  |  | 0 | 1 | 0 | croak "addsheet not implemented!!\n"; | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | sub freeze | 
| 176 |  |  |  |  |  |  | { | 
| 177 | 0 |  |  | 0 | 1 | 0 | return $_[0]; | 
| 178 |  |  |  |  |  |  | } | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | sub close | 
| 181 | 0 |  |  | 0 | 1 | 0 | { | 
| 182 |  |  |  |  |  |  | # noop | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | 1; | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | __END__ |