in reply to require different modules per OS?

use if $^O =~ /win32/i, 'Win32::TieRegistry', Delimiter=>"/", ArrayVal +ues=>0;

(see if)  Or

if ( $^O =~ /win32/i ) { require Win32::TieRegistry; Win32::TieRegistry->import(Delimiter=>"/", ArrayValues=>0); }

Replies are listed 'Best First'.
Re^2: require different modules per OS?
by keszler (Priest) on Nov 18, 2009 at 15:08 UTC
    "use if" - wow, learn something new everyday. ++!
      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).