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

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


Solution 1:

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

Solution 2:

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

Solution 3:

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]

Solution 4:

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