| 1 | package Thesaurus::CSV; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Thesaurus; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use base 'Thesaurus'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Params::Validate qw( validate SCALAR BOOLEAN ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Text::CSV_XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | sub new | ||||||||||
| 14 | 4 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 15 | 4 | return shift->SUPER::new(@_); | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | sub _init | ||||||||||
| 19 | 4 | ▶ | { | ||||||||
|
Subroutine
_init: called CC=5 SCAR=16.2
| |||||||||||
| 20 | 4 | my $self = shift; | |||||||||
| 21 | 4 | my %p = validate( @_, | |||||||||
| 22 | { filename => { type => SCALAR } } | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 4 | $self->{csv} = Text::CSV_XS->new( { binary => 1 } ); | |||||||||
| 26 | 4 | $self->{params}{filename} = $p{filename}; | |||||||||
| 27 | |||||||||||
| 28 | 4 | ▶ | if ( -e $self->{params}{filename} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | { | ||||||||||
| 30 | 1 | ▶ | my $fh = IO::File->new("<$self->{params}{filename}") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | or die "Cannot read $self->{params}{filename}: $!"; | ||||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | while ( ! $fh->eof ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | { | ||||||||||
| 35 | 2 | my $cols = $self->{csv}->getline($fh); | |||||||||
| 36 | |||||||||||
| 37 | die "Text::CSV_XS can't parse " . $self->{csv}->error_input | ||||||||||
| 38 | 2 | ▶ | unless defined $cols; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 2 | $self->add($cols); | |||||||||
| 41 | } | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 4 | return $self; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | sub save | ||||||||||
| 48 | 1 | ▶ | { | ||||||||
|
Subroutine
save: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 49 | 1 | my $self = shift; | |||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | my $fh = IO::File->new(">$self->{params}{filename}") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | or die "Cannot write to $self->{params}{filename}: $!"; | ||||||||||
| 53 | |||||||||||
| 54 | 1 | foreach my $list ( $self->all ) | |||||||||
| 55 | { | ||||||||||
| 56 | 2 | $self->{csv}->print( $fh, $list ); | |||||||||
| 57 | 2 | print $fh "\n"; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | close $fh; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 2 | 1; | |||||||||
| 64 | |||||||||||
| 65 | __END__ | ||||||||||