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

hi, i have a generic script that needs to run on on both unix n NT, is it possible to load modules based on the os on which it is running ...i mean loading a perl module based on the OS on which the script is running....i cannot use Autoload becoz it does not come with core perl installation and i cann't add new modules to existing perl environment. looking forward to your replies. thnkx
  • Comment on loading a perl module based on the os on which script is running

Replies are listed 'Best First'.
Re: loading a perl module based on the os on which script is running
by valdez (Monsignor) on Nov 11, 2002 at 11:55 UTC

    If you look inside File::Spec (shipped with Perl), you will find this code:

    package File::Spec; use strict; use vars qw(@ISA $VERSION); $VERSION = 0.82 ; my %module = (MacOS => 'Mac', MSWin32 => 'Win32', os2 => 'OS2', VMS => 'VMS'); my $module = $module{$^O} || 'Unix'; require "File/Spec/$module.pm"; @ISA = ("File::Spec::$module"); 1; __END__

    Is this what you need?

    Ciao, Valerio

Re: loading a perl module based on the os on which script is running
by broquaint (Abbot) on Nov 11, 2002 at 13:21 UTC
    Just check what OS you're running through the $^O variable (see. perlvar) e.g
    BEGIN { require autouse; autouse->import( $^O =~ /^win32/i ? "Module::WinNT" : "Module::Unix" ); }
    Also see the recent using 'use' conditionally.
    HTH

    _________
    broquaint