BEGIN {
no strict 'refs';
*{'CORE::GLOBAL::-e'} = sub {
my ($name) = @_;
warn "trying mocked -e\n";
return exists $file{$name};
};
} [snip]
C:\temp>perl foo.pl
ok 1 - file foo exists
not ok 2 - file bar exists
# Failed test 'file bar exists'
# at foo.pl line 20.
1..2
# Looks like you failed 1 test of 2.
Hmmm ... where is the output of 'warn "trying mocked -e\n";'?
Maybe you have 'foo' in your filesystem?
Yep, I believe you're right. That's what you get out of posting when
your eyes just can hardly stay open! [End of *standard* disclaimer...]
Actually, now that I think of it, I don't know if -X functions are
overridable, and you made me discover something interesting: people
generally check the prototype() of CORE:: functions because IF they
are not ovverridable THEN it returns undef() - but then please note
that the inverse implication does not hold[*]. Now, I tried to see
what happens with -e() and it turns out that it gives a run-time error
I had *never* seen:
whisky:~ [09:58:08]$ perl -E 'say prototype "CORE::$_" // "undef"
;$
undef
Can't find an opnumber for "-e" at -e line 2.
Anyway, as they say, the proof of the pudding is in the eating: the
above in fact would imply that -X functions are *not* overridable.
As far as your problem is concerned, I thought that perhaps -X's would
use stat() behind the courtain and that you may override the latter,
(at the expense of some flexibility,) which is doable. But that's not
the case:
whisky:~/test [10:38:06]$ ls
foo.pl
whisky:~/test [10:38:09]$ cat foo.pl
#!/usr/bin/perl
use strict;
use warnings;
my %file;
BEGIN {
no strict 'refs';
*{'CORE::GLOBAL::stat'} = sub {
warn "trying mocked stat()\n";
(my $f)=@_;
CORE::stat( @_ && !ref($f) &&
exists $file{$f} ? $0 : @_ );
};
}
use Test::More qw(no_plan);
$file{foo} = 1;
ok( (scalar stat $_) => "file $_ exists") for qw/foo bar/;
ok( (-e $_) => "file $_ exists") for qw/foo bar/;
__END__
whisky:~/test [10:38:13]$ ./foo.pl
trying mocked stat()
ok 1 - file foo exists
trying mocked stat()
not ok 2 - file bar exists
# Failed test 'file bar exists'
# at ./foo.pl line 21.
not ok 3 - file foo exists
# Failed test 'file foo exists'
# at ./foo.pl line 22.
not ok 4 - file bar exists
# Failed test 'file bar exists'
# at ./foo.pl line 22.
1..4
# Looks like you failed 3 tests of 4.
[*] E.g. require() returns undef() but I have *seen* it duly
overridden.
Michele