[Tallahassee-pm] Perl, a better way to do this with OOP

Phillip Tyre phillip.tyre at fcul.com
Fri Dec 5 09:54:46 CST 2003


It's not that I'm not a huge fan of the object oriented parts of Perl,
I'm just not that versed in it. That said, I find my self working on a
for fun project that uses a lot of OOP. I'm trying to get up to speed,
and I have something that almost works, but it's not elegant. It's also
not very readable!

 

I started with a program someone else had written to walk through the
object and print out the information.

Abstracting it a bit, it looks like this:

 

            I have a library that someone else wrote that unpacks a
binary file into an object.

            # I get an object containing the file contents with:

            my $object = Module:Method->new_object_from_file(file);

 

            # The names used by the object are not the best in the
world, so we have a line like this:

 

            %RecordParams = (

                                     "lousy_name_1" => "Meaningful name
1",

                                    "lousy_name_2" => "Meaningful name
2",

                                    "lousy_name_3" => "Meaningful name
3",

                                    );

 

            # Then we create a new array with:

            @RecordOrder = qw ( lousy_name_1 lousy_name_2 lousy_name_3
);

            

            T# hen we have a subroutine to print out the info for a
record:

            Sub printRecordDetails {

                        my $record = shift;

                        foreach my $line ( @RecordOrder) {

                                    print $RecordParams{$line} . " =  "
. $record->param($line) . "\n";

                        }

            }

 

            # Use a method of the object to return a (multidimensional?)
array of all the records.

            my @records = @{$object->{"Record"}};

 

            # Then we call the subroutine in a loop for each record

            for my $record  ( @records ) {

                        printRecordDetails( $record );

 

            # end

 

And that's it, now keep in mind this is an example script provided by
someone else to make use of the library. It just doesn't seem like the
best way to do things, but I suppose I don't know enough about OOP to
know what questions to ask to find answers to make it better.

            

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/tallahassee-pm/attachments/20031205/4eb84b56/attachment.htm


More information about the Tallahassee-pm mailing list