NAME
Text::FixedWidth - Easy OO manipulation of fixed width text files
SYNOPSIS
use Text::FixedWidth;
my $fw = new Text::FixedWidth;
$fw->set_attributes(qw(
fname undef %10s
lname undef %-10s
points 0 %04d
));
$fw->parse(string => ' JayHannah 0003');
$fw->get_fname; # Jay
$fw->get_lname; # Hannah
$fw->get_points; # 0003
$fw->set_fname('Chuck');
$fw->set_lname('Norris');
$fw->set_points(17);
$fw->string; # ' ChuckNorris 0017'
If you're familiar with printf formats, then this class should make processing fixed width files trivial. Just define your attributes and then you can get_* and set_* all day long. When you're happy w/ your values envoke string() to spit out your object in your defined fixed width format.
When reading a fixed width file, simply pass each line of the file into parse(), and then you can use the get_ methods to retrieve the value of whatever attributes you care about.
METHODS
new
Constructor. Does nothing fancy.
set_attributes
Pass in arguments in sets of 3 and we'll set up attributes for you.
The first argument is the attribute name. The second argument is the default value we should use until told otherwise. The third is the printf format we should use to read and write this attribute from/to a string.
$fw->set_attributes(qw(
fname undef %10s
lname undef %-10s
points 0 %04d
);
parse
Parses the string you hand in. Sets each attribute to the value it finds in the string.
$fw->parse(string => ' JayHannah 0003');
string
Dump the object to a string. Walks each attribute in order and outputs each in the format that was specified during set_attributes().
print $fw->string; # ' ChuckNorris 0017'
auto_truncate
Text::FixedWidth can automatically truncate long values for you. Use this method to tell your $fw object which attributes should behave this way.
$fw->auto_truncate("fname", "lname");
(The default behavior if you pass in a value that is too long is to carp out a warning, ignore your set(), and return undef.)
ALTERNATIVES
Other modules that may do similar things: Parse::FixedLength, Text::FixedLength, Data::FixedFormat, AnyData::Format::Fixed
AUTHOR
Jay Hannah, <jay at jays.net>
, http://jays.net
BUGS
Please report any bugs or feature requests to bug-text-fixedwidth at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-FixedWidth. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Text::FixedWidth
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
Source code
COPYRIGHT & LICENSE
Copyright 2008-2009 Jay Hannah, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.