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

Peter Edwards peter at dragonstaff.com
Thu Oct 5 13:31:50 PDT 2006


To generate compile time errors in this kind of situation I normally use
closures, which will catch typos.

sub SOAPOptimisticLock { 'OptimisticLock'; }
sub SOAPId { 'Id'; }

sub get
{
  my $arg = shift || confess 'expected arg';
  my $code = &$arg || confess 'unknown SOAP function: typo in arg
'.$arg.'?';

  $soapobj->"get_${arg}";
}

If there are more than a handful of tokens it's easier to write entries in
the symbol table straight from an array using some madness like this

#!/usr/bin/perl
use strict;
use warnings;

BEGIN
{
  my @key = qw( FOO BAR BLETCH );
   no strict 'refs';
  for my $k (@key)
  {
   *{__PACKAGE__.'::'.$k} = sub { $k; }
  }
  use strict 'refs';
}

sub get
{
  "here's your $_[0]";
}

print get(FOO), "\n";

Regards, Peter
-----Original Message-----
From: Nik Clayton [mailto:nik at ngo.org.uk] 
Sent: 05 October 2006 21:04
To: Nik Clayton
Cc: Peter Edwards; miltonkeynes-pm at pm.org
Subject: Re: Opinions: API wrapping, how close to original API should you
stay?

Nik Clayton wrote:
>     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.

s/compile time error/run time error that immediately points to the cause of 
the problem/

N




More information about the MiltonKeynes-pm mailing list