How can I check if a Perl module is installed on my system from the command line?

前端 未结 10 1036
旧巷少年郎
旧巷少年郎 2020-12-07 14:33

I tried to check if XML::Simple is installed in my system or not.

perl -e \'while (<@INC>) { while (<$_/*.pm>) { print \"$_\\n\"; } }\'


        
相关标签:
10条回答
  • 2020-12-07 15:00

    Quick and dirty:

    $ perl -MXML::Simple -e 1
    
    0 讨论(0)
  • 2020-12-07 15:01

    For example, to check if the DBI module is installed or not, use

    perl -e 'use DBI;'
    

    You will see error if not installed. (from http://www.linuxask.com)

    0 讨论(0)
  • 2020-12-07 15:05

    You can check for a module's installation path by:

    perldoc -l XML::Simple
    

    The problem with your one-liner is that, it is not recursively traversing directories/sub-directories. Hence, you get only pragmatic module names as output.

    0 讨论(0)
  • 2020-12-07 15:06

    while (<@INC>)

    This joins the paths in @INC together in a string, separated by spaces, then calls glob() on the string, which then iterates through the space-separated components (unless there are file-globbing meta-characters.)

    This doesn't work so well if there are paths in @INC containing spaces, \, [], {}, *, ?, or ~, and there seems to be no reason to avoid the safe alternative:

    for (@INC)
    
    0 讨论(0)
  • 2020-12-07 15:07

    What you're doing there is not recursing into directories. It is only listing the modules in the root directory of the @INC directory.

    The module XML::Simple will live in one of the @INC paths under XML/Simple.pm.

    What he said above to find specific modules.

    CPAN explains how to find all modules here, see How to find installed modules.

    0 讨论(0)
  • 2020-12-07 15:15

    If you want to quickly check if a module is installed (at least on Unix systems, with Bash as shell), add this to your .bashrc file:

    alias modver="perl -e\"eval qq{use \\\$ARGV[0];\\\\\\\$v=\\\\\\\$\\\${ARGV[0]}::VERSION;};\ print\\\$@?qq{No module found\\n}:\\\$v?qq{Version \\\$v\\n}:qq{Found.\\n};\"\$1"
    

    Then you can:

    => modver XML::Simple
    No module found
    
    => modver DBI
    Version 1.607
    
    0 讨论(0)
提交回复
热议问题