How to declare and use variables in Perl?

In Perl, variables can be declared using the `my` keyword. The variable names must start with a `$` sign and can be any combination of letters, numbers, and underscores. For example:

my $name = "Alice";
my $age = 30;

To use variables, simply need to use the variable name directly. For example:

print "My name is $name and I am $age years old.\n";

In addition to scalar variables, Perl also supports array and hash variables. Arrays start with the @ symbol, while hashes start with the % symbol. For example:

my @numbers = (1, 2, 3, 4, 5);
my %person = ("name" => "Bob", "age" => 25);

Using arrays and hashes is similar to scalar variables. For example:

print "The first number is $numbers[0].\n";
print "The person's name is $person{'name'} and age is $person{'age'}.\n";
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds