qaqa12345667 发表于 2015-12-27 09:36:19

Perl子过程调用

  Perl子过程调用有以下几种方式
  &name(para1, para2, ... paran) ;
  &name para1, para2, ... paran ;
  name(para1, para2, ... paran) ;
  name para1, para2, ... paran ;

什么时候&必须使用?

一 调用代码位于函数声明之后的



use strict ;
&test ; # & is must, since test was invoked before declaration

sub test{
    print "This is test\n" ;
}

1 ;
  上面的调用代码如果不加&将会出现如下编译错误
  areword "test" not allowed while "strict subs"

二 子过程与内置函数重名时



use strict ;
sub reverse{
    print "This is not built-in reverse!\n" ;
}
&reverse() ;
1 ;
  上面的代码如果不加&,则会产生如下编译错误
  Ambiguous call resolved as CORE::reverse(), qualify as such or use &

什么时候&可以省略?

一 函数调用前已经声明的,见上面第一点。

二 确信函数不与内置函数重名的,见上面第二点。

什么时候&不能使用

使用函数原型时
  因为&会忽略所有函数原型检查
页: [1]
查看完整版本: Perl子过程调用