Skip to content

Instantly share code, notes, and snippets.

@hisaichi5518
Created May 28, 2014 09:05
Show Gist options
  • Select an option

  • Save hisaichi5518/03957bd6c44057cc6297 to your computer and use it in GitHub Desktop.

Select an option

Save hisaichi5518/03957bd6c44057cc6297 to your computer and use it in GitHub Desktop.
use 5.10.1;
use strict;
use warnings;
use Text::CSV;
use Text::CSV_XS;
use Encode;
my $csv = Text::CSV->new ({ binary => 1 });
my $xs = Text::CSV_XS->new({ binary => 1 });
my $nb_csv = Text::CSV->new ({ binary => 0 });
my $nb_xs = Text::CSV_XS->new({ binary => 0 });
sub parse {
my ($driver, $body) = @_;
my $encoded_body = encode_utf8 $body;
warn $encoded_body;
open my $fh, '<', \$encoded_body or die "Cant open body: $!";
my $rows = $driver->getline_all($fh);
close $fh or die "Cant close body: $!";
$rows;
}
use Encode;
{
warn "テキスト文字列で渡す";
my $result = parse($csv, decode_utf8 "test,test\n1,日本語");
warn Encode::is_utf8($result->[1][1]); # 1が返ってくる
warn $result->[1][1]; # Wideなんちゃらがでる
};
{
warn "バイト文字列で渡す";
my $result = parse($csv, "test,test\n1,日本語");
warn Encode::is_utf8($result->[1][1]); # なぜか1が返ってくる
warn $result->[1][1]; # Wide なんちゃらは出ない
};
{
warn "テキスト文字列で渡す";
my $result = parse($xs, decode_utf8 "test,test\n1,日本語");
warn Encode::is_utf8($result->[1][1]); # 1が返ってくる
warn $result->[1][1]; # Wideなんちゃらがでる
};
{
warn "バイト文字列で渡す";
my $result = parse($xs, "test,test\n1,日本語");
warn Encode::is_utf8($result->[1][1]); # なぜか1が返ってくる
warn $result->[1][1]; # Wide なんちゃらは出ない
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment