NAME
Text::Parser::AutoSplit - A role that adds the ability to auto-split a line into fields
VERSION
version 0.917
SYNOPSIS
package MyNewParser;
use parent 'Text::Parser';
sub new {
my $pkg = shift;
$pkg->SUPER::new(
auto_split => 1,
FS => qr/\s+\(*|\s*\)/,
@_,
);
}
sub save_record {
my $self = shift;
return $self->abort_reading if $self->NF > 0 and $self->field(0) eq 'STOP_READING';
$self->SUPER::save_record(@_) if $self->NF > 0 and $self->field(0) !~ /^[#]/;
}
package main;
my $parser = MyNewParser->new();
$parser->read(shift);
print $parser->get_records(), "\n";
DESCRIPTION
Text::Parser::AutoSplit
is a role that gets automatically composed into an object of Text::Parser if the auto_split
attribute is set during object construction. It is useful for writing complex parsers as derived classes of Text::Parser, because one has access to the fields. The field separator is controlled by another attribute FS
, which can be accessed via an accessor method of the same name. When the auto_split
attribute is set to a true value, the object of Text::Parser
will be able to use methods described in this role.
METHODS AVAILABLE ON AUTO-SPLIT
These methods become available when auto_split
attribute is true. You'll get a runtime error if you try to use them otherwise. They would be most likely used inside your own implementation of save_record
since the splits are done for each line.
NF
The name of this method comes from the NF
variable in the popular GNU Awk program. It stands for number of fields. Takes no arguments, and returns the number of fields.
sub save_record {
my $self = shift;
$self->save_record(@_) if $self->NF > 0;
}
field
Takes an integer argument and returns the field whose index is passed as argument. You can specify negative elements to start counting from the end. For example index -1
is the last element, -2
is the penultimate one, etc.
sub save_record {
my $self = shift;
$self->abort if $self->field(0) eq 'END';
}
find_field
This method finds an element matching a given criterion. The match is done by a subroutine reference passed as argument to this method. The subroutine will be called against each field on the line, until one matches or all elements have been checked. Each field will be available in the subroutine as $_
. Its behavior is the same as the first
function of List::Util.
sub save_record {
my $self = shift;
my $param = $self->find_field(
sub { $_ =~ /[=]/ }
);
}
find_field_index
This is similar to the find_field
method above, except that it returns the index of the element instead of the element itself.
sub save_record {
my $self = shift;
my $idx = $self->find_field_index(
sub { $_ =~ /[=]/ }
);
}
splice_fields
Just like Perl's built-in splice
function.
## Inside your own save_record method ...
$self->splice_fields($offset, $length, @values);
$self->splice_fields($offset, $length);
$self->splice_fields($offset);
The offset above is a required argument. It can be negative.
fields
Takes no argument and returns all the fields as an array.
## Inside your own save_record method ...
foreach my $fld ($self->fields) {
# do something ...
}
SEE ALSO
BUGS
Please report any bugs or feature requests on the bugtracker website http://github.com/balajirama/Text-Parser/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
AUTHOR
Balaji Ramasubramanian <balajiram@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2018-2019 by Balaji Ramasubramanian.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.