in reply to Using a paramter value to call a module

I have faced this issue enough times to construct the following function:

sub require_if_present { # So here is a trick for discovering if the module is # available (and loading it if it is). This function returns # True if the module is there and this generally sets a # flag to tell the rest of the program how to behave my($module_name,$loaded) = @_; my($module_found); $module_found = 0; eval(<<"LoadModule"); # Use require so we don't load the module if it is not there require $module_name; \$module_found = 1; LoadModule return($module_found); }

So now I can load different modules depending on run time features such as the OS being used or the value of a command line option.

# Load different modules depending on OS if($operating_system =~ /^MSWin/i) { if(!&require_if_present("Win32")) { $no_win32 = 1; } } # The $no_tk etc vars are set by command line # options before I get here if(!$no_tk) { if(&require_if_present("Tk")) { &require_if_present("Tk::DialogBox"); if($no_fileselect || !&require_if_present("Tk::FileSelect")) { $no_fileselect = 1; } if($no_progress || !&require_if_present("Tk::ProgressBar")) { $no_progress = 1; } if($no_dirtree || !&require_if_present("Tk::DirTree")) { $no_dirtree = 1; } } else { $no_tk = 1; } } # Then later I can... if($no_tk) { # Run without an interface ... } else { $main_win = new Tk::MainWindow( -title => "My App", ); ... }

Replies are listed 'Best First'.
Re: Re: Using a paramter value to call a module
by pike (Monk) on Feb 07, 2003 at 11:13 UTC
    Interesting solution, but could you please explain the eval statement in the require_if_present sub? It looks as if you are using a here document, but would a try-catch-block eval not be more appropriate here? E.g.

    sub require_if_present { my $mod = shift; eval { require $mod; return 1; } return 0 if $@; }

    pike

      require() has different meanings depending on the argument. See perlfunc. It even has an example of what you're trying to do, and why it's helps to use eval EXPR.

      <nit>eval requires a semi-colon after it.</nit>

      ihb

      Interesting solution, but could you please explain the eval statement in the require_if_present sub? It looks as if you are using a here document, but would a try-catch-block eval not be more appropriate here?

      TMTOWTDI: That just happens to be the way I implemented it. A here document is convenient for some of the other things I do this way, but that is not a good reason to use it here. Using an eval as you suggest sounds like a cleaner approach. But against that this works (I have used it many times in the last year) and I try not to fix things that work.

      Update: Thinking about this last night I realised why I used a here document to do an eval, its because this was a very common construct in Perl 4 (now that is a really bad reason for doing it here).