gpt4 book ai didi

perl - 在部署 Perl 项目之前如何确定 CPAN 依赖项?

转载 作者:行者123 更新时间:2023-12-03 22:48:38 25 4
gpt4 key购买 nike

有没有人对找到定制开发项目中可能出现的所有 CPAN 依赖项的好方法有任何建议。通常情况下,您的本地开发环境很少与您的实际环境相匹配,并且随着您构建越来越多的项目,您倾向于构建一个已安装模块的本地库。然后这些会导致您不一定注意到您的最新项目对非核心模块有要求。由于通常需要将整个项目打包以部署到另一个组(在我们的例子中是我们的运营团队),因此了解包中应包含哪些模块非常重要。

有没有人对这个问题有任何见解。

谢谢

彼得

最佳答案

我自己也遇到过这个问题。 Devel::Modlist (正如 this answer 所建议的)采用动态方法。它报告在脚本的特定运行期间实际加载的模块。这会捕获以任何方式加载的模块,但它可能无法捕获条件需求。也就是说,如果你有这样的代码:

if ($some_condition) { require Some::Module }

$some_condition恰好是假的, Devel::Modlist不会列出 Some::Module作为要求。

我决定使用 Module::ExtractUse反而。它进行静态分析,这意味着它将始终捕获 Some::Module在上面的例子中。另一方面,它对以下代码无能为力:
my $module = "Other::Module";
eval "use $module;";

当然,您可以同时使用这两种方法,然后将这两个列表结合起来。

无论如何,这是我想出的解决方案:
#! /usr/bin/perl
#---------------------------------------------------------------------
# Copyright 2008 Christopher J. Madsen <perl at cjmweb.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
# GNU General Public License or the Artistic License for more details.
#
# Recursively collect dependencies of Perl scripts
#---------------------------------------------------------------------

use strict;
use warnings;
use File::Spec ();
use Module::CoreList ();
use Module::ExtractUse ();

my %need;
my $core = $Module::CoreList::version{'5.008'};

# These modules have lots of dependencies. I don't need to see them now.
my %noRecurse = map { $_ => 1 } qw(
Log::Log4perl
XML::Twig
);

foreach my $file (@ARGV) {
findDeps($file);
}

foreach my $module (sort keys %need) {
print " $module\n";
}

#---------------------------------------------------------------------
sub findDeps
{
my ($file) = @_;

my $p = Module::ExtractUse->new;

$p->extract_use($file);

foreach my $module ($p->array) {
next if exists $core->{$module};
next if $module =~ /^5[._\d]+/; # Ignore "use MIN-PERL-VERSION"
next if $module =~ /\$/; # Run-time specified module

if (++$need{$module} == 1 and not $noRecurse{$module}) {
my $path = findModule($module);
if ($path) { findDeps($path) }
else { warn "WARNING: Can't find $module\n" }
} # end if first use of $module
} # end foreach $module used
} # end findDeps

#---------------------------------------------------------------------
sub findModule
{
my ($module) = @_;

$module =~ s!::|\'!/!g;
$module .= '.pm';

foreach my $dir (@INC) {
my $path = File::Spec->catfile($dir, $module);
return $path if -f $path;
}

return;
} # end findModule

你会这样运行:
perl finddeps.pl scriptToCheck.pl otherScriptToCheck.pl

它打印运行列出的脚本所需的所有非核心模块的列表。 (除非他们在模块加载方面做了一些花哨的技巧来阻止 Module::ExtractUse 看到它们。)

关于perl - 在部署 Perl 项目之前如何确定 CPAN 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/240704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com