Take the 2-minute tour ×

I have the following Perl code which relies on Term::ReadKey to get the terminal width; My NetBSD build is missing this module, so I want to default the width of the terminal to 80 when the module is missing.

I can't figure out how to conditionally use a module, knowing ahead of time whether it is available. My current implementation just quits with a message saying it can't find Term::ReadKey if it's absent.

#/usr/pkg/bin/perl -w
# Try loading Term::ReadKey
use Term::ReadKey;
my @p=(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97);
my $plen=$#p+1;
printf("num |".("%".int(($wchar-5)/$plen)."d") x $plen."\n",@p);

I'm using Perl 5.8.7 on NetBSD and 5.8.8 on CygWin Can you help me implement this into my script more effectively?

share|improve this question
In my opinion, either the title is wrong, or all the answers (except perhaps the one using Module::Load::Conditional, if check_install() is used) are wrong. The title asks how to check "if I have a Perl module before using it". All the answers use some variation of "detect errors with eval while requiring/loading/using it". –  Peter V. Mørch May 12 at 13:37

6 Answers 6

up vote 66 down vote accepted

Here's a bare-bones solution that does not require another module:

my $rc = eval
  require Term::ReadKey;

  # Term::ReadKey loaded and imported successfully

Note that all the answers below (I hope they're below this one! :-) that use eval { use SomeModule } are wrong because use statements are evaluated at compile time, regardless of where in the code they appear. So if SomeModule is not available, the script will die immediately upon compiling.

(A string eval of a use statement will also work (eval 'use SomeModule';), but there's no sense parsing and compiling new code at runtime when the require/import pair does the same thing, and is syntax-checked at compile time to boot.)

Finally, note that my use of eval { ... } and $@ here is succinct for the purpose of this example. In real code, you should use something like Try::Tiny, or at least be aware of the issues it addresses.

share|improve this answer
D'oh, I should have thought of that first. +1 –  ephemient Oct 30 '08 at 21:11
Yes that does really does work, and the semi-colon after the eval block is very important. –  dlamblin Oct 30 '08 at 21:23
Avoid relying on $@ as much as possible. F.ex., some modules could set $@ as a side effect while being loaded without actually throwing an exception. The better option is to rely on the fact that eval will return undef when an exception was caught, ie. if ( eval "use Term::ReadKey" ) { ... }. –  Aristotle Pagaltzis Nov 2 '08 at 15:31
If the module (or anything else) executes an eval after it generates an exception (such as, in a SIGDIE handler or in a DESTROY method), the value of $@ is replaced with the result of the latest eval. –  daotoad Apr 29 '09 at 17:17
A tiny difference between use Term::ReadKey; and eval { require Term::ReadKey }; in the answer is that now Term::ReadKey won't be available in BEGIN blocks. But that isn't a problem in the OP's example. –  Peter V. Mørch Oct 27 '11 at 9:39

Check out the CPAN module Module::Load::Conditional. It will do what you want.

share|improve this answer
Of course, that only works if you have that one installed, too. Probably a better solution if you do. –  tvanfosson Oct 30 '08 at 20:57
Yeah... I can't guarantee I have that one. And while Detect::Module is, it doesn't list all the modules in it's $installed->modules() returned list of module names. –  dlamblin Oct 30 '08 at 21:03

And if you require a specific version of the module:

    eval {
        require Term::ReadKey;
        $GOT_READKEY = 1 if $Term::ReadKey::VERSION >= 2.30;

# elsewhere in the code
    # ...
share|improve this answer

The classic answer (dating back to Perl 4, at least, long before there was a 'use') was to 'require()' a module. This is executed as the script is run, rather than when compiled, and you can test for success or failure and react appropriately.

share|improve this answer
if  (eval {require Term::ReadKey;1;} ne 1) {
# if module can't load
} else {


if  (eval {require Term::ReadKey;1;}) {
#module loaded

Note: the 1; only executes if require Term::... loaded properly.

share|improve this answer

I think it doesn't work when using variables. Please check this link which explains how it can be used with variable

$class = 'Foo::Bar';
        require $class;       # $class is not a bareword


 eval "require $class";
share|improve this answer

Your Answer


