In this tutorial, we will learn about variables in Perl. We will cover the basics of declaring and using variables, including naming conventions.
Variables in Perl are used to store data values that can be manipulated during program execution. Perl is a dynamically typed language, meaning the data type of a variable can change at runtime.
When naming variables in Perl, follow these conventions:
$
for scalars, @
for arrays, and %
for hashes.The syntax to declare variables in Perl is:
my $scalar = value;
my @array = (values);
my %hash = (key => value);
$num
.# Declare and initialize an integer scalar variable
my $num = 10;
# Print the value of the variable
print "The value of num is: $num\n";
The value of num is: 10
$name
.# Declare and initialize a string scalar variable
my $name = 'John';
# Print the value of the variable
print "The value of name is: $name\n";
The value of name is: John
$isTrue
.1
(true) to the variable.# Declare and initialize a boolean scalar variable
my $isTrue = 1;
# Print the value of the variable
print "The value of isTrue is: $isTrue\n";
The value of isTrue is: 1