如何在 Perl 中打印源代码行号?
Perl 中是否可以获取当前源代码行号? C++ 中的等效项是 __LINE__
。
Is it possible to get the current source line number in Perl?
The equivalent in C++ is __LINE__
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
__LINE__
文字记录在 特殊文字 部分perldata 手册页。或者
The
__LINE__
literal is documented in the Special Literals section of the perldata man page.or
请注意,有一个问题:
如果它以换行符结尾,则不会打印行号。
这在
perldoc -f die
中有记录,但在perldoc -f warn
部分对die
的引用。Note there's a gotcha with
If it ends with a newline it won't print the line number
This is documented in
perldoc -f die
, but is perhaps easy to miss in theperldoc -f warn
section's reference todie
.这会打印出您所在的行,以及“堆栈”(调用程序(脚本/模块/等)中导致您现在所在位置的行列表)
This prints out the line where you are, and also the "stack" (list of lines from the calling programs (scripts/modules/etc) that lead to the place you are now)
“使用 Carp”并使用各种例程,您还会得到一个堆栈 - 不确定这种方式是否比 cnd 建议的“调用者”方法更好或更差。 我在 C 和 Perl 中使用了 LINE 和 FILE 变量(可能还有其他类似的变量)来显示调试时在代码和其他信息中获得的位置,但在调试环境之外看不到什么价值。
"use Carp" and play with the various routines and you also get a stack - not sure if this way is better or worse than the "caller" method suggested by cnd. I have used the LINE and FILE variables (and probably other similar variables) in C and Perl to show where I got in the code and other information when debugging but have seen little value outside a debug environment.