Text::Parser::AutoSplit - A role that adds the ability to auto-split a line into fields


version 1.000


    use Text::Parser;

    my $p1 = Text::Parser->new();
    my $p2 = Text::Parser->new();
    $p2->add_rule( do => '$this->field(0);' );
        ## add_rule method automatically sets up auto_split


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.


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.


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.

        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:

        do          => '$this->collect_info($2);', 
        dont_record => 1, 

It has the same results.


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 ];' );


Takes an integer argument and returns the field whose index is passed as argument.

        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)


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).

        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:

        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


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.

        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


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 { $_ =~ /[=]/ }


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 { $_ =~ /[=]/ }


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.



Please report any bugs or feature requests on the bugtracker website

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 <>


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.