Scalars
Learn Perl scalars: the $ sigil, storing numbers, strings, and references in scalar variables, and string interpolation in double-quoted strings.
Introduction
The scalar is Perl's most fundamental data type — a single value, whether that is a number, a string of text, or a reference to a more complex structure. Nearly every Perl script you write will use scalar variables constantly, so getting comfortable with them now pays off immediately.
- What a scalar is and how the $ sigil works.
- How to declare scalars with my.
- How Perl stores numbers and strings in scalars.
- What a reference is, at a basic level.
- How string interpolation works inside double-quoted strings.
What is a Scalar?
In Perl, a scalar holds exactly one single value at a time. Every scalar variable name is prefixed with a dollar sign ($), called the scalar sigil. This is different from many other languages, where the type of a variable is usually shown by a keyword (like int or string); in Perl, the $ tells you the variable holds one value, regardless of whether that value happens to be a number or a piece of text.
use strict;use warnings;
my $language = "Perl";my $year_created = 1987;my $is_interpreted = 1; # Perl has no true boolean type; 1 and 0/"" act as true/false
print "Language: $language\n";print "Created: $year_created\n";Click Run to see what this code prints.
Declaring Scalars with my
The my keyword declares a new, lexically scoped variable — meaning it only exists and is visible within the enclosing block of code (we will explore scoping fully in the "Variables & Context" lesson). Combined with use strict;, my is required before you can use any variable, which prevents an entire category of typo-related bugs.
use strict;use warnings;
my $count = 0;$count = $count + 1;$count += 1; # shorthand for the same thing
print "Count is now: $count\n";Click Run to see what this code prints.
Numbers and Strings
A scalar can hold an integer, a floating-point number, or a string, and Perl converts freely between them depending on how you use the value — this is called context-sensitive conversion, and it is one of Perl's most distinctive traits.
use strict;use warnings;
my $price = 19.99;my $quantity = "3"; # a string that looks like a numbermy $total = $price * $quantity; # Perl converts "3" to the number 3
print "Total: $total\n";
my $mixed = "5 apples";my $sum = $mixed + 10; # Perl reads the leading numeric part, 5, and warns about the restprint "Sum: $sum\n";Click Run to see what this code prints.
Because use warnings; is enabled, the second calculation above would also print a runtime warning to your terminal ("Argument \"5 apples\" isn't numeric") even though it still produces a result. Warnings like this are exactly why the pragma is so valuable — it flags fragile code without stopping execution.
References as Scalars
A scalar can also hold a reference — essentially an address pointing to another piece of data, such as an array or a hash. References let a single scalar variable represent an entire complex structure, which is essential for building nested data. We only introduce the concept briefly here; references get their own deeper treatment later in this course.
use strict;use warnings;
my @colors = ("red", "green", "blue");my $colors_ref = \@colors; # $colors_ref is a scalar holding a reference to @colors
print "Reference: $colors_ref\n";print "First color via reference: $colors_ref->[0]\n";Click Run to see what this code prints.
Do not worry if the arrow syntax ($colors_ref->[0]) looks unfamiliar — the key idea for now is simply that a single scalar can point to something much larger.
String Interpolation
Inside a double-quoted string, Perl automatically replaces any scalar variable with its current value. This is called interpolation, and it is one of the most convenient features for building readable output without manually concatenating pieces of text.
use strict;use warnings;
my $name = "Ada";my $score = 97;
print "Student $name scored $score points.\n";print "Next year, $name will be in grade " . 10 . ".\n";Click Run to see what this code prints.
The second line shows the . operator, which concatenates (joins) strings together — a manual alternative to interpolation, useful when you are combining values that are not simple variables.
Single vs Double Quotes
Perl has two main string quoting styles, and choosing the right one matters. Double-quoted strings ("...") interpolate variables and interpret escape sequences like \n. Single-quoted strings ('...') take everything literally — no interpolation, no escape sequence processing (aside from \\ and \\').
use strict;use warnings;
my $name = "Sam";
print "Double-quoted: Hello, $name!\n";print 'Single-quoted: Hello, $name!' . "\n";Click Run to see what this code prints.
Notice that in the single-quoted line, $name is printed literally as the text "$name", not replaced with "Sam". Choosing single quotes is a common, intentional way to print a literal dollar sign or curly brace without needing to escape it.
Common Mistakes
- Expecting single-quoted strings to interpolate variables — they never do.
- Forgetting my before a new variable, which fails immediately under use strict;.
- Assuming Perl has strict, separate integer/float/string types the way statically typed languages do — scalars convert between these forms based on context.
- Confusing a reference (like $colors_ref) with the original data structure itself — they are related but distinct.
Best Practices
- Use double quotes when you need interpolation or escape sequences; use single quotes for literal text.
- Always declare scalars with my as close as possible to where you first use them.
- Give scalar variables clear, descriptive names ($total_price, not $x) for readability.
- Watch for numeric warnings from use warnings; — they usually point to a real bug in how a string is being used as a number.
Frequently Asked Questions
Not in the way statically typed languages do. A scalar simply stores a number, and Perl handles integer versus floating-point representation internally as needed.
The $ sigil signals "this variable holds a single value," in contrast with @ for arrays (multiple values) and % for hashes (key-value pairs), which you will learn in the next two lessons.
Yes. Scalars are not locked to one kind of value; you can reassign a scalar variable to hold a different type of value at any point.
Key Takeaways
- A scalar holds exactly one value: a number, a string, or a reference.
- Scalar variables are always prefixed with the $ sigil.
- my declares a new lexically scoped scalar variable, required under use strict;.
- Double-quoted strings interpolate variables and escape sequences; single-quoted strings do not.
- A reference is a scalar that points to another data structure, such as an array or hash.
Summary
Scalars are the foundation of Perl variables. Next, you will learn about arrays, which use the @ sigil to store ordered lists of multiple values.