Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

A Question of Style.

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


pmarquess at bfsec

Sep 26, 1995, 2:04 PM

Post #1 of 4 (111 views)
Permalink
A Question of Style.

I'm writing an interface to a library that has a number functions which
are variations on the pseudo-prototype below.

status = doit(char * in_buffer, char * out_buffer)

You know the sort of thing, the function processes in_buffer, writes to
out_buffer and returns a status.

There are a few possibilities for a Perl interface to this type of
function.

Here are a few which spring to mind.

1. $status = doit($in_buffer, $out_buffer) ;

2. ($out_buffer, $status) = doit($in_buffer) ;

3. $out_buffer = doit($in_buffer) ;
status accessed via a global scalar

All three have their advantages and disadvantages. All three are used
in Perl.

Does anyone have any strong options of the pros and cons of each?


Paul


Marc.Paquette at Softimage

Sep 26, 1995, 6:27 AM

Post #2 of 4 (107 views)
Permalink
A Question of Style. [In reply to]

>>>>> "Paul" == Paul Marquess <pmarquess [at] bfsec> writes:

PM> I'm writing an interface to a library that has a number
PM> functions which are variations on the pseudo-prototype
PM> below.

PM> status = doit(char * in_buffer, char * out_buffer)

PM> You know the sort of thing, the function processes
PM> in_buffer, writes to out_buffer and returns a status.

PM> There are a few possibilities for a Perl interface to this
PM> type of function.

PM> Here are a few which spring to mind.

PM> 1. $status = doit($in_buffer, $out_buffer) ;

PM> 2. ($out_buffer, $status) = doit($in_buffer) ;

PM> 3. $out_buffer = doit($in_buffer) ; status accessed via a
PM> global scalar

PM> All three have their advantages and disadvantages. All three
PM> are used in Perl.

PM> Does anyone have any strong options of the pros and cons of
PM> each?

I tend to prefer #1 when the C library you are interfacing to has a
well published API and you are providing an alternative "language
binding" to it. You are then able to reuse a large part of the
documentation.

If this is not an issue, I like the #4 put forth by Paul Hudson:

PH> I like ($status, $outbuffer) = ....

PH> It makes it clear the $outbuffer is a result, not an input,
PH> and makes it slightly more difficult to ignore status than
PH> your original version (since you have to put something to get
PH> the status value...)

Marc Paquette | Marc.Paquette [at] Softimage
Administrateur de Systemes / Sysadmin | Softimage Inc
tel: (514) 845-1636 ext. 3426 | 3510 Boulevard St-Laurent
fax: (514) 845-5676 | Montreal (Quebec) H2X 2V2


lwall at scalpel

Sep 26, 1995, 10:47 AM

Post #3 of 4 (105 views)
Permalink
Re: A Question of Style. [In reply to]

: > From: Paul Hudson <paulh [at] harlequin>
: >
: >
: > PM> 2. ($out_buffer, $status) = doit($in_buffer) ;
: >
: > I like ($status, $outbuffer) = ....
: >
: > It makes it clear the $outbuffer is a result, not an input, and makes
: > it slightly more difficult to ignore status than your original version
: > (since you have to put something to get the status value...)
:
: I've actually implemented my interface to be
:
: $out_buffer = doit($in_buffer)
:
: in a scalar context, and
:
: ($out_buffer, $status) = doit($in_buffer)
:
: in an array context.

Quibble: I'm calling it a list context nowadays.

: That is workable in my particular case because $out_buffer will only be
: undef/empty if the status is an error.

Yes, you get the most natural looking code if the data itself can carry
the success/failure signal. That's been a part of Perl's design from
the beginning. There are various ways to get the status information
on failure, but I don't believe in punishing the innocent with the guilty.

That being said, conformance to a published API is often the weightier
consideration, as Marc pointed out. But sometimes you can change things
a little, as long as you give a regular transformational rule, such as
"Always omit the buffer length." However, moving things to the other
side of the = is a bit of a stretch, unless the API is very regular
to begin with.

Larry


pmarquess at bfsec

Sep 26, 1995, 2:24 PM

Post #4 of 4 (106 views)
Permalink
Re: A Question of Style. [In reply to]

> From: Paul Hudson <paulh [at] harlequin>
>
>
> PM> 2. ($out_buffer, $status) = doit($in_buffer) ;
>
> I like ($status, $outbuffer) = ....
>
> It makes it clear the $outbuffer is a result, not an input, and makes
> it slightly more difficult to ignore status than your original version
> (since you have to put something to get the status value...)

I've actually implemented my interface to be

$out_buffer = doit($in_buffer)

in a scalar context, and

($out_buffer, $status) = doit($in_buffer)

in an array context.

That is workable in my particular case because $out_buffer will only be
undef/empty if the status is an error.

Paul

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.