Perl 中的 `$this`、`@that` 和 `%those` 有什么区别?

发布于 2024-08-30 06:27:34 字数 83 浏览 9 评论 0原文

Perl 中的 $this@that%those 之间有什么区别?

What is the difference between $this, @that, and %those in Perl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水波映月 2024-09-06 06:27:34

Perl 符号的有用助记符是:

  • $calar
  • @rray
  • %ash

Matt Trout 在 blog.fogus.me 上写了一篇关于 Perl 的精彩评论我认为有用的印记因此粘贴在下面:

实际上,perl 符号并不表示变量类型 – 它们表示共轭 – $ 是“the”,@ 是
“这些”,% 是“映射”左右 – 变量类型通过 [] 或 {} 表示。您可以通过以下方式查看:

my $foo = 'foo';
我的@foo = ('零', '一', '二');
我的 $second_foo = $foo[1];
我的@first_and_third_foos = @foo[0,2];
我的 %foo = (key1 => '值1', key2 => '值2', key3 ==> '值3');
我的 $key2_foo = $foo{key2};
我的($key1_foo, $key3_foo) = @foo{'key1','key3'};

所以当浏览 Perl 代码时查看印记会告诉你你将会得到什么
比你正在操作的东西要好得多。

不可否认,在您习惯之前,这确实令人困惑,但是一旦您-习惯了它
它是一个非常有用的工具,可以在浏览代码时吸收信息。

当然,你仍然完全有权利讨厌它,但这是一个有趣的概念,我
认为你可能更喜欢讨厌实际发生的事情而不是你认为的事情
继续:)

A useful mnemonic for Perl sigils are:

  • $calar
  • @rray
  • %ash

Matt Trout wrote a great comment on blog.fogus.me about Perl sigils which I think is useful so have pasted below:

Actually, perl sigils don’t denote variable type – they denote conjugation – $ is ‘the’, @ is
‘these’, % is ‘map of’ or so – variable type is denoted via [] or {}. You can see this with:

my $foo = 'foo';
my @foo = ('zero', 'one', 'two');
my $second_foo = $foo[1];
my @first_and_third_foos = @foo[0,2];
my %foo = (key1 => 'value1', key2 => 'value2', key3 => 'value3');
my $key2_foo = $foo{key2};
my ($key1_foo, $key3_foo) = @foo{'key1','key3'};

so looking at the sigil when skimming perl code tells you what you’re going to -get- rather
than what you’re operating on, pretty much.

This is, admittedly, really confusing until you get used to it, but once you -are- used to it
it can be an extremely useful tool for absorbing information while skimming code.

You’re still perfectly entitled to hate it, of course, but it’s an interesting concept and I
figure you might prefer to hate what’s -actually- going on rather than what you thought was
going on :)

旧人 2024-09-06 06:27:34

$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 like apple

@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.

寂寞美少年 2024-09-06 06:27:34

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]

相思故 2024-09-06 06:27:34

人们经常尝试将符号与变量类型联系起来,但它们只是松散相关。这是我们在学习 Perl有效的 Perl 编程 因为当您了解 sigils 时,理解 Perl 会容易得多。

许多人忘记了变量和数据实际上是不同的东西。变量可以存储数据,但使用数据不需要变量。

$ 表示单个标量值(不一定是标量变量):

$scalar_var
$array[1]
$hash{key}

@ 表示多个值。这可能是整个数组、一个切片或取消引用:

@array;
@array[1,2]
@hash{qw(key1 key2)}
@{ func_returning_array_ref };

% 表示对(键和值),它可能是哈希变量或取消引用:

%hash
%$hash_ref

在 Perl v5.20 下, % 现在可以表示 键/值切片或者哈希或数组:

%array[ @indices ];  # returns pairs of indices and elements
%hash{ @keys };      # returns pairs of key-values for those keys

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):

$scalar_var
$array[1]
$hash{key}

The @ denotes multiple values. That could be the array as a whole, a slice, or a dereference:

@array;
@array[1,2]
@hash{qw(key1 key2)}
@{ func_returning_array_ref };

The % denotes pairs (keys and values), which might be a hash variable or a dereference:

%hash
%$hash_ref

Under Perl v5.20, the % can now denote a key/value slice or either a hash or array:

%array[ @indices ];  # returns pairs of indices and elements
%hash{ @keys };      # returns pairs of key-values for those keys
可是我不能没有你 2024-09-06 06:27:34

您可能需要查看 perlintroperlsyn 文档,以便真正开始理解 Perl(即,阅读 The Flipping手动的)。 :-)

也就是说:

  • $this 是一个标量,它可以存储数字(int 或 float)、字符串或引用(见下文);
  • @that 是一个数组,它可以存储标量的有序列表(见上文)。您可以使用 pushunshift 函数向数组添加标量(请参阅 perlfunc),并且可以使用括号内的逗号用于创建数组文字的标量文字或变量的分隔列表(即,my @array = ($a, $b, 6, "seven");
  • %those是一个散列,它是一个关联数组。哈希具有条目的键值对,因此您可以通过提供哈希的键来访问哈希的值。散列文字也可以像列表一样指定,只不过每个奇数条目都是一个键,每个偶数条目都是一个值。您还可以使用 => 字符代替逗号来分隔键和值。 (即 my %ordinals = ("one" => "first", "two" => "second");

通常,当您将数组或散列传递给子例程调用时,各个列表被扁平化为一个长列表。这有时是可取的,有时则不是。在后一种情况下,您可以使用引用将整个列表的引用作为单个标量参数传递。不过,引用的语法和语义很棘手,超出了本答案的范围。不过,如果您想查看一下,请参阅 perlref

You might want to look at the perlintro and perlsyn 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 the push or unshift functions (see perlfunc), 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文