Opinions: API wrapping, how close to original API should you stay?

Nik Clayton nik at ngo.org.uk
Thu Oct 5 01:23:01 PDT 2006

Peter Edwards wrote:
> Or you could use XML::Simple to map to a perl structure and auto-generate
> some accessor methods from a list of keywords if the XML structure is simple
> enough.

That's (sort of) what I'm doing -- my library is making SOAP requests, and 
SOAP::SOM converts the XML in the SOAP response to a hash ref for me.

But I don't want to return this hash ref from my library, because it leads 
to run time bugs like this:

    my $lock = $user->{OptimisticLck};   # <-- typo, missing 'o'

So my code is converting the hash ref in to a very simple Object::InsideOut 
object with accessors (no setters) for each hash key[1].  Since the hash 
keys are used exactly once (when the object is created) there's only one 
place where I need to verify that the spelling is correct.  This will lead 
to user code like this:

    my $lock = $user->get_optimistic_lck();

which will result in a friendly(ish) compile time error, rather than an 
irritating, hard to track down run-time error.


[1] As an aside, there's quite a lot of tedious make-work code involved in 
doing this.  Before I sit down and write one, is anyone aware of a simple 
mechanism to take a hash and a mapping of hash keys to method names, and 
generate a simple object from the two?

More information about the MiltonKeynes-pm mailing list