Perl 解引用语法 [英] Perl Dereferencing Syntax

查看:17
本文介绍了Perl 解引用语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Perl 中取消引用引用的语法是什么?

解决方案

只要可以使用变量的名称,就可以使用计算结果为引用的块.例如,以下是指定数组的有效语法:

@NAME # 如果你有名字,例如@大批@BLOCK # 如果您有参考资料,例如@{ $ref }

这是下表中说明的circumfix 语法".

另一种语法通俗地称为箭头符号"更容易阅读,但在某些情况下需要 Perl 5.20+.这是下表中说明的后缀语法".

<前>直接 使用参考文献 使用参考文献Circumfix 语法[1] 后缀语法标量 本身 $s ${$sr} $sr->$* [3]数组 本身 @a @{$ar} $ar->@* [3]数组 元素 $a[0] ${$ar}[0] $ar->[0]数组 切片 @a[0,1,2] @{$ar}[0,1,2] $ar->@[0,1,2][3]数组 索引/值切片[2] %a[0,1,2] %{$ar}[0,1,2]$ar->%[0,1,2] [3]数组 最后一个索引 $#a $#{$ar} $ar->$#* [3]hash 本身 %h %{$hr} $hr->%* [3]hash element $h{'a'} ${$hr}{'a'} $hr->{'a'}hash slice @h{'a','b','c'} @{$hr}{'a','b','c'} $hr->@{'a','b','c'} [3]hash 键/值切片[2] %h{'a','b','c'} %{$hr}{'a','b','c'} $hr->%{'a','b','c'} [3]代码 调用 func(@args)代码 忽略原型 &func(@args) &{$cr}(@args) $cr->(@args)代码 w/继承@_ &func &{$cr} $cr->&* [3]glob 本身 *glob *{$gr} $gr->** [3]glob slot *glob{ARRAY} *{$gr}{ARRAY} $gr->*{ARRAY} [3]

注意事项:

  1. $sr$ar$hr$cr$cr 周围的大括号$gr 当卷曲的内容只是一个标量时是可选的.

    如果使用它们,这些卷曲会形成块,因此它们实际上可以包含多个语句.

  2. 需要 Perl 5.20+.

  3. 需要 Perl 5.24+.在 Perl 5.20+ 中可用,通过添加 use feature qw( postderef );no warnings qw(experimental::postderef );,或添加 use Experimental qw( postderef );.这是安全的,因为当时的实验性功能已被 Perl 接受而无需更改.

另见:

What is the syntax to dereference a reference in Perl?

解决方案

Whenever you can use the name of a variable, you can use a block that evaluates to a reference instead. For example, the following are valid syntax for specifying an array:

@NAME    # If you have the name      e.g. @array
@BLOCK   # If you have a reference   e.g. @{ $ref }

This is the "circumfix syntax" illustrated in the table below.

An alternative syntax colloquially known as the "arrow notation" is easier to read, but requires Perl 5.20+ in some cases. This is the "postfix syntax" illustrated in the table below.

                              Direct           Using References     Using References
                                               Circumfix Syntax[1]   Postfix Syntax

scalar  itself                $s               ${$sr}               $sr->$* [3]

array   itself                @a               @{$ar}               $ar->@* [3]
array   element               $a[0]            ${$ar}[0]            $ar->[0]
array   slice                 @a[0,1,2]        @{$ar}[0,1,2]        $ar->@[0,1,2] [3]
array   index/value slice[2]   %a[0,1,2]        %{$ar}[0,1,2]        $ar->%[0,1,2] [3]
array   last index            $#a              $#{$ar}              $ar->$#* [3]

hash    itself                %h               %{$hr}               $hr->%* [3]
hash    element               $h{'a'}          ${$hr}{'a'}          $hr->{'a'}
hash    slice                 @h{'a','b','c'}  @{$hr}{'a','b','c'}  $hr->@{'a','b','c'} [3]
hash    key/value slice[2]     %h{'a','b','c'}  %{$hr}{'a','b','c'}  $hr->%{'a','b','c'} [3]

code    call                  func(@args)
code    w/ prototype ignored  &func(@args)     &{$cr}(@args)        $cr->(@args)
code    w/ inherited @_       &func            &{$cr}               $cr->&* [3]

glob    itself                *glob            *{$gr}               $gr->** [3]
glob    slot                  *glob{ARRAY}     *{$gr}{ARRAY}        $gr->*{ARRAY} [3]

Notes:

  1. The curly brackets around $sr, $ar, $hr, $cr and $gr are optional when the contents of the curlies is simply a scalar.

    If they are used, those curlies form blocks, so they can actually contain multiple statements.

  2. Requires Perl 5.20+.

  3. Requires Perl 5.24+. Available in Perl 5.20+ by adding both use feature qw( postderef ); and no warnings qw( experimental::postderef );, or by adding use experimental qw( postderef );. This is safe because the then-experimental feature was accepted into Perl without change.

See also:

这篇关于Perl 解引用语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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