Perl 中没有更简单的多行注释的原因是什么?

发布于 2024-10-18 03:41:48 字数 247 浏览 5 评论 0原文

我知道在 Perl 中引入多行注释的不同方法。但我想知道为什么它没有更简单的多行注释功能,例如 /* comment */ ,这会让它变得更容易。

我目前按照 http://www.perlmonks.org/?node_id=560985 实现多行评论。是否有计划很快在 Perl 中包含此功能?

I know different methods of introducing multi-line comments in Perl. But I want to know why it doesn't have something simpler multi-line comment feature such /* comment */, which would make it much easier.

I currently follow http://www.perlmonks.org/?node_id=560985 to achieve multi-line comments. Are there any plans to include this feature in Perl anytime soon?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

七分※倦醒 2024-10-25 03:41:48

C 风格的注释会带来各种难题,包括无法注释掉注释。此外,方便的一行语法鼓励简洁的编写,这使您的代码更易于阅读。另一个好处是没有在 C 和 C++ 中不时看到的 10 行“作者:...输入:...输出:...最喜欢的颜色:...”块。

多行注释鼓励长的写作,这样可以更好地更简洁地表达或作为文档,因此这就是 Perl 所鼓励的 #=pod 运算符(分别)。

最后,如果您对 Perl 的注释风格有困难,您应该将编辑器配置为编辑程序,而不是文本。在 Emacs 中,如果你开始写类似 # this is a comment 的内容,而且越来越长,天哪,我们这一行已经没有空间了,现在该怎么办 并输入 Mq,Emacs 会自动在它创建的每一行的开头插入 #

如果您只想注释掉一段代码,那么您只需要查看 Mx comment-regionMx uncomment-region,它们将注释掉该区域几乎任何语言;包括 Perl。

不要强调注释的语法;这就是计算机的工作!

C-style comments come with a variety of difficult problems, including the inability to comment-out comments. Additionally, convenient syntax for one-line encourages concise writing, which makes your code easier to read. The absence of 10-line "Author: ... Input: ... Output: ... Favorite Color: ..." blocks that you see from time to time in C and C++ is another benefit.

Multi-line comments encourage long writing that is better expressed more concisely or as documentation, so this is what Perl encourages with its # and =pod operators (respectively).

Finally, if you are having trouble with Perl's style of commenting, you should configure your editor to edit programs, not text. In Emacs, if you start writing something like # this is a comment that is getting longer and longer and longer and oh my goodness we are out of space on this line what to do now and type M-q, Emacs will automatically insert the # at the beginning of each line that it creates.

If you just want to comment-out a block of code, then you need look no further than M-x comment-region and M-x uncomment-region, which will comment out the region in pretty-much any language; including Perl.

Don't stress about the syntax of comments; that's the computer's job!

翻了热茶 2024-10-25 03:41:48

[电子邮件受保护]邮件列表上对此进行了讨论。尽管最终讨论没有得出结论,但此处发布的摘要值得一读。

There was a discussion regarding this on the [email protected] mailing list. Although in the end the discussion was inconclusive, the summary posted here makes for interesting reading.

孤独岁月 2024-10-25 03:41:48

与任何多行注释结构一样,都会有“打开”和“关闭”注释条件,这会导致嵌套注释出现问题。

例如,C 使用 /* 作为打开,*/ 作为关闭。多行评论系统如何处理评论中的评论?如果您尝试注释已注释的块,C 将失败。

请注意,基于行的注释(例如 c++ // 注释)不会遇到此问题。

As with any multiline comment structure, there will be a "open" and a "close" comment condition, and that leads to problems with nested comments.

for example, C uses /* as the open and */ as the close. How does a multiline comment system handle comments within comments? C will fail if you try to comment a block that is already commented.

Note that line-based comments (e.g. c++ // comments) do not suffer this problem.

红颜悴 2024-10-25 03:41:48

简单是情人眼中出西施。也就是说,已经有很多方法可以进行多行注释。首先,无效字符串文字:

q{This text won't do anything.
Neither will this.};

这会产生触发警告的不幸副作用:

Useless use of a constant in void context at - line 4.

另一个选择是在无效上下文中使用定界符 - 由于某种原因,这不会导致警告:

<<ENDCOMMENT;
Foo comment bar.
ENDCOMMENT

如您所见,问题不在于缺少这样的语法(事实上,python 文档注释看起来与 q{} 方法非常相似) - 更重要的是 Perl 程序员社区已经使用 # 和 POD。此时使用不同的方法只会让稍后必须阅读您的代码的人感到困惑。

Simplicity is in the eye of the beholder. That said, there are already a number of ways to do multi-line comments. First, void string literals:

q{This text won't do anything.
Neither will this.};

This has the unfortunate side effect of triggering a warning:

Useless use of a constant in void context at - line 4.

Another option is using a heredoc in void context - for some reason this doesn't cause a warning:

<<ENDCOMMENT;
Foo comment bar.
ENDCOMMENT

As you can see, the problem isn't the lack of syntax as such (python doc comments look vaugely similar to the q{} method in fact) - it's more just that the community of perl programmers has settled on line comments using # and POD. Using a different method at this point will just confuse people who have to read your code later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文