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

前端 未结 10 1037
旧巷少年郎
旧巷少年郎 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:17

    I believe your solution will only look in the root of each directory path contained in the @INC array. You need something recursive, like:

     perl -e 'foreach (@INC) {
        print `find $_ -type f -name "*.pm"`;
     }'
    
    0 讨论(0)
  • 2020-12-07 15:18

    Bravo for @user80168's solution (I'm still counting \'s !) but to avoid all the escaping involved with aliases and shells:

    %~/ cat ~/bin/perlmod
    perl -le'eval qq{require $ARGV[0]; } 
        ? print ( "Found $ARGV[0] Version: ", eval "$ARGV[0]->VERSION" ) 
        : print "Not installed" ' $1
    

    works reasonably well.

    Here might be the simplest and most "modern" approach, using Module::Runtime:

    perl -MModule::Runtime=use_module -E '
         say "$ARGV[0] ", use_module($ARGV[0])->VERSION' DBI
    

    This will give a useful error if the module is not installed.

    Using -MModule::Runtime requires it to be installed (it is not a core module).

    0 讨论(0)
  • 2020-12-07 15:19
    $ perl -MXML::Simple -le 'print $INC{"XML/Simple.pm"}'

    From the perlvar entry on %INC:

    • %INC

    The hash %INC contains entries for each filename included via the do, require, or use operators. The key is the filename you specified (with module names converted to pathnames), and the value is the location of the file found. The require operator uses this hash to determine whether a particular file has already been included.

    If the file was loaded via a hook (e.g. a subroutine reference, see require for a description of these hooks), this hook is by default inserted into %INC in place of a filename. Note, however, that the hook may have set the %INC entry by itself to provide some more specific info.

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

    If you're running ActivePerl under Windows:

    • C:\>ppm query * to get a list of all installed modules

    • C:\>ppm query XML-Simple to check if XML::Simple is installed

    0 讨论(0)
提交回复
热议问题