如何在 Perl 中打印源代码行号?

发布于 2024-07-10 05:45:46 字数 62 浏览 7 评论 0原文

Perl 中是否可以获取当前源代码行号? C++ 中的等效项是 __LINE__

Is it possible to get the current source line number in Perl?
The equivalent in C++ is __LINE__.

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

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

发布评论

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

评论(4

夜雨飘雪 2024-07-17 05:45:46

__LINE__ 文字记录在 特殊文字 部分perldata 手册页

print "File: ", __FILE__, " Line: ", __LINE__, "\n";

或者

warn("foo");

The __LINE__ literal is documented in the Special Literals section of the perldata man page.

print "File: ", __FILE__, " Line: ", __LINE__, "\n";

or

warn("foo");
人心善变 2024-07-17 05:45:46

请注意,有一个问题:

$ perl -e'warn("foo")'
foo at -e line 1.

如果它以换行符结尾,则不会打印行号。

$ perl -e'warn("foo\n")'
foo

这在 perldoc -f die 中有记录,但在perldoc -f warn 部分对 die 的引用。

Note there's a gotcha with

$ perl -e'warn("foo")'
foo at -e line 1.

If it ends with a newline it won't print the line number

$ perl -e'warn("foo\n")'
foo

This is documented in perldoc -f die, but is perhaps easy to miss in the perldoc -f warn section's reference to die.

私野 2024-07-17 05:45:46

这会打印出您所在的行,以及“堆栈”(调用程序(脚本/模块/等)中导致您现在所在位置的行列表)

while(my @where=caller($frame++)) { print "$frame:" . join(",",@where) . "\n"; }

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)

while(my @where=caller($frame++)) { print "$frame:" . join(",",@where) . "\n"; }
淡看悲欢离合 2024-07-17 05:45:46

“使用 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.

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