This talk will show how it's possible to mock PerlOps using XS and provide a convenient Pure Perl hook for each of the file check -X.
Overload::FileCheck provides a way to mock one or more file checks. It is also possible to mock stat/lstat functions using "mock_all_from_stat" and let Overload::FileCheck mock for you for any other -X checks.
16. PP ?
Push / Pop
pp ("push/pop") functions execute the opcodes
A typical pp function
- expects to find its arguments on the stack,
- and usually pushes its results onto the stack,
- hence the 'pp' terminology.
- Each OP structure contains a pointer to the relevant pp_foo() function.
32. -r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-e File exists.
-z File has zero size (is empty).
-s File has nonzero size (returns size in bytes).
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link (false if symlinks aren't
supported by the file system).
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
-T File is an ASCII or UTF-8 text file (heuristic guess).
-B File is a "binary" file (opposite of -T).
-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time
> perldoc -f -X
perldoc.perl.org/functions/-X.html
Overload::FileCheck -X
36. Test::MockFile -Todd Rinaldo
use Test::MockFile;
# Be sure to assign the output of mocks / scoped
my $mock_file = Test::MockFile->file(
"/foo/bar", “contents"
);
# Does not actually open the file on disk.
open( my $fh, "<", “/foo/bar" ) or die;
say "ok" if -e $fh;
close $fh;
say "ok" if -e "/foo/bar";
37. Test::MockFile strict mode
use Test::MockFile qw/strict/;
# This will not die.
my $file = Test::MockFile->file("/bar", "...");
my $symlink = Test::MockFile->symlink("/foo", "/bar");
-l "/foo" or print "okn";
open(my $fh, ">", "/foo");
# All of these will die
open(my $fh, ">", "/unmocked/file"); # Dies
sysopen(my $fh, "/other/file", O_RDONLY);
opendir(my $fh, "/dir");
-e "/file";
-l "/file"
38. ✤ better knowledge of PerlOPs
✤ mocking filesystem for testing
✤ B::C Lazy OPs
✤ B::C Lazy RegExp
✤ p5p improvement for CV
Lessons learned
39. The Perl Conference 2019 June 16-21
Images: Pixabay License, free for commercial use
Overloading Perl OPs using XS
ℕicolas ℝ.
@atoomic
thank you