加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

%$var是否取消引用Perl哈希?

发布时间:2020-12-15 21:50:29 所属栏目:大数据 来源:网络整理
导读:我正在向子程序发送一个哈希,并用我的($arg_ref)= @_获取它; 但究竟是什么%$arg_ref?是$$解除引用哈希? 解决方法 $arg_ref是一个标量,因为它使用$sigil.据推测,它拥有哈希引用.所以是的,%$arg_ref推断了哈希引用.写它的另一种方法是%{$arg_ref}.这使得
我正在向子程序发送一个哈希,并用我的($arg_ref)= @_获取它;

但究竟是什么%$arg_ref?是$$解除引用哈希?

解决方法

$arg_ref是一个标量,因为它使用$sigil.据推测,它拥有哈希引用.所以是的,%$arg_ref推断了哈希引用.写它的另一种方法是%{$arg_ref}.这使得代码的意图更加清晰,尽管更加冗长.

引用perldata(1):

Scalar values are always named with '$',even when referring
 to a scalar that is part of an array or a hash.  The '$'
 symbol works semantically like the English word "the" in
 that it indicates a single value is expected.

     $days               # the simple scalar value "days"
     $days[28]           # the 29th element of array @days
     $days{'Feb'}        # the 'Feb' value from hash %days
     $#days              # the last index of array @days

所以你的例子是:

%$arg_ref           # hash dereferenced from the value "arg_ref"

我的($arg_ref)= @_;获取函数参数堆栈中的第一项,并将其放在名为$arg_ref的局部变量中.调用者负责传递哈希引用.一种更规范的写作方式是:

my $arg_ref = shift;

要创建哈希引用,您可以从哈希开始:

some_sub(%hash);

或者您可以使用匿名哈希引用创建它:

some_sub({pi => 3.14,C => 4}); # Pi is a gross approximation.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读