使用 Filter::Util::Call 访问 Perl 中调用源代码行的剩余部分
下面是一个基本的源过滤器,它只是在 Perl 源代码中 use
的位置插入一个新行,然后删除自身:
use warnings;
use strict;
use 5.010;
{package insert;
use Filter::Util::Call;
BEGIN {$INC{'insert.pm'}++}
sub import {
my ($class, @data) = @_;
filter_add sub {
my $status = filter_read;
if ($status) {
$_ = "@data;$_";
filter_del;
}
$status
}
}
}
my $x = 'init';
say $x; # init
use insert '$x = "hello"'; say $x; # init (should be hello)
say $x; # hello
问题,如最后的注释所示,是 use 语句的终止分号后面但仍在同一行上的任何代码最终都会在插入的源代码之前进行编译。
我是否错误地使用了 Filter::Util::Call ?是否有其他方法来设置过滤器以捕获第一行?或者这只是 Perl 源过滤机制的限制?
The following is a basic source filter that simply inserts a new line into the Perl source code at the point where it is use
'd and then removes itself:
use warnings;
use strict;
use 5.010;
{package insert;
use Filter::Util::Call;
BEGIN {$INC{'insert.pm'}++}
sub import {
my ($class, @data) = @_;
filter_add sub {
my $status = filter_read;
if ($status) {
$_ = "@data;$_";
filter_del;
}
$status
}
}
}
my $x = 'init';
say $x; # init
use insert '$x = "hello"'; say $x; # init (should be hello)
say $x; # hello
The problem, as shown in the comments at the end, is that any code following the terminal semicolon of the use statement, but still on the same line, ends up being compiled before the inserted source.
Am I using Filter::Util::Call incorrectly? Is there some other way to setup the filter so that it will catch the first line? Or is this just a limitation of Perl's source filtering mechanisms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Filter::Util::Call 似乎确实受到您所描述的方式的限制。也许这是您给出的一个过于简化的示例,但是您是否有任何理由不能只使用字符串 eval 而不是源过滤器?
Filter::Util::Call does appear to be limited in the way you described. Perhaps this is an over-simplified example you gave, but is there any reason you can't just use a string eval instead of a source filter?