package a;
sub func {
print 1;
}
package main;
a::->func;
Run Code Online (Sandbox Code Playgroud)
IMO足够了a::func
,a->func
.
a::->func;
对我来说看起来很奇怪,为什么Perl支持这种奇怪的语法?
DVK*_*DVK 25
在Modern Perl博客上引用chromatic关于这个话题的最新博客文章:"避免使用赤字解析模糊性."
为了说明为什么这样的语法有用,这里是一个从您的示例演变而来的示例:
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"
Run Code Online (Sandbox Code Playgroud)
a::
是一个生成字符串的字符串文字a
.全都一样:
a->func() # Only if a doesn't exist as a function.
"a"->func()
'a'->func()
a::->func()
v97->func()
chr(97)->func()
Run Code Online (Sandbox Code Playgroud)
等等
>perl -E"say('a', a, a::, v97, chr(97))"
aaaaa
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
306 次 |
最近记录: |