Perl   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了perl – 如何使用Devel :: Declare注入多行?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用 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";];
}

大佬总结

以上是大佬教程为你收集整理的perl – 如何使用Devel :: Declare注入多行?全部内容,希望文章能够帮你解决perl – 如何使用Devel :: Declare注入多行?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。