简体   繁体   English

如何使用 CPAN.pm 下载其他 Perl 模块?

[英]How do I use CPAN.pm to download other Perl modules?

I'm new to Object-Oriented programming and the perldoc page on CPAN.pm confuses the hell out of me.我是面向对象编程的新手,CPAN.pm 上的 perldoc 页面让我很困惑。 My program needs to download a couple of modules if they don't already exist.如果它们尚不存在,我的程序需要下载几个模块。 Is this basically just:这基本上只是:

CPAN::Shell->install("Module::Name::Here");

or is there more to it?或者还有更多吗? Does that download the package, unarchive it, and install it, or just one or two of those steps?那是下载 package、解压缩并安装它,还是只是其中的一两个步骤? If it's not all three, how do I do the other one (or two)?如果不是全部三个,我该怎么做另一个(或两个)? I would like it to make sure it doesn't try to re-install anything if the package is already there - is this the default behavior of the function or no?如果 package 已经存在,我希望它确保它不会尝试重新安装任何东西 - 这是 function 的默认行为还是否?

And how can I tell if Perl couldn't connect to CPAN to get the package?我如何判断 Perl 是否无法连接到 CPAN 以获取 package?

No one else has mentioned it, but you have to load the CPAN config first: 没有人提到它,但你必须先加载CPAN配置:

use CPAN;

CPAN::HandleConfig->load;
CPAN::Shell::setup_output;
CPAN::Index->reload;

# now do your stuff

You can also look at the cpan(1) script that comes with CPAN.pm to see a lot of the programmer's interface in action. 您还可以查看CPAN.pm附带的cpan(1)脚本,以查看许多程序员的界面。 I also wrote on article for the latest issue of The Perl Review showing examples of the programmer's interface to CPAN.pm. 我还写了最新一期Perl Review的文章,其中展示了CPAN.pm的程序员界面示例。

However, you might not need to do any of this. 但是,您可能不需要执行任何操作。 Why is your program downloading modules on its own? 为什么你的程序自己下载模块? Are you trying to create a distribution that has dependencies? 您是否尝试创建具有依赖关系的分发? There are better ways to handle that so you don't have to repeat the work that's already done in other tools. 有更好的方法来处理它,所以你不必重复已经在其他工具中完成的工作。 For instance, see my article Creating Perl Application Distributions . 例如,请参阅我的文章Creating Perl Application Distributions You treat your program as if it's a module and get the benefit of all the cool module tools so you don't have to reinvent something. 您将程序看作是一个模块,并获得所有酷模块工具的好处,因此您不必重新发明一些东西。

If you tell us more about the problem that you're actually trying to solve, we might have other good answers too. 如果你告诉我们更多你正在尝试解决的问题,我们也可能有其他好的答案。 :) Good luck, :) 祝好运,

the perldoc page on CPAN.pm confuses the hell out of me. CPAN.pm上的perldoc页面让我感到困惑。

Yes, documentation of the CPAN API is still a bit lacking. 是的,CPAN API的文档仍然有点缺乏。 It wasn't every really designed for programmatic use by others. 并非每个人都为其他人的程序化使用而设计。 You might have better luck with CPANPLUS , if that's available to you. 如果您可以使用CPANPLUS ,那么您可能会有更好的运气。

My program needs to download a couple of modules if they don't already exist. 我的程序需要下载几个模块(如果它们尚不存在)。 Is this basically just: CPAN::Shell->install("Module::Name::Here"); 这基本上只是:CPAN :: Shell-> install(“Module :: Name :: Here”);

Yes, that's pretty much it for the simplest possible thing. 是的,这几乎是最简单的事情。 In fact, that's pretty much all the 'cpan' command line program does when you type "cpan Module::Name::Here". 事实上,当你输入“cpan Module :: Name :: Here”时,几乎所有'cpan'命令行程序都会这样做。 However, you will need to have CPAN.pm configured in advance. 但是,您需要事先配置CPAN.pm。

Does that download the package, unarchive it, and install it? 是下载软件包,取消归档并安装它吗?

Yes, all three. 是的,这三个。

I would like it to make sure it doesn't try to re-install anything if the package is already there - is this the default behavior of the function or no? 我希望它确保它不会尝试重新安装任何东西,如果包已经存在 - 这是该函数的默认行为还是没有?

