C-TinyCompiler
view release on metacpan or search on metacpan
lib/C/TinyCompiler/Perl.pm view on Meta::CPAN
package C::TinyCompiler::Perl;
use strict;
use warnings;
use parent 'C::TinyCompiler::package';
# Find the CORE diretory:
my $core_dir;
foreach (@INC) {
if (-d "$_/CORE/") {
$core_dir = "$_/CORE/";
last;
}
}
die "Unable to locate Perl CORE directory!" unless $core_dir;
# Pull in the local includes:
use Config;
my @local_inc = grep {-d} split / /, $Config{locincpth};
# Add i386-linux-gnu, since that's causing trouble on my Ubuntu. I wish there
# were a better way to handle this. :-(
for (@local_inc, '/usr/include') {
push @local_inc, "$_/i386-linux-gnu" if -d "$_/i386-linux-gnu";
}
sub apply {
my (undef, $state) = @_;
# Add Perl's CORE directory to the compiler's list of includes:
$state->add_include_paths($core_dir, @local_inc);
if ($^O =~ /MSWin/) {
$state->code('Head') .= C::TinyCompiler::line_number(__LINE__) . q{
#define __C89_NAMELESS __extension__
#define __MINGW_EXTENSION __extension__
typedef long __int64;
typedef int uid_t;
typedef int gid_t;
};
# Also add Perl's library to the compiler's lib list
$state->add_library_paths("$Config{archlib}/CORE");
$state->add_librarys('perl');
}
# Add function declarations and symbols:
$state->code('Head') .= C::TinyCompiler::line_number(__LINE__) . q{
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#line 1 "whatever comes after C::TinyCompiler::Perl"
};
}
1;
__END__
=head1 NAME
C::TinyCompiler::Perl - Enabling Perl's full C-API in your C::TinyCompiler context
=head1 SYNOPSIS
use C::TinyCompiler;
# Declare the compiler context with the Perl bindings:
my $context= C::TinyCompiler->new('::Perl');
# Or add them afterwards:
my $context = C::TinyCompiler->new;
$context->apply_packages('::Perl');
# Create a function that tells us how many arguments we sent:
$context->code('Body') = q{
void test_func(AV * inputs, AV * outputs) {
printf("You sent %d arguments\n", av_len(inputs));
}
};
# Compile and call:
$context->compile;
$context->call_function('test_func', 1, 2, 3);
=head1 DESCRIPTION
This module provides access to the full Perl C API in your compiler context.
This is a very blunt tool, but it is guaranteed to always reflect the API of
whichever version of Perl you use to run your script. It is equivalent to
including the following in your C<Head> code section:
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
where it determines the path to your Perl's include directory during your
script's compile phase. On my machine, the resulting text of this pull-in is
equivalent to approximately 8,000 or 10,000 lines of I<real> code, depending on
how you define I<real> (as well as many blank lines), which is why I consider it
( run in 1.959 second using v1.01-cache-2.11-cpan-97f6503c9c8 )