gpt4 book ai didi

io - 在 Perl 6 REPL 中使用 chdir 的奇怪(内部?)默认构造函数错误消息

转载 作者:行者123 更新时间:2023-12-02 02:44:28 25 4
gpt4 key购买 nike

我遇到了以下错误:

&*chdir('/home/account')   

Failed to change the working directory to '/home/account': Default constructor for 'IO::Path' only takes named arguments

我需要更改什么才能克服此错误?

(我使用的是 perl6 版本 2013.12 Ubuntu 14.04)

最佳答案

2013.12 是 4.5 岁。我不建议用那个版本学习 Perl 6。请尝试获取更新的版本:它的文档将是最新的,并且速度将提高一个数量级(如果不是两个数量级的话)。

此外,为什么不使用 chdir 而不是 &*chdir?后者是古代遗留下来的东西,afaik。如果你只是 chdir,你会得到:

$ perl6 -e 'chdir("/home/account")'
Failed to change the working directory to '/home/account': does not exist

这肯定已经更容易理解了。

其次,如果 chdir 失败,它会返回一个 Failure。当一个 Failure 失败时(也就是在 Perl 5 术语中的“void”上下文中被调用),它将抛出它包含的 Exception。这就是您刚才看到的。

chdir 如果成功则返回一个 IO::Path 对象,它在 bool 上下文中为 True,例如 if 或三元:

$ perl6 -e 'say chdir("/home/account") ?? "Yeah!" !! "Alas"'
Alas

但在大多数情况下,如果您想在目录中执行某些操作,您将需要使用 indir( $path, { code to execute } ) .这将确保除了作为第二个参数给出的代码外,不会在该目录中执行任何代码。

关于io - 在 Perl 6 REPL 中使用 chdir 的奇怪(内部?)默认构造函数错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49989144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com