Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my class IO::File is Cool does IO::FileTestable {
- has Str $.file;
- has Str $.dir = '.';
- multi method Str(IO::File:D:) {
- self.file;
- }
- multi method gist(IO::File:D:) {
- "IO::File<$.path>";
- }
- multi method Numeric(IO::File:D:) {
- self.file.Numeric;
- }
- multi method Bridge(IO::File:D:) {
- self.file.Bridge;
- }
- multi method Int(IO::File:D:) {
- self.file.Int;
- }
- method path(IO::File:D:) {
- $.dir eq '.' ?? $.file !! join('/', $.dir, $.file);
- }
- method open(IO::File:D: *%opts) {
- open($.path, |%opts);
- }
- }
- sub dir(Cool $path = '.', Mu :$test = none('.', '..')) {
- my Mu $RSA := pir::new__PS('OS').readdir(nqp::unbox_s($path.Str));
- my int $elems = pir::set__IP($RSA);
- my @res;
- loop (my int $i = 0; $i < $elems; $i = $i + 1) {
- my Str $file := nqp::p6box_s(nqp::atpos($RSA, $i));
- @res.push: IO::File.new(:$file, :dir($path)) if $test.ACCEPTS($file);
- }
- return @res;
- CATCH {
- default {
- X::IO::Dir.new(
- :$path,
- os-error => .Str,
- ).throw;
- }
- }
- }
- for dir() {
- .say if .f && /<+[A..Z]>/;
- }
Add Comment
Please, Sign In to add comment