perl - 如何在 Perl 中获取文件名和行号?

标签 perl

我想获取 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/

相关文章:

perl - 使用 cpan 安装 Params::Validate - 无法启动 'Build'

perl - fetchall_arrayref 中的 foreach 值

perl - 设置AutoCommit和begin_work/rollback是一样的吗?

perl - perl split() 中的分隔符保留模式

perl - 如何修复 Perl 的 Font::FreeType 编译错误?

php - 相当于 PHP 的 $_SESSION 的 perl

perl - 合并由两列组成的文件

php - Encryption Crypt - 尝试以与 Perl 相同的方式在 PHP 中工作

Perl Mojolicious + PgAsync 插件——泄漏数据库连接?

mysql - 找不到我的错误