The Parse::FixedLength module facilitates the process of breaking a string into its fixed-length components. Sure, it's a glorified (and in some ways more limited) substitute for the perl functions pack and unpack, but it's my belief that this module helps in the maintainability of working with fixed length formats as the number of fields in a format grows.

If the format argument is a string, then new will attempt to return the result of calling the new method for "Parse::FixedLength::$format". You can include the '$format' in the import list of the 'use Parse::FixedLength' statement if you want to require the format at compile time (See EXAMPLES).

You can use ':all' as an argument in the import list, e.g., 'use Parse::Length qw(:all)', to require all available Parse::FixedLength::* modules, but obviously you can't use ':all' as a format argument in new().

Otherwise the format must be an array reference of field names and lengths as either alternating elements, or delimited args in the same field, e.g.:

If the first format is chosen, then no delimiter characters may appear in the field names (see delim option below).

To right justify a field (during the 'pack' method), an "R" may be appended to the length of the field followed by (optionally) the character to pad the string with (if no character follows the "R", then a space is assumed). This is somewhat inefficient, so its only recommended if actually necessary to preserve the format during operations such as math or converting format lengths. If its not needed but you'd like to specify it anyway for documentation purposes, you can use the no_justify option below. Also, it does change the data in the hash ref argument.

New (and barely tested): The length of the field may also be any valid format string for the perl functions pack/unpack which would return a single element. E.g., this is valid:

The delimiter used to separate the name and length in the format array. If another delimiter follows the length then the next two fields are assumed to be start and end position, and after that any 'extra' fields are ignored. The package variable DELIM may also be used. (default: ":")

This option ignores any lengths supplied in the format argument (or allows having no length args in the format), and sets the lengths for all the fields to this value. As well as the obvious case where all formats are the same length, this can help facilitate converting from a non-fixed length format (where you just have field names) to a fixed-length format. (default: false)

This option controls the behavior of new() when duplicate field names are found. By default a fatal error will be generated if duplicate field names are found. If you have, e.g., some unused filler fields, then as the value to this option, you can either supply an arrayref containing valid duplicate names or a simple true value to accept all duplicate values. If there is more than one duplicate field, then when parsed, they will be renamed '<name>_1', '<name>_2', etc. (default: false)

By default, if two fields exist after the length argument in the format (delimited by whatever delimiter is set), then they are assumed to be the start and end position (starting at 1), of the field, and these fields are validated to be correct, and a fatal error will be generated if they are not correct. If this option is true, then the start and end are not validated. (default: false)

If true, print field names and values during parsing and packing (as a quick format validation check). The package variable DEBUG may also be used. If a non-reference argument is given, output is sent to STDOUT, otherwise we assume we have a filehandle open for writing. (default: false)

This method takes a string and returns 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. The hash reference returned is an object, so you can either get/set values the normal way:

This turns a hash reference into an object where the keys of the hash can be used as methods for accessing or setting the values of the hash. This turns the hash into a semi-secure hash which is a sort of combination of Hash::AsObject and Tie::SecureHash in that no new keys will be added to the hash if only methods are used to access the hash. Hashes with the same set of keys are blessed into the same package, so adding keys to one hash may affect the methods allowed on another hash.

This method trims leading pad characters from the data. It is the method implicitly called during the parse method when the 'trim' option is set in new(). The data passed is modified, so there is no return value.

Returns the parser's format layout information in a format suitable for cutting and pasting into the format array argument of a Parse::FixedFormat->new() call, and includes the start and end positions of all the fields (starting with position 1). If a true argument is supplied then it will include the start and ending positions as comments. E.g.:

Returns a format converting object. $parser1 is the parsing object to convert from, $parser2 is the parsing object to convert to.

By default, common field names will be mapped from one format to the other. Fields with different names can be mapped from the first format to the other (or you can override the default) using the second argument. The keys are the source field names and the corresponding values are the target field names. This argument can be a hash ref or an array ref since you may want to map one source field to more than one target field.

Defaults for any field in the target format can be supplied using the third argument, where the keys are the field names of the target format, and the value can be a scalar constant, or a subroutine reference where the first argument is simply the mapped value (or the empty string if there was no mapping), and the second argument is the entire hash reference that results from parsing the data with the 'from' parser object. E.g. if you were mapping from a separate 'zip' and 'plus_4' field to a 'zip_plus_4' field, you could map 'zip' to 'zip_plus_4' and then supply as one of the key/value pairs in the 'defaults' hash ref the following:

zip_plus_4 => sub { shift() . $_[0]{plus_4} }

The fourth argument is an optional hash ref may which may contain the following: