in reply to error handling by pm initialization

Hello homeveg

Usually the check condition if a module is present is done in BEGIN block using eval to test a require instead of use because the latter is done at compiletime.

Something like the following can be used:

my $gzip; BEGIN{ $gzip = eval { require IO::Compress::Gzip; IO::Compress::Gzip->import(); 1; #UPDATE as per Eily's comment: this +is needed }; print "IO::Compress::Gzip found!" if $gzip; }

Notice also that on CPAN there is Module::Load::Conditional that can be usefull... but must be installed!

Also interesting is perlancar article

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: error handling by pm initialization
by homeveg (Acolyte) on Dec 13, 2016 at 13:35 UTC
    Thanks for your solution and links. I've checked as well CPAN Module::Load::Conditional - looks promising. Will definitely help distributing Perl scripts to others.