BoutrosLab-TSVStream

 view release on metacpan or  search on metacpan

lib/BoutrosLab/TSVStream/IO/Role/Reader/Dyn.pm  view on Meta::CPAN

package BoutrosLab::TSVStream::IO::Role::Reader::Dyn;

# safe Perl
use warnings;
use strict;
use Carp;

=head1 NAME

    BoutrosLab::TSVStream:IO::Role::Reader::Dyn

=cut

use Moose::Role;
use namespace::autoclean;

with 'BoutrosLab::TSVStream::IO::Role::Reader::Fixed';

sub _read_no_header {
	my $self = shift;
	my $none = $self->header eq 'none';
	( $none, ($none && $self->_has_dyn_fields) );
	}

sub _fill_dyn_fields {
	my ($self, $none, $is_head, $stream_fields ) = @_;
    my $num_fixed_fields = $#{ $self->fields };

	if (!$self->_has_dyn_fields) {
		$self->_set_dyn_fields( [
			(!$none && $is_head)
				? @{$stream_fields}[ $num_fixed_fields+1 .. $#$stream_fields ]
				: $self->_extra_names( $#$stream_fields - $num_fixed_fields )
			] );
		}
	}

=head1 SYNOPSIS

	$class->reader( ... );

	# ($class will use the role BoutrosLab::TSVStream which will provide
	# the reader method, that method will return a Reader object with:
	# ...
	# return BoutrosLab::TSVStream::Reader->new(
		# handle => $fd,	# (required)
		# class  => $class,	# (required) class
		# file   => $file,	# (optional) used (as filename) in error messages
		# header => $str,	# (optional) one of: check none (default 'check')
		# );

	while (my $record = $reader->read) {
		# ... $record is a $class object
		# ... use $record->field1, $record->field2, etc. - all of the methods of $class object
		}

=head1 DESCRIPTION

This object provides an iterator to read through the lines
of a data stream (C<$fd>), converting each from a line with
tab separated fields into an object of a class (C<$classs>)
that has attributes for those fields.

Usually, the data stream will start with a line that has the
fieldnames in a tab separated list, and the rest of the stream
has lines that contain the field values in a tab separated list.

Any error diagnostics will refer to the stream using the
C<$file> filename if it is provided.

The C<$class> class will have a class attribute named
C<_fields>.  Usually, this will be a read-only method that
returns a list of fieldnames that will be used to validate
the first line in the data stream (which should contain the
field names as the column vlues).



( run in 0.736 second using v1.01-cache-2.11-cpan-39bf76dae61 )