Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...