in reply to Question about module interface style

Generally, it's a mistake to map a C interface directly to a Perl one. With some careful thought, you can usually do much better by moulding the interface to Perl's strengths. An example that springs to mind is the Win32 Registry API: painful and tedious to use from C, with all those API calls, and all that mucking about checking return codes. By contrast, Win32::TieRegistry is a joy to use: to read from the Registry, you just read from a (tied) hash; to write to the Registry, you simply write to the hash.

As described in On Interfaces and APIs, the way to go about designing a new API is to "play test" it. Write some tests for it. Start by imagining the perfect interface without worrying about implementation constraints. Then refine it.

  • Comment on Re: Question about module interface style

Replies are listed 'Best First'.
Re^2: Question about module interface style (XS like C)
by tye (Sage) on Jul 23, 2006 at 17:05 UTC

    Note that I wrote Win32::TieRegistry and I find that the things I most hate about many XS modules are caused by their attempts to provide a Perl-like interface. If you look under the covers of Win32::TieRegistry you'll find Win32API::Registry which provides a very raw transation of the C API for dealing with the Win32 Registry. There are a few minor niceties added to Win32API::Registry's API, but only ones that I was sure didn't take away any of the possible ways to use the C API and that couldn't be more easily implemented in Perl (actually, some of the niceties are implemented in Perl wrappers inside Win32API::Registry).

    Trying to make Perl-like interfaces in XS is usually a grave mistake, IMO. Even fairly minor steps in that direction tend to lead to code that is hard to write, hard to debug, hard to extend, breaks easily in the face of the simplest of things (a Perl upgrade or any type of Perl magic such as tie), is more likely to have ref-counting bugs, etc.

    Make XS interfaces that are very C-like and expose the full power of the underlying API and then provide Perl wrappers that provide a Perl-like interface. Take Tuppence's advice.

    - tye