=pod 第十一章 perl模块 寻找模块: cpan -a 安装模块: perl Makefile.PL make install #如果没有权限安装在系统级目录,那就安装在个人目录 perl Makefile.PL INSTALL_BASE=/Users/fred/lib 使用简易模块: my $name = "/usr/local/bin/perl"; 对上面路径得到perl子串 my $basename = $name =~ s#.*/##s; #/前面的有空替换掉,包括中间有 的 但perl自带的函数已经解决了这个问题 File::Basename模块: 通过perldoc Filedoc File::Basename来了解他的使用信息 use File::Basename; my $name = "/usr/local/bin/perl"; my $basename = basename $name; #返回perl use File::Basename qw//; #不导入函数名称,但可以使用全程使用 my $dirname = File::Basename::dirname $name; #使用模块中的dirname函数,因为有时自己写的函数有可能和模块导入的函数重名 use File::Base qw /basename/; #导入basename函数 File::Spec模块: 由文件和路径结合成全路径 my $dirname = dirname $old_name; my $basename = basename $old_name; my $newname = File::Spec->catfile($dirname, $basename); Path::Class:比File::Spec更好用 my $dir = dir(qw(User fred lib)); my $subdir = $dir->subdir('perl5'); #users/fred/perl5 my$parent = $dir->parent; #usrs/fred my $windir = $dir->as_foreign('Win32'); #Usersfredlib CGI.pm: 数据库和DBI模块: DBI(Database Interface,数据库接口)并没有随标注版发行 DBI的美妙之处是不管哪种常见的数据库,都可以用相同的接口对其进行操作,从csv文件到oracle之类的大型数据库服务器,他还具有ODBC驱动程序 通过DBI模块连接数据库,执行数据库指令,断开数据库,结束 处理日期和时间的模块: 经典的是Dave Rolsky编写的DateTime模块 Time::Piece是perl5.10后自带的模块 =cut
简单练习:
#!/usr/bin/perl -w use strict; use File::Basename; use File::Spec; use Path::Class; use Time::Piece; my $t = localtime; print "Time is $t "; print "Year is ", $t->year, " "; =pod my $dir = dir(qw(User fred lib)); my $subdir = $dir->subdir('perl5'); #users/fred/perl5 my $parent = $dir->parent; #usrs/fred my $windir = $dir->as_foreign('Win32'); #Usersfredlib print "$dir "; print "$subdir "; print "$parent "; print "$windir "; =cut =pod print "Please enter a filename: "; chomp(my $old_name = <STDIN>); my $dirname = dirname $old_name; my $basename = basename $old_name; my $newname = File::Spec->catfile($dirname, $basename); rename($old_name, $newname) or warn "Can't rename '$old_name' to '$newname': $!"; =cut =pod my $name = "/usr/local/bin/perl"; my @array = (); #my $basename = basename $name, @array; my $basename = fileparse $name, @array; print $basename, " "; print '@array = ', "@array "; =cut system("pause");