diff --git a/lib/Test/MockFile.pm b/lib/Test/MockFile.pm index deac888..9cdd2aa 100644 --- a/lib/Test/MockFile.pm +++ b/lib/Test/MockFile.pm @@ -2800,8 +2800,11 @@ sub _io_file_mock_open { return; } + # Cache contents() — it's non-trivial for directories (scans %files_being_mocked). + my $contents = $mock_file->contents(); + # If contents is undef and reading, file doesn't exist - if ( !defined $mock_file->contents() && grep { $mode eq $_ } qw/< +contents() ) { + if ( defined $contents ) { # Existing file: check file permissions my $need = 0; $need |= 4 if $rw =~ /r/; @@ -3136,8 +3139,11 @@ sub __open (*;$@) { return undef; } + # Cache contents() — it's non-trivial for directories (scans %files_being_mocked). + my $contents = $mock_file->contents(); + # If contents is undef, we act like the file isn't there. - if ( !defined $mock_file->contents() && grep { $mode eq $_ } qw/< +contents() ) { + if ( defined $contents ) { my $need = 0; $need |= 4 if $rw =~ /r/; $need |= 2 if $rw =~ /w/;