On Tue, Apr 19, 2005 at 01:51:45PM +0530 Sriram Mokkapati wrote:
> Hi to all,
>  I've a problem in my embedding Perl program.I've a macro in C. I would like 
> to access it from Perl.How i can do that?
>  ex:
> In C:
> #define TEST_MACRO 50
>  how it can be accessible in perl like 
> $var= TEST_MACRO;

Several ways are possible. One is to create a Perl header using h2ph. 
More robust is probably to create a Makefile thusly:

    use ExtUtils::MakeMaker;
    use ExtUtils::Constant;
    
    WriteMakefile(
        NAME            => 'Constants',
        VERSION_FROM    => 'Constants.pm', 
    );
    
    my @names = qw/TEST_MACRO1 TEST_MACRO2/;
    
    ExtUtils::Constant::WriteConstants(
                                     NAME         => 'Constants',
                                     NAMES        => [EMAIL PROTECTED],
                                     DEFAULT_TYPE => 'IV',
                                     C_FILE       => 'const-c.inc',
                                     XS_FILE      => 'const-xs.inc',
                                  );

and a minimal Constants.xs:

    #include "EXTERN.h"
    #include "perl.h"
    #include "XSUB.h"

    #include "ppport.h"

    #include "const-c.inc"

    #include <the_header_with_macros.h>

    MODULE = Constants          PACKAGE = Constants

    INCLUDE: const-xs.inc

and Constants.pm:

    package Constants;
    use base qw/Exporter/;
    our $VERSION = '0.01';
    our @EXPORT = qw/TEST_MACRO1 TEST_MACRO2/;
    1;

More convenient might be to just run 

    h2xs -n Constants the_header_with_macros.h

Tassilo
-- 
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);

Reply via email to