Text::Parser::AutoSplit - A role that adds the ability to auto-split a line into fields
version 0.925
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";
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.
Text::Parser::AutoSplit
auto_split
FS
Text::Parser
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 used inside the subclass implementation of save_record.
save_record
The name of this method comes from the NF variable in the popular GNU Awk program. Takes no arguments, and returns the number of fields.
NF
sub save_record { my $self = shift; $self->save_record(@_) if $self->NF > 0; }
Takes no argument and returns all the fields as an array.
## Inside your own save_record method ... foreach my $fld ($self->fields) { # do something ... }
Takes an integer argument and returns the field whose index is passed as argument.
sub save_record { my $self = shift; $self->abort if $self->field(0) eq 'END'; }
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:
-1
-2
THIS IS SOME TEXT field(0) field(1) field(2) field(3) field(-4) field(-3) field(-2) field(-1)
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).
$i
$j
field($i)
field($i+1)
field($j)
## returns 4 elements starting with field(3) upto field(6) my (@flds) = $self->field_range(3, 6);
Both $i and $j can be negative, as is allowed by the field() method. So, for example:
field()
$self->field_range(-2, -1); # Returns the last two elements
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:
undef
0
$self->field_range(1); # Returns all elements omitting the first $self->field_range(); # same as fields() $self->field_range(undef, -2); # Returns all elements omitting the last
This method essentially joins the return value of the field_range method. It takes three arguments. The first argument is the joining string, and the other two are optional integer arguments $i and $j just like field_range method.
field_range
$self->join_range(); # Joins all fields with $" (see perlvar) $self->join_range(0, -1, '#'); # Joins with # separator $self->join_range(2); # Joins all elements starting with index 2 to the end # with $" $self->join_range(1, -2); # Joins all elements in specified range with $"
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.
$_
first
sub save_record { my $self = shift; my $param = $self->find_field( sub { $_ =~ /[=]/ } ); }
This is similar to the find_field method above, except that it returns the index of the element instead of the element itself.
find_field
sub save_record { my $self = shift; my $idx = $self->find_field_index( sub { $_ =~ /[=]/ } ); }
Just like Perl's built-in splice function.
splice
## 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.
List::Util
List::SomeUtils
GNU Awk program
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.
Balaji Ramasubramanian <balajiram@cpan.org>
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.
To install Text::Parser, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Parser
CPAN shell
perl -MCPAN -e shell install Text::Parser
For more information on module installation, please visit the detailed CPAN module installation guide.