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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.