如何在Perl中找到打印声明的源位置?
#!/usr/bin/perl foo(); bar(); sub foo { print "foo\n"; } sub bar { print "bar\n"; }
输出为:
>perl test.pl foo bar
我想以某种方式找到能够看到(或类似的东西)
>perl test.pl main::foo> foo main::bar> bar
解决方法
尝试这个:
#!/usr/bin/env perl use strict; use warnings; use Tie::STDOUT print => sub { my ( $pkg,$file,$line ) = caller(2); print "$pkg,$line :: ",@_; }; print "Hello,World\n";
这使:
$perl tp.pl main,tp.pl,10 :: Hello,World
更新:我刚刚发布了Devel::Ditto:
$perl -MDevel::Ditto myprog.pl [main,t/myprog.pl,9] This is regular text [main,10] This is a warning [MyPrinter,t/lib/MyPrinter.pm,7] Hello,World [MyPrinter,8] Whappen?