8 |
8 |
use Text::CSV;
|
9 |
9 |
use Params::Validate qw(:all);
|
10 |
10 |
use Rose::Object::MakeMethods::Generic scalar => [ qw(
|
11 |
|
file encoding sep_char quote_char escape_char header profile class
|
12 |
|
numberformat dateformat _io _csv _objects _parsed _data _errors
|
|
11 |
file encoding sep_char quote_char escape_char header profile class
|
|
12 |
numberformat dateformat ignore_unknown_columns _io _csv _objects _parsed
|
|
13 |
_data _errors
|
13 |
14 |
) ];
|
14 |
15 |
|
15 |
16 |
|
... | ... | |
18 |
19 |
sub new {
|
19 |
20 |
my $class = shift;
|
20 |
21 |
my %params = validate(@_, {
|
21 |
|
sep_char => { default => ';' },
|
22 |
|
quote_char => { default => '"' },
|
23 |
|
escape_char => { default => '"' },
|
24 |
|
header => { type => ARRAYREF, optional => 1 },
|
25 |
|
profile => { type => HASHREF, optional => 1 },
|
26 |
|
file => 1,
|
27 |
|
encoding => 0,
|
28 |
|
class => 0,
|
29 |
|
numberformat => 0,
|
30 |
|
dateformat => 0,
|
|
22 |
sep_char => { default => ';' },
|
|
23 |
quote_char => { default => '"' },
|
|
24 |
escape_char => { default => '"' },
|
|
25 |
header => { type => ARRAYREF, optional => 1 },
|
|
26 |
profile => { type => HASHREF, optional => 1 },
|
|
27 |
file => 1,
|
|
28 |
encoding => 0,
|
|
29 |
class => 0,
|
|
30 |
numberformat => 0,
|
|
31 |
dateformat => 0,
|
|
32 |
ignore_unknown_columns => 0,
|
31 |
33 |
});
|
32 |
34 |
my $self = bless {}, $class;
|
33 |
35 |
|
... | ... | |
50 |
52 |
my ($self, %params) = @_;
|
51 |
53 |
|
52 |
54 |
$self->_open_file;
|
53 |
|
return unless $self->_check_header;
|
54 |
|
return unless $self->_parse_data;
|
|
55 |
return if ! $self->_check_header;
|
|
56 |
return if $self->class && ! $self->_check_header_for_class;
|
|
57 |
return if ! $self->_parse_data;
|
55 |
58 |
|
56 |
59 |
$self->_parsed(1);
|
57 |
60 |
return $self;
|
... | ... | |
110 |
113 |
my ($self, %params) = @_;
|
111 |
114 |
my @errors;
|
112 |
115 |
|
113 |
|
return unless $self->class;
|
114 |
|
return $self->header;
|
|
116 |
carp 'this should never be called without' unless $self->class;
|
115 |
117 |
|
116 |
|
for my $method (@{ $self->header }) {
|
117 |
|
next if $self->class->can($self->_real_method($method));
|
|
118 |
if ($self->ignore_unknown_columns) {
|
|
119 |
my @new_header;
|
|
120 |
for my $method (@{ $self->header }) {
|
|
121 |
push @new_header, $self->class->can($self->_real_method($method))
|
|
122 |
? $method : undef;
|
|
123 |
}
|
118 |
124 |
|
119 |
|
push @errors, [
|
120 |
|
$method,
|
121 |
|
undef,
|
122 |
|
"header field $method is not recognized",
|
123 |
|
undef,
|
124 |
|
0,
|
125 |
|
];
|
126 |
|
}
|
|
125 |
$self->header(\@new_header);
|
127 |
126 |
|
128 |
|
$self->_push_error(@errors);
|
|
127 |
return 1;
|
|
128 |
} else {
|
|
129 |
for my $method (@{ $self->header }) {
|
|
130 |
next if ! $method;
|
|
131 |
next if $self->class->can($self->_real_method($method));
|
129 |
132 |
|
130 |
|
return ! @errors;
|
|
133 |
push @errors, [
|
|
134 |
$method,
|
|
135 |
undef,
|
|
136 |
"header field $method is not recognized",
|
|
137 |
undef,
|
|
138 |
0,
|
|
139 |
];
|
|
140 |
}
|
|
141 |
|
|
142 |
$self->_push_error(@errors);
|
|
143 |
return ! @errors;
|
|
144 |
}
|
131 |
145 |
}
|
132 |
146 |
|
133 |
147 |
sub _parse_data {
|
... | ... | |
308 |
322 |
If present, the line will be handed to the new sub of this class,
|
309 |
323 |
and the return value used instead of the line itself.
|
310 |
324 |
|
|
325 |
=item C<ignore_unknown_columns>
|
|
326 |
|
|
327 |
If set, the import will ignore unkown header columns. Useful for lazy imports,
|
|
328 |
but deactivated by default.
|
|
329 |
|
311 |
330 |
=back
|
312 |
331 |
|
313 |
332 |
=head1 ERROR HANDLING
|
SL::Helper::Csv -> ignore_unkown_columns flag