in reply to Re: require different modules per OS?
in thread require different modules per OS?

"use if" - wow, learn something new everyday. ++!
  • Comment on Re^2: require different modules per OS?

Replies are listed 'Best First'.
Re^3: require different modules per OS?
by Anonymous Monk on Nov 18, 2009 at 15:11 UTC
    adding an eval it is still looking for IO::Interface::Simple on Win32...
    eval { # check and load module at runtime and not compile time if ( $os !~ /darwin/i ) { use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>0 ); use Win32::Process; use Net::Address::Ethernet qw( get_address ); use Wx::ActiveX::IE qw(:iexplorer); } else { use IO::Interface::Simple; } };
    Am I missing something?

      You need string-eval, not block-eval.

      eval 'use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>0 );' if $ +^O =~ /win32/i;

      Or

      BEGIN { eval 'use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>0 +);' if $^O =~ /win32/i; }

      if you want it to take effect early (before the rest of the code is compiled), like with a normal use.

      Also note that the if $^O =~ ... test needs to be outside of the eval.  Reason is that use implies a BEGIN {} block, which is being run as soon as it is defined (when the eval'ed code is being compiled), i.e. before an if within the eval would be tested  (this is the same reason your original attempt failed, btw).

        thanks, I missed that detail :)