[Melbourne-pm] Q re perlxs

Sisyphus sisyphus1 at optusnet.com.au
Mon Aug 22 00:19:30 PDT 2005


----- Original Message ----- 
From: "Brendon Oliver" <brendon.oliver at gmail.com>
To: <melbourne-pm at pm.org>
Sent: Monday, August 22, 2005 4:01 PM
Subject: [Melbourne-pm] Q re perlxs


> Hey all,
>
> I'm writing a perl wrapper for some functions in a C library we have
> here at work. I have a situation where I want to return 0 if a
> function call fails and set $! accordingly. Unfortunately, the
> perlxstut man page for "Example 8 (setting $!)" is "coming soon". :-(
>
> Anybody have an example of how you do this? I've not been able to
> figure it out from the perlxs man page and so far not much joy from
> google either (maybe it's just a bad choice of search criteria), but
> I'm still looking.
>

Seems you can just assign the appropriate value to errno. (Check 'errno.h'
to see what the allowable values are and what they mean.) Here's an
Inline::C demo, which prints a different message for $!, depending upon the
random value that gets assigned to errno.

use warnings;

use Inline C => Config =>
    BUILD_NOISY => 1;

use Inline C => <<'EOC';

#include <errno.h>

void foo(SV * a) {
     errno = (int)SvIV(a);
     }


EOC

# Assign a random number in the range 1..42
$x = 1 + int(rand(42));

foo($x);

print $x, "\n", $!, "\n";

__END__

Cheers,
Rob



More information about the Melbourne-pm mailing list