Perl 5 中的父级和基级有什么区别?

发布于 2024-07-22 05:56:10 字数 255 浏览 7 评论 0原文

似乎有一个名为 parentbase 的作用大致相同。 parent 做什么来保证新的(非核心)模块? 我错过了什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

违心° 2024-07-29 05:56:10

base 试图做太多的事情——自动处理加载模块,但也允许从已加载的类(可能是从名称不基于模块名称的文件)建立继承。 为了让它发挥作用,一些黑客行为在某些情况下导致了令人惊讶的结果。 引入了一个具有更清晰语义的新的替换 pragma parent,而不是破坏向后兼容性。

自 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 pragma parent was introduced with cleaner semantics.

parent will be a core module as of 5.10.1.

Update: forgot that base handles fields (if you are using the fields pragma), which parent doesn't do.

薆情海 2024-07-29 05:56:10

有了来自 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:

  • adds the named package to @ISA
  • loads the module with the same name as the named package using require (unless it detects that the package has already been loaded)
  • won't fail if a module with the same name as the package doesn't exist
  • dies if there are no symbols in the named package
  • if $VERSION does not exist in named package, base sets it to "-1, set by base.pm"
  • initializes the fields of the named package if they exist
  • does not call the import function of the named package

The parent pragma does the following things:

  • adds the named package to @ISA
  • loads the module with the same name as the named package using require
  • accepts an option that tells it not to die if a module with the same name as the package doesn't exist
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文