The short answer is to use eval.

Before I get into it, you may want to precompile things before you package
them. But, on a development system, you could always check the platform at
runtime, then eval the appropriate wrapper module for your Inline::C code
and use it as usual. You could always use this sort of an approach to create
a factory or platform neutral proxy that returns platform specific modules
that conform to the same interface but are implemented entirely differently.
I don't really know what you're trying to solve, so its just a thought.

The down side, of course, is that you will need to implement classes on a
per-OS basis. If I had a suggestion as to how to make the actual use
statement be dynamic, I'd suggest it. But, as you correctly point out, it's
a compile time thing. So, the best I can do is to suggest a way to lazy load
a platform specific module at run time. Since you'll have access to @INC,
etc, you shouldn't have too much difficulty with it. If you push the
platform specific functions down far enough, you shouldn't really need to
implement more than you need on an OS by OS basis and just load the OS
specific modules on demand, behind the scenes, at run time.

For example, this script lazy loads an module (Foo) that uses Inline::C. Its
just a simple mock up to illustrate my point:

use strict;
use warnings;

eval {
  # Do whatever you need to do to @INC or whatever here.
  # Load the module.
  require Foo;
};
if ($@) {
  die "Error loading Foo : $@";
}
Foo->say_hi;


### And the package somewhere in @INC that it calls into.
package Foo;

use strict;
use Inline 'C';
Inline->init;

sub say_hi {
   # Simple wrapper, perhaps do more setup here before calling into C.
   Foo::hi();
}
1;
__DATA__
__C__

void hi(){
  printf("Hello, worldd\n");
}


On Tue, Jan 27, 2009 at 4:09 PM, John R. Hogerhuis <jho...@pobox.com> wrote:

> I need parts of
>
> use Inline =>
>
> to be conditional on platform ($^O)
>
> i.e., different LIB and INC name value pairs depending on windows vs linux.
>
> But use happens at compile time.
>
> How do I do this?
>
> thx
>
> -- John.
>

Reply via email to