在perl中,有没有办法让子例程在调用时打印它们的名称和参数?

发布于 2024-11-02 20:30:44 字数 176 浏览 2 评论 0原文

我需要快速了解在一堆意大利面条代码 perl 模块中调用了哪些子例程。

我可以编写一个脚本来遍历所有模块,并添加代码以在每个子例程开始时打印出子例程名称和参数。

但是有没有办法覆盖内部 perl“子例程”机制本身来做到这一点?

该代码在 mod_perl 下运行,因此我无法轻松使用调试器。

I need to quickly understand which subroutines are being called in a mess of spaghetti code perl modules.

I could write a script to go through all the modules and add code to print out the subroutine name and arguments at the start of every subroutine.

But is there a way to override the internal perl 'subroutine' mechanism itself to do this?

The code is running under mod_perl so I can't easily use a debugger.

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

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

发布评论

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

评论(4

长梦不多时 2024-11-09 20:30:45

Devel:Trace 将显示子例程调用和参数。它还将显示执行的每一行代码,这可能比您需要的更详细。

perl -d:Trace program

Devel::DumpTrace 将更加详细,显示值< /strong> 变量也是如此。

Devel:Trace will show subroutine calls and arguments. It will also show every line of code that is executed, which may be more verbose than you need.

perl -d:Trace program

Devel::DumpTrace will be even more verbose, showing the values of variables as well.

一曲琵琶半遮面シ 2024-11-09 20:30:45

您可以通过 NYTProf 运行代码当通过 mod_perl 在 Apache 下运行

You can run the code through NYTProf when when running under Apache via mod_perl

霊感 2024-11-09 20:30:45

我知道您说您“无法轻松使用调试器”,因为您在 mod_perl 下运行,但是有一些选项可以实现此处描述的操作:

http://perl.apache.org/docs/1.0/guide/debug.html#Non_Interactive_Perl_Debugging_under_mod_perl

I know you say you "can't easily use the debugger" because you're running under mod_perl, but there are some options for doing just that described here:

http://perl.apache.org/docs/1.0/guide/debug.html#Non_Interactive_Perl_Debugging_under_mod_perl

诗化ㄋ丶相逢 2024-11-09 20:30:44

您可以使用 Moose::方法修饰符。我对 Moose 不太了解,但从手册中我可以看到你可以做到。就这样吧。

#!/usr/bin/perl -w
use 5.010;
use strict;
use Moose;

sub sub_one {
    say "I am sub one!";
}

sub sub_two {
    say "Guess who!";
}

# Note that the name of the function being modified isn't passed in in
# any way

for my $func qw(sub_one sub_two) {
    around $func => sub {
        my $orig = shift;

        say "Running ${func}(@_)";

        # call the original sub
        my $self = shift;
        $self->$orig(@_);
    }
}

sub_one(21, 12);
sub_two();

这会产生类似“

cnicutar@aiur:~$ perl method_modifiers.pl
Running sub_one(21 12)
I am sub one!
Running sub_two()
Guess who!

请注意我只是一个初学者,所以要小心该代码”的内容。

You could use Moose::MethodModifiers. I don't know much about Moose, but from the manual I can see you can do it. Here goes.

#!/usr/bin/perl -w
use 5.010;
use strict;
use Moose;

sub sub_one {
    say "I am sub one!";
}

sub sub_two {
    say "Guess who!";
}

# Note that the name of the function being modified isn't passed in in
# any way

for my $func qw(sub_one sub_two) {
    around $func => sub {
        my $orig = shift;

        say "Running ${func}(@_)";

        # call the original sub
        my $self = shift;
        $self->$orig(@_);
    }
}

sub_one(21, 12);
sub_two();

This produces something like

cnicutar@aiur:~$ perl method_modifiers.pl
Running sub_one(21 12)
I am sub one!
Running sub_two()
Guess who!

Please note I'm only a beginner so be cautious about that code.

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