Top 25 Perl Interview Questions You Should Prepare in 2019

The Most Popular Perl Interview Questions with Code Examples:

Perl is one of the most powerful scripting languages in the field of computer programming with features like string manipulation, array manipulation etc., for building various real-time applications.

This article includes a list of potential questions that can be asked to the candidates appearing for interviews in Perl Programming.

Though we do not claim these questions and answers will suffice to prepare for an interview, we are sure that it will greatly help the candidates at all level to gain an understanding of Perl scripting language.


perl programming (1)

Top Perl Interview Questions

Enlisted below are the most frequently asked Perl Scripting Interview Questions that were answered by experts.

Let's Explore!!

Introduction to Perl

Q #1) Explain what is Perl language?

Answer: Perl stands for “Practical Extraction and Reporting Language”. It’s a powerful scripting language and is rich in features. Using Perl, we can write powerful and efficient code that can be used in mission-critical projects.

Q #2) What are the various advantages and disadvantages of Perl?

Answer: Advantages of Perl include:

  • Perl is efficient and is easy-to-use.
  • It is an Interpreted language i.e. Perl program is interpreted on a statement-by-statement basis.
  • Perl is portable and cross-platform. Currently, it can run on more than 100 platforms.
  • Perl is extendable. We can include various open source packages and modules in a Perl program for any additional functionality. For example, we can import CPAN modules for database support in the Perl program.

The main disadvantage of Perl is that as it is an interpreted language, the execution speed is quite slow. Although it allows us to write high-level code, we cannot write complex code using Perl. Perl has too many features that can be exhaustive for a programmer to comprehend.

Q #3) What are the various uses of Perl?

Answer: Perl is used in a mission-critical project – like the defense industry. It is also used in “Rapid Prototyping”.

Q #4) Explain the various characteristics of Perl.

Answer: Enlisted below are the various Characteristics of Perl:

  • Case-sensitive
  • Easy to code
  • Open source
  • Portable and cross-platform.
  • Extendable
  • No distinction between the types of variables.
  • Can return non-linear types likes arrays etc.
  • Non-scalars can be used as loop indices.
  • Supports high-level intrinsic operations – Example: stack Push/pop.
  • Powerful text manipulation API including Regular expressions.

Q #5) Explain the execution of a program in Perl.

Answer: Perl is portable and Perl programs can be executed on any platform. Though having a Perl IDE is useful, we can even write the Perl code in a notepad and then execute the program using the command prompt.

For Example, consider the following simple program to print “Hello, World!!”

Print(“Hello, World!!”);

In this code, the first line “#!/usr/bin/perl”, is the path to the Perl interpreter.

Let’s name this file as “”. We can execute this program by just giving the following command in the command window:

Output: Hello, World!!

Q #6) What are the various flags/arguments that can be used while executing a Perl program?

Answer: The following arguments can be used while executing a Perl program.

  • w – argument shows a warning.
  • d – used for debugging.
  • c – compiles only do not run.
  • e – execute.

We can also use a combination of arguments like:

pl –wd

Variables in Perl

Q #7) Comment on data types and variables in Perl.

Answer: Perl variables do not have a data type. The data type of a variable in Perl is inferred from its value.

A variable in Perl can be defined as follows:

$x = 10;
$base_str = ‘Hello’;

A value needs to be assigned to a variable before using it. Without this, the program may result in an unexpected output.

Q #8) What are Scalars in Perl?

Answer: Variables having values with linear data types like integer, float or string are called scalar variables in Perl.


These are all scalar variables.

Q #9) Comment on the scope of variables in Perl.

Answer: By default, all the variables in Perl are global in scope. This means that a variable can be used for a reminder of the program from the point of its declaration.

You can use ‘my’ keyword for a variable and this makes a variable to have local scope.

Example: my $x=10;


Q #10) What are Numeric Operators in Perl?

Answer: Numeric operators in Perl are as follows:

  • Arithmetic operators (+,-,*/).
  • Comparison operators to compare two numbers (>, <, ==, !=,<=,>=,<=>).
  • Bitwise Operators(&(and), |(or),^(ex-or),~(not),<<(shift left),>>(shift right)).

Arithmetic operators perform from left to right while Bitwise operators perform an operation from right to left.

