[Omaha.pm] code redux

Jay Hannah jay at jays.net
Tue Sep 12 11:21:14 PDT 2006


On Tue, 12 Sep 2006, Dave M wrote:
> Looks like it to me. It's not like $prt{'blah'} will keep the
> justification properties, right?
>
> Although, I would write things like this:
>
>   $prt{'reversal_flag'} = (" ") x 3;
>   $prt{'reversal_period'} = (" ") x 5;
>
> I hate counting spaces.... :)

I wrote a class a couple years back that we call Model::FixedWidth. It 
makes fixed width files a breeze. Unfortunately there are a lot of fixed 
width formats in our lives so we use the heck out of it. No more counting 
columns of *anything*. :)

The code I posted earlier does everything the hard way, and I can't be 
bothered to port it today... lazy, lazy boy.

j



NAME
        Model::FixedWidth - Easy objects for fixed width text
        files. One object per line.

SYNOPSIS
           my $fw = new Model::FixedWidth, "new()");
           $fw->set_attributes(qw(
              fname            undef  %10s
              lname            undef  %-10s
              points           0      %04d
           );
           $fw->parse(message => "       JayHannah    0003");
           print $fw->get_fname;   # Jay
           print $fw->get_lname;   # Hannah
           print $fw->get_points;  # 0003

           $fw->set_fname('Chuck');
           $fw->set_lname('Norris');
           $fw->set_points(17);
           print $fw->string;      #  '     ChuckNorris    0017'

DESCRIPTION
        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_ meth-
        ods to retrieve the value of whatever attributes you care
        about.

        See Model/Rewards/* for gobs of real-world uses of this
        class.

METHODS
        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 argu-
        ment is the default value we should use until told other-
        wise. The third is the printf format we should use to read
        or write this attribute from/to a string.

          $fw->set_attributes(qw(
            fname            undef  %10s
            lname            undef  %-10s
            points           0      %04d
          );

        parse

        Parse a string. Set each attribute to the value listed in
        the string.

          $fw->parse(message => "       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'



More information about the Omaha-pm mailing list