On 2020-05-16 05:34, Kevin Pye wrote:
The documentation for .d states:

Returns |True| if the invocant is a path that exists and is a directory. The method will |fail| <https://docs.raku.org/routine/fail> with |X::IO::DoesNotExist| if the path points to a non-existent filesystem entity.

(and by implication False if the file exists and is not a directory). In other words it is behaving exactly as specified.

If you want to allow for the possibility that the file does not exist at all, then you either need to catch the exception, or check for file existence first.

Something like
'p7lib'.IO ~~ :e & :d
might work, although I haven't tested it.

Kevin.


Hi Keven,

I had forgotten the -e in my Windows script
as I have it build in to my alias of P6 in Linux.
It was a real egg on the face moment.

From my point of view, IO.d giving True or crash
makes it pretty worthless.  But there are
two workaround:

IO.e also works on directories

And if you tack a .Bool on the end of IO.d it
gives you back a True or False

My notes from my perl6.IO.txt keeper:


Exists (directory):

   Warning: if you don't tack the Bool at the end, IO.d will return
            True or Crash

   Note: IO.e also works on directories and is safer to use.

   DIRPATH.IO.d.Bool;
       if not $WorkingDir.IO.d.Bool { mkdir $WorkingDir, 0o766; }
       Note: do not use {} around the variable

       $ p6 'say "zef.git".IO.d.Bool;'
       True

       >raku -e "say 'h:/NotMyDocsBackup'.IO.d.Bool"
       False

   Workaround: send your command to a batch file and run the batch file


Exists (file):
   FILEPATH.IO.e
if $NewFileName.IO.e { $NewFileSize = $NewFileName.IO.s; } else { return 4; }

   Note: IO.e.Bool misses hidden files
         https://github.com/rakudo/rakudo/issues/3594

         IO.e also works on directories



-T

Reply via email to