vlad_tepesch
vlad_tepesch

Reputation: 6891

perl optionally load module only if available

I have some rarely used code in my perl script that requires a module that is not installed by default (but it is a standard cpan deployed module).

Is there a way to optionally load a module if available and just disable some functionality if not available to make the script run on most default perl distributions if not this special functionality is needed?

Upvotes: 3

Views: 318

Answers (1)

Georg Mavridis
Georg Mavridis

Reputation: 2331

you can use a try-catch block (called "eval" in Perl) while requiring this module.

e.g:

my $zlib_available=0;
eval
{
    require Compress::Zlib;
    $zlib_available=1;
};

and later on:

if ($zlib_available) {
    $page = Compress::Zlib::memGzip($page);
}

Upvotes: 6

Related Questions