our
$VERSION
=
'0.08'
;
sub
new {
my
(
$self
,
@kv
) =
@_
;
return
bless
{
_kv
=> \
@kv
,
_wants
=> [],
_ignores
=> [],
},
$self
;
}
sub
ignore_fields {
my
(
$self
,
@keys
) =
@_
;
@keys
?
$self
->{_ignores} = \
@keys
:
return
$self
->{_ignores};
}
sub
want_fields {
my
(
$self
,
@keys
) =
@_
;
@keys
?
$self
->{_wants} = \
@keys
:
return
$self
->{_wants};
}
sub
ordered {
my
$self
=
shift
;
@_
?
$self
->{_ordered} =
shift
:
return
$self
->{_ordered};
}
sub
has_wants {
@{
shift
->want_fields} ? 1 : 0;
}
sub
has_ignores {
@{
shift
->ignore_fields} ? 1 : 0;
}
sub
parse_line {
my
(
$self
,
$line
) =
@_
;
chomp
$line
;
my
$has_wants
=
ref
$self
?
$self
->has_wants :
undef
;
my
$has_ignores
=
ref
$self
?
$self
->has_ignores :
undef
;
my
%wants
;
if
(
$has_wants
) {
%wants
=
map
{
$_
=> 1 } @{
$self
->want_fields};
}
my
%ignores
;
if
(
$has_ignores
) {
%ignores
=
map
{
$_
=> 1 } @{
$self
->ignore_fields};
}
my
%kv
;
if
(
ref
$self
and
$self
->ordered) {
tie
%kv
,
'Tie::IxHash'
;
}
for
(
map
{ [
split
':'
,
$_
, 2 ] }
split
"\t"
,
$line
) {
next
if
$has_ignores
and
$ignores
{
$_
->[0]};
next
if
$has_wants
and not
$wants
{
$_
->[0]};
$kv
{
$_
->[0]} =
$_
->[1];
}
return
\
%kv
;
}
sub
parse_file {
my
(
$self
,
$file
,
$opt
) =
@_
;
$self
=
$self
->new
unless
ref
$self
;
my
$fh
=
$self
->_open(
$file
,
$opt
);
my
@out
;
while
(
my
$line
=
$fh
->getline) {
push
@out
,
$self
->parse_line(
$line
);
}
$fh
->
close
;
return
\
@out
;
}
sub
parse_file_utf8 {
my
(
$self
,
$file
) =
@_
;
return
$self
->parse_file(
$file
, {
utf8
=> 1 });
}
sub
parse_file_iter {
my
(
$self
,
$file
,
$opt
) =
@_
;
$self
=
$self
->new
unless
ref
$self
;
my
$fh
=
$self
->_open(
$file
,
$opt
);
return
Text::LTSV::Iterator->new(
$self
,
$fh
);
}
sub
parse_file_iter_utf8 {
my
(
$self
,
$file
) =
@_
;
return
$self
->parse_file_iter(
$file
, {
utf8
=> 1 });
}
sub
to_s {
my
$self
=
shift
;
my
$n
= @{
$self
->{_kv}};
my
@out
;
for
(
my
$i
= 0;
$i
<
$n
;
$i
+= 2) {
push
@out
,
join
':'
,
$self
->{_kv}->[
$i
],
$self
->{_kv}->[
$i
+1];
}
return
join
"\t"
,
@out
;
}
sub
_open {
my
(
$self
,
$file
,
$opt
) =
@_
;
$opt
||= {};
if
(Scalar::Util::openhandle(
$file
)) {
open
my
$fh
,
'<&'
,
$file
;
if
(
$opt
->{utf8}) {
binmode
$fh
,
':raw'
;
binmode
$fh
,
':encoding(utf-8)'
;
}
return
$fh
;
}
return
IO::File->new(
$file
,
$opt
->{utf8} ?
'<:utf8'
:
'r'
) or croak $!;
}
1;