txt: ## indir.md
  indir
  
  Combined from primary sources listed below.
  
  # [In Independent routines](#___top "go to top of document")[§](#(Independent_routines)_sub_indir "direct link")
  
  See primary documentation
  [in context](/type/independent-routines#sub_indir)
  for **sub indir**.
  
  ```raku
  sub indir(IO() $path, &code, :$d = True, :$r, :$w, :$x)
  ```
  
  Takes [`Callable`](/type/Callable) `&code` and executes it after locally (to `&code`) changing `$*CWD` variable to an [`IO::Path`](/type/IO/Path) object based on `$path`, optionally ensuring the new path passes several file tests. If `$path` is relative, it will be turned into an absolute path, even if an [`IO::Path`](/type/IO/Path) object was given. **NOTE:** that this routine does *NOT* alter the process's current directory (see [`&*chdir`](/routine/&*chdir)). The `$*CWD` outside of the `&code` is not affected, even if `&code` explicitly assigns a new value to `$*CWD`.
  
  Returns the value returned by the `&code` call on success. On failure to successfully change `$*CWD`, returns [`Failure`](/type/Failure). **WARNING:** keep in mind that lazily evaluated things might end up NOT having the `$*CWD` set by `indir` in their dynamic scope by the time they're actually evaluated. Either ensure the generators have their `$*CWD` set or [eagerly evaluate](/routine/eager) them before returning the results from `indir`:
  
  ```raku
  say indir("/tmp", {
      gather { take ".".IO }
  })».CWD; # OUTPUT: «(/home/camelia)␤»
  
  say indir("/tmp", {
      eager gather { take ".".IO }
  })».CWD; # OUTPUT: «(/tmp)␤»
  
  say indir("/tmp", {
      my $cwd = $*CWD;
      gather { temp $*CWD = $cwd; take ".".IO }
  })».CWD; # OUTPUT: «(/tmp)␤»
  ```
  
  The routine's `$path` argument can be any object with an IO method that returns an [`IO::Path`](/type/IO/Path) object. The available file tests are:
  
  * `:d` — check [`.d`](/routine/d) returns `True`
  * `:r` — check [`.r`](/routine/d) returns `True`
  * `:w` — check [`.w`](/routine/d) returns `True`
  * `:x` — check [`.x`](/routine/d) returns `True`
  
  By default, only `:d` test is performed.
  
  ```raku
  say $*CWD;                   # OUTPUT: «"/home/camelia".IO␤»
  indir '/tmp', { say $*CWD }; # OUTPUT: «"/tmp".IO␤»
  say $*CWD;                   # OUTPUT: «"/home/camelia".IO␤»
  
  indir '/not-there', {;};     # returns Failure; path does not exist
  ```
  