File: DKVP.pm

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (79 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package Catmandu::Importer::DKVP;

use Catmandu::Sane;

our $VERSION = '1.2024';

use Moo;
use namespace::clean;

with 'Catmandu::Importer';

has pair_sep_char => (is => 'ro', default => sub {'='});
has sep_char      => (is => 'ro', default => sub {','});
has _re           => (is => 'lazy');

sub _build__re {
    my ($self)        = @_;
    my $sep_char      = $self->sep_char;
    my $pair_sep_char = $self->pair_sep_char;
    qr/([^$pair_sep_char]+)$pair_sep_char([^$sep_char]+)(?:$sep_char|$)/;
}

sub generator {
    my ($self) = @_;

    return sub {
        state $fh = $self->fh;
        state $re = $self->_re;
        if (defined(my $line = <$fh>)) {
            chomp $line;
            my %rec = $line =~ /$re/g;
            return \%rec;
        }
        return;
    };
}

1;

__END__

=pod

=head1 NAME

Catmandu::Importer::DKVP - Delimited key-value pairs importer

=head1 DESCRIPTION

This package imports text files containing delimited key-value pairs.

    a=pan,b=pan,i=1,x=0.3467901443380824,y=0.7268028627434533
    a=eks,b=pan,i=2,x=0.7586799647899636,y=0.5221511083334797
    a=wye,b=wye,i=3,x=0.20460330576630303,y=0.33831852551664776

=head1 CONFIGURATION

=over

=item sep_char

The character that separates fields. Default is C<,>.

=item pair_sep_char

The character that separates key-value pairs. Default is C<=>.

=back

=head1 METHODS

Every L<Catmandu::Importer> is a L<Catmandu::Iterable>. All their methods are
inherited.

=head1 SEE ALSO

L<Miller|http://johnkerl.org/miller/doc/file-formats.html#DKVP:_Key-value_pairs>

=cut