How to declare and assign a scalar variable in Perl?
Question
How to declare and assign a scalar variable in Perl?
How to Declare and Assign a Scalar Variable in Perl
In Perl, a scalar variable is a variable that holds a single value—this can be a number, a string, or a reference. Scalars are fundamental to Perl programming and are denoted by a leading dollar sign $ (called a sigil). This sigil tells Perl that you are working with a scalar variable.
Unlike some languages, Perl does not require you to explicitly declare variable types. You just use the scalar sigil $ followed by the variable name to create or use a scalar variable. Assigning a value is done with the = operator. Perl uses dynamic typing, so you can reassign a scalar variable to a different type of value at any time.
Basic Syntax
my $variable_name = value;
mydeclares a new lexical variable (variable limited in scope to the current block or file). This is preferred over using global variables.$variable_nameis the scalar variable. Variable names usually start with a letter or underscore and can include letters, digits, and underscores.=is the assignment operator.valuecan be a string (usually in quotes), a number, or any expression evaluating to a scalar.
Example: Declaring and Assigning Scalar Variables
#!/usr/bin/perl
use strict;
use warnings;
# Declare a scalar with a string
my $greeting = "Hello, World!";
# Declare a scalar with a number
my $year = 2024;
# Print both variables using interpolation
print "$greeting The year is $year.\n";
# Reassign the scalar variable to a different type
$year = "Two Thousand Twenty-Four";
print "Now the year is: $year\n";
This script demonstrates the following:
mydeclares local lexical variables$greetingand$year.$greetingis assigned a string value.$yearis initially assigned a number, then reassigned a string, showing Perl’s flexible typing.- Scalar variables are interpolated inside double quotes, meaning their values are substituted when printed.
Important Perl Concepts Highlighted
- Sigils: The
$sigil identifies a scalar variable. This is distinct from arrays (@) and hashes (%). - my: Use
myto create lexical variables, which restrict scope and avoid unwanted side effects. - Context: Scalars in scalar context return a single value, as opposed to arrays or hashes which have more complex contexts.
- TMTOWTDI: Perl has “There’s More Than One Way To Do It” philosophy, so you could assign without
my(making it global), but lexical variables are best practice.
Common Pitfalls
- Forgetting
mycauses your variables to be global, leading to potential bugs—always declare withmy. - Using single quotes for strings prevents interpolation. For example,
'$year'literally prints$year, not its value. - Attempting to assign multiple items to a scalar directly from a list may cause warnings or unexpected behavior.
By following this pattern, you can easily declare and assign scalars in Perl, the building blocks for handling data in your scripts.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 7ms
(empty)(empty)