微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl中的subzz

基本sub格式
sub subname{             
local ($filevar) = @_;           
local ($s1,$s1); 
...

  }  
&subname;                     调用加&
do my_sub(1,2,3);    另一种调用方式:do调用
等价于&my_sub(1,3);

 
  
 sub中局部变量的定义有两种方法:my和local。
my($scalar) = 43;
my定义的变量只在该子程序中存在;(在PERL4中没有my) local(@array) = (1,3);
而local定义的变量不仅存在于该子程序中,还存在于该子程序调用的子程序中
local($i,$wildmask)=0; 等于只初始化了一个值。
应该local($i,$wildmask)=(0,0);
sub中三种变量的应用例子
#!/usr/bin/perl

&gotest;
print "---------------main--------------------\n";
print "\$glo is:",$glo;
print "\$local is:",$local;
print "\$my is:",$my;
print "\n";

sub gotest{
print "---------------sub gotest--------------------\n";

$glo="glo\n";
sub中使用不用local 和my而直接定义的变量,必然是全局变量或上级变量
local($local)="local\n";
my($my)="my\n";

print "\$glo is:",$glo;
print "\$local is:",$my;

print "\n";
&subgotest;
}


sub subgotest{

print "---------------sub subgotest-----------------\n";
print "\$glo is:",$my;
sub中使用不用local 和my而直接定义的变量,必然是全局变量或上级变量
print "\n";

}
[macg@localhost perltest]$ ./tip.pl
---------------sub gotest--------------------
$glo is:glo
$local is:local
$my is:my

---------------sub subgotest--------------------
$glo is:glo
$local is:local
$my is:
local可以传给下层子sub,my是真正的“局部变量,只在当前sub内使用


---------------main--------------------
$glo is:glo
不用local和my定义的sub变量是全局变量,在所有地方都能使
$local is:$my is:    
不用local,my而直接定义的变量,可以用在任何地方,存活时间等于程序运行时间,其实就是全局变量,所以建议少用。还是经常用local和myperl 真正的全局变量,用our,其实不用local和my的都是全局变量
our @int_name;
our @int_address;
our $int_num=0;
不要把my用在while语句里,要提前用
while (my($line)=<MYFILE>) {
 print $line;
   }
[macg@localhost perltest]$ ./tip.pl
total 16 
显示一行,为什么?
因为等于每次循环都初始化一次变量。
my($line);
while ($line=<MYFILE>) {
 print $line;
   }
[macg@localhost perltest]$ ./tip.pl
total 16
-rw-rw-r--  1 macg macg   6 Mar 16 13:06 gogo
-rwxrwxr-x  1 macg macg 195 Mar 17 17:22 tip.pl  
最基本的sub参数传递--------实际是将参数@_赋值给局部变量
    sub参数 的两种表示方式:
整体形式(数组形式) 
 @_ 单独形式(数组元素形式)  $_[ ]
#!/usr/bin/perl
&gotest("stringgogo",1,3,"4test");

sub gotest{
local($tmp1,$tmp2,$tmp3,$tmp4)=@_;
或my(@tmp)=@_;

print "no.1 para is ",$tmp1,"\n";
print "no.2 para is ","\n";
print "no.3 para is ","\n";
print "no.4 para is ",$tmp4,"\n";

 
 #!/usr/bin/perl
&gotest("stringgogo","4test");

sub gotest{


print "no.1 para is ",$_[0],$_[1],$_[2],$_[3],"\n";

}
[macg@localhost perltest]$ ./tip.pl
no.1 para is stringgogo
no.2 para is 1
no.3 para is 3
no.4 para is 4test
[macg@localhost perltest]$ ./tip.pl
no.1 para is stringgogo
no.2 para is 1
no.3 para is 3
no.4 para is 4test
传送数组到sub 中的数组变量
#!/usr/bin/perl
@go=qw(a b cc dd);
&gotest(@go);

sub gotest{
my(@tmp)=@_;
foreach (@tmp) {
 print $_,"\n";
   }
 
[macg@localhost perltest]$ ./tip.pl
a
b
cc
dd); 
sub返回值,两种方式
最后一条语句的最大值
return语句,直接返回
#!/usr/bin/perl

@go=qw(a b cc dd);
$ret=&gotest(@go);
print $ret,"\n";

sub gotest{
my(@tmp)=@_;

9;
}

sub gotest{
my(@tmp)=@_;

return 9;
}
[macg@localhost perltest]$ ./tip.pl
9
 [macg@localhost perltest]$ ./tip.pl
9
sub 传参传字符串
1。可以传入字符串
2。可以return 字符串
#!/usr/bin/perl -w

sub test
{
local($a)=@_        
local($retstring);

 if($a eq "mac"                
  {
   $retstring="hahaha";
   return $retstring; 
   }
 if($a eq "macg")
  {
   $retstring="hahaha+ggg";
   return $retstring;
   }

}

$ret=&test("mac");
print $ret,"\n";
$ret=&test("macg");
print $ret,"\n"
[root@nm testpl]# ./tip.pl hahaha hahaha+ggg

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