| 1 | package App::PerlShell::Plugin::TextCSV; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | ######################################################## | ||||||||||||||
| 4 | # AUTHOR = Michael Vincent | ||||||||||||||
| 5 | # www.VinsWorld.com | ||||||||||||||
| 6 | ######################################################## | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use Pod::Usage; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Pod::Find qw( pod_where ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 0 | ▶ | eval "use Text::CSV"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||
| 14 | 1 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 15 | 1 | print "Text::CSV required.\n"; | |||||||||||||
| 16 | 1 | return 1; | |||||||||||||
| 17 | } | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 1 | ▶ | use Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | 0 | our @EXPORT = qw( | |||||||||||||
| 22 | TextCSV | ||||||||||||||
| 23 | textcsv | ||||||||||||||
| 24 | ); | ||||||||||||||
| 25 | |||||||||||||||
| 26 | 0 | our @ISA = qw ( Exporter ); | |||||||||||||
| 27 | |||||||||||||||
| 28 | 0 | ▶ | sub TextCSV { | ||||||||||||
|
Subroutine
TextCSV: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 29 | 0 | pod2usage( | |||||||||||||
| 30 | -verbose => 2, | ||||||||||||||
| 31 | -exitval => "NOEXIT", | ||||||||||||||
| 32 | -input => pod_where( {-inc => 1}, __PACKAGE__ ) | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | ######################################################## | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 0 | ▶ | sub textcsv { | ||||||||||||
|
Subroutine
textcsv: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||
| 39 | 0 | my ( $file, $params ) = @_; | |||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | ▶ | if ( not defined $file ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | 0 | print "file required\n"; | |||||||||||||
| 43 | 0 | return; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | 0 | ▶ | if ( not defined $params ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | $params = {}; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | ▶ | my $csv = Text::CSV->new( $params ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | or die Text::CSV->error_diag; | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 0 | ▶ | open my $fh, "<", $file | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | or die "$!"; | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | my @rets; | |||||||||||||
| 56 | 0 | my $retType = wantarray; | |||||||||||||
| 57 | 0 | my ( $line, $skip ) = ( 0, 0 ); | |||||||||||||
| 58 | 0 | ▶ | while ( my $row = $csv->getline($fh) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 0 | $line++; # for error message if needed | |||||||||||||
| 60 | 0 | push @rets, $row; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | 0 | ▶ | if ( not $csv->eof ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | my ( $cde, $str, $pos ) = $csv->error_diag; | |||||||||||||
| 64 | 0 | print "$line,$pos: ($cde) $str\n"; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 0 | close $fh; | |||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | ▶ | if ( not defined($retType) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 70 | 0 | for ( @rets ) { | |||||||||||||
| 71 | 0 | print join $csv->sep_char, @{$_}; | |||||||||||||
| 72 | 0 | print "\n"; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | return | ||||||||||||||
| 75 | 0 | } elsif ($retType) { | |||||||||||||
| 76 | 0 | return @rets; | |||||||||||||
| 77 | } else { | ||||||||||||||
| 78 | 0 | return \@rets; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 0 | 1; | |||||||||||||
| 83 | |||||||||||||||
| 84 | __END__ | ||||||||||||||