Skip to content
VLSI Mentor

Perl Part I · Module 2 · Variables, Scalars, and Data Types

The Three Sigils: $, @ and %

Every Perl variable begins with a punctuation character, and readers arriving from other languages usually assume it is noise to be tolerated. It is not. The character says what kind of thing the name refers to — one value, an ordered list, or a set of name-and-value pairs — and Perl uses it to decide what your line means. This chapter introduces all three so that you can read any Perl you meet, and proves the point that makes the system coherent: $item, @item and %item are three completely separate variables that happen to share a name.

Foundation12 min readPerlPart IFundamentalsSigilsVariables

Part I · Module 2 · Chapter 2.1 · The Three Sigils: $, @ and %

1. Why a Script Needs Variables at All

Every script in Module 1 printed fixed text. Chapter 1.3 created check.pl, which announced itself and stopped:

Azvya Education Pvt. Ltd.VLSI Mentor
What Module 1 could do
print "check starting\n";

That is a script with nothing to say about anything. To report on a particular simulation run it would have to hold on to the run's details — the test's name, the seed it used, how many errors it found — and print those. Holding on to a value so that a later line can use it is what a variable is for.

The moment you want one, Perl asks a question other languages do not: what kind of thing is this? The answer goes in front of the name.

2. The Three Kinds

Azvya Education Pvt. Ltd.VLSI Mentor
three-kinds.pl
use strict;
use warnings;

my $name  = "Ravi";              # one value
my @marks = (72, 65, 90);        # a list of values, in order
my %score = (maths => 72);       # name-and-value pairs

print "$name\n";
print scalar(@marks), "\n";
print $score{maths}, "\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
Ravi
3
72
  • $ — a scalar. Exactly one value: a number, a piece of text, or a reference. This is the workhorse, and Chapter 2.2 is devoted to it.
  • @ — an array. Several values kept in order, reached by position. Module 5.
  • % — a hash. Values looked up by name rather than position. Module 6.

For now, recognising the three on sight is the entire goal. You are not expected to work with arrays or hashes yet, and nothing in the rest of this module will require it.

Three independent pairs. The dollar sigil names a scalar, which holds one value. The at sigil names an array, which holds several values in order. The percent sigil names a hash, which holds values looked up by name. Each sigil connects only to its own kind.$namescalarone value"Ravi"@marksarrayvalues in order72, 65, 90%scorehashvalues by namemaths => 7212
Figure 1 — a conceptual view of what each sigil names. It is deliberately not a picture of memory: Perl's internal storage is more involved than three boxes, and nothing in this module depends on it. What matters is the question each sigil answers — one value, several in order, or several by name.

3. The Sigil Is Part of the Identity

Here is the demonstration that makes the system make sense. These three declarations are all legal in one program, and they create three unrelated variables:

Azvya Education Pvt. Ltd.VLSI Mentor
same-name.pl
use strict;
use warnings;

my $item = "one value";
my @item = (72, 65, 90);
my %item = (maths => 72);

print "$item\n";
print "$item[0]\n";
print "$item{maths}\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
one value
72
72

Nothing was overwritten. $item still holds its text; @item still holds its three numbers; %item still has its pair. Perl kept them apart because the sigil is part of the name, so $item, @item and %item are as distinct as $a, $b and $c.

This is why the punctuation is not decoration. It is how Perl knows which of three possible variables you meant.

4. Reading an Element Changes the Sigil

One detail from §3 that catches everyone, stated now so the rest of Part I reads correctly.

Look at how an array element was printed: $item[0], with a $ — not @item[0]. That looks inconsistent and is not. The rule is:

The sigil describes what you are taking out, not what you stored it in.

@item is a list of values. One element of it is a single value, so you ask for it with $. The same applies to a hash: %item is a set of pairs, but $item{maths} is one value out of it, so it takes $ too.

You do not need to work with this yet. You need to recognise it, because otherwise $item[0] looks like a typo when you meet it in someone's script. Modules 5 and 6 cover the mechanics properly.

5. Why This Matters in Engineering Work

The three kinds map onto three questions a flow script actually asks, which is the fastest way to remember them.

  • One value. What was this run's seed? How many errors did it report? Which tool produced this log? A scalar.
  • Several, in order. Which tests are in this regression, in the order they should run? Which source files does this design compile, in dependency order? An array — order is part of the information.
  • Several, by name. What was the result of the test called alu_smoke? What slack did this particular endpoint have? A hash — you arrive knowing the name and want the value.

Choosing wrongly is a real cost rather than a style preference. A script that keeps per-test results in an array has to search it to answer "how did alu_smoke do?", and must keep a second array of names in step by position. The same data in a hash answers the question directly. Part II is largely built on hashes for exactly this reason, and Chapter 6.11 is devoted to the choice.

6. Common Mistakes

Leaving the sigil off. Perl has no bare variable names. Under use strict, which Chapter 1.10 made compulsory, writing name = "Ravi"; fails to compile — Perl sees a bare word where it expected something it understands.

Assuming the sigil follows the container. Writing @marks[0] for one element is the most common early slip. It is not a syntax error, which is what makes it worth knowing about: Perl reads it as a one-element slice and will usually warn. Ask for one value with $.

Expecting $name and @name to be related. They are not, as §3 proved. This surprises people who assume the sigil is a type annotation on a single variable rather than part of its identity.

Reading % as a modulus. % in front of a name is a hash sigil; % between two numbers is the remainder operator from Chapter 3.2. Perl tells them apart by position, and so will you after a week.

7. What You Should Be Able to Say Now

  1. A variable's sigil says what kind of thing the name refers to: $ one value, @ a list in order, % values by name.
  2. The sigil is part of the name, so $item, @item and %item are three unrelated variables.
  3. The sigil at the point of use describes what you are taking out — hence $item[0] for one element of @item.
  4. Scalars carry most of the data in most scripts; arrays and hashes arrive in Modules 5 and 6.
  5. The choice between array and hash is a question about how you will look the data up, not a matter of taste.

8. Exercise

Everything needed is in this chapter. No array or hash operations are required.

Exercise 1 — identify the kind. For each, say whether you would store it in a scalar, an array or a hash, and why in one clause: (a) the name of the test currently running; (b) the list of tests to run tonight, in order; (c) the result of each test, looked up by test name; (d) the number of errors found in total.

Exercise 2 — predict the output. Without running it, say exactly what same-name.pl from §3 prints and why nothing is overwritten. Then run it and check.

Exercise 3 — read someone else's line. You find print $results[2]; in a script. Say what kind of variable @results is, what $results[2] refers to, and why the sigil changed.

Exercise 4 — make it fail, then fix it. Write a one-line script that assigns to a name with no sigil at all, under use strict. Record the error, then fix it by adding the sigil that makes it one value. Explain which stage the error came from, using Chapter 1.5.

9. Summary

  • The sigil names the kind: $ one value, @ several in order, % several by name.
  • It is part of the variable's identity, which is why $item, @item and %item coexist without interfering.
  • At the point of use it describes what you are taking out — one element of an array is $item[0], not @item[0].
  • Scalars do most of the work and are the subject of the next chapter; arrays and hashes wait for Modules 5 and 6.
  • The array-or-hash decision is about lookup — by position or by name — and it has real consequences for the code you end up writing.

Chapter 2.2 takes the first of the three and looks at it properly: what a scalar can hold, and why it is the variable you will reach for most.

Where this fits

Part of the Perl curriculum.