Skip to content
VLSI Mentor

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

Scalars: A Single Value

A scalar is a variable that holds exactly one value, and it is where most of the data in most Perl scripts lives. This chapter declares one, puts a value in it, uses it, and changes it — the first time in this course that a script remembers anything. It then spends its second half on a single idea that is obvious to some readers and actively wrong in the mental model of others: in Perl, the equals sign copies a value at the instant the line runs. It does not create a lasting relationship between two names, which is exactly what it does in the hardware description languages many readers arrive from.

Foundation13 min readPerlPart IFundamentalsScalarsAssignment

Part I · Module 2 · Chapter 2.2 · Scalars: A Single Value

1. One Value, One Name

A scalar holds exactly one value. Its name begins with $, which Chapter 2.1 established as the marker for "one of something":

Azvya Education Pvt. Ltd.VLSI Mentor
scalar-basics.pl
use strict;
use warnings;

my $name  = "Perl";
my $count = 5;

print "$name: $count\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
Perl: 5

Four things are happening on each declaration line, and all four are worth naming:

  • my declares the variable — it creates the name. Chapter 1.7 introduced it and Chapter 2.3 covers it properly. Under use strict it is compulsory, which Chapter 1.10 explained.
  • $name is the name, sigil included.
  • = is assignment: put the value on the right into the variable on the left.
  • "Perl" and 5 are the values. One is text, one is a number — and §3 shows that the scalar does not care which.

The print line then uses both. Writing $name inside double quotes substitutes its value, which is the interpolation first seen in Chapter 1.7 and covered fully in Chapter 4.2.

2. Why This Is the Point of the Module

Compare that to every script in Module 1, which could only print text typed into the file. A scalar lets a script hold something it was given and refer to it later, in more than one place.

That is the whole difference between a script that announces a fixed message and one that reports on a particular run:

Azvya Education Pvt. Ltd.VLSI Mentor
run-report.pl
use strict;
use warnings;

my $test_name       = "alu_smoke";
my $seed            = 42;
my $clock_period_ns = 10;
my $error_count     = 0;

print "Test:         $test_name\n";
print "Seed:         $seed\n";
print "Clock period: $clock_period_ns ns\n";
print "Errors:       $error_count\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
Test:         alu_smoke
Seed:         42
Clock period: 10 ns
Errors:       0

Everything printed came from a name defined once at the top. Change $test_name on line 4 and the output changes, with no other edit — and the values are in one place where a reader can see them. That is a small thing in a ten-line script and the difference between maintainable and unmaintainable in a three-hundred-line one.

3. What a Scalar Can Hold

Three kinds of thing:

  • A number — 42, 10, 72.5. Chapter 2.4.
  • A piece of text — "alu_smoke". Chapter 2.5.
  • A reference, which is a value that points at something larger such as a whole array. Module 7. Named here only so the definition is complete; you need nothing from it yet.

What a scalar notably does not have is a declared type. You do not say "this variable holds a number". The same variable can hold text on one line and a number on the next, and Perl will not object:

Azvya Education Pvt. Ltd.VLSI Mentor
no-declared-type.pl
use strict;
use warnings;

my $value = 42;
print "$value\n";

$value = "alu_smoke";
print "$value\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
42
alu_smoke

This flexibility is genuinely useful and genuinely dangerous, and it is the subject of Chapter 2.6. For now, note only that my creates a name, not a type.

4. Reassignment

Assigning again replaces the value. The old one is gone:

Azvya Education Pvt. Ltd.VLSI Mentor
reassign.pl
use strict;
use warnings;

my $error_count = 0;
print "start:  $error_count\n";

$error_count = 3;
print "later:  $error_count\n";

$error_count = 7;
print "finally: $error_count\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
start:  0
later:  3
finally: 7

Note that my appears only on the first line. my declares; = assigns. Declaring the same variable twice in the same scope is a different thing — it creates a second variable — and Chapter 2.3 covers why that matters.

A table makes the sequence explicit, which is worth doing once because the next section depends on reading it:

After line$error_count holds
my $error_count = 0;0
$error_count = 3;3
$error_count = 7;7

5. = Copies Once — It Does Not Connect

This is the most important idea in the chapter, and for a particular group of readers it corrects an active misconception rather than filling a gap.

Azvya Education Pvt. Ltd.VLSI Mentor
copy-not-link.pl
use strict;
use warnings;

my $total  = 10;
my $errors = $total;      # copies the value NOW

