package a; sub func { print 1; } package main; a::->func;
IMO只需要一个:: func,a-&funk就可以了。
一个:: – > FUNC;看起来很奇怪,为什么Perl支持这种奇怪的语法?
解决方法
引用色彩优雅的最新博客文章在
Modern Perl blog的主题:“避免裸词解析歧义”。
为了说明为什么这样的语法是有用的,这里是一个从您的示例演变的示例:
package a; our $fh; use IO::File; sub s { return $fh = IO::File->new(); } package a::s; sub binmode { print "BINMODE\n"; } package main; a::s->binmode; # does that mean a::s()->binmode ? # calling sub "s()" from package a; # and then executing sub "open" of the returned object? # In other words,equivalent to $obj = a::s(); $obj->binmode(); # Meaning,set the binmode on a newly created IO::File object? a::s->binmode; # OR,does that mean "a::s"->binmode() ? # calling sub "binmode()" from package a::s; # Meaning,print "BINMODE" a::s::->binmode; # Not ambiguous - we KNOW it's the latter option - print "BINMODE"