How can I conditionally import a package in Perl?

前端 未结 4 2064
死守一世寂寞
死守一世寂寞 2021-01-21 09:53

I have a Perl script which uses a not-so-common module, and I want it to be usable without that module being installed, although with limited functionality. Is it possible?

4条回答
  •  不要未来只要你来
    2021-01-21 10:44

    You can use require to load modules at runtime, and eval to trap possible exceptions:

    eval {
        require Foobar;
        Foobar->import();
    };  
    if ($@) {
        warn "Error including Foobar: $@";
    }
    

    See also perldoc use.

提交回复
热议问题