Perl 中的 `$this`、`@that` 和 `%those` 之间有什么区别? [英] What is the difference between `$this`, `@that`, and `%those` in Perl?
问题描述
Perl 中的 $this
、@that
和 %those
有什么区别?
What is the difference between $this
, @that
, and %those
in Perl?
推荐答案
Perl sigils 的一个有用的助记符是:
A useful mnemonic for Perl sigils are:
- $calar
- @rray
- %ash
Matt Trout 对 blog.fogus.me 关于我认为有用的 Perl 标志,所以粘贴在下面:
Matt Trout wrote a great comment on blog.fogus.me about Perl sigils which I think is useful so have pasted below:
实际上,perl sigils 不表示变量类型——它们表示共轭——$ 是the",@ 是‘这些’, % 是 ‘map of’ 左右——变量类型通过 [] 或 {} 表示.您可以通过以下方式查看:
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'};
所以在浏览 perl 代码时查看 sigil 会告诉你你将要 -get- 而不是比你正在做的手术要多得多.
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 :)
这篇关于Perl 中的 `$this`、`@that` 和 `%those` 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!