Text::FixedWidth - Easy OO manipulation of fixed width text files.
Version 0.03
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.
Constructor. Does nothing fancy.
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 );
Parses the string you hand in. Sets each attribute to the value it finds in the string.
$fw->parse(string => ' JayHannah 0003');
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'
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.)
Other modules that may do similar things: Parse::FixedLength, Text::FixedLength, Data::FixedFormat, AnyData::Format::Fixed
Jay Hannah, <jay at jays.net>
<jay at jays.net>
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.
bug-text-fixedwidth at rt.cpan.org
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
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-FixedWidth
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Text-FixedWidth
CPAN Ratings
http://cpanratings.perl.org/d/Text-FixedWidth
Search CPAN
http://search.cpan.org/dist/Text-FixedWidth
Copyright 2008 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.
To install Text::FixedWidth, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::FixedWidth
CPAN shell
perl -MCPAN -e shell install Text::FixedWidth
For more information on module installation, please visit the detailed CPAN module installation guide.