切换时,文件测试在给定的...中使用`$ _`或`_`?

Pab*_*cia 3 perl

使用文件测试操作符时,stat()结果将缓存在中,_因此,如果要执行多个测试,则无需执行更多系统调用.

if (-f $file_) {
  say 'readable' if -r _;
  say 'writable' if -w _;
}
Run Code Online (Sandbox Code Playgroud)

如果我在a中使用它们given…when,他们会使用_或$_吗?

given ($file) {
  when (! -f) {}
  when (-r) {say 'readable';continue}
  when (-w) {say 'writable';continue}
  ....
}
Run Code Online (Sandbox Code Playgroud)

我觉得这会使用$_.真的吗?此外,因为given…when没有默认的直通,所以有必要使用continue以查看其他匹配.

可能这种情况可以通过ifs 更好地解决,并且使用比使用该given…when方法更少的类型.但是,given…when在重构软件时,看起来是否合适是很诱人的.;-)

Axe*_*man 5

这就是它所摒弃的:

sub {
    use warnings;
    use strict 'refs';
    BEGIN {
        $^H{'feature_unicode'} = q(1);
        $^H{'feature_say'} = q(1);
        $^H{'feature_state'} = q(1);
        $^H{'feature_switch'} = q(1);
    }
    my $file = shift @_;
    given ($file) {
        when (not -f $_) {
            ();
        }
        when (-r $_) {
            say 'readable';
            continue;
        }
        when (-w $_) {
            say 'writable';
            continue;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此它不会在操作码中显示智能匹配.