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", ); ... }
In reply to Re: Using a paramter value to call a module
by hawtin
in thread Using a paramter value to call a module
by Hagbone
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |