| 1 | package PerlIO::via::csv; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Spreadsheet::Read; # I can't get this to `require' in OPEN | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Text::CSV; # ::Encoded ? | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | # class methods | ||||||||||||||||||||||||||||
| 15 | 3 | my $sheet = 1; | |||||||||||||||||||||||||||
| 16 | 30 | ▶ | sub sheet { | ||||||||||||||||||||||||||
|
Subroutine
sheet: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 30 | ▶ | $sheet = $_[1] if defined $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 30 | return $sheet; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | 3 | my $debug = 0; | |||||||||||||||||||||||||||
| 21 | 3 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | $debug = $_[1] if defined $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 3 | return $debug; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 27 | 3 | my $pkg = shift; | |||||||||||||||||||||||||||
| 28 | 3 | my %args = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # class methods passed to C<use> | ||||||||||||||||||||||||||||
| 31 | 3 | for (qw(sheet)) { | |||||||||||||||||||||||||||
| 32 | 3 | ▶ | $pkg->$_($args{$_}) if exists $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # args to Spreadsheet::Read or ParseExcel? | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # function | ||||||||||||||||||||||||||||
| 39 | 3 | ▶ | sub damn (@) { | ||||||||||||||||||||||||||
|
Subroutine
damn: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 3 | require Carp; | |||||||||||||||||||||||||||
| 41 | 3 | ▶ | if (__PACKAGE__->debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | Carp::confess(join("\n", @_)); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else { | ||||||||||||||||||||||||||||
| 45 | 3 | Carp::croak(join("\n", @_)); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | ### PerlIO::via interface | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 8 | ▶ | sub PUSHED { | ||||||||||||||||||||||||||
|
Subroutine
PUSHED: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 8 | my ($pkg, $mode, $fh) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 8 | bless { | |||||||||||||||||||||||||||
| 57 | row => 1, | ||||||||||||||||||||||||||||
| 58 | col => 1, | ||||||||||||||||||||||||||||
| 59 | # meta => {}, | ||||||||||||||||||||||||||||
| 60 | rows => [], | ||||||||||||||||||||||||||||
| 61 | }, $pkg; | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 8 | ▶ | sub OPEN { | ||||||||||||||||||||||||||
|
Subroutine
OPEN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 8 | my ($self, $path, $mode, $fh) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | my $ref = Spreadsheet::Read::ReadData($path); | |||||||||||||||||||||||||||
| 68 | 8 | __PACKAGE__->sheet(_lookup_sheet_number($ref, $path)); | |||||||||||||||||||||||||||
| 69 | 5 | $self->{rows} = [ Spreadsheet::Read::rows($ref->[__PACKAGE__->sheet]) ]; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 5 | return 1; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | # need FDOPEN and SYSOPEN? | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 10 | ▶ | sub FILL { | ||||||||||||||||||||||||||
|
Subroutine
FILL: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 10 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 10 | my $buf = undef; | |||||||||||||||||||||||||||
| 79 | 10 | ▶ | while (my $row = $self->read_row()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 11 | $buf .= $row . "\n"; | |||||||||||||||||||||||||||
| 81 | 11 | ▶ | last if defined $/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 10 | return $buf; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 3 | my $csv = Text::CSV->new({binary => 1}); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 14 | ▶ | sub read_row { | ||||||||||||||||||||||||||
|
Subroutine
read_row: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||
| 91 | 14 | my $buf = undef; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 14 | my $row_index = $self->{row} - 1; | |||||||||||||||||||||||||||
| 94 | 14 | my $rows = $self->{rows}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 14 | ▶ | if ($row_index < @$rows) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 11 | my @cols = @{ $rows->[$row_index] }; | |||||||||||||||||||||||||||
| 98 | 11 | ▶ | if ($csv->combine(@cols)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 11 | $buf = $csv->string; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # on to the next row | ||||||||||||||||||||||||||||
| 103 | 11 | $self->{row}++; | |||||||||||||||||||||||||||
| 104 | 11 | $self->{col} = 1; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 14 | return $buf; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | sub _lookup_sheet_number { | ||||||||||||||||||||||||||
|
Subroutine
_lookup_sheet_number: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 112 | 8 | my ($ref, $path) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | my $sheet = __PACKAGE__->sheet; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 8 | ▶ | if ($sheet =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 6 | my $max = $ref->[0]{sheets}; | |||||||||||||||||||||||||||
| 118 | 6 | ▶ | if ($sheet < 1 or $sheet > $max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sheet < 1 or $sheet > $max
MC/DC100%2/2
$sheet < 1 or $sheet > $max
$sheet < 1
$sheet > $max
Truth table100%3/3
A or B
A= $sheet < 1B= $sheet > $max
| |||||||||||||||||||||||||||||
| 119 | 2 | damn "There's no worksheet '$sheet' in '$path' (numbers 1 .. $max)"; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | # try to lookup sheet by name | ||||||||||||||||||||||||||||
| 124 | # (does it have to be UCS2 for Excel?) | ||||||||||||||||||||||||||||
| 125 | 2 | ▶ | if (exists $ref->[0]{sheet}{$sheet}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 1 | __PACKAGE__->sheet($ref->[0]{sheet}{$sheet}); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||
| 129 | 1 | damn "There's no worksheet '$sheet' in '$path'"; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | return __PACKAGE__->sheet; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 3 | 1; | |||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||