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

I am trying to use Inline::C in a perl module. The C code appears to compile fine (at least there is a .so file in the right place) but the module does not return a true value to the calling program and I cannot figure out how to make that work. I've tried putting the usual "1;" at various places in the code but it either chokes when compiling or tells me:

"DLDtest.pm did not return a true value at bin/dldtest.fcgi line 11."

Anyone know how to make this work?

This is some stripped down code I am using to test with:

dldtest.fcgi

#!/usr/bin/perl use strict; use warnings; use File::Basename; use FindBin; use lib dirname($FindBin::Bin) . "/modules"; use DLDtest; my ($dlid,@dlfiles) = dld_initialize("lic_file", "123456", 5, "/path/t +o/data/files"); exit 0;

DLDtest.pm

package DLDtest; use strict; use warnings; use Exporter qw(import); use English; our @EXPORT = qw (dld_initialize); #------------------------------------------------ # initialize dld and open data files #------------------------------------------------ sub dld_initialize { my ($lic_file, $password, $num_files, $dl_format) = @_; my @files = (); # initalize the DLD library my $dlid = dl_DlInit(""); # set the license file and password unless (dl_DlSetLicense($dlid, $lic_file, $password)) { return (0,@files); } # open the DLD files for (my $cntr = 0; $cntr < $num_files; $cntr++) { my $path = sprintf($dl_format, $cntr + 1); my $fileid = dl_DlFileOpen($dlid, $path); $files[$cntr] = $fileid ? $fileid : 0; } return ($dlid,@files); } #------------------------------------------------ # C functions to interface with Pitney library #------------------------------------------------ use Inline (C => Config => DIRECTORY => '/var/www/addrez/Inline', INC => "-I/var/www/addrez/ext.att/include", LIBS => '-L/var/www/addrez/ext.att/lib -ldemolibMT'); use Inline "C"; Inline->init; __DATA__ __C__ #include "dl.h" /*----------------------------------------------- * Initialize the DLD librariy *---------------------------------------------*/ long dl_DlInit (SV* initPath) { return DlInit(SvPV (initPath, PL_na)); } /*----------------------------------------------- * associate a license file and password with dl *---------------------------------------------*/ int dl_DlSetLicense(long dl, char* licenseFile, long password) { return DlSetLicense(dl, licenseFile, password); } /*----------------------------------------------- * open a data file and return FileID *---------------------------------------------*/ long dl_DlFileOpen (long dl, char *path) { return DlFileOpen(dl, path); }

Replies are listed 'Best First'.
Re: Inline::C in a Perl module
by huck (Prior) on Sep 18, 2017 at 19:14 UTC

      Specifically, because __DATA__ ends the perl, the 1; needs to go before the __DATA__:

      ... use Inline "C"; Inline->init; 1; __DATA__ __C__ ...

      update: for example, Inline: "More about the DATA Section". That also gives examples of doing the source code as a heredoc (look for << on the page), where you could then have perl after the Inline source code as well.

        Well, darn. It's easy once you know the answer.

        Thank you

        Opps, my bad, you are so right!

Re: Inline::C in a Perl module
by syphilis (Archbishop) on Sep 18, 2017 at 23:16 UTC
    return DlInit(SvPV (initPath, PL_na));

    (Unrelated to your actual question.)
    PL_na is not much used these days. The perlapi documentation has this to say about it:

    <quote>
    PL_na A convenience variable which is typically used with "SvPV" when one doesn't care about the length of the string.
    It is usually more efficient to either declare a local variable and use that instead or to use the "SvPV_nolen" macro.

    STRLEN PL_na
    </quote>

    Cheers,
    Rob