perl – 如何使用Devel :: Declare注入多行?

栏目: Perl · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/25171619/how-can-i-inject-multiple-lines-with-develdeclare
我想使用 Devel::Declare

来注入多行 Perl 代码.但是,Devel :: Declare :: set_linestr()不能处理多行.

通常我会把多个语句一起加入一行.这些语句必须是单独的行,以保留其行号用于错误报告.这是为了解决 this bug in Method::Signaturesthis related bug .我打开了替代解决方案.

例如,Method :: Signatures当前会转换此代码…

use Method::Signatures;

func hello(
    $who = "World",
    $greeting = get_greeting($who)
) {
    die "$greeting, $who";
}

…进入…

func  \&hello; sub hello  { BEGIN { Method::Signatures->inject_scope('') }; my $who = (@_ > 0) ? ($_[0]) : ( get_greeting($who)); my $greeting = (@_ > 1) ? ($_[1]) : ( "Hello"); Method::Signatures->too_many_args_error(2) if @_ > 2;
    die "$greeting, $who";
}

然后,$谁报告第4行而不是第7行.

我想要这样做(或者也可能涉及#line).

func  \&hello; sub hello  { BEGIN { Method::Signatures->inject_scope('') };
    my $who = (@_ > 0) ? ($_[0]) : ( "World");
    my $greeting = (@_ > 1) ? ($_[1]) : ( get_greeting($who));
    Method::Signatures->too_many_args_error(2) if @_ > 2;
    die "$greeting, $who";
}

这不仅忠实地重现线数,应该get_greeting croak它会从正确的行被调用.

根据您自己的答案,以下作品:

sub __empty() { '' }

sub parse_proto {
    my $self = shift;
    return q[print __LINE__."\n"; Foo::__empty(
);print __LINE__."\n"; Foo::__empty(
);print __LINE__."\n";];
}

但是引入不可接受的开销,因为必须为每个参数调用__empty()函数.可以通过调用__empty()有条件地使用永远不会评估为true的条件来消除开销.

sub __empty() { '' }

sub parse_proto {
    my $self = shift;
    return q[print __LINE__."\n"; 0 and Foo::__empty(
);print __LINE__."\n"; 0 and Foo::__empty(
);print __LINE__."\n";];
}

http://stackoverflow.com/questions/25171619/how-can-i-inject-multiple-lines-with-develdeclare


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

TensorFlow实战

TensorFlow实战

黄文坚、唐源 / 电子工业出版社 / 2017-2-1 / 79

Google近日发布了TensorFlow 1.0候选版,这个稳定版将是深度学习框架发展中的里程碑的一步。自TensorFlow于2015年底正式开源,距今已有一年多,这期间TensorFlow不断给人以惊喜,推出了分布式版本,服务框架TensorFlow Serving,可视化工具TensorFlow,上层封装TF.Learn,其他语言(Go、Java、Rust、Haskell)的绑定、Wind......一起来看看 《TensorFlow实战》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换