Tripletail
view release on metacpan or search on metacpan
lib/Tripletail/CSV.pm view on Meta::CPAN
# -----------------------------------------------------------------------------
# Tripletail::CSV - ãã¡ã¤ã«ãã³ãã«ããã®ãã¼ã¿ã®èªã¿è¾¼ã¿
# -----------------------------------------------------------------------------
package Tripletail::CSV;
use strict;
use warnings;
use IO::Handle ();
use IO::Scalar ();
use version 0.77;
1;
sub _new {
my $class = shift;
my $this = bless {} => $class;
eval {
require Text::CSV_XS;
};
if ($@) {
die __PACKAGE__."#new: Text::CSV_XS is unavailable. (Text::CSV_XSã使ç¨ã§ãã¾ãã)\n";
}
my $opts = do {
if (Text::CSV_XS->VERSION >= version->parse('1.02')) {
{
binary => 1,
decode_utf8 => 0,
};
} else {
{
binary => 1,
};
}
};
$this->{csv} = Text::CSV_XS->new($opts);
$this;
}
sub parseCsv {
my $this = shift;
my $src = shift;
my $fh;
if (ref $src) {
$fh = $src;
}
else {
$fh = IO::Scalar->new(\$src);
}
Tripletail::CSV::Parser->_new($this, $fh);
}
sub makeCsv {
my $this = shift;
my $row = shift;
if (ref($row) ne 'ARRAY') {
die __PACKAGE__."#makeCsv: arg[1] is not an ARRAY ref. (第1弿°ãé
åã®ãªãã¡ã¬ã³ã¹ã§ã¯ããã¾ãã)\n";
}
$this->{csv}->combine(@$row);
$this->{csv}->string;
}
package Tripletail::CSV::Parser;
use strict;
use warnings;
sub _new {
my $class = shift;
my $csv = shift;
my $fh = shift;
my $this = bless {} => $class;
$this->{csv} = $csv;
$this->{fh} = $fh;
$this;
}
sub next {
my $this = shift;
if ($this->{fh}->eof) {
return;
}
else {
if (my $row = $this->{csv}{csv}->getline($this->{fh})) {
$row;
}
else {
die __PACKAGE__."#next: parse error. (䏿£ãªCSVå½¢å¼ã§ã)\n";
}
}
}
__END__
=encoding utf-8
=for stopwords
CSV
YMIRLINK
=head1 NAME
Tripletail::CSV - CSV ã®ãã¼ã¹ã¨çæ
=head1 SYNOPSIS
# ãã¼ã¹
my $csv = $TL->getCsv;
my $parser = $csv->parseCsv($CGI->getFile('upload'));
while (my $row = $parser->next) {
# $row: ['ã«ã©ã 1', 'ã«ã©ã 2', ...]
}
# çæ
$TL->print($csv->makeCsv([ qw(aaa bbb ccc) ]), "\n");
$TL->print($csv->makeCsv([ qw(aaa bbb ccc) ]), "\n");
=head1 DESCRIPTION
CSV ã®ãã¼ã¹ã¨çæãè¡ãçºã®ã¯ã©ã¹ã
ã«ã³ããå«ãã«ã©ã ãæ¹è¡ã³ã¼ããå«ãã«ã©ã çã
æ£ããå¦çããäºãåºæ¥ãã
æååã®ãã¼ã¹ã®ä»ã«ããã¡ã¤ã«ãã³ãã«ããã®ãã¼ã¹ãå¯è½ã
=head2 METHODS
=over 4
( run in 1.141 second using v1.01-cache-2.11-cpan-c221a9de4ec )