package a;
sub func {
print 1;
}
package main;
a::->func;
IMO有
a::func
,a->func
就足够了。a::->func;
在我看来很奇怪,为什么Perl支持这种奇怪的语法? 最佳答案
在Modern Perl blog上引用chrom近期关于该主题的出色博客文章:“避免裸词解析歧义。”
为了说明为什么使用这种语法,下面是从您的示例演变而来的示例:
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"
关于perl - 为什么“a::-> func;”有效?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7303806/