On Mon, 10 Sep 2007 15:00:30 -0400 Ronald J Kimball <[EMAIL PROTECTED]> wrote: 

RJK> On Mon, Sep 10, 2007 at 11:51:56AM -0700, Palit, Nilanjan wrote:
>> I have to do some format conversions, so I'm defining subroutines like
>> "sub FormatConv_X2Y()"
...

RJK> Other options include:

RJK> Creating a converter object, and doing
RJK> C<$converter->can('FormatConv_X2Y')>.

An even better option for the converter is to rethink the procedural
approach; store data in a neutral format and extract what you need.  You
can use an object for this, and your code would look like this:

# hint that the input data is in format X
# (but the code could try to guess!)
my $item = new Formatter($x_data, 'X');

# did we get valid input?
if (... check valid input here, maybe with (defined $item) or something
        more elaborate ...)
{
 # request output in format Y
 my $y_data = $item->as_Y();
}
else
{
 ... error handling ...
}

You can always pass data structures around if you don't want to use
objects (and Perl objects are basically data structures), but the idea
is the same: instead of defining individual X->Y mappings, try to
abstract X->generic and generic->Y as appropriate to your problem.  Of
course you can use any data structure as your generic data format, but
it's simplest to use strings if possible.

This makes it easy to add new input and output formats, especially if
they are related to the ones already known.

Ted
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to