原文:
14. How do I install new scripts | modules | extensions?
(Thanks to Ilya Zakharevich mailto:ilya@math.ohio-state.edu for pointing out that perl code comes in a variety of flavors and some code requires more work than others to install. Hence I have expanded this topic and will refer to three distinct categories here: Scripts Modules and Extensions:) Scripts
A "self-contained" script needs little modification (in principle!) to run. It is a good idea to check the #! line at the very top of the file to reflect your local perl setup (e.g.#!/usr/bin/perl -w (change to) #!/usr/gnu/local/perl -w or what have you). There are allegedly "more portable" ways to invoke the perl interpretor as well - they are more fully documented in the perl FAQ and the perlrun(1) man page, however.
Other things you do not want to forget when trying to run a perl script include giving yourself permission to do so, e.g.: chmod u+x newscriptname
You also want to be sure your DISPLAY environment variable is set up properly when attempting to run a perl/Tk script. You may also need to look at the xhost(1) or the xauth(1) man pages for setting up your X-display properly.
If you are still experiencing difficulty check to be sure that extraneous /newsgroup|e-mail|HTML headers|footers|markup//; are not in the file and that you have on hand all that is requireed or useed by the script (if not you may need to install a module - or even a perl4 style lib.pl file). Modules
Check out the module - make sure it is OK and will run on your system - does it require a specific location? For testing purposes (always a good idea) or if you do not have root priveleges set the file in a directory that you do have write access to and try to include it in a test script. Assuming you have a module to test called "Foo.pm" and are simply running the test script in the same directory as the module begin by adding to the @INC array like so: #!/usr/bin/perl -w BEGIN { @INC = ("$ENV{'PWD'}",@INC); } use Tk; use Foo;
or #!/usr/bin/perl -w use lib $ENV{PWD}; use Tk; use Foo;
Another approach is to set either the PERLLIB or PERL5LIB environment variable from your shell. This method allows invoking the test script from within a number of different directories without having to edit a hard coded use lib or push(@INC,".") kind of statement within the script. Yet another way to do it is with the -I switch on the command line like so: perl -Ipath/to/Foo -e fooscriptname
After a successful test; if you are a system administrator, or have root priveleges, or are modifying your own copy of perl; then copy it to the perl5/Tk directory. Depending on how the module was written it should be possible to use it either with the use Tk; statement itself or with an explicit use Tk::Foo; (for module perl5/Tk/Foo.pm). Extensions (Overgrown Modules)
These may come as a multi-file kit (tape archive usually) and may require a C compiler for part of the installation (perl/Tk itself falls into this category). You know you have an Overgrown Module (Extension) when there is one or more files with an .xs extension (perl->C meta code) and a Makefile.PL (perl->make meta code). One invokes the perl MakeMaker on the file called Makefile.PL in order to create a Makefile via: perl Makefile.PL
You may now run make on the resultant Makefile - but the details of this process are module dependent and should be documented in a README or an INSTALL file. A very standard perl extension requires 4 (or 5 if making static) standard commands to make and install: perl Makefile.PL make make test make install
If you have the appropriate CPAN and FTP modules already installed you can retrieve a module from CPAN and carry out all of the above steps with a perl one-liner like this: perl -MCPAN -e 'install "Foo"'
译文:
14. 如何安装新的脚本、模块或扩展?
(感谢Ilya Zakharevich指出Perl代码的风格多种多样,安装有些代码时就是会比其它代码更多的要求。因此,我在这里就把这个主题扩展成了三个对立的部分:脚本、模块和扩展)
脚本
原则上说,一个完整对立的脚本是不需要什么修改就应该可以运行的。一般我们还需要检查开头“#!”的行所指向的本地Perl安装的位置是否正确(例如,也许你需要把#!/usr/bin/perl -w 改为#!/usr/gnu/local/perl –w以适应你的系统)。当然,其实还有很多其它更方便的方法来调用perl解释器的,但是这些内容应该在perlFAQ和perlrun的手册页中。
另外还有一个事情你不应该忘记的,就是在你试图运行这个脚本perl脚本之前先给自己运行的权限,例如:
chmod u+x newscriptname (译者注:这只是对于Unix/Linux系统而言的。)
同时,如果你正在尝试运行一个Perl/Tk的脚本,那么你可能必须先正确的设定你的DISPLAY环境变量。你也许还需要看一下xhost或xauth的手册页,以便正确的设置你系统的X显示。(译者注:这都是对于Unix/Linux系统而言的。)
如果你在实践的过程中仍然有困难,请检查一下你的脚本,确保其中没有诸如:新闻组、电子邮件地址、HTML头、HTML尾或“//”等特殊标记。另外,还要确保你的Perl版本是符号这个脚本所要求的(否则,你可能需要安装模块或甚至是一个Perl4风格的库文件)。
模块
首先检查这个模块,确保它可以在你的系统上使用——例如它是否需要一个特别的位置?作为测试(这总是个好主意)或者如果你没有root权限,可以先把他装在一个你有写权限的目录中,然后在你的脚本中包含(include)这个路径。假设你需要测试的模块名为“Foo.pm”,我们可以简单的在和这个模块相同的目录中运行你的测试脚本——只要用下面的方法把当前目录加入到@INC数组中去就可以了:
#!/usr/bin/perl -w
BEGIN { @INC = ("$ENV{'PWD'}",@INC); }
use Tk;
use Foo;
or
#!/usr/bin/perl -w
use lib $ENV{PWD};
use Tk;
use Foo;
另一种解决方法是给你的shell设置PERLLIB或PERL5LIB环境变量。这个方法的好处是你可以需要去修改脚本(加入use lib或push(@INC,”.”)的语句)而在任何目录中之间运行你的测试脚本。还有一个方法是在命令行中使用-I开关,如下:
perl -Ipath/to/Foo -e fooscriptname
测试成功以后,如果你是系统管理员,或者是有root权限,或者你在修改你自己安装的Perl,那么你就可以把它复制到Perl5/Tk的目录中。根据模块自身的写法的不同,它可能被使用use Tk;语句直接引入,或者需要明确的使用use Tk::Foo;(对于安装在perl5/Tk/Foo.pm的模块而言)。
扩展(庞大的模块)
他们可能是一个包含多个文件的kit(通常是磁带档案——译者注:好老呀!),并且可能需要一个C编译器来完成部分的安装(Perl/Tk本身就属于这个分类)。当你发现在你的包中有一个或多个.xs扩展名的文件(perl->C meta代码),并且还有一个Makefile.PL(perl->make meta代码)时,则说明你要安装的是一个庞大的模块(扩展)。这样,我们只需要通过下面的方法来对Makefile.PL调用perl的MakeMaker来创建Makefile:
perl Makefile.PL
这样,你就可以对刚才生成的Makefile进行make了。但是,详细的步骤会依不同的模块而有所不同的,请参考README或INSTALL文件。下面是一个标准的Perl扩展的标准安装命令:
perl Makefile.PL
make
make test
make install
如果你已经正确定安装了CPAN和FTP模块,你还可以直接从CPAN上获取一个所需的模块并安装它,也就是使用下面的一行命令来代替上面的步骤:
perl -MCPAN -e 'install "Foo"'