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

I'm writing a program which needs to use external module or library files for platform specific functions (like for Linux, Solaris, AIX, etc.). I'm going to organize these files in platfor-specific subdirs, but I don't know to query the directory for all the modules or libraries and then "use" them in my program. Any suggestions or opinions on which will be easier for me and other developers to use? TIA! Scott

Replies are listed 'Best First'.
Re: Modules vs. Libraries
by swiftone (Curate) on Jun 28, 2000 at 19:18 UTC
    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.

      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}).

Re: Modules vs. Libraries
by smccrory (Novice) on Jun 28, 2000 at 19:33 UTC
    Yes, perfect, that's exactly what I was wondering (despite my bad wording :-). Thanks!! - Scott