标签:
#!/usr/bin/perl package xxx; package指令创建“类”; sub ... 创建sub就是创建“类”的“方法” sub ... sub ... 1; 包文件结尾要retrun 1 |
&mypack‘printval(); |
在包mypack外调用包内sub |
$mypack‘line=10; or $mypack::line=10; |
在包mypack外调用包内变量 perl5中建议$mypack::var 单引号引用的方式仍然支持,但将来的版本中未必支持 |
#!/usr/bin/perl $i=0; sub gotest { … } |
等价 | #!/usr/bin/perl package main; $i=0; sub gotest { … } |
1: #!/usr/local/bin/perl 2: 3: package pack1; 4: $var = 26; 5: package pack2; 6: $var = 34; 7: package pack1; 8: print ("$var\n"); |
$ ./tip.pl 26 $ |
package; $var = 21; |
#error - no current package |
package; $mypack::var = 21; |
1 : package privpack; 2 : $valtoprint = 46; 3 : 4 : package main; 5 : # This function is the link to the outside world. 6 : sub printval { 7 : &privpack‘printval(); 8 : } 9 : 10: package privpack; 11: sub printval { 12: print ("$valtoprint\n"); 只有同一个包中,才能“直接”调用此变量,其他包中调用此变量要带::前缀或切换package ... 13: } 14: 15: package main; 16: 1; |
vi file1.pm #!/usr/local/bin/perl package file1; |
require Exporter; @ISA = qw(Exporter); |
@EXPORT = qw(readfile checkfile gotest); # @EXPORT_OK = qw($myvar1 $myvar2); |
sub readfile{ my(@tmp)=@_; my($line); open (MYFILE, $tmp[0]) || die ("Could not open file"); while ($line=<MYFILE>) { print $line; } } sub checkfile{ my(@tmp)=@_; open (MYFILE, $tmp[0]) || die ("Could not open file"); my($line,$pattern,$lamp); $pattern=$tmp[1]; $lamp=0; while (chomp($line=<MYFILE>)) { if($line!~/$pattern/) { print "[$line ] :this line is wrong format\n"; $lamp=1; } } if($lamp) { print "\n",$tmp[2],"\n";} close(MYFILE); } sub gotest{ my(@tmp)=@_; open (MYFILE, $tmp[0]) || die ("Could not open file"); my($line,$newline); while ($line=<MYFILE>) { $line=~ tr/a-zA-Z//s; print "good \n"; print "\$line is :$line"; print "\$\& is : $&", "\n"; } close(MYFILE); } |
1; |
[macg@localhost perltest]$ ls testdir file1.pl file1.pm test1 [macg@localhost perltest]$ su Password: [root@localhost perltest]# cp testdir/file1.pm /usr/lib/perl5/5.8.6/ [root@localhost perltest]# exit exit |
[macg@localhost perltest]$ vi tip.pl #!/usr/bin/perl use file1; use 模块 $file="/home/macg/perltest/gogo"; $pattern="\^\\d+\\.\\d+\\.\\d+\\.\\d+\$"; &readfile($file); 直接调用模块中的sub print "-----------------------------------------\n"; $pattern="\^[0-9a-z]+[\\t ]+\\d+\\.\\d+\\.\\d+\\.\\d+\$"; $message="example:hostname1 10.10.20.2"; &checkfile($file,$pattern,$message); |
[macg@localhost perltest]$ ./tip.pl host1 202.106.0.20 host2 9.89.9.1 host3 10.0.23.6 11.0.25.9 host5 12.0.1.0as ----------------------------------------- [11.0.25.9 ] :this line is wrong format [host5 12.0.1.0as ] :this line is wrong format example:hostname1 10.10.20.2 |
vi tip.pl #!/usr/bin/perl package main; use file1; |
[root@localhost perltest]# rm /usr/lib/perl5/5.8.6/file1.pm rm: remove regular file `/usr/lib/perl5/5.8.6/file1.pm‘? y [macg@localhost perltest]$ ls 当前目录中 file1.pm gogo newdir newtest test testdir tip.pl |
[macg@localhost perltest]$ ./tip.pl host1 202.106.0.20 host2 9.89.9.1 host3 10.0.23.6 |
1: #!/usr/local/bin/perl 2: 3: use integer; 4: $result = 2.4 + 2.4; integer模块要求所有数字运算基于整数,浮点数在运算前均被转化为整数。 5: print ("$result\n"); 6: 7: no integer; 8: $result = 2.4 + 2.4; 9: print ("$result\n"); |
$./tip.pl 4 4.8 $ |
[root@localhost perltest]# ls -F /usr/lib/perl5/5.8.6/File Basename.pm CheckTree.pm Compare.pm Copy.pm DosGlob.pm Find.pm Path.pm Spec/ Spec.pm stat.pm Temp.pm [root@localhost perltest]# ls -F /usr/lib/perl5/5.8.6/CGI Apache.pm Carp.pm Cookie.pm eg/ Fast.pm Pretty.pm Push.pm Switch.pm Util.pm |
#!/usr/bin/perl chomp($file=<>); chomp($file2=<>); &gotest($file,$file2); sub gotest{ my(@tmp)=@_; use File::Copy; File::Copy即/usr/lib/perl5/5.8.6/File/Copy.pm copy($tmp[0], $tmp[1]); } |
[macg@localhost perltest]$ ./tip.pl test newtest [macg@localhost perltest]$ ls newtest test testdir tip.pl |
[macg@localhost perltest]$ ls /usr/lib/perl5/5.8.6 abbrev.pl bigrat.pm DB.pm Fatal.pm I18N NEXT shellwords.pl Thread.pm AnyDBM_File.pm blib.pm Devel fields.pm i386-linux-thread-multi NEXT.pm sigtrap.pm Tie assert.pl bytes_heavy.pl diagnostics.pm File if.pm open2.pl sort.pm Time Attribute bytes.pm Digest file1.pm importenv.pl open3.pl |
[macg@localhost perltest]$ ls /usr/lib/perl5/5.8.6/Net Changes.libnet Config.eg demos FTP hostent.pm libnet.cfg netent.pm NNTP.pm Ping.pm protoent.pm servent.pm Time.pm Cmd.pm Config.pm Domain.pm FTP.pm Hostname.eg libnetFAQ.pod Netrc.pm Ping POP3.pm README.libnet SMTP.pm |
[macg@localhost perltest]$ perldoc Net::FTP 基于perl lib根目录 NAME Net::FTP - FTP Client class SYNOPSIS use Net::FTP; $ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@"; $ftp->login("anonymous",鈥?anonymous@鈥? or die "Cannot login ", $ftp->message; $ftp->get("that.file") or die "get failed ", $ftp->message; $ftp->quit; |
下载DBI-1.37.tar.gz |
tar xvzf DBI-1.37.tar.gz cd DBI-1.37 perl Makefile.PL make make test make install |
#! /usr/bin/perl use DBI; # 宣告使用 DBI module內的所有"方法" my $db="test"; my $host=‘localhost‘; my $user=‘root‘; my $password=‘ppp123‘; |
标签:
原文地址:http://www.cnblogs.com/chip/p/4292778.html