use base 'BSE::Importer::Source::Base';
use Text::CSV;
-our $VERSION = "1.000";
+our $VERSION = "1.001";
my @text_csv_options = qw(quote_char escape_char sep_char binary allow_loose_quotes allow_loose_escapes allow_whitespace);
sub each_row {
my ($self, $importer, $filename) = @_;
- open my $fh, "<encoding($self->{encoding})", $filename
- or die "Cannot open file $filename: $!\n";
+ my $fh;
+
+ if (ref $filename) {
+ $fh = $filename;
+ binmode $fh, ":encoding($self->{encoding})";
+ }
+ else {
+ open $fh, "<:encoding($self->{encoding})", $filename
+ or die "Cannot open file $filename: $!\n";
+ }
my $csv = Text::CSV->new($self->{csv_opts})
or die "Cannot use CSV: ", Text::CSV->error_diag(), "\n";
#!perl -w
use strict;
-use Test::More tests => 6;
+use Test::More tests => 8;
use BSE::Cfg;
BEGIN {
eval "require Text::CSV;"
], "check data read");
}
+{
+ my $cfg = BSE::Cfg->new_from_text(text => <<CFG);
+[import profile test]
+source=CSV
+CFG
+ my $importer = DummyImporter->new(columns => 3, cfg => $cfg);
+ my $src = BSE::Importer::Source::CSV->new
+ (
+ importer => $importer,
+ opts => { profile => "test", cfg => $cfg },
+ );
+ ok($src, "make a CSV source");
+
+ my $csv = <<EOS;
+a,b,c
+1,2,3
+abc,def,hij
+EOS
+ open my $fh, "<", \$csv;
+
+ $src->each_row($importer, $fh);
+ is_deeply($importer->{rows},
+ [
+ [ "Line 2", 1, 2, 3 ],
+ [ "Line 3", qw(abc def hij) ],
+ ], "check data read with fh source");
+}
+
{
my $cfg = BSE::Cfg->new_from_text(text => <<'CFG');
[import profile test]