我想模仿Perl
unlink
以测试我的代码是否删除了正确的文件。基于
this question and its answers
和
this other question and its answers
,我尝试过:
use strict;
use warnings;
use subs 'unlink';
sub mock_unlink {
use Data::Printer; p @_;
return;
}
BEGIN {
no warnings qw/redefine/;
*CORE::GLOBAL::unlink = \mock_unlink;
# *unlink = \mock_unlink;
use warnings;
}
unlink "some file";
但当我运行这个时,我的模拟函数确实被调用了,但参数列表是空的。我也得到了警告
取消链接
未定义。
$ perl mcve.pl
[]
Undefined subroutine &main::unlink called at mcve.pl line 17.
我希望它能打印出来
["some file"]
我试过留言
*unlink = \mock_unlink;
相反,但这并没有改变任何事情。
我需要如何嘲笑
取消链接
检查我的代码试图删除哪些文件?