See Original text in context
multi sub dir(*)multi sub dir(IO::Path , |c)multi sub dir(IO() , |c)method dir(IO::Path: Mu : = .curupdir)
Returns the contents of a directory as a lazy list of IO::Path
objects representing relative paths, filtered by smartmatching their names (as strings) against the :test
parameter. The path of returned files will be absolute or relative depending on what $path
is.
Since the tests are performed against Str
arguments, not IO
, the tests are executed in the $*CWD
, instead of the target directory. When testing against file test operators, this won't work:
dir('mydir', test => )
while this will:
dir('mydir', test => )
NOTE: a dir
call opens a directory for reading, which counts towards maximum per-process open files for your program. Be sure to exhaust returned Seq before doing something like recursively performing more dir
calls. You can exhaust it by assigning to a @-
sigiled variable or simply looping over it. Note how examples below push further dirs to look through into an Array, rather than immediately calling dir
on them. See also IO::Dir
module that gives you finer control over closing dir handles.
Examples:
# To iterate over the contents of the current directory:for dir() -># As before, but include even '.' and '..' which are filtered out by# the default :test matcher:for dir(test => *) -># To get the names of all .jpg and .jpeg files in the home directory of the current user:my = .dir: test => /:i '.' jpe?g $/;
An example program that lists all files and directories recursively:
sub MAIN( = '.')
A lazy way to find the first three files ending in ".p6" recursively starting from the current directory:
my = '.'.IO;my = gather while.put for [^3];