This is probably the best technique to use.  I would note that your code is
not handling exceptions in the safest way.

You can increase the safety of your code by saying:

eval {
    autoload($module);
    1; #force true value on success
} or do {
    if ($@ =~ /Compilation failed in require/) {
        say "compilation failed";
    }
    elsif ($@ =~ /Can't locate (.*) in \@INC/) {
        say "module $1 not found";
    } else {
        say "unknown error: $@"
};

This isn't 100% safe, but it covers the most common issue.  You can read
more at https://metacpan.org/pod/Try::Tiny#BACKGROUND

On Fri, Nov 17, 2017 at 11:31 AM Simon Reinhardt <si...@keinstein.org>
wrote:

> Hi list,
>
> I need to check the cause of a module loading error.
> Currently I'm parsing the text of the thrown exception (see below). This
> seems to work, but is there a more idiomatic way?
>
> Best,
> Simon
>
> #!/usr/bin/env perl
> use 5.020;
> use warnings;
> use strict;
>
> use Module::Load;
>
> my $module= 'AB::CD';
>
> eval {
>     autoload($module);
> };
>
> if ($@) {
>     if ($@ =~ /Compilation failed in require/) {
>         say "compilation failed";
>     }
>     elsif ($@ =~ /Can't locate .* in \@INC/) {
>         say "module not found";
>     }
> }
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to