CORE文檔向我展示瞭如何快速模擬各種構建的Perl函數。但是,我不確定如何替換'-d'& c。用我的方法。所以這實際上只是一個問題,我如何在CORE :: GLOBAL中用一個破折號替換一個函數。Perl:嘲笑-d -f和朋友。如何將它們放入CORE :: GLOBAL
手動引用會很好。
package Testing::MockDir;
use strict;
use warnings;
use Exporter();
use Symbol 'qualify_to_ref';
*import = \&Exporter::import;
our @EXPORT_OK = qw(SetMockDir UnsetMockDir);
our %EXPORT_TAGS = (
'all' => \@EXPORT_OK,
);
my %path2List =();
my %handle2List =();
BEGIN {
*CORE::GLOBAL::opendir = \&Testing::MockDir::opendir;
*CORE::GLOBAL::readdir = \&Testing::MockDir::readdir;
*CORE::GLOBAL::closedir = \&Testing::MockDir::closedir;
######################### the "-" is really the problem here
*CORE::GLOBAL::-d = \&Testing::MockDir::mock_d; # This does not work <<<<<
}
sub mock_d ($) {
die 'It worked';
}
sub SetMockDir {
my ($path, @files) = @_;
$path2List{$path} = [@files];
}
sub UnsetMockDir {
my ($path) = @_;
delete $path2List{$path};
}
sub opendir (*$) {
my $handle = qualify_to_ref(shift, caller);
my ($path) = @_;
return CORE::opendir($handle, $path) unless defined $path2List{$path};
$handle2List{$handle} = $path2List{$path};
return 1;
}
sub readdir (*) {
my $handle = qualify_to_ref(shift, caller);
return CORE::readdir($handle) unless defined $handle2List{$handle};
return shift @{$handle2List{$handle}} unless wantarray;
my @files = @{$handle2List{$handle}};
$handle2List{$handle} = [];
return @files;
}
sub closedir (*) {
my $handle = qualify_to_ref(shift, caller);
return CORE::closedir($handle) unless defined $handle2List{$handle};
delete $handle2List{$handle};
return 1;
}
1;
爲什麼'* import = \&Exporter :: import;'而不是'使用導出'導入';'? – Ether 2010-09-09 17:55:47