Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl: mocking -d -f and friends. How to put them into CORE::GLOBAL
    text
    copied!<p>The CORE documentation has shown me how to merrily mock various built Perl functions. However, I'm not really sure how to replace '-d' &amp;c. with my methods. So this is really just a question on how do i replace a function with a dash in it in CORE::GLOBAL.</p> <p>A manual reference would be nice.</p> <pre><code>package Testing::MockDir; use strict; use warnings; use Exporter(); use Symbol 'qualify_to_ref'; *import = \&amp;Exporter::import; our @EXPORT_OK = qw(SetMockDir UnsetMockDir); our %EXPORT_TAGS = ( 'all' =&gt; \@EXPORT_OK, ); my %path2List = (); my %handle2List = (); BEGIN { *CORE::GLOBAL::opendir = \&amp;Testing::MockDir::opendir; *CORE::GLOBAL::readdir = \&amp;Testing::MockDir::readdir; *CORE::GLOBAL::closedir = \&amp;Testing::MockDir::closedir; ######################### the "-" is really the problem here *CORE::GLOBAL::-d = \&amp;Testing::MockDir::mock_d; # This does not work &lt;&lt;&lt;&lt;&lt; } 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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload