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

前端 未结 6 796
伪装坚强ぢ
伪装坚强ぢ 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:14

    Here's how I'm going about it:

    sub do_optional_thing {
        init_special_support();
        Module::Special::wow();
    }
    
    sub init_special_support {
        # check whether module is already loaded
        return if defined $INC{'Module/Special'};
    
        eval {
            require Module::Special;
            Module::Special->import();
        };
    
        croak "Special feature not supported: Module::Special not available" if $@;
    }
    

提交回复
热议问题