此語句定義一個名爲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 |