Yes, the default behavior is not to install anything if the module is up to date. 是的,如果模块是最新的,默认行为是不安装任何东西。 You can actually check that yourself with the "uptodate()" method like this: 您可以使用“uptodate()”方法检查自己,如下所示:

my $mod = CPAN::Shell->expand("Module", "Module::Name::Here");
$mod->install unless $mod->uptodate;

And how can I tell if Perl couldn't connect to CPAN to get the package? 如何判断Perl是否无法连接到CPAN以获取包?

That's hard to do programmatically in a way that would be simple to explain. 以编程方式很难以简单的方式解释。 You either need to look at the output or else just check $mod->uptodate afterwards; 你要么需要查看输出,要么之后只需检查$ mod-> uptodate;

my $mod = CPAN::Shell->expand("Module", "Module::Name::Here");
if ( ! $mod->uptodate ) {
    $mod->install;
    die "Problems installing" unless $mod->uptodate;
}

Best of luck! 祝你好运!

Basically using CPAN is the following: 基本上使用CPAN如下:

perl -MCPAN -e shell

if this is the first time you are running it, it will ask you a few questions and save the results in a configuration file. 如果这是您第一次运行它,它会询问您几个问题并将结果保存在配置文件中。

then to install PGP::Sign just type: 然后安装PGP :: Sign只需输入:

install PGP::Sign

and you're set. 而且你已经定下来了。

As for you last question, don't worry, it will say to you whether it can connect or not. 至于你的最后一个问题,不要担心,它会告诉你它是否可以连接。

I tried doing things programmatically with CPAN module in the past (relatively distant past - say 5 years ago) without much success, so I stopped trying. 我过去曾尝试用CPAN模块进行编程(相对较远的过去 - 比如5年前)没有太大成功,所以我不再尝试了。 That means, of course, that things may have changed since then. 当然,这意味着事情可能已经发生了变化。 However, the documentation on CPAN here should help. 但是,这里的CPAN文档应该有所帮助。 The promising looking CPAN::API::HOWTO only has two recipes, neither of them relevant to your problem. 看起来很有前途的CPAN :: API :: HOWTO只有两个配方,它们都与你的问题无关。 You might also investigate whether CPANPLUS is better - I use it interactively far more often than I do CPAN. 您可能还会调查CPANPLUS是否更好 - 我以交互方式使用它的频率远远超过CPAN。 Both are incredible modules though. 两者都是令人难以置信的模块。

So, as @Keltia suggests, I do CPAN (CPANPLUS) interactively. 因此,正如@Keltia建议的那样,我以交互方式进行CPAN(CPANPLUS)。

As you can tell, most of us use only use CPAN.pm in the interactive mode, however, you're on the right track. 正如您所知,我们大多数人只在交互模式下使用CPAN.pm,但是,您处于正确的轨道上。

Things I can point out for the moment: 我现在可以指出的事情:

  • Yes, calling CPAN::Shell->install() will download, compile, test and install a package. 是的,调用CPAN :: Shell-> install()将下载,编译,测试和安装包。 It should also do the same for any dependencies the package has, recursively. 对于包具有的任何依赖项,它也应该以递归方式执行相同的操作。
  • The default behaviour is to not install anything which is already installed (unless a newer version is available). 默认行为是不安装已安装的任何内容(除非有更新的版本可用)。
  • I'm not strictly sure how the error handling works - I'll look into it, and report back. 我不是很确定错误处理是如何工作的 - 我会调查它,并报告回来。
  • It might prompt your user, though. 但它可能会提示您的用户。

Keltia has it right. 凯莉亚说得对。 I'll add that his first instruction is done from the command prompt, usually as root, but not necessarily so. 我将补充说,他的第一条指令是从命令提示符完成的,通常以root身份完成,但不一定如此。 The second command is done from the CPAN prompt. 第二个命令是从CPAN提示符完成的。 You can also do it all on the command line, but I usually don't. 您也可以在命令行上完成所有操作,但我通常不这样做。

If you're using windows, your best bet is to use PPM, but its repositories are annoyingly out of date most times. 如果您正在使用Windows,最好的办法是使用PPM,但其存储库大多数时候都已经过时了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM