Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

First let my appologize if this isn't the right place to ask this question, my first time asking about perl online. ok, I am a VERY novice perl user but an experienced C programmer. I want to use Inline C to create a module(C subs) that I can call from perl scripts. I have written a perl script that works with the C code in the script but I want to move the C code to a module so others can call it from other scripts. When I try to execute the script I get the following message: Undefined subroutine &main::pltc called at ./dtcp line 60. Any help would be appreciated(heaven = a_full_example + procedure;), -parman- (at least thats my goal)
perl script(dtcp): #!/usr/bin/perl -W use MYMODU::mystuff; pltc(); module(MYMODU::mystuff): package MYMODU::mystuff; #note: this module originally generated using 'h2xs -PAXn MYMODU::myst +uff require 5.005_62; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use MYMODU::mystuff ':all'; # If you do not need this, moving things directly into @EXPORT or @EXP +ORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); use base 'Exporter'; our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( pltc ); our $VERSION = '0.02'; use Inline C => 'DATA', VERSION => '0.02', NAME => 'MYMODU::mystuff'; __DATA__ =pod =cut __C__ use Inline C => Config => INC => '-I/perlib/MYMODU/mystuff/blib/arch/auto/MYMODU/mystuff '; use Inline C => Config => LIBS => '-L/perlib/MYMODU/mystuff/blib/arch/auto/MYMODU/mystuff -L/perlib/MYMODU/mystuff -lmytclib '; int pltc(SV* var1, char *name) { sv_setpvn(var1, dumm, strlen(dumm)); exit(1); }

Replies are listed 'Best First'.
Re: creating module for Inline C
by tall_man (Parson) on Jan 29, 2003 at 17:34 UTC
    It's ok to create a module that uses Inline::C for part of its implementation. To the outside world it looks like any other module.

    I've done it like this for example:

    package ABC::DEF::G; ... # Preloaded methods go here. use Inline CPP => Config => INC => '-I/usr/X11R6/include/', LIBS => '-L/usr/X11R6/lib -lX11', CLEAN_AFTER_BUILD => 0; use Inline CPP => <<'END';
    What you may not know is that the extension will be rebuilt for every perl program that uses the module, in the directory where the program is run. If you want real compiled-once-and-installed extensions you have to bite the bullet and use XS.

    Update: Recompilation will happen for the Inline::CPP example I show here, but it is possible to make the extension behave more like a true module by proper use of VERSION, NAME, and Inline::MakeMaker, as documented in Writing Modules with Inline.

    As for exporting a symbol from the extension as you try to do, I doubt that will work. Why not wrap the call in a perl routine and just export that?

    Thanks to Fletch and dbp for corrections.

      If you want real compiled-once-and-installed extensions you have to bite the bullet and use XS.

      Actually this is incorrect. See the section entitled Writing Modules with Inline in the Inline perldoc.

      As for exporting a symbol from the extension as you try to do, I doubt that will work.

      Actually, that will work just fine, check out the source of Audio::Ao for an example.

      To the original poster: Move the config directives out of the __DATA__ section and into the perl portion like so:

      use Inline C => 'DATA', INC => '...', LIBS => '...', VERSION => '0.02', NAME => 'MYMODU::mystuff';

Re: creating module for Inline C
by zentara (Cardinal) on Jan 30, 2003 at 14:41 UTC
    I'm no expert on the matter, other than to say that it is easier to create modules for c-code with Swig Swig

    Inline C is great, but swig will make a nice little .pm and .so file for you.

      I want to thank all of you for your help! I do have it working, well kinda. My next task is to figure out how to pass arrays and structures to C and back....I think its done thru the ST() stuff, but thats for tomorrow. Again, thanks to all who responded(and SO QUICKLY!) - parman -