Perl 5 中的父级和基级有什么区别?
There appears to be a new pragma named parent
that does roughly the same thing as base
. What does parent
do that warrants a new (non-core) module? I am missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
base
试图做太多的事情——自动处理加载模块,但也允许从已加载的类(可能是从名称不基于模块名称的文件)建立继承。 为了让它发挥作用,一些黑客行为在某些情况下导致了令人惊讶的结果。 引入了一个具有更清晰语义的新的替换 pragmaparent
,而不是破坏向后兼容性。自 5.10.1 起,
parent
将成为核心模块。更新:忘记
base
处理fields
(如果您使用fields
pragma),它的parent
不行。base
tried to do one too many things – automatically handling loading modules but also allowing establishing inheritance from classes already loaded (possibly from a file whose name wasn't based on the module name). To sort of make it work, there was some hackery that caused surprising results in some cases. Rather than break backwards compatibility, a new, replacement pragmaparent
was introduced with cleaner semantics.parent
will be a core module as of 5.10.1.Update: forgot that
base
handlesfields
(if you are using thefields
pragma), whichparent
doesn't do.有了来自 ysth 的额外信息,我能够看到文档中的差异:
base
pragma 执行以下操作:@ISA
require
(除非它检测到包已经被加载)$VERSION
,则将其设置为"-1,设置方式为base.pm"
字段
包(如果存在)parent
pragma 执行以下操作:@ISA
require
加载与命名包同名的模块Armed with the extra bit of information from ysth, I was able to see the differences in the docs:
The
base
pragma does the following things:@ISA
require
(unless it detects that the package has already been loaded)$VERSION
does not exist in named package, base sets it to"-1, set by base.pm"
fields
of the named package if they existThe
parent
pragma does the following things:@ISA
require