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

Fellow Monks, I have a script which uses Inline::C to pull in several files. Currently it looks like this.
use warnings; use strict; use File::Spec; use File::Basename; my $inlineCFile; my $inlineCFile2; my $relSourcePath; my $inlineBuildDir; my $libDir; my $incDir; BEGIN { $relSourcePath = File::Spec->rel2abs(dirname($0)); $inlineCFile = File::Spec->catfile($relSourcePath, "foo.c"); $inlineCFile2 = File::Spec->catfile($relSourcePath, "bar.c"); my @dirs = (); push @dirs, $relSourcePath; push @dirs, ".Inline"; $inlineBuildDir = File::Spec->catdir(@dirs); pop @dirs; push @dirs, "lib"; $libDir = File::Spec->catdir(@dirs); pop @dirs; push @dirs, "include"; $incDir = File::Spec->catdir(@dirs); } use Inline ( C => Config => LIBS => "-L$libDir", INC => "-I$incDir", DIRECTORY => $inlineBuildDir, ); use Inline C => $inlineCFile; use Inline C => $inlineCFile2; HelloFoo(); HelloBar();
where, for the sake of this question, foo.c is
void HelloFoo() { printf("hello from foo\n"); }
and bar.c is
void HelloBar() { printf("hello from bar\n"); }
Now, let's say that I want to pull in many more such Inline::C files into my script. Is there a better way than the individual scalar definition for each C file inside the BEGIN block followed by the individual use Inline::C => $myFile?

Replies are listed 'Best First'.
Re: Using multiple Inline::C files
by tilly (Archbishop) on Feb 13, 2014 at 15:51 UTC
    Have an array of files. And then call import in a loop to load those files.
    for my $file (@files) { Inline->import("C", scalar File::Spec->catfile($relSourcePath, $file)); }
      Thank you. That's exactly what I was looking for.