第15章:Perl模块
在CPAN可以找到我们需要的模块!
15.1 查找模块
例如:我们查找一个叫“XB”的模块
$perldoc XB
15.2 安装模块
默认安装:
$perl Makerfile.PL
$make install
指定安装目录:
$perl Makerfile.PL PREFIX=/usr/perl/lib
如果安装模块依赖于其他模块,我们可以:
$perl-MCPAN –e shell
更简单的是,使用Perl附带的脚本:
$cpan Module::CoreList LWP XB::Test
15.3 使用系统自带的模块
a. use File::Basename
b. use File::Spec
15.4 DBI
database interface: http://dbi.perl.org
使用DBI的例子:
use DBI;
my $data_source = “dbi:Pg:dbname=name_of_database”; # PostgreSQL
$dbh = DBI->connect($data_sourc, $username, $password);
$sth = $dbh->prepare(“SELECT * FROM foo WHERE bla”);
$sth->execute();
@row_ary = $sth->fetchrow_array;
$sth->finish;
$dbh->disconnect(); # 断开连接