Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

raku

I am trying to get a list of directories by age in Perl6, 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?

like image 994
con Avatar asked Aug 07 '19 20:08

con


2 Answers

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.

like image 151
raiph Avatar answered Nov 17 '22 16:11

raiph


Use nqp::stat:

use nqp;
my @sorted = dir($*CWD).sort({ nqp::stat($_.Str, nqp::const::STAT_CREATETIME) });
like image 3
ugexe Avatar answered Nov 17 '22 16:11

ugexe