为什么“进口”不是“进口”? Perl 中大写的子例程
我很好奇。大多数 Perl 隐式调用的子例程必须全部大写命名。 TIESCALAR、DESTROY 等。事实上 perldoc perltoot
说
如果构造函数可以任意 名称,那么为什么不析构函数呢? 因为虽然构造函数是 显式调用,析构函数是 不是。破坏发生了 自动通过 Perl 的垃圾 收集(GC)系统,这是一个 快但有点懒 基于参考的 GC 系统。要知道 该怎么称呼,Perl 坚持认为 析构函数被命名为DESTROY。珀尔的 打电话的正确时间的概念 析构函数没有明确定义 目前,这就是为什么你的 析构函数不应依赖于何时 他们被称为。
为什么 DESTROY 全部大写?佩尔 场合使用纯大写 函数名称作为约定 表明该函数将是 在某些情况下由 Perl 自动调用 方式。其他被称为 隐式包括 BEGIN、END、 AUTOLOAD,以及使用的所有方法 绑定对象,在 perltie 中描述。
那么为什么 import
子例程保留为小写呢?有人对此有很好的见解吗?
I am curious. Most of Perl's implicitly called subroutines must be named in all caps. TIESCALAR, DESTROY, etc. In fact perldoc perltoot
says
If constructors can have arbitrary
names, then why not destructors?
Because while a constructor is
explicitly called, a destructor is
not. Destruction happens
automatically via Perl's garbage
collection (GC) system, which is a
quick but somewhat lazy
reference-based GC system. To know
what to call, Perl insists that the
destructor be named DESTROY. Perl's
notion of the right time to call a
destructor is not well-defined
currently, which is why your
destructors should not rely on when
they are called.Why is DESTROY in all caps? Perl on
occasion uses purely uppercase
function names as a convention to
indicate that the function will be
automatically called by Perl in some
way. Others that are called
implicitly include BEGIN, END,
AUTOLOAD, plus all methods used by
tied objects, described in perltie.
Why then is the import
subroutine left to be lower case? Does anyone have a good insight on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想说“
import
”不是隐式调用的。这是由use
的实现发出的显式调用。引用 perldoc 使用:I'd say that "
import
" is not called implicitly. It's an explicit call issued by implementation ofuse
. To quote from perldoc use:为了稍微扩展 DVK 的答案,在某些情况下,您合法地想要显式调用
import
,例如在加载可选模块或自动填充命名空间时:我想不出任何情况您可能想要显式调用 DESTROY、TIESCALAR 等。
To expand on DVK's answer a little, there are situations where you'd legitimately want to invoke
import
explicitly, for example when loading an optional module or auto-populating namespaces:I can't think of any situation where you would ever want to invoke DESTROY, TIESCALAR, etc. explicitly.
这只是设计上的疏忽。想要改变已经太晚了。
It's simply an oversight in the design. It's too late to change.