perl6 的统计函数

stat function for perl6

perl6 中是否有替代方法来获取文件属性详细信息,例如大小、access_time、modified_time.. 等而无需调用本机调用?

根据 doc,它“不太可能作为其 POSIX 特定 的内置实现”。

除了对 stat 的系统调用之外,还有哪些解决方法可用?

非常感谢任何想法或指示。

谢谢。

参见 the IO::Path doc

例如:

say 'foo'.IO.s; # 3 if 'foo' is an existing file of size 3 bytes

.IO 在字符串上创建一个 IO::Path 对象,该对象对应于字符串给定路径对应的文件系统条目。

the doc on ACCEPTS 查看使用联结同时获取多个属性的示例。


我不确定下面是不是太多了。如果是请忽略它。希望对您有所帮助。

您可以 discover/explore Perl 6 中的一些可用对象(又名 Higher Order Workings 对象、How Objects Work 对象、metaobjects——随便你怎么称呼它们),这些对象知道 HOW 对象特定类型的作品。

say IO::Path.^methods

显示:

(BUILD new is-absolute is-relative parts volume dirname basename extension
 Numeric sibling succ pred open watch absolute relative cleanup resolve
 parent child add chdir rename copy move chmod unlink symlink link mkdir
 rmdir dir slurp spurt lines comb split words e d f s l r w rw x rwx z
 modified accessed changed mode ACCEPTS Str gist perl IO SPEC CWD path BUILDALL)

这些是 IO::Path 对象可用的一些方法。

(您可以通过副词获得或多或少的信息,例如 say IO::Path.^methods(:all),但默认显示旨在为您提供您可能最感兴趣的副词。向上箭头(^ ) 表示方法调用 (.methods) 不会发送给调用者,而是发送 "upwards",直到其 HOW 对象,如上文所述。)

这是一次应用其中一些的示例:

spurt 'foo', 'bar'; # write a three letter string to a file called 'foo'. 
for <e d f s l r w rw x rwx z modified accessed changed mode>
  -> $method { say 'foo'.IO."$method"() }

第二行对 <...> 构造中按字符串名称列出的方法进行 for 循环。要在变量 $qux 中给定名称的调用者调用方法,请编写 ."$qux"(...).

在2021年寻找这个问题的答案时,有File::Stat模块。它提供了一些额外的 stat(2) 信息,例如 UIDGID 和模式。

#!/usr/bin/env raku
use File::Stat <stat>;
say File::Stat.new(path => $?FILE).mode.base(8);
say stat($?FILE).uid;
say stat($?FILE).gid;