BoutrosLab-TSVStream
view release on metacpan or search on metacpan
lib/BoutrosLab/TSVStream/Format/None/Dyn.pm view on Meta::CPAN
package BoutrosLab::TSVStream::Format::None::Dyn;
# safe Perl
use warnings;
use strict;
use Carp;
use BoutrosLab::TSVStream::IO::Role::Dyn;
use Moose;
use namespace::autoclean;
use MooseX::ClassAttribute;
class_has '_fields' => (
is => 'ro',
isa => 'ArrayRef',
default => sub { [ ] }
);
with 'BoutrosLab::TSVStream::IO::Role::Dyn';
=head1 NAME
BoutrosLab::TSVStream::Format::None::Dyn
=cut
=head1 SYNOPSIS
use BoutrosLab::TSVStream::Format::None::Dyn;
# you know ./myfile1 has a valid header line
my $reader1 = BoutrosLab::TSVStream::Format::None::Dyn->reader(
file => "./myfile1" );
my $flds;
while (my $rec1 = $reader1->read) {
$flds //= $rec->dyn_fields; # first time, get the field names
$vals = $rec->dyn_values; # get the field values
# use the record: $flds: [f1,f2,f3,...], $vals: [v1,v2,v3,...]
}
# ./myfile2 has no header line
my $reader2 = BoutrosLab::TSVStream::Format::None::Dyn->reader(
file => "./myfile2",
dyn_fields => [ qw(foo bar baz) ],
);
while (my $rec = $reader2->read) {
$vals = $rec->dyn_values; # get the field values
# use the record: flds: [foo,bar,baz], $vals: [v1,v2,v3]
}
my $writer1 = BoutrosLab::TSVStream::Format::None::Dyn->writer(
file => "myoutfile",
dyn_fields => [ qw(name address ],
);
$writer1->write( 'Larry Wall', 'Republic of Perl' );
# myfile will contain 2 lines:
# name<TAB>address
# Larry Wall<TAB>Republic of Perl
my $writer2 = BoutrosLab::TSVStream::Format::None::Dyn->writer(
file => "myoutfile",
header => 'skip',
);
$writer2->write( 'Larry Wall', 'Republic of Perl' );
# myfile will contain 1 line:
# Larry Wall<TAB>Republic of Perl
( run in 1.344 second using v1.01-cache-2.11-cpan-39bf76dae61 )