NAME

Parse::FixedLength - parse a string containing fixed length fields into component parts

SYNOPSIS

use Parse::FixedLength;

my $parser = Parse::FixedLength->new([
    first_name => 10,
    last_name  => 10,
    address    => 20,
]);

my $data = 'Bob       Jones     1122 Main St.       ';
my $parsed = $parser->parse($data);

or:

my $parser = Parse::FixedLength->new([qw(
    first_name:10
    last_name:10
    address:20
)], {delim=>":"});

DESCRIPTION

The Parse::FixedLength module facilitates the process of breaking a string into its fixed-length components.

PARSING ROUTINES

new()
$parser = Parser::FixedLength->new($aref_format, $href_parameters)

This method takes an array reference of field names and lengths as either alternating elements, or delimited args in the same field.

An optional hash ref may also be supplied which may contain the following:

delim - The delimiter used to separate the name and length in
        the format array.

spaces - If true, preserve trailing spaces in the parsed output.

debug  - Print field names and values during parsing (as a quick
         format validation check).
parse()
$href_parsed_data = $parser->parse($string_to_parse)

This function takes a string and returns the results of fixed length parsing as a hash reference of field names and values if called in scalar context, or just a list of the values if called in list context.

pack $packed_str = $parser->pack($href_data_to_pack);

This function takes a hash reference and returns a fixed length format output string.

names()
$aref_names = $parser->names;

Return an ordered arrayref of the field names.

length()
$tot_length   = $parser->length;
$field_length = $parser->length($field_name);

Returns the total length of all the fields, or of just one field name.

EXAMPLES

use Parse::FixedLength;

my $parser = Parse::FixedLength->new([
    first_name => 10,
    last_name  => 10,
    widgets_this_month => 5,
]);

# Do a simple name casing of names
# and print widgets projected for the year for each person
# (Numbers are right justified for cut n paste purposes,
# but will end up left justified to simplify this example)
while (<DATA>) {
    warn "No record terminator found!\n" unless chomp;
    warn "Short Record!\n" unless $parser->length == length;
    my $data = $parser->parse($_);
    # See Lingua::EN::NameCase for a real attempt at name casing
    s/(\w+)/\u\L$1/g for @$data{qw(first_name last_name)};
    $data->{widgets_this_month} *= 12;
    print $parser->pack($data), "\n";
}
__DATA__
BOB       JONES        24
JOHN      SMITH         5
JANE      DOE           7

AUTHOR

Douglas Wilson <dougw@cpan.org>
original by Terrence Brannon <tbone@cpan.org>

COPYRIGHT

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.