G'day learnedbyerror,

You can conditionally load modules using the if pragma.

Command line options will be processed at runtime, which will be too late to affect compile-time loading of modules. However, you can modify the command line in a different way, by setting an environment variable, which will be available at compile time.

Here's a minimal example (pm_1155832_cond_use_mod.pl) using List::Util:

#!/usr/bin/env perl -l use strict; use warnings; # Compile time use if $ENV{PM_1155832_USE}, 'List::Util' => qw{max}; BEGIN { print 'Check for List::Util::max() at compile time'; eval { max(1, 2) }; print $@ if $@; } # Runtime if (! $ENV{PM_1155832_USE}) { require List::Util; List::Util->import(qw{max}); } print 'Max is ', max(1, 2);

Load module at runtime:

$ pm_1155832_cond_use_mod.pl Check for List::Util::max() at compile time Undefined subroutine &main::max called at ./pm_1155832_cond_use_mod.pl + line 11. Max is 2

Load module at compile time:

$ PM_1155832_USE=1 pm_1155832_cond_use_mod.pl Check for List::Util::max() at compile time Max is 2

— Ken


In reply to Re: Conditional loading of module with global exports by kcott
in thread Conditional loading of module with global exports by learnedbyerror

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.