简体   繁体   English

Perl除非在哈希声明中声明 - 是否可能?

[英]Perl unless statement in hash declaration - is it possible?

I'm working on a hash table to help iterate over permissions to a file. 我正在使用哈希表来帮助迭代文件的权限。

I want to flag $permissions{$perm}[0] as 0 if the test is not successful, however I am not sure if this is even possible? 如果测试不成功,我想将$permissions{$perm}[0]标记为0 ,但是我不确定这是否可能?

        my %permissions = (
            'w'     => {    'write'         => '0' unless (-w $file) }, #dies here
            'r'     => {    'read'          => '0' unless (-r $file) },
            'x'     => {    'execute'       => '0' unless (-x $file) },
    );

It fails with syntax error near "'0' unless" . 它失败, syntax error near "'0' unless"

Is there a way to do this inside the hash declaration? 有没有办法在哈希声明中执行此操作?

That should be equivalent of what you want 这应该等同于你想要的

my %permissions = (
        'w'     => { 'write'         => -w $file ? 1 : 0 },
);

or shorter, 或更短,

        'w'     => { 'write'         => (-w $file) +0 },

Unless there is more to your hash than you have said, it is pointless having two levels of hash as it just means you will have to write $permissions{r}{read} , $permissions{w}{write} etc. I suggest it's best to just use the first level key alone. 除非你的哈希值比你说的要多,否则有两个哈希值是毫无意义的,因为它只意味着你必须写$permissions{r}{read}$permissions{w}{write}等我建议最好只使用第一级密钥。

In addition, as you will probably be using the hash values as booleans in something like 另外,因为您可能会将哈希值用作类似的布尔值

if ( $permissions{x} ) {
   # Code that runs $file
}

you may as well just store the values returned by the file tests instead of converting them to 1 and 0. 您也可以只存储文件测试返回的值,而不是将它们转换为1和0。

So your hash becomes 所以你的哈希变成了

my %permissions = (
   w => -w $file,
   r => -r $file,
   x => -x $file,
)

Which will use values of 1 for true and the null string '' for false . 将使用值1 ,空字符串''

Or you could just assign a hash slice 或者你可以只分配一个哈希切片

my %permissions;
@permissions{qw/ r w x /} = (-r, -w, -x) for $file;

which creates the same structure, because the file test operators use the value of $_ if you don't supply a parameter. 这会创建相同的结构,因为如果您不提供参数,文件测试运算符将使用$_的值。

Such construct is not permitted, you can use the unless clause only at the end of a statement. 不允许使用此类构造,只能在语句末尾使用unless子句。

You can use the ternary operator: 您可以使用三元运算符:

my %permissions = (
    'w' => { 'write' => (-w $file ? 'some_value' : 0 ) },
    'r' => { 'read' => (-r $file ? 'some_value' : 0 ) },
    'x' => { 'execute' => (-x $file ? 'some_value' : 0 ) },
)

The correct ways to do this have already been shown here but I thought I'd write a bit about why the example given doesn't work 这里已经显示了正确的方法,但我想我会写一些关于为什么给出的例子不起作用的原因

  1. It's nesting hashes when not needed 它在不需要时嵌套哈希
  2. It's a syntax error to use unless with an expression inside a list, hash reference constructor in this case. 这是一个语法错误,除非在列表中包含表达式,在这种情况下是哈希引用构造函数。 It only works with normal statements 它只适用于普通语句

Here's how the example code could be made valid 以下是示例代码的有效性

my %permissions = (
    w     => do { 0 unless -w $file},
    r     => do { 0 unless -r $file},
    x     => do { 0 unless -x $file});

This works because a do block contains statements but is itself an expression 这是有效的,因为do块包含语句,但它本身就是一个表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM