Perl $$var -- 两个美元符号作为符号? [英] Perl $$var -- two dollar signs as a sigil?

查看:59
本文介绍了Perl $$var -- 两个美元符号作为符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是我应该能够轻松使用 Google、Bing 或 DDG 的东西,但我对所有内容都一无所知.

This seems like something I should be able to easily Google, Bing, or DDG, but I'm coming up completely empty on everything.

基本上,我的任务是(部分)重写一组旧的 Perl 脚本.我自己不是 Perl 程序员,在阅读这些脚本以弄清楚它们在做什么时肯定有一个学习曲线,但我已经用这样的行撞到了绝对的砖墙:

Basically, I'm tasked with (in part) rewriting a set of old Perl scripts. Not being a Perl programmer myself, there's definitely a learning curve in reading these scripts to figure out what they're doing, but I've hit an absolute brick wall with lines like this one:

$comment = $$LOC{'DESCRIPTION'};

据我所知,右侧是一个字典,或者更准确地说,它正在获取由所述字典中的键 'DESCRIPTION' 引用的值.但是前面的额外"美元符号是怎么回事?

Near as I can tell, the right-hand side is a dictionary, or more precisely it's getting the value referenced by the key 'DESCRIPTION' in said dictionary. But what's with the "extra" dollar sign in front of it?

它看起来很像 PHP 的 变量变量,但是经过搜索引擎的搜索,各种StackExchange 站点和 perlvar,我找不到任何迹象表明 Perl 甚至这样的功能,更不用说这是调用它的方式了.我发现的最多的是 '$' 不是变量名中的有效字符,所以我知道 $$LOC 不仅仅是调用一个刚好被命名为 $LOC.

It looks suspiciously like PHP's variable variables, but after scouring search engines, assorted StackExchange sites, and perlvar, I can't find any indication that Perl even has such a feature, let alone that this is how it's invoked. The most I've turned up is that '$' is not a valid character in a variable name, so I know $$LOC is not merely calling up a variable that just happens to be named $LOC.

这个额外的美元符号在这里做什么?

What is this extra dollar sign doing here?

推荐答案

这是取消引用的另一种形式.可以(老实说可能应该)写成:

This is an alternative form of dereferencing. Could (and honestly probably should) be written as:

$LOC->{'DESCRIPTION'};

有时您会在其他所有基本数据类型中看到相同的情况:

You'll sometimes see the same with other all basic data types:

my $str = 'a string';
my %hash = (a => 1, b => 2);
my @array = (1..4);

my $strref = \$str;
my $hashref = \%hash;
my $arrayref = \@array;

print "String value is $$strref\n";
print "Hash values are either $$hashref{a} or $hashref->{b}\n";
print "Array values are accessed by either $$arrayref[0] or $arrayref->[2]\n";

这篇关于Perl $$var -- 两个美元符号作为符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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