P5NCI::Declare - declarative syntax for P5NCI bindings


P5NCI documentation Contained in the P5NCI distribution.

Index


Code Index:

NAME

Top

P5NCI::Declare - declarative syntax for P5NCI bindings

SYNOPSIS

Top

  use P5NCI::Declare library => 'shared_library';

  sub perl_function :NCI( c_function => 'vii' );

  perl_function( 101, 77 );

DESCRIPTION

Top

P5NCI::Declare allows you to bind Perl functions to C functions with subroutine attributes.

When you use this module, you must pass: the key library where the value is the name of the shared library you want to load (following the normal P5NCI conventions). You may pass an optional path key, where the value is the path to the library.

To bind a Perl function name to a C function, use a subroutine declaration with the NCI attribute. The attribute takes a pair where the key is the name of the function and the value is its P5NCI signature.

AUTHOR

Top

chromatic, <chromatic at wgz dot org>

BUGS

Top

No known bugs.

COPYRIGHT

Top


P5NCI documentation Contained in the P5NCI distribution.

package P5NCI::Declare;

use strict;
use warnings;

use vars '$VERSION';
$VERSION = '0.31';

use P5NCI::Library;

use Attribute::Handlers autotie => { '__CALLER__::NCI' => __PACKAGE__ };

my %libs;

sub import
{
	my $class = shift;
	$libs{ caller() }  = P5NCI::Library->new( @_ );
}

sub UNIVERSAL::NCI :ATTR
{
	my ($package, $symbol, $referent, $attr, $data) = @_;

	my $lib          = $libs{$package};
	my ($name, $sig) = @$data;
	my $function     = $lib->load_function( $name, $sig );
	*$symbol         = $function;
}

1;
__END__