Perl 中的 `$this`、`@that` 和 `%those` 有什么区别?
Perl 中的 $this
、@that
和 %those
之间有什么区别?
What is the difference between $this
, @that
, and %those
in Perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Perl 符号的有用助记符是:
Matt Trout 在 blog.fogus.me 上写了一篇关于 Perl 的精彩评论我认为有用的印记因此粘贴在下面:
A useful mnemonic for Perl sigils are:
Matt Trout wrote a great comment on blog.fogus.me about Perl sigils which I think is useful so have pasted below:
$this
是一个标量值,它包含 1 个项目,例如apple
@that
是一个值数组,它包含多个值,例如( "apple", "orange", "pear")
%those
是一个值的哈希值,它保存像("apple" => "red" 这样的键值对, "orange" => "orange", "pear" => "yellow")
参见 perlintro 了解有关 Perl 变量类型的更多信息。
$this
is a scalar value, it holds 1 item likeapple
@that
is an array of values, it holds several like("apple", "orange", "pear")
%those
is a hash of values, it holds key value pairs like("apple" => "red", "orange" => "orange", "pear" => "yellow")
See perlintro for more on Perl variable types.
Perl 的发明者是一位语言学家,他力图使 Perl 成为一种“自然语言”。
来自这篇文章:
通过数字、大小写和单词消除歧义 。
一种语言能够摆脱某些局部歧义的部分原因是其他歧义受到各种机制的抑制 英语使用数字和词序,代词中带有格系统的痕迹:“那个男人看着男人,他们也看着他。”这句话中非常清楚谁对谁做了什么。类似地,Perl 在其名词上有数字标记;也就是说,$dog 是一只狗,@dog(可能)是很多只。 所以$和@有点像英语中的“this”和“these”。 [强调]
Perl's inventor was a linguist, and he sought to make Perl like a "natural language".
From this post:
Disambiguation by number, case and word order
Part of the reason a language can get away with certain local ambiguities is that other ambiguities are suppressed by various mechanisms. English uses number and word order, with vestiges of a case system in the pronouns: "The man looked at the men, and they looked back at him." It's perfectly clear in that sentence who is doing what to whom. Similarly, Perl has number markers on its nouns; that is, $dog is one pooch, and @dog is (potentially) many. So $ and @ are a little like "this" and "these" in English. [emphasis added]
人们经常尝试将符号与变量类型联系起来,但它们只是松散相关。这是我们在学习 Perl 和 有效的 Perl 编程 因为当您了解 sigils 时,理解 Perl 会容易得多。
许多人忘记了变量和数据实际上是不同的东西。变量可以存储数据,但使用数据不需要变量。
$
表示单个标量值(不一定是标量变量):@
表示多个值。这可能是整个数组、一个切片或取消引用:%
表示对(键和值),它可能是哈希变量或取消引用:在 Perl v5.20 下,
%
现在可以表示 键/值切片或者哈希或数组:People often try to tie sigils to variable types, but they are only loosely related. It's a topic we hit very hard in Learning Perl and Effective Perl Programming because it's much easier to understand Perl when you understand sigils.
Many people forget that variables and data are actually separate things. Variables can store data, but you don't need variables to use data.
The
$
denotes a single scalar value (not necessarily a scalar variable):The
@
denotes multiple values. That could be the array as a whole, a slice, or a dereference:The
%
denotes pairs (keys and values), which might be a hash variable or a dereference:Under Perl v5.20, the
%
can now denote a key/value slice or either a hash or array:您可能需要查看
perlintro
和perlsyn
文档,以便真正开始理解 Perl(即,阅读 The Flipping手动的)。 :-)也就是说:
$this
是一个标量,它可以存储数字(int 或 float)、字符串或引用(见下文);@that
是一个数组,它可以存储标量的有序列表(见上文)。您可以使用push
或unshift
函数向数组添加标量(请参阅perlfunc
),并且可以使用括号内的逗号用于创建数组文字的标量文字或变量的分隔列表(即,my @array = ($a, $b, 6, "seven");
)%those
是一个散列,它是一个关联数组。哈希具有条目的键值对,因此您可以通过提供哈希的键来访问哈希的值。散列文字也可以像列表一样指定,只不过每个奇数条目都是一个键,每个偶数条目都是一个值。您还可以使用=>
字符代替逗号来分隔键和值。 (即my %ordinals = ("one" => "first", "two" => "second");
)通常,当您将数组或散列传递给子例程调用时,各个列表被扁平化为一个长列表。这有时是可取的,有时则不是。在后一种情况下,您可以使用引用将整个列表的引用作为单个标量参数传递。不过,引用的语法和语义很棘手,超出了本答案的范围。不过,如果您想查看一下,请参阅
perlref
。You might want to look at the
perlintro
andperlsyn
documents in order to really get started with understanding Perl (i.e., Read The Flipping Manual). :-)That said:
$this
is a scalar, which can store a number (int or float), a string, or a reference (see below);@that
is an array, which can store an ordered list of scalars (see above). You can add a scalar to an array with thepush
orunshift
functions (seeperlfunc
), and you can use a parentheses-bounded comma-separated list of scalar literals or variables to create an array literal (i.e.,my @array = ($a, $b, 6, "seven");
)%those
is a hash, which is an associative array. Hashes have key-value pairs of entries, such that you can access the value of a hash by supplying its key. Hash literals can also be specified much like lists, except that every odd entry is a key and every even one is a value. You can also use a=>
character instead of a comma to separate a key and a value. (i.e.,my %ordinals = ("one" => "first", "two" => "second");
)Normally, when you pass arrays or hashes to subroutine calls, the individual lists are flattened into one long list. This is sometimes desirable, sometimes not. In the latter case, you can use references to pass a reference to an entire list as a single scalar argument. The syntax and semantics of references are tricky, though, and fall beyond the scope of this answer. If you want to check it out, though, see
perlref
.