try to use Module in Perl and print message if module not available

前端 未结 6 762
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 02:38

I wanted to be able to do this in Perl (the code below is Python lol)

try:
  import Module
except:
  print \"You need module Module to run this program.\"
         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 03:01

    There are many modules for doing that; see the list of CPAN modules that (can) load other modules. However, it is a bit risky to rely on an external module (what if it is not present?). Well, at least, if you rely on Moose, Class::Load can be used safely as it is Moose's prerequisite:

    #!/usr/bin/env perl
    use strict;
    use utf8;
    use warnings qw(all);
    
    use Class::Load qw(try_load_class);
    
    try_load_class('Module')
        or die "You need module Module to run this program.";
    

提交回复
热议问题