Text::FixedWidth - Easy OO manipulation of fixed width text files


Text-FixedWidth documentation  | view source Contained in the Text-FixedWidth distribution.

Index


NAME

Top

Text::FixedWidth - Easy OO manipulation of fixed width text files

SYNOPSIS

Top

   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

Top

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

clone

Provides a clone of a Text::FixedWidth object. If available it will attempt to use Clone::Fast or Clone::More falling back on dclone in Storable.

   my $fw_copy = $fw->clone;

This method is most useful when being called from with in the parse method.

   while( my $row = $fw->parse( clone => 1, string => $str ) ) {
      print $row->foobar;
   }

See parse for further information.

ALTERNATIVES

Top

Other modules that may do similar things: Parse::FixedLength, Text::FixedLength, Data::FixedFormat, AnyData::Format::Fixed

AUTHOR

Top

Jay Hannah, <jay at jays.net>, http://jays.net

BUGS

Top

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

Top

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

* Source code

http://github.com/jhannah/text-fixedwidth

COPYRIGHT & LICENSE

Top


Text-FixedWidth documentation  | view source Contained in the Text-FixedWidth distribution.