Search code examples
raku

Raku: how to sort `dir` results by directory age?


I am trying to get a list of directories by age in Raku, which is equivalent to Bash ls -tl | grep ^dr but I am unsure how to sort the results by age, if this is even possible

for dir (test => {$_.IO.d}) -> $dir {...}

how can I sort these results by directory age?


Solution

  • I think what you're looking for is:

    dir(test => *.IO.d) .sort: *.changed
    

    Afaik the -t in ls -tl corresponds to the IO::Path method .changed

    Note that there's no space between dir and (test...). That lack of space is very important. In P6, foo(bar,baz) means something completely different than foo (bar,baz):

    # Call `foo` with two arguments `bar` and `baz`:
    foo(bar, baz);
    
    # Juxtapose identifier `foo` with a single `List` of 2 elements (bar, baz): 
    foo (bar,baz);
    
    # Juxtapose identifier `foo` with two `List`s: 
    foo (bar,baz), (qux, waldo);
    

    In the latter cases, if a symbol &foo has been declared (which is what a sub foo ... does) then foo will be called, just as it was with the first case, but this time it'll be with either one List (of two elements) as its only argument or two arguments (two Lists). If a symbol &foo has not been declared but a symbol foo has, then you'll get compile-time syntax error.