获取传递给子程序的所有参数如在Perl字符串 [英] Getting all arguments passed to a subroutine as a string in Perl

查看:140
本文介绍了获取传递给子程序的所有参数如在Perl字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写,可以采取它的所有参数,并打印出来的,正是因为他们被输入一个字符串的函数。

I am trying to write a function that can take all of its arguments and print them as a string exactly as they were entered.

例如使用下面的函数:

test('arg1' => $arg1, 'arg2' => $arg2);

我想获得的功能里面以下字符串的格式的完全(如下所示)的:

I would like to get the following string inside of the function formatted EXACTLY as seen below:

"'arg1' => $arg1, 'arg2' => $arg2"

我要做到这一点,所以我可以打印所有的参数以同样的方式,他们进入调试/测试目的。

I want to do this so I can print all of the arguments the same way that they were entered for debugging/testing purposes.

推荐答案

Perl提供特殊调试钩,让你看到编译源文件的原始行。你可以写,每一个子程序调用时打印原有线路的自定义调试器。

Perl provides special debugging hooks that let you see the raw lines of compiled source files. You can write a custom debugger that prints the original line every time a subroutine is invoked.

下面,您可以指定要匹配一个或多个子程序;每一个匹配的子程序调用时,相应的行打印出来。

The following lets you specify one or more subroutines you want to match; every time a matching subroutine is invoked, the corresponding line is printed.

package Devel::ShowCalls;

our %targets;

sub import {
    my $self = shift;

    for (@_) {
        # Prepend 'main::' for names without a package specifier
        $_ = "main::$_" unless /::/;
        $targets{$_} = 1;        
    }
}

package DB;

sub DB {
    ($package, $file, $line) = caller;
}

sub sub {
    print ">> $file:$line: ",
          ${ $main::{"_<$file"} }[$line] if $Devel::ShowCalls::targets{$sub};
    &$sub;
}

1;


要跟踪的功能调用巴兹:: qux 在下面的程序:

sub foo {}
sub bar {}
sub Baz::qux {}

foo(now => time);
bar rand;
Baz::qux( qw/unicorn pony waffles/ );

运行:

$ perl -d:ShowCalls=foo,Baz::qux myscript.pl 
>> myscript.pl:5: foo(now => time);
>> myscript.pl:7: Baz::qux( qw/unicorn pony waffles/ );

请注意,这将只打印调用的第一行,所以像

Note that this will only print the first line of the invocation, so it won't work for calls like

foo( bar,
     baz );

这篇关于获取传递给子程序的所有参数如在Perl字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