perl module and its package
==================================包package===========================
此语句定义一个名为mypack的包,从此以后定义的所有变量和sub的名字都存贮在该包关联的符号表中,直到遇到另一个package语句为止。
#!/usr/bin/perl package xxx; sub ... sub ... sub ... 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 { … } |
其中模块文件,文件名必须与package 名相同
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; |
|
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 : 8 : } 9 : 10: package privpack; 11: sub printval { 12: 13: } 14: 15: package main; 16: 1; |
=============================================模块===============================
创建包并将之存在同名的文件中,就是模块。
-
创建包文件,要求文件名为”包名.pm”(和里面的package “包名”同名)
vi file1.pm #!/usr/local/bin/perl package file1; |
-
Require Exporter
require Exporter; @ISA = qw(Exporter); |
-
定义@EXPORT和@EXPORT_OK
- sub 赋值给数组@EXPORT数组后,就能被其他文件调用,否则,在模块中定义但没有赋给数组@EXPORT的sub都是私有的,只能在模块内部调用
- @EXPORT_OK
定义能被其他文件调用的变量(全局变量),不在这个数组中的变量也是私有变量,则出不了模块文件。
@EXPORT = qw(readfile
checkfile gotest); # @EXPORT_OK = qw($myvar1 $myvar2); |
-
具体的sub和全局变量定义
sub readfile{ my(@tmp)=@_; my($line); open (MYFILE, $tmp[0]) || die ("Could not open file"); while ($line=<MYFILE>) { } 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($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; close(MYFILE); } |
-
文件结束要return 1;
1; |
-
如何使用模块
[macg@localhost perltest]$ ls testdir file1.pl [macg@localhost perltest]$ su Password: [root@localhost perltest]# cp testdir/file1.pm /usr/lib/perl5/5.8.6/ [root@localhost perltest]# exit exit |
-
在perl编程中使用模块
[macg@localhost perltest]$ vi tip.pl #!/usr/bin/perl use file1; $file="/home/macg/perltest/gogo"; $pattern="\^\\d+\\.\\d+\\.\\d+\\.\\d+\$"; &readfile($file); print "-----------------------------------------\n"; $pattern="\^[0-9a-z]+[\\t ]+\\d+\\.\\d+\\.\\d+\\.\\d+\$"; $message="example:hostname1 &checkfile($file,$pattern,$message); |
[macg@localhost perltest]$ ./tip.pl host1 202.106.0.20 host2 host3 11.0.25.9 host5 ----------------------------------------- [11.0.25.9 ] :this line is wrong format [host5 example:hostname1 |
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 |
[macg@localhost perltest]$ ./tip.pl host1 202.106.0.20 host2 host3 |
integer
Diagnostics
English
Env
POSIX
Socket
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 [root@localhost perltest]# ls -F /usr/lib/perl5/5.8.6/CGI Apache.pm |
#!/usr/bin/perl chomp($file=<>); chomp($file2=<>); &gotest($file,$file2); sub gotest{ my(@tmp)=@_; use File::Copy; copy($tmp[0], $tmp[1]); } |
[macg@localhost perltest]$
./tip.pl test newtest [macg@localhost perltest]$ ls newtest |
-
先确定perl5的lib根目录
[macg@localhost perltest]$ ls /usr/lib/perl5/5.8.6 abbrev.pl AnyDBM_File.pm assert.pl |