in reply to Modules vs. Libraries

You question is unclear: Are you asking how to select from multiple modules, or are you asking how to load a module determined at run-time?

Either way, this answer appears in the Perl Cookbook from O'Reilly (co-author gnat is a PerlMonk!)

The basic answer is to use an eval block around your require and use statements. Thus you can have the module names in a string, and you can trap errors if there is a problem (such as the module being for the wrong OS).

You can use the perl variable $^O to learn what system you are on, and the perl function perlfunc:readdir to get the filenames to try to load.

Replies are listed 'Best First'.
RE: Re: Modules vs. Libraries
by Intrepid (Curate) on Jun 28, 2000 at 21:25 UTC
    You can use the perl variable $^O to learn what system you are on

    Unless you are on Windows 95 or 98 .. in which case $^O holds the undefined value. In the Win32 section here at PerlMonks there's some code posted that is a library subroutine to make such things portable to Perl for Win32 running on Win9(5|8). This might not apply to the kind of script the inquirer is running, but as a general principle if a script of widespread usefulness CAN be made to run on Win95, why not do it? I anticipate opposing opinions however.

    Intrepid

    Q: What is the sound of several hundred Perler-dogs chasing cool code?
    A: YAPC - YAPC - YAPC .. ! :)

      Eh? I thought $^O worked on Win32 with at least 5.005_3 and up, if not even earlier. It certainly works with 5.6.0.

      Incidentally, I don't see the code you mentioned, but I assume it uses Config.pm (ie, $Config{archname}).

        Eh? I thought $^O worked on Win32 with at least 5.005_3 and up, if not even earlier. It certainly works with 5.6.0.
        Incidentally, I don't see the code you mentioned, but I assume it uses Config.pm (ie, $Config{archname}).
        if ("Win32" ne  "WinNT")  {
         @Win32 = ("Win95","Win98","Win2000","WinNT");
        }
        

        Sorry, the code snippet I refer to is OS.pl, it has just been posted.


        Intrepid

        Q: What is the sound of several hundred Perler-dogs chasing cool code?
        A: YAPC - YAPC - YAPC .. ! :)