NAME
Text::CSV::Encoded - Encoding aware Text::CSV.
VERSION
version 0.25
SYNOPSIS
# Here in Perl 5.8 or later
$csv = Text::CSV::Encoded->new ({
encoding_in => "iso-8859-1", # the encoding comes into Perl
encoding_out => "cp1252", # the encoding comes out of Perl
});
# parsing CSV is regarded as input
$csv->parse( $line ); # $line is a iso-8859-1 encoded string
@columns = $csv->fields(); # they are unicode data
# combining list is regarded as output
$csv->combine(@columns); # they are unicode data
$line = $csv->string(); # $line is a cp1252 encoded string
# if you want for returned @columns to be encoded in $encoding
# or want for combining @columns to be assumed in $encoding
$csv->encoding( $encoding );
# change input/output encodings
$csv->encoding_in('shiftjis')->encoding_out('utf8');
$csv->eol("\n");
open (my $in, "sjis.csv");
open (my $out, "output.csv");
# change an encoding from shiftjis to utf8
while( my $columns = $csv->getline( $in ) ) {
$csv->print( $out, $columns );
}
close($in);
close($out);
# simple shortcuts
# (regardless of encoding_in/out and encoding)
$uni_columns = $csv->decode( 'euc-jp', $line ); # euc-jp => unicode
$line = $csv->encode( 'euc-jp', $uni_columns ); # unicode => euc-jp
# pass check value to coder class
$csv->coder->encode_check_value( Encode::FB_PERLQQ );
DESCRIPTION
This module inherits Text::CSV and is aware of input/output encodings.
ENCODINGS
Acceptable names of encodings (encoding_in
, encoding_out
and encoding
) are depend upon its coder class (see to "CODER CLASS"). But these names should be based on Encode supported names. See to Encode::Supported and Encode::Alias.
METHODS
new
$csv = Text::CSV::Encoded->new();
Text::CSV::Encoded->error_diag unless $csv; # report error message
Creates a new Text::CSV::Encoded object. It can take all options of Text::CSV. Of course, binary
option is always on.
If Text::CSV::Encoded fails in constructing, you can get an error message using error_diag
. See to "error_diag" in Text::CSV.
The following options are supported by this method:
- encoding
-
The encoding of list data in below cases.
* list data returned by fields() after successful parse(). * list data consumed by combine(). * list reference returned by getline(). * list reference taken by print().
See to "encoding".
- encoding_in
- encoding_io_in
- encoding_to_parse
-
The encoding for pre-parsing CSV strings. See to "encoding_in".
encoding_io_in
is an alias toencoding_in
. If bothencoding_in
andencoding_io_in
are set at the same time, theencoding_in
takes precedence.encoding_to_parse
is an alias toencoding_in
. If bothencoding_in
andencoding_to_parse
are set at the same time, theencoding_in
takes precedence. - encoding_out
- encoding_io_out
- encoding_to_combine
-
The encoding for combined CSV strings. See to "encoding_out".
encoding_io_out
is an alias toencoding_out
. If bothencoding_out
andencoding_io_out
are set at the same time, theencoding_out
takes precedence.encoding_to_combine
is an alias toencoding_out
. If bothencoding_out
andencoding_io_out
are set at the same time, theencoding_out
takes precedence. - coder_class
-
A name of coder class that really decodes and encodes data.
encoding_in
$csv = $csv->encoding_in( $encoding );
The accessor to an encoding for pre-parsing CSV strings. If no encoding is given, returns current $encoding
, otherwise the object itself.
$encoding = $csv->encoding_in()
In parse
or getline
, the $csv
will assume CSV data as the given encoding. If encoding_in
is not specified or is set with false value (undef), it will assume input CSV strings as Unicode (not UTF-8) when Text::CSV::Encoded::Coder::Encode is used.
$csv->encoding_in( undef );
# assume as Unicode when Text::CSV::Encoded::Coder::Encode is used.
If you pass a list reference that contains multiple encodings to the method, the working are depend upon the coder class. For example, if you use the coder class with Text::CSV::Encoded::Coder::EncodeGuess, it might guess the encoding from the given list.
$csv->coder_class( 'Text::CSV::Encoded::Coder::EncodeGuess' );
$csv->encoding_in( ['shiftjis', 'euc-jp', 'iso-20022-jp'] );
See to "Coder Class" and Text::CSV::Encoded::Coder::EncodeGuess.
encoding_out
$csv = $csv->encoding_out( $encoding );
The accessor to an encoding for converting combined CSV strings. If no encoding is given, returns current $encoding
, otherwise the object itself.
$encoding = $csv->encoding_out();
In combine
or print
, the $csv
will return a result string encoded in the given encoding. If encoding_out
is not specified or is set with false value, it will return a result string as Unicode (not UTF-8).
$csv->encoding_out( undef );
# return as Unicode when Text::CSV::Encoded::Coder::Encode is used.
You must not pass a list reference to encoding_out
, unlike encoding_in
or encoding
.
encoding
$csv = $csv->encoding( $encoding );
$encoding = $csv->encoding();
The accessor to an encoding for list data in the below cases.
* list data returned by fields() after successful parse().
* list data consumed by combine().
* list reference returned by getline().
* list reference taken by print().
In other word, in parse
and getline
, encoding
is an encoding of the returned list. And in combine
and print
, it is assumed as an encoding for the passing list data.
If encoding
is not specified or is set with false value (undef
), the field data will be regarded as Unicode (when Text::CSV::Encoded::Coder::Encode is used).
# ex.) a souce code is encoded in euc-jp, and print to stdout in shiftjis.
@fields = ( .... );
$csv->encoding('euc-jp')
->encoding_to_combine('shiftjis') # same as encoding_out
->combine( @fields ); # from euc-jp to shift_jis
print $csv->string;
$csv->encoding('shiftjis')
->encoding_to_parse('shiftjis') # same as encoding_in
->parse( $csv->string ); # from shift_jis to shift_jis
print join(", ", $csv->fields );
If you pass a list reference contains multiple encodings to the method, The working are depend upon the coder class. For example, Text::CSV::Encoded::EncodeGuess might guess the encoding from the given list.
$csv->coder_class( 'Text::CSV::Encoded::Coder::EncodeGuess' );
$csv->encoding( ['ascii', 'ucs2'] )->combine( @cols );
See to