Skip to content

Perl Scalars Guide — Numbers, Strings, and the Undef Value

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl Scalars Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl scalars ($variable) hold a single value -- either a number, a string, or the special undef value -- with automatic conversion between numeric and string representations depending on the operator.

Scalar Variables

use strict;
use warnings;

# Scalars use $ prefix
my $name = "Alice";
my $age = 30;
my $pi = 3.14159;
my $is_active = 1;

Numbers

my $integer = 42;
my $float = 3.14;
my $negative = -10;
my $scientific = 1.5e10;
my $hex = 0xFF;    # 255
my $octal = 0777;  # 511
my $binary = 0b1010;  # 10

Strings

my $single = 'Single quotes';
my $double = "Double quotes";
my $interpolated = "Hello, $name!";  # Variable interpolation
my $escape = "Line 1\nLine 2\n";
my $multiline = "Line 1
Line 2
Line 3";

String Operators

my $greeting = "Hello" . " " . "World";  # Concatenation
my $repeated = "ha" x 3;  # "hahaha"
my $length = length("hello");  # 5
my $upper = uc("hello");  # "HELLO"
my $lower = lc("HELLO");  # "hello"

Undef

my $value;  # undef by default

# Check if defined
if (defined $value) {
    print "Defined!\n";
} else {
    print "Not defined!\n";  # prints
}

# Assign
$value = 42;
if (defined $value) {
    print "Now defined: $value\n";  # prints
}

Automatic Type Conversion

my $num = "42" + 1;   # 43 (string to number)
my $str = 42 . "abc"; # "42abc" (number to string)
my $sum = "10" + "20"; # 30 (both converted to numbers)

Common Mistakes

1. Not using my

Without my, variables are global. Always use my for local variables.

2. Confusing == and eq

== compares numbers, eq compares strings. "42" == "42.0" is true but "42" eq "42.0" is false.

3. Using undef in numeric context

my $x; my $y = $x + 5; produces a warning under use warnings; and treats undef as 0.

Practice Questions

1. What prefix do scalar variables use? The $ sigil. Arrays use @ and hashes use %.

2. What is undef? The undefined value, similar to null/nil. Check with defined($var).

3. How does Perl handle type conversion? Automatically based on operators. + forces numeric context, . forces string context.

FAQ

{{< faq question="What is the difference between single and double quotes?" >}} Double quotes interpolate variables and escape sequences (\n, \t). Single quotes are literal -- everything inside is treated as plain text. {{< /faq >}}

{{< faq question="How do I check if a string is numeric?" >}} Use Scalar::Util::looks_like_number($str) from the Scalar::Util module (core module). {{< /faq >}}

{{< faq question="What is the maximum integer size?" >}} Perl uses arbitrary-precision integers when compiled with 64-bit support. On 32-bit systems, integers are limited to 32 bits. {{< /faq >}}

What's Next

Now learn about Perl arrays -- ordered lists of scalars.

Topic Description Link
Arrays Lists and array operations {{< ref "04-arrays" >}}
Hashes Key-value pairs {{< ref "05-hashes" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro