Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Overloading Perl OPs using XS

155 views

Published on

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.

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Overloading Perl OPs using XS

  1. 1. The Perl Conference 2019 June 16-21 Overloading Perl OPs using XS ℕicolas ℝ. @atoomic
  2. 2. The Perl Conference 2019 June 16-21 ℕicolas ℝ.
  3. 3. The Perl Conference 2019 June 16-21 ℕicolas ℝ. @atoomic
  4. 4. The Perl Conference 2019 June 16-21 ℕicolas ℝ. @atoomic
  5. 5. The Perl Conference 2019 June 16-21 Overloading Perl OPs using XS ℕicolas ℝ. @atoomic
  6. 6. Overloading Perl OPs using XS
  7. 7. Overloading… a.k.a. mocking
  8. 8. Overloading
  9. 9. Overloading
  10. 10. …Perl OPs… sample OP Tree
  11. 11. Printing the OPTree - B::Terse > perl -MO=Terse,-exec -e '$a = $b + 42’ | perl -p -e 's/(.*)//'; -e syntax OK OP enter COP nextstate SVOP *b SVOP 42 BINOP add [1] SVOP *a BINOP sassign LISTOP leave [1]
  12. 12. Printing the OPTree - B::Concise > perl -MO=Concise,-exec -e '$a = $b + 42' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> gvsv(*b) s 4 <$> const(IV 42) s 5 <2> add[t1] sK/2 6 <$> gvsv(*a) s 7 <2> sassign vKS/2 8 <@> leave[1 ref] vKP/REFC -e syntax OK
  13. 13. Perl OPs > perl -MO=Concise,-exec -e '$f' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> gvsv(*f) s 4 <1> srefgen vK/1 5 <@> leave[1 ref] vKP/REFC
  14. 14. Perl OPs - opnames.h
  15. 15. Perl OPs: opcode.h
  16. 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.
  17. 17. Perl_PP_ ? > grep 'PP(' pp.c | sort | head PP(pp_abs) PP(pp_aeach) PP(pp_akeys) PP(pp_anoncode) PP(pp_anonconst) PP(pp_anonhash) PP(pp_anonlist) PP(pp_argcheck) PP(pp_argdefelem) PP(pp_argelem)
  18. 18. Perl_PP_ ?
  19. 19. Perl OPs > perl -MO=Concise,-exec -e '$f' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> gvsv(*f) s 4 <1> srefgen vK/1 5 <@> leave[1 ref] vKP/REFC
  20. 20. Perl OPs > perl -MO=Concise,-exec -e '$f' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> gvsv(*f) s 4 <1> srefgen vK/1 5 <@> leave[1 ref] vKP/REFC
  21. 21. Overloading Perl OPs…
  22. 22. …using XS the glue between C and Perl API
  23. 23. Overloading Perl OPs using XS Why ? Why ? Why ? Why ? Why ?
  24. 24. OP_DIE pp_sys.c:456:PP(pp_die) opnames.h:209: OP_DIE = 192, *GLOBAL::CORE::die
  25. 25. OP_DIE > perl die.pl This is the end at die.pl line 8.
  26. 26. OP_DIE > perl -MO=Terse,-exec die.pl | perl -pe ’s/(.*)/t/' OP enter COP nextstate LOOP enterloop REDO => COP nextstate SVOP UNOP srefgen SVOP *CORE::GLOBAL::die UNOP rv2gv BINOP sassign LAST => BINOP leaveloop COP nextstate OP pushmark SVOP "This is the end" LISTOP die [2] COP nextstate OP pushmark SVOP "...or not" LISTOP say LISTOP leave [1]
  27. 27. OP_DIE > perl die.pl Still Alive... ...or not
  28. 28. OP_DIE > perl -MO=Terse,-exec die.pl | perl -pe ’s/(.*)/t/' OP enter COP nextstate OP pushmark SVOP "This is the end" SVOP *CORE::GLOBAL::die UNOP entersub COP nextstate OP pushmark SVOP "...or not" LISTOP say LISTOP leave [1]
  29. 29. Not all OPs can be mocked using CORE::GLOBAL::* How to mock ‘-e’? -X?
  30. 30. Not all OPs can be mocked using CORE::GLOBAL::*
  31. 31. Overloading `-e` check github.com/atoomic/Overload-FileCheck/tree/demo Code Demo
  32. 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
  33. 33. Overload::FileCheck -X -r -w -x -o -R -W -X -O -e -z -s -f -d -l -p -S -b -c -t -u -g -k -T -B -M -A -C * boolean / integer * using stat * stat($f) && -e _
  34. 34. Overload::FileCheck github.com/atoomic/Slides-Overload-FileCheck Bonus Slides
  35. 35. slides…
  36. 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. 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. 38. ✤ better knowledge of PerlOPs ✤ mocking filesystem for testing ✤ B::C Lazy OPs ✤ B::C Lazy RegExp ✤ p5p improvement for CV Lessons learned
  39. 39. The Perl Conference 2019 June 16-21 Images: Pixabay License, free for commercial use Overloading Perl OPs using XS ℕicolas ℝ. @atoomic thank you

×