WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Perl Β· Basic Β· question 4 of 100

How do you declare and use variables in Perl? Explain scalars, arrays, and hashes.?

πŸ“• Buy this interview preparation book: 100 Perl questions & answers β€” PDF + EPUB for $5

In Perl, variables are declared using a sigil, which is a special character that specifies the type of value the variable holds. The three main types of variables in Perl are scalars, arrays, and hashes.

Scalars:

A scalar variable can hold a single value, such as a number, string, or reference. Scalar variables are declared using a dollar sign β€˜$β€˜ as a sigil.

Here’s an example of how to declare and use a scalar variable:

my $name = 'John';
print "Hello, $name!n";

Arrays:

An array variable holds an ordered list of values, which can be of any type. Array variables are declared using an at-sign β€˜@β€˜ as a sigil.

Here’s an example of how to declare and use an array variable:

my @numbers = (1, 2, 3, 4, 5);
print "The fourth element of the array is $numbers[3]n";

In Perl, array indices start at 0, so the fourth element of the array is indexed by β€˜3β€˜, not β€˜4β€˜.

Hashes:

A hash variable holds an unordered set of key-value pairs, where each key is unique. Hash variables are declared using a percent sign β€˜%β€˜ as a sigil.

Here’s an example of how to declare and use a hash variable:

my %person = (
    name => 'John',
    age => 35,
    address => '123 Main St.',
);
print "$person{name} is $person{age} years old and lives at $person{address}n";

In this example, β€˜nameβ€˜, β€˜ageβ€˜, and β€˜addressβ€˜ are keys, and β€˜β€™Johnβ€™β€˜, β€˜35β€˜, and β€˜β€™123 Main St.β€™β€˜ are values associated with those keys.

Assigning Values to Variables:

To assign a value to a variable in Perl, use the β€˜=β€˜ operator. Here are some examples:

my $x = 10;          # Assigns the integer 10 to $x
my $y = "Hello";     # Assigns the string "Hello" to $y
my @nums = (1, 2, 3); # Assigns an array of integers to @nums
my %info = (         # Assigns a hash of key-value pairs to %info
    name => 'John',
    age => 35,
    address => '123 Main St.',
);

Accessing Values in Variables:

To access the value of a variable in Perl, use its name preceded by its sigil. Here are some examples:

my $x = 10;
my $y = "Hello";
print "$xn";  # Prints 10
print "$yn";  # Prints "Hello"

my @nums = (1, 2, 3);
print "$nums[1]n";  # Prints 2, which is the second element of the array

my %info = (
    name => 'John',
    age => 35,
    address => '123 Main St.',
);
print "$info{name}n";  # Prints "John", which is the value associated with the key 'name'

In summary, scalars hold a single value, arrays hold an ordered list of values, and hashes hold an unordered set of key-value pairs. In Perl, the sigil of a variable specifies its type, and values can be assigned to and accessed from variables using their names preceded by their sigils.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview β€” then scores it.
πŸ“ž Practice Perl β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Perl questions & answers β€” PDF + EPUB for $5

All 100 Perl questions Β· All topics