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

Many hours have been spent googling and trying different ideas to no avail. The issue is I have some code that when run on a Win32 system needs to use Inline::C to tap into some C routines. This same code on UNIX should NOT use Inline::C.

The code ln question looks like this:

if ($^O eq 'MSWin32') { eval {
use Inline C => 'DATA',
NAME => 'inline_code_pl_2d59';
}}


which works fine for Win32 but UNIX wants it to look like:
if ($^O eq 'MSWin32') {
eval qq{
use Inline C => 'DATA',
NAME => 'inline_code_pl_2d59';
}
}

(note the 'qq')
However, that does not work on Win32 systems and the Inline::C code is not properly used.

We have been unable to come up with a solution such that the code is properly used on a Win32 sytem while not having issues on UNIX. It would be so wonderful if someone might have any ideas/solutions so we don't need to keep hacking the code after changes between the PC and UNIX versions.
  • Comment on Inline::C Needed for Win32, Want it Off For UNIX

Replies are listed 'Best First'.
Re: Inline::C Needed for Win32, Want it Off For UNIX
by diotalevi (Canon) on Oct 28, 2005 at 19:42 UTC

    You already have a good answer but I thought I'd add on and tell you why your Win32 syntax didn't work. Its broken as-is. The use Inline C statement is executed during BEGIN-time regardless of whether the if() condition is true or not. That is, use() does a require() and import() during the compilation time and isn't affected at all about what happens later when the if() executes.

    Actual results

    use Inline C => DATA => NAME => 'inline_code_pl_2d59'; if ( $^O eq 'MSWin32' ) { # Do nothing }

    What you could have written that would have worked on both unix and Win32.

    BEGIN { if ( $^O eq 'MSWin32' ) { # Option A require Inline; Inline->import( C => DATA => NAME => 'inline_code_pl_2d59' ); # Option B eval "use Inline C => DATA => NAME => 'inline_code_pl_2d59'"; die $@ if $@; # Option B with line numbers that make sense if die() happens eval "#line " . ( 1 + __LINE __ ) . " \"" . __FILE __ . "\"\n" . "use Inline C => DATA => NAME => 'inline_code_pl_2d59' +"; die $@ if $@; } }
      Thanks so much for the help to both of you.

      In direct correlation to diotalevi's post, the code now reads

      BEGIN {
      if ($^O eq 'MSWin32') {
      require Inline;
      Inline->import(C => 'DATA', NAME => 'inline_code_pl_2d59');
      }
      }

      And seems to be working exactly as needed.

      Thank you

        I hope you have some indentation in the code. Its standard to indent things in blocks.

Re: Inline::C Needed for Win32, Want it Off For UNIX
by dragonchild (Archbishop) on Oct 28, 2005 at 18:36 UTC
    Look at how File::Spec or Module::Build does things. They have a module for each OS that has a difference, allowing mutually-exclusive syntaxes to peacefully coexist.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?