Q #11) Explain string comparison operators in Perl.

Answer: Perl supports various string operators as shown below:

Not Equalne
Less thanlt
Greater thangt
Less than or equalle
Greater than or equalge

These operators can be used to compare two strings.

Q #12) What are the different string manipulation operators in Perl?

Answer: Perl provides two different operators to manipulate strings.

  • Concatenation operator (.) : Combines two strings to form a result string.
  • Repetition operator (x): Repeats string for a specified number of times.


 $str1 = “abc”;
 $str2 = “def”;
 $str3 = $str1.$str2; #concatenates the string and str3 has value ‘abcdef’

Q #13) What is chomp() operator/function?

Answer: chomp() operator removes the last character of a string and returns the number of characters removed.

chomp() operator is useful while reading input data from the console where it can be used to remove a newline (\n) character.

For Example,

$str = <STDIN>; #enter hello through standard input and press Enter. 

This will chomp the ‘\n’ character that was entered after hello.

Arrays in Perl

Q #14) Differentiate between arrays and list in Perl.

Answer: Both list and array can be defined as a set of elements. The main difference between a list and an array in Perl is that a list is immutable i.e. it cannot be altered directly.

In Perl, a list is an array without a name. Hence, most of the times array and list are used interchangeably. An array is mutable and its contents can grow, shrink in size, etc.

Thus in order to change the contents of a list, we can store the list as an array. An array is a variable that provides dynamic storage for a list.

Q #15) Explain Arrays in Perl.

Answer: Arrays in Perl contain only scalar elements i.e. arrays cannot contain another array as its element.

Arrays are declared as follows:

@myarray = (1,4,5);

As each array element is a scalar element, we access each element using ‘$’ and not ‘@’.

$index = $myarray [2]; # this will access 3<sup>rd</sup> element.
Print $index;

Output: 5

In Perl, it is not required to specify the length of an array during definition. Arrays grow or shrink dynamically.

For Example, in the above array if we say

$myarray [4] = 7;
@myarray will grow in size and contain 4 elements (1,4,5,7).

An array can contain a mix of values, unlike other programming languages. For Example, following array

@listarray = (1,2,”string1”,4.5);

This is a valid array in Perl.

Q #16) Comment on Array Slicing and Range Operator

Answer: Array slicing allows a user to retrieve more than one element of an array at once.

For Example,

@myarray = (1,2,3,4,5);
@subarray = @myarray [0,1];
Print (“@subarray\n”);

Output: 1 2

Thus we can ‘Slice’ the existing array and retrieve its elements.

We can also slice an array into large slices using “List-range operator” of Perl. List-range operator allows us to specify a range which will return all the elements in that range.


@daysOfMonth = (1..31); #daysOfMonth will contain 1 to 31 elements.
@fortnight = @daysOfMonth[1..15];

This will assign a slice consisting of first 15 elements to fortnight array.

Perl Packages and Modules

In Perl, we can directly use external modules and packages to extend functionality. Various Database packages, protocol implementations, network packages/modules can be used or imported and used in the Perl program. These module or package files usually have a “.pm” extension.

Q #17) Explain the various functions/directives in Perl that allow you to include/import a module. Also, state the differences between them.

Answer: There are two directives which we use in Perl to include modules or packages.


Use directive is used to include modules with .pm extensions and the included objects are verified at compile time. We need not specify the file extension. When “Use” is specified, the module included is loaded at compile time.


The “require” method is used to include both libraries as well as modules. The included objects are verified at the run time. It is required that we specify the file extension for the object and the object is loaded at run-time.

For Example, we have a module named “Oracle.dbm” and we can use both the directives as follows:

use Oracle;


require “Oracle.dbm”;

File Handling in Perl

Q #18) What are the various file operations in Perl. Explain with example.

Answer: Apart from reading from standard input and writing to standard output, files can also be used to read data from and write data to.

Basic operations of a file in Perl are as follows:

Open a file:
“open” is a library function to open a file.
where filevar=> like a file pointer used by Perl to refer a file
filename=>location of the file to be opened.

By default open function opens a file in ‘read’ mode.

If the file is to be opened in the write mode, then we need to precede the filename with a ‘>’ character.

Similarly, if the file is to be opened in the ‘Append’ mode, then we need to precede the filename with ‘>>’ characters.

open(filevar, >>filename);
‘open’ function returns a non-zero value if the file is opened successfully, otherwise, it returns zero.

Read from a file:
Once the file is opened, we can read data line-by-line from the file using the following code:
$line = <filevar>;

The above line of code reads a line pointed by file var and stores that line in $line.
We can also read the contents of the file into an array variable as shown below:

@filearray = <filevar>;

Write/Append to a file:
We can write data to a file using the following line of code:
Print filevar(“This is to be written to file\n”);

Close a file:
Once reading/writing a file is over, we can close the file using the following code:

Q #19) Differences between DIE and EXIT.

Answer: DIE and EXIT are two library functions in Perl to exit the program. The differences between DIE and EXIT is that DIE exits the program and prints a specified message. Exit simply exits the program.


open(myfile,filename) ||DIE(“File cannot be opened\n”);

The above line of code will print a message “File cannot be opened” in case if open fails and then exits the program.

Subroutines in Perl

Q #20) What is Subroutine in Perl?

Answer: Subroutine is a block of code that can be reused by a program either internally or externally.

A general representation of subroutine is as follows:
Here, the sub is a keyword followed by subroutine name NAME.
PROTOTYPE represents the parameters for a subroutine.

ATTRIBUTES give additional semantics about subroutine. Value of attribute can be either be “locked”, ”method” or “lvalue”.

BLOCK is a block of code for the subroutine.
Once subroutine is defined, we can call it using the statement,

The ampersand(&) is optional unless we are using references that refer to a subroutine name.

Subroutines in Perl can also be called as follows:

Q #21) Demonstrate subroutines in Perl with a simple example.

Answer: Let's take an example of a subroutine to print “Hello,World!” string.

Sub print_str{
 Print “Hello,World!”;

We can call this subroutine using the following statements:


Output: Hello,World!

Q #22) How are parameters passed to subroutines in Perl?

Answer: In Perl, all input or actual parameters of the subroutine are stored in an array ‘@_’. In other words, array @_ is used as an alias for subroutine arguments.

Let’s demonstrate this with an Example:

print &sum(1..4),”\n”;
sub sum{
my $sum = 0;
for my $i(@_){
$sum += $i;
return $sum;

In this example, we are calculating the sum of elements 1 to 4. We pass these elements as a range to a subroutine. In the subroutine code, @_ that contains parameters is iterated to find the sum and then the sum is returned.

Q #23) What is the peculiarity of returning values by subroutines in Perl?

Answer: In Perl, subroutines may have an explicit return statement to return a value as

return @result;

If the return statement is missing, then subroutine implicitly returns the value of the last expression in its body.

Take the following Example:

print &sum(1..4),”\n”;
sub sum{
my $sum = 0;
for my $i(@_){
$sum += $i;

In the above example, subroutine sum will return the value of variable sum ($sum) which is the last expression in its body.

We can also have multiple return statements in the subroutine body. Once the return statement is encountered, the rest of the subroutine code is skipped and the value is returned.

Environment Variables

Q #24) How do you set environment variables in Perl?

Answer: Perl has a special hash variable ‘%ENV’ that contains the value of all environment variables.

Hence, if I want the value of the environment variable PATH, I can say:

$path = $ENV{ ‘PATH’ };

Similarly, if I have to set the PATH variable to a new value, then I can do it as follows:

$ENV{ ‘PATH’ } = ‘/bin:/usr/bin’;

Grep Function

Q #25) What is “grep” function in Perl?

Answer: The grep function in Perl is mainly used for Pattern matching as in other scripting languages.

The “grep” function works on a list. It evaluates an expression or a block for each element of the List. For each statement that returns true as a result of evaluating an expression, it adds that element to the list of returning values.

Look at the following code snippet:

@list = (“foo”,10,0,”bar”,20);
@has_string = grep( /\s/,@list );
Print “@has_string\n”;

Output: foo bar

The above code executes “grep” command on a list and matches the pattern string (/s) to the list. The output is only the elements which are a string.


With this, we conclude this article on core Perl Interview Questions.

We hope this will help both freshers as well as experienced candidates to get an insight into Perl programming and crack the interview successfully.

We wish you all the best!!