$total = 99;              # change the original

print "total:  $total\n";
print "errors: $errors\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
total:  99
errors: 10

$errors is 10, not 99. Line 5 took the value $total had at that moment and put a copy in $errors. It did not record that $errors should follow $total. When $total changed, nothing told $errors.

After line$total$errors
my $total = 10;10—
my $errors = $total;1010
$total = 99;9910

If you have never written RTL, there is nothing to unlearn here — copy-once is probably what you already assumed.

6. Why This Matters in Engineering Work

Two things, both about scripts that outlive the hour they were written in.

Values belong at the top, named. A script with my $seed = 42; on line 4 and $seed used in three places can be re-run with a different seed by editing one line. The same script with 42 typed into three print statements requires finding all three, and the day someone finds only two is the day it reports a run it did not perform. Naming a value once is the cheapest form of correctness there is.

A name is documentation that cannot drift. $clock_period_ns tells a reader both what the number is and what unit it is in. The literal 10 tells them nothing, and a comment saying # nanoseconds can be left behind when the code changes. Chapter 1.9 argued for comments that record why; a good variable name removes the need for a comment that records what.

7. Common Mistakes

Omitting my. Under use strict this is a compile error — Global symbol "$seed" requires explicit package name — and Chapter 1.10 showed what happens in the far worse case where use strict is absent.

Repeating my on every assignment. Writing my $error_count = 3; on a later line does not update the variable; it declares a second one. In the same block Perl warns — "my" variable $error_count masks earlier declaration in same scope — and the printed value does change, so the script looks fine and carries a latent fault. In an inner block there is no warning and the outer variable keeps its old value, which is the version that actually bites; Chapter 2.3 covers it. my once, assign as often as you like.

Expecting a copy to track the original. As §5 showed. The symptom is a variable holding a stale value with no apparent cause.

Misspelling the name. $eror_count is a different variable from $error_count, and use strict is what turns that from a silent wrong answer into a refusal to compile. This is the concrete reason the pragma is compulsory.

Assuming a declared type. Nothing stops a scalar holding text where you expected a number. That is Chapter 2.6's subject and a real source of bugs in Part II.

8. What You Should Be Able to Say Now

  1. A scalar holds exactly one value and its name begins with $.
  2. my declares a name; = assigns a value. my is needed once, assignment as often as required.
  3. A scalar can hold a number, text, or a reference, and has no declared type.
  4. Reassignment replaces the value; the previous one is gone.
  5. = copies the value at the moment the line runs — it does not create a lasting link between two names.
  6. Naming a value once, at the top, is what makes a script changeable without hunting for literals.

9. Exercise

Exercise 1 — write the report. Create a script that declares scalars for a test name, a seed and an error count, then prints one labelled line for each. Change only the test-name line and re-run to confirm one edit changes the output.

Exercise 2 — predict the copy. Without running it, write down what copy-not-link.pl from §5 prints for both variables. Then run it. If you predicted 99 for $errors, write one sentence explaining what you now believe instead.

Exercise 3 — trace the sequence. Given my $n = 1; then my $m = $n; then $n = 5; then $m = $n;, build the two-column table from §5 and state the final value of each variable. Verify by running it.

Exercise 4 — debug a warning on a script that "works". This script prints 3, which is what its author wanted, and Perl still complains:

Azvya Education Pvt. Ltd.VLSI Mentor
Exercise 4 — it prints 3, so what is Perl objecting to?
use strict;
use warnings;

my $error_count = 0;
my $error_count = 3;

print "$error_count\n";

Run it and record the exact warning. Then answer two questions: how many variables did this script create, and why is Perl concerned about a program that produced the right answer? Finally, rewrite it so the warning disappears and the output is unchanged.

10. Summary

  • A scalar is one value, named with a leading $, and it holds most of the data in most scripts.
  • my declares, = assigns. Declare once; assign as often as needed.
  • There is no declared type. A scalar can hold a number, text or a reference, and can change which.
  • Assignment copies, once. $b = $a takes the value $a has at that instant and does not track it afterwards.
  • For readers from RTL this is a correction, not a detail — Perl's = is an action, not a continuous relationship.
  • Naming values once, at the top, is what makes a script safe to change later.

Chapter 2.3 looks more closely at the my you have now used a dozen times: what it actually creates, and why the block it sits in decides how long the name exists.

Where this fits

Part of the Perl curriculum.