[libcatmandu-perl] 12/30: new DKVP (delimited key-value pairs) importer

Jonas Smedegaard dr at jones.dk
Tue Dec 19 11:10:13 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/1.0700-1
in repository libcatmandu-perl.

commit 05141e6c47156f57d9603fdded8ebbb96470f7d2
Author: Nicolas Steenlant <nicolas.steenlant at ugent.be>
Date:   Fri Oct 6 16:02:41 2017 +0200

    new DKVP (delimited key-value pairs) importer
---
 Changes                       |  1 +
 lib/Catmandu/Importer/DKVP.pm | 79 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git a/Changes b/Changes
index 9690671..6ac9fdf 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 Revision history for Catmandu
 
 {{$NEXT}}
+  - new DKVP importer for delimited key-value pairs
 
 1.0606  2017-09-26 10:42:35 CEST
   - fix bug with numeric strings in fix preprocessing
diff --git a/lib/Catmandu/Importer/DKVP.pm b/lib/Catmandu/Importer/DKVP.pm
new file mode 100644
index 0000000..3abe2a6
--- /dev/null
+++ b/lib/Catmandu/Importer/DKVP.pm
@@ -0,0 +1,79 @@
+package Catmandu::Importer::DKVP;
+
+use Catmandu::Sane;
+
+our $VERSION = '1.0606';
+
+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

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-perl.git



More information about the Pkg-perl-cvs-commits mailing list