| 1 | package Acme::CPANModules::CheckingModuleInstalledLoadable; | ||
| 2 | |||
| 3 | 1 | our $DATE = '2019-01-09'; # DATE | |
| 4 | 1 | our $VERSION = '0.001'; # VERSION | |
| 5 | |||
| 6 | 1 | our $LIST = { | |
| 7 | summary => 'Checking if a module is installed or loadable', | ||
| 8 | description => <<'_', | ||
| 9 | |||
| 10 | If you simply want to check that a module's `.pm` file is locatable in `@INC`, | ||
| 11 | you can just do something like: | ||
| 12 | |||
| 13 | my $mod = "Foo/Bar.pm"; | ||
| 14 | for my $dir (@INC) { | ||
| 15 | next if ref $dir; | ||
| 16 | if (-f "$dir/$mod") { | ||
| 17 | print "Module $mod is installed"; | ||
| 18 | last; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | Or you can use something like <pm:Module::Path> or <pm:Module::Path::More> which | ||
| 23 | does similar to the above. | ||
| 24 | |||
| 25 | A module can also be loaded from a require hook in ~@INC~ (like in the case of | ||
| 26 | fatpacked or datapacked script) and the above methods does not handle it. | ||
| 27 | Instead, you'll need to use <pm:Module::Load::Conditional>'s `check_install` or | ||
| 28 | <pm:Module::Installed::Tiny>'s `module_installed`: | ||
| 29 | |||
| 30 | use Module::Load::Conditional qw(check_install); | ||
| 31 | if (check_install(module => "Foo::Bar")) { | ||
| 32 | # Foo::Bar is installed | ||
| 33 | } | ||
| 34 | |||
| 35 | The above does not guarantee that the module will be loaded successfully. To | ||
| 36 | check that, there's no other way but to actually try to load it: | ||
| 37 | |||
| 38 | if (eval { require Foo::Bar; 1 }) { | ||
| 39 | # Foo::Bar can be loaded (and was loaded!) | ||
| 40 | } | ||
| 41 | |||
| 42 | _ | ||
| 43 | tags => ['task'], | ||
| 44 | entries => [ | ||
| 45 | { | ||
| 46 | module=>'Module::Path', | ||
| 47 | }, | ||
| 48 | { | ||
| 49 | module=>'Module::Path::More', | ||
| 50 | }, | ||
| 51 | { | ||
| 52 | module=>'Module::Load::Conditional', | ||
| 53 | }, | ||
| 54 | { | ||
| 55 | module=>'Module::Installed::Tiny', | ||
| 56 | }, | ||
| 57 | ], | ||
| 58 | }; | ||
| 59 | |||
| 60 | 1 | 1; | |
| 61 | # ABSTRACT: Checking if a module is installed or loadable | ||
| 62 | |||
| 63 | __END__ |