NAME
Text::Parser::AutoSplit - A role that adds the ability to auto-split a line into fields
VERSION
version 1.000
SYNOPSIS
use Text::Parser;
my $p1 = Text::Parser->new();
$p1->read('/path/to/file');
my $p2 = Text::Parser->new();
$p2->add_rule( do => '$this->field(0);' );
## add_rule method automatically sets up auto_split
$p2->read('/another/file');
DESCRIPTION
Text::Parser::AutoSplit
is a role that is automatically composed into an object of Text::Parser if the auto_split
attribute is set during object construction, or when add_rule
method is called. The field separator is controlled by another Text::Parser
attribute FS
.
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. A runtime error will be thrown if they are called without auto_split
being set. They can be used inside a subclass or in the rules.
NF
The name of this method comes from the NF
variable in the popular GNU Awk program.
Returns the number of fields on a line. The field separator is specified with FS
attribute.
$parser->applies_rule(
if => '$this->NF >= 2'
do => '$this->collect_info($2);',
dont_record => 1,
);
If your rule contains any positional identifiers (like $1
, $2
, $3
etc., to identify the field) the rule automatically checks that there are at least as many fields as the largest positional identifier. So the above rule could also be written as:
$parser->applies_rule(
do => '$this->collect_info($2);',
dont_record => 1,
);
It has the same results.
fields
Takes no argument and returns all the fields as an array. The FS
field separator controls how fields are defined. Leading and trailing spaces are trimmed.
$parser->add_rule( do => 'return [ $this->fields ];' );
field
Takes an integer argument and returns the field whose index is passed as argument.
$parser->add_rule(
if => '$this->field(0) eq "END"',
do => '$this->abort_reading;',
dont_record => 1,
);
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. Let's say the following is the text on a line in a file:
THIS IS SOME TEXT
field(0) field(1) field(2) field(3)
field(-4) field(-3) field(-2) field(-1)
field_range
Takes two optional integers $i
and $j
as arguments and returns an array, where the first element is field($i)
, the second field($i+1)
, and so on, till field($j)
.
$parser->add_rule(
if => '$1 eq "NAME:"',
do => 'return [ $this->field_range(1, -1) ];',
);
Both $i
and $j
can be negative, as is allowed by the field()
method. So, for example:
$parser->add_rule(
do => 'return [ $this->field_range(-2, -1) ];' # Saves the last two fields of every line
);
If $j
argument is omitted or set to undef
, it will be treated as -1
and if $i
is omitted, it is treated as 0
. For example the following may be used inside rules:
$this->field_range(1); # Returns all elements omitting the first
$this->field_range(); # same as fields()
$this->field_range(undef, -2); # Returns all elements omitting the last
join_range
This method essentially joins the return value of the field_range
method. It takes three arguments. The last argument is the joining string, and the first two are optional integer arguments $i
and $j
just like field_range
method.
$parser->add_rule(
do => qq(
$this->join_range(); # Joins all fields with $" (see perlvar)
$this->join_range(0, -1, '#'); # Joins with # separator
$this->join_range(2); # Joins all elements starting with index 2 to the end
# with $"
$this->join_range(1, -2); # Joins all elements in specified range with $"
));
## The return value of the last statement in the 'do' block is saved as a record
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 ...
my (@removed1) = $self->splice_fields($offset, $length, @values);
my (@removed2) = $self->splice_fields($offset, $length);
my (@removed3) = $self->splice_fields($offset);
The offset above is a required argument and can be negative.
WARNING: This is a destructive function. It will remove elements just like Perl's built-in splice
does, and the removed will be returned. If you only want to get the elements in a specific range of indices, try the field_range
method instead.
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.