在我的程序中,我将file names命令行列表传递给我的程序,并检查每个文件是否为-executable和..readablewritable
我正在使用语句来解决上述问题..但是使用和语句foreach-when似乎存在一些问题,这可能是我没有正确使用,但它给了我意想不到的结果..whendefault
这是我的代码: -
#!/perl/bin
use v5.14;
use warnings;
foreach (@ARGV) {
say "*************Checking file $_ *******************";
when (-r $_) { say "File is Readable"; continue; }
when (-w $_) { say "File is Writable"; continue; } # This condition is true
when (-x $_) { say "File is Executable" } # This condition is false
default { say "None of them" } # Executed
}
我continue只在前两个中添加了一个 ,when以使 perl 检查所有条件,而不管文件名如何。
另外,我没有continue在倒数第二个中添加 a when,因为我只希望default在没有执行的when情况下执行我的..
这里的问题是,如果最后一个when条件为 false,它不会进入块,然后它继续执行default即使我的前两条when语句都已满足。
我通过更改 my 的顺序检查了这个问题的原因,when发现如果只when执行最后一个,它会看到没有continue,因此它不会执行该default语句..
所以,在上面的代码中,我已经交换了-x..-r我的文件是可读的,所以最后when在这种情况下将被执行..然后我的default语句没有被执行..
#!/perl/bin
use v5.14;
use warnings;
foreach (@ARGV) {
say "*************Checking file $_ *******************";
when (-x $_) { say "File is Executable"; continue; }
when (-w $_) { say "File is Writable"; continue; }
when (-r $_) { say "File is Readable" } # This condition is true
default { say "None of them" } # Not executed
}
所以,我想问一下,如何处理这些情况..我希望它像given-when添加到 Perl 中的语句一样工作..
它应该检查所有,如果至少执行了一个,则when跳过..defaultwhen