什么时候在 Perl 中使用花括号取消引用有好处? [英] When is it advantageous to use curly braces for dereferencing in Perl?

查看:52
本文介绍了什么时候在 Perl 中使用花括号取消引用有好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到一段代码是这样的~

I saw a bit of code that went like this~

my @array = @{ $array_ref };

而且我发现如果我把它改成~

And I found that it works just the same if I change it to~

my @array = @$array_ref;

同样,这个~

my %hash = (frogs => sub {print "Frogs\n"});
&{$hash{frogs}}();

效果一样~

%hash = (frogs => sub {print "Frogs\n"});
$hash{frogs}();

(虽然我最初尝试这样做,因为 &hash{frogs}(); 希望它可以工作,结果出现了我不明白的错误——印记不应该与事物相对应吗?在散列中访问,在这种情况下 & 用于子例程?)

(Although I initially tried that as &hash{frogs}(); expecting it to work, with a resulting error I did not understand-- shouldn't the sigil correspond to the thing being accessed in the hash, in this case & for a subroutine?)

所以我想知道为什么这些片段的作者会以另一种方式编写它们,因为它是额外的字符,我认为没有任何优势.

So I am wondering why the authors of these snippets would have written them the other way, when it's extra characters without any advantage that I can perceive.

这里有什么我没有注意到的事情吗?什么时候可能会出现需要使用花括号而不仅仅是符号的情况?

Is something going on here which I'm not noticing? When might there be a situation where you need to use the curly braces instead of just the sigil?

推荐答案

你混淆了两个概念.

您需要了解的第一件事是两种语法用于尊重.

The first thing you need to learn is that there is there are two syntaxes for deferencing.

 Circumfix            Postfix
 "Block syntax"       "Arrow syntax"

 $BLOCK               EXPR->$*
 @BLOCK               EXPR->@*
 $BLOCK[$i]           EXPR->[$i]
 &BLOCK()             EXPR->()
 ...                  ...

此外,两种语法都可以简化.

Furthermore, both syntax have a simplification available to them.

您的数组示例是块语法可用的简化示例.如果块只包含一个简单的标量 ($ref),则可以省略花括号.

Your array example is an example of a simplification available to the block syntax. If the block contains nothing but a simple scalar ($ref), you can omit the curlies.

${ $ref }                   # Can be simplified to $$ref
@{ $ref }                   # Can be simplified to @$ref
${ $ref }[$i]               # Can be simplified to $$ref[$i]
&{ $ref }()                 # Can be simplified to &$ref()

如果块包含其他任何内容,您将无法利用这种简化.

If the block contains anything else, you can't avail yourself of this simplification.

@{ $refs{$key} }            # Can't be simplified
@{ f() }                    # Can't be simplified
@{ my $ref = f(); $ref }    # Can't be simplified

&{ $refs{$key} }()          # Can't be simplified
&{ f() }()                  # Can't be simplified
&{ my $ref = f(); $ref }    # Can't be simplified

<小时>

另一方面,您的代码引用示例是从块语法切换到箭头语法的示例.这不是简化本身.

"Block syntax"               "Arrow syntax"
&{ $hash{frogs} }()    ⇒     $hash{frogs}->()

<小时>

您的代码引用示例也是箭头语法可用的简化示例.当箭头在 [...]{...}[...] 之间时,{...}(...),箭头可以省略.


Your code ref example is also an example of a simplification available to the arrow syntax. When the arrow is between [...] or {...}, and [...], {...} or (...), the arrow can be omitted.

$ref->{$k}->[$i]            # Can be simplified to $ref->{$k}[$i]
$ref->{$k}->{$l}            # Can be simplified to $ref->{$k}{$l}
$ref->{$k}->()              # Can be simplified to $ref->{$k}()

$ref->[$i]->[$j]            # Can be simplified to $ref->[$i][$j]
$ref->[$i]->{$k}            # Can be simplified to $ref->[$i]{$k}
$ref->[$i]->()              # Can be simplified to $ref->[$i]()

<小时>

最后,您应该使用哪个?


Finally, which should you use?

选择块语法还是箭头语法是个人喜好之一,但通常遵循以下约定以最大限度地提高可读性:

The choice of whether to the block syntax or the arrow syntax is one of personal preference, but the following conventions are usually followed to maximize readability:

  • 块语法是标量、数组和散列解引用的首选.

  • The block syntax is preferred for scalar, array and hash dereferences.

$$ref                is generally preferred over     $ref->$*
@$ref                is generally preferred over     $ref->@*
%$ref                is generally preferred over     $ref->%*

  • 当使用块语法时,尽可能省略卷曲.

  • When using the block syntax, curlies are omitted if possible.

    $$ref                is generally preferred over     ${ $ref }
    @$ref                is generally preferred over     @{ $ref }
    %$ref                is generally preferred over     %{ $ref }
    

  • 箭头语法是数组元素、哈希元素和代码取消引用的首选.

  • The arrow syntax is preferred for array element, hash element and code dereferences.

    $ref->[...]          is generally preferred over     $$ref[...]
    $ref->{...}          is generally preferred over     $$ref{...}
    $ref->(...)          is generally preferred over     &$ref(...)
    

  • 箭头本身在可能的情况下被省略,除非用于代码.

  • The arrow itself is omitted when possible, except when used to deference code.

    $ref->[...][...]     is generally preferred over     $ref->[...]->[...]
    $ref->[...]{...}     is generally preferred over     $ref->[...]->{...}
    $ref->[...]->(...)   is generally preferred over     $ref->[...](...)
    $ref->{...}[...]     is generally preferred over     $ref->{...}->[...]
    $ref->{...}{...}     is generally preferred over     $ref->{...}->{...}
    $ref->{...}->(...)   is generally preferred over     $ref->{...}(...)
    

  • 数组切片和散列切片首选块语法.它的可读性远不如箭头语法,但这些箭头语法需要 Perl 5.24.

  • The block syntax is preferred for array slices and hash slices. It's far less readable than the arrow syntax, but the arrow syntax for these requires Perl 5.24.

    @$ref[...]           is generally preferred over     $ref->@[...]
    @$ref{...}           is generally preferred over     $ref->@{...}
    

  • 这篇关于什么时候在 Perl 中使用花括号取消引用有好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