Calling API Functions

All LayGO API function names and argument sequences are identical to those documented in the C language API. For instance, the code to open a CID which might be written in C as:

#include <laygo.h>

LCid cid;

cid = lgo_Open("PHYS0");

if (cid < 0)
{
    PostOpenError(cid);
}
else
{
    ConnectCid(cid);
}

translates directly into the following Perl code:

use strict;
use Laygo;

my $cid;

$cid = lgo_Open('PHYS0');

if ($cid < 0)
{
    &PostOpenError($cid);
}
else
{
    &ConnectCid($cid);
}

For functions which take C pointer types such as LDataBuffer or LCtlBuffer, substitute a buffer allocated with the LayGO memory allocation API. The C code to read data:

unsigned char buffer[1024];
LResult       result;

result = lgo_Read(cid, buffer, 1024);

if (result > 0)
{
    ProcessData(buffer, result);
}
else if (result == lgo_ERROR_EVENT_WAITING)
{
    ReadEvent();
}

becomes the Perl code:

my $buffer = lgo_BufferNew(1024);
my $result;

$result = lgo_Read($cid, $buffer, 1024);

if ($result > 0)
{
    &ProcessData($buffer, $result);
}
elsif ($result == Laygo::ERROR_EVENT_WAITING)
{
    &ReadEvent();
}

Where a NULL pointer would be used in C, substitute the manifest constant $Laygo::NULL:

LResult result;

result = lgo_ConnectRequest(cid, NULL, 0);

becomes in Perl:

my $result;

$result = lgo_ConnectRequest($cid, $Laygo::NULL, 0);

Finally, where a pointer to an integral type would be used in C, substitute a Perl reference:

unsigned char buffer[128];
LBufferSize    size;
LResult        result;

size   = sizeof(buffer);
result = lgo_Event(cid, buffer, &size);

becomes in Perl:

my $buffer = lgo_BufferNew(128);
my $size   = 128;
my $result;

$result = lgo_Event($cid, $buffer, \$size);