Guest User

Untitled

a guest
Nov 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. my class IO::File is Cool does IO::FileTestable {
  2. has Str $.file;
  3. has Str $.dir = '.';
  4.  
  5. multi method Str(IO::File:D:) {
  6. self.file;
  7. }
  8. multi method gist(IO::File:D:) {
  9. "IO::File<$.path>";
  10. }
  11. multi method Numeric(IO::File:D:) {
  12. self.file.Numeric;
  13. }
  14. multi method Bridge(IO::File:D:) {
  15. self.file.Bridge;
  16. }
  17. multi method Int(IO::File:D:) {
  18. self.file.Int;
  19. }
  20.  
  21. method path(IO::File:D:) {
  22. $.dir eq '.' ?? $.file !! join('/', $.dir, $.file);
  23. }
  24.  
  25. method open(IO::File:D: *%opts) {
  26. open($.path, |%opts);
  27. }
  28. }
  29.  
  30.  
  31. sub dir(Cool $path = '.', Mu :$test = none('.', '..')) {
  32. my Mu $RSA := pir::new__PS('OS').readdir(nqp::unbox_s($path.Str));
  33. my int $elems = pir::set__IP($RSA);
  34. my @res;
  35. loop (my int $i = 0; $i < $elems; $i = $i + 1) {
  36. my Str $file := nqp::p6box_s(nqp::atpos($RSA, $i));
  37. @res.push: IO::File.new(:$file, :dir($path)) if $test.ACCEPTS($file);
  38. }
  39. return @res;
  40.  
  41. CATCH {
  42. default {
  43. X::IO::Dir.new(
  44. :$path,
  45. os-error => .Str,
  46. ).throw;
  47. }
  48. }
  49. }
  50.  
  51. for dir() {
  52. .say if .f && /<+[A..Z]>/;
  53. }
Add Comment
Please, Sign In to add comment