如何从命令行检查我的系统上是否安装了Perl模块?

时间:2021-08-23 07:15:39

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

我试着检查我的系统中是否安装了XML :: Simple。

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

The above one-liner was used for listing all modules installed in my system. However, it is not listing XML modules.

上面的单行用于列出我系统中安装的所有模块。但是,它没有列出XML模块。

However, the following executes fine.

但是,以下执行正常。

perl -e "use XML::Simple "

What might be the issue?

可能是什么问题?

10 个解决方案

#1


81  

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.

单行的问题在于,它不是递归遍历目录/子目录。因此,您只获得实用的模块名称作为输出。

#2


33  

Quick and dirty:

快而脏:

$ perl -MXML::Simple -e 1

#3


17  

$ perl -MXML::Simple -le 'print $INC{"XML/Simple.pm"}'

From the perlvar entry on %INC:

来自%INC的perlvar条目:

  • %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.

散列%INC包含通过do,require或use运算符包含的每个文件名的条目。键是您指定的文件名(模块名称转换为路径名),值是找到的文件的位置。 require运算符使用此哈希来确定是否已包含特定文件。

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.

如果文件是通过钩子加载的(例如子程序引用,请参阅require以获取这些钩子的描述),默认情况下,此钩子插入%INC而不是文件名。但请注意,挂钩可能已自行设置%INC条目以提供更具体的信息。

#4


10  

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

例如,要检查是否安装了DBI模块,请使用

perl -e 'use DBI;'

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

如果没有安装,您将看到错误。 (来自http://www.linuxask.com)

#5


8  

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

你在那里做的不是递归到目录。它仅列出@INC目录的根目录中的模块。

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

XML :: Simple模块将存在于XML / Simple.pm下的一个@INC路径中。

What he said above to find specific modules.

他上面说的是找到具体的模块。

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

CPAN解释了如何在此查找所有模块,请参阅如何查找已安装的模块。

#6


7  

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:

如果要快速检查是否已安装模块(至少在Unix系统上,将Bash作为shell),请将其添加到.bashrc文件中:

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

#7


3  

while (<@INC>)

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.)

这将@C中的路径连接在一个字符串中,用空格分隔,然后在字符串上调用glob(),然后遍历空格分隔的组件(除非有文件全局元字符。)

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:

如果@INC中的路径包含空格,\,[],{},* ,?或〜,并且似乎没有理由避免安全替代方法,则这不能很好地工作:

for (@INC)

#8


3  

If you're running ActivePerl under Windows:

如果您在Windows下运行ActivePerl:

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

    C:\> ppm query *获取所有已安装模块的列表

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

    C:\> ppm查询XML-检查XML :: Simple是否安装简单

#9


0  

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

我相信您的解决方案只会查看@INC数组中包含的每个目录路径的根目录。你需要一些递归的东西,比如:

 perl -e 'foreach (@INC) {
    print `find $_ -type f -name "*.pm"`;
 }'

#10


0  

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

Bravo for @ user80168的解决方案(我还在计算!)但要避免所有涉及别名和shell的转义:

%~/ 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:

这可能是最简单,最“现代”的方法,使用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).

使用-MModule :: Runtime需要安装它(它不是核心模块)。

#1


81  

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.

单行的问题在于,它不是递归遍历目录/子目录。因此,您只获得实用的模块名称作为输出。

#2


33  

Quick and dirty:

快而脏:

$ perl -MXML::Simple -e 1

#3


17  

$ perl -MXML::Simple -le 'print $INC{"XML/Simple.pm"}'

From the perlvar entry on %INC:

来自%INC的perlvar条目:

  • %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.

散列%INC包含通过do,require或use运算符包含的每个文件名的条目。键是您指定的文件名(模块名称转换为路径名),值是找到的文件的位置。 require运算符使用此哈希来确定是否已包含特定文件。

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.

如果文件是通过钩子加载的(例如子程序引用,请参阅require以获取这些钩子的描述),默认情况下,此钩子插入%INC而不是文件名。但请注意,挂钩可能已自行设置%INC条目以提供更具体的信息。

#4


10  

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

例如,要检查是否安装了DBI模块,请使用

perl -e 'use DBI;'

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

如果没有安装,您将看到错误。 (来自http://www.linuxask.com)

#5


8  

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

你在那里做的不是递归到目录。它仅列出@INC目录的根目录中的模块。

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

XML :: Simple模块将存在于XML / Simple.pm下的一个@INC路径中。

What he said above to find specific modules.

他上面说的是找到具体的模块。

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

CPAN解释了如何在此查找所有模块,请参阅如何查找已安装的模块。

#6


7  

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:

如果要快速检查是否已安装模块(至少在Unix系统上,将Bash作为shell),请将其添加到.bashrc文件中:

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

#7


3  

while (<@INC>)

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.)

这将@C中的路径连接在一个字符串中,用空格分隔,然后在字符串上调用glob(),然后遍历空格分隔的组件(除非有文件全局元字符。)

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:

如果@INC中的路径包含空格,\,[],{},* ,?或〜,并且似乎没有理由避免安全替代方法,则这不能很好地工作:

for (@INC)

#8


3  

If you're running ActivePerl under Windows:

如果您在Windows下运行ActivePerl:

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

    C:\> ppm query *获取所有已安装模块的列表

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

    C:\> ppm查询XML-检查XML :: Simple是否安装简单

#9


0  

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

我相信您的解决方案只会查看@INC数组中包含的每个目录路径的根目录。你需要一些递归的东西,比如:

 perl -e 'foreach (@INC) {
    print `find $_ -type f -name "*.pm"`;
 }'

#10


0  

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

Bravo for @ user80168的解决方案(我还在计算!)但要避免所有涉及别名和shell的转义:

%~/ 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:

这可能是最简单,最“现代”的方法,使用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).

使用-MModule :: Runtime需要安装它(它不是核心模块)。