我想获取 Perl 脚本中的当前文件名和行号。我该怎么做?
例如,在文件中调用test.pl
:
my $foo = 'bar';
print 'Hello, World!';
print functionForFilename() . ':' . functionForLineNo();
它会输出:
Hello, World!
test.pl:3
最佳答案
这些可通过 __LINE__
和 __FILE__
token 使用,如 perldoc perldata 中所述。在“特殊文字”下:
The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program. They may be used only as separate tokens; they will not be interpolated into strings. If there is no current package (due to an empty package; directive), __PACKAGE__ is the undefined value.
关于perl - 如何在 Perl 中获取文件名和行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4071447/