Perl Scripting Interview Questions & Answers - GUIDE To CRACK
Perl Scripting Interview Questions and Answers

Perl Scripting Interview Questions & Answers – GUIDE To CRACK

Last updated on 04th Jul 2020, Blog, Interview Questions

About author

Gupta (Sr Project Manager )

High level Domain Expert in TOP MNCs with 8+ Years of Experience. Also, Handled Around 20+ Projects and Shared his Knowledge by Writing these Blogs for us.

(5.0) | 16212 Ratings 1760

These Perl Scripting Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Perl Scripting . As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer.we are going to cover top 100 Perl Scripting  Interview questions along with their detailed answers. We will be covering Perl Scripting  scenario based interview questions, Perl Scripting  interview questions for freshers as well as Perl Scripting  interview questions and answers for experienced. 

1) Explain what is Perl language?

Ans:

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.

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

Ans:

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.

3) What are the various uses of Perl?

Ans:

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

4) Explain the various characteristics of Perl?

Ans:

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.

5) Explain the execution of a program in Perl?

Ans:

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!!”

#!/usr/bin/perlPrint(“Hello, World!!”);

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

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

pl hello.pl

Output: Hello, World!!

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

Ans:

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 filename.pl

7) Comment on data types and variables in Perl?

Ans:

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’;

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

8) What are Scalars in Perl?

Ans:

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

$x=10;$mystr=”abc”;

These are all scalar variables.

9) Comment on the scope of variables in Perl?

Ans:

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;

10) What are Numeric Operators in Perl?

Ans:

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.

11)When do you use Perl for programming?

Ans:

PERL is a free open source language. PERL is used to develop web based applications even though libraries are available to program web server applications, database interfaces and networking components

12)What factors do you take into consideration to decide if Perl is a suitable programming language for a situation? Explain the different types of data Perl can handle.

Ans:

If the application to be developed is web based, Perl provides a lot of flexibility in programming such applications and is most popularly used.

As PERL is free, we can save on the cost of acquiring license for the programming language.

  • Scalars: store single values and are preceded by $ sign
  • Arrays: store a list of scalar values and are preceded by @ sign
  • Hashes: store associative arrays which use a key value as index instead of numerical indexes. Use % as prefix.

13)What is the importance of Perl warnings? How do you turn them on?

Ans:

Warnings are one of the most basic ways in which you can get Perl to check the quality of the code that you have produced. Mandatory warnings highlight problems in the lexical analysis stage. Optional warnings highlight cases of possible anomaly.

14)When used gives out warnings about the possible interpretation errors in the script?

Ans:

Strict: Strict is a pragma which is used to force checks on the definition and usage of variables, references and other bare words used in the script. This can be invoked using the use strict command. If there are any unsafe or ambiguous commands in the script, this pragma stops the execution of the script instead of just giving warnings.

Different types of Perl Operators:

  • Arithmetic operators, +, – ,* etc
  • Assignment operators: +=, -+, *= etc
  • Increment/ decrement operators: ++, —
  • String concatenation: ‘.’ operator
  • comparison operators: ==, !=, >, < , >= etc
  • Logical operators: &&, ||,!

15)What is Perl one-liner?

Ans:

There are two ways a Perl script can be run:

  • from a command line, called one-liner, that means you type and execute immediately on the command line. You’ll need the -e option to start like “C:\ %gt perl -e “print \” Hello\”;”. One-liner doesn’t mean one Perl statement. One-liner may contain many statements in one line.
  • from a script file, called Perl program.

    Subscribe For Free Demo

    16)Why aren’t Perl’s patterns regular expressions?

    Ans:

    Because Perl patterns have back references.

    A regular expression by definition must be able to determine the next state in the finite automaton without requiring any extra memory to keep around previous state. A pattern /([ab]+)c\1/ requires the state machine to remember old states, and thus disqualifies such patterns as being regular expressions in the classic sense of the term.

    17)What is hash in Perl?

    Ans:

    A hash is like an associative array, in that it is a collection of scalar data, with individual elements selected by some index value which essentially are scalars and called as keys. Each key corresponds to some value. Hashes are represented by %followed by some name.

    18)What does Perl do if you try to exploit the execve (2) race involving setuid scripts?

    Ans:

    Sends mail to root and exits.

    It has been said that all programs advance to the point of being able to automatically read mail. While not quite at that point (well, without having a module loaded), Perl does at least automatically send it.

    19)Why does Perl not have overloaded functions?

    Ans:

    Because you can inspect the argument count, return context, and object types all by yourself.

    In Perl, the number of arguments is trivially available to a function via the scalar sense of @_, the return context via want array (), and the types of the arguments via ref () if they’re references and simple pattern matching like /^\d+$/ otherwise. In languages like C++ where you can’t do this, you simply must resort to overloading of functions.

    20)Does Perl have reference type?

    Ans:

    Yes. Perl can make a scalar or hash type reference by using backslash operator.

    21)What is ‘->’ in Perl?

    Ans:

    It is a symbolic link to link one file name to a new name. so let’s say we do it likefile1-> file2, if we read file1, we end up reading file2

    22)What is a subroutine?

    Ans:

    Subroutine is perl is a block of code specially combined/grouped to perform a particular task. Which can be called at any point of time in a perl program. <br><br>Advantage using Subroutine<br> a) helps in modular programming making it easier to understand and maintain<br> b) eliminates duplication by reusing the same code/calling the subroutine.

    23)What is meant by a ‘pack’ in Perl?

    Ans:

    Pack converts a list into a binary representation. Takes an array or list of values and packs it into a binary structure, returning the string containing the structure It takes a list of values and converts it into a string. The string contains a con-concatenation of the converted values. Typically, each converted values looks like its machine-level representation. for example, on 32-bit machines a converted integer may be represented by a sequence of 4 bytes

    24)How to implement stack in Perl?

    Ans:

    Through push () and shift () function. push adds the element at the last of array and shift () removes from the beginning of an array.

    25)What is Grep used for in Perl?

    Ans:

    Grep is used with regular expression to check if a particular value exists in an array. It returns 0 it the value does not exists, 1 otherwise

    26)How to code in Perl to implement the tail function in unix?

    Ans:

    You have to maintain a structure to store the line number and the size of the file at that time eg. 1-10bytes, 2-18bytes. You have a counter to increase the number of lines to find out the number of lines in the file. once you are through the file, you will know the size of the file at any nth line, use ‘sysseek’ to move the file pointer back to that position(last 10) and then start reading till the end.

    27) How to replace Perl array elements?

    Ans:

    The Perl splice array function removes elements and replaces them with the specified list of elements.

    28) How to convert strings into an array in Perl?

    Ans:

    The Perl split array function splits a string into an array of strings. Thus converting strings into an array.

    29) How to convert arrays into a string in Perl?

    Ans:

    The Perl join array function combines more than one array into a single string. Thus converting arrays into a string.

    30) How to merge two arrays in Perl?

    Ans:

    The Perl merged array function merges two arrays into a single array by removing all the commas in between them.

    31) How to sort arrays in Perl?

    Ans:

    The Perl sort array function sorts all the elements of an array according to the ASCII standard.

    32) How to know whether a key exists or not in Perl?

    Ans:

    Using Perl exists function, you can check whether a key exists or not in a hash. It returns true if the key exists.

    33) How to add elements in a hash in Perl?

    Ans:

    To add a new key-value pair in the hash, declare them as a single variable in the hash variable.

    Course Curriculum

    Enroll in PERL Scripting Training & Build Your Skills to Next Level

    • Instructor-led Sessions
    • Real-life Case Studies
    • Assignments
    Explore Curriculum

    34) What does delete function do in Perl?

    Ans:

    To remove a hash element, use delete function. It removes both key and value element from the hash.

    35) What does undef function in Perl?

    Ans:

    The undef function removes the value from the hash, but its key remains there.

    36) What is the difference between Perl array and Perl hash?

    Ans:

    • Perl array: They are an ordered list of elements, positioned by index number. It is denoted with @ sign.
    • Perl hash: They are an unordered list of elements, positioned by their key values. It is denoted with a % sign.

    37) What is the difference between Perl list and Perl array?

    Ans:

    Perl list is a method to organize data in the Perl source code. It is a fixed collection of scalars. They are always one dimensional.

    Perl array is a method to store data in the form of variables. They are multi-dimensional.

    38) What is the difference between use and require in Perl?

    Ans:

    • Use: It is used only for the Perl modules. The included modules are verified at the time of compilation. It does not need the file extension.
    • Require: It is used for both Perl modules and libraries. The included objects are verified at runtime. It does need the file extension.

    39) What is Perl? What do you understand by it?

    Ans:

    When it comes to choosing to the most powerful and reliable scripting language in the field of computer programming “Pearls” appears on the top of the list. The language has already gained popularity among the developers for its high-level features such as array manipulation, string manipulation, and Unicode support that are useful when it comes to developing real-life applications.

    40) What is the full form of Perl?

    Ans:

    The ‘Perl’ is the abbreviated term for the “Practical Extraction and Reporting Language” which is one of the strongest programming languages one the earth enriched with a number of features.

    41) Describe the uses of the Perl?

    Ans:

    Perl is basically used for critical projects including the defense projects and rapid prototyping.

    42) Does the Perl programming have objects? Does it force you to use it?

    Ans:

    Yes, the Perl programming contains objects and the programming language itself doesn’t force you to use any certain objects.

    43) Can you dynamically load the binary extension in Perl?

    Ans:

    Yes, the language allows you to load the binary extension dynamically if your system supports it. If your system doesn’t support it then you have to statistically compile the extension.

    44) Write down flags or arguments that are used while executing a program in Perl?

    Ans:

    There are so many different flags or arguments that are used in Pearl and some of them are given below:

    • e- Denotes execute
    • d-Denotes debugging
    • w- Denotes warning
    • c- Denotes compile only

    Apart from these, the user can also leverage the combination of different arguments together.

    45) Do the Perl variables have data type?

    Ans:

    No, the Perl variables do not have any data type.

    46) Which one has the highest precedence, Terms or list?

    Ans:

    When it comes to making a comparison among the Terms or list, the terms have the highest precedence in the Perl programming.

    47) Which operator in Perl is used for the concatenation of two strings?

    Ans:

    The operator used for the concatenation of strings is (.) operator.

    48)What is CPAN in Perl?

    Ans:

    CPAN stands for Comprehensive Perl Archive Network. It is a repository which contains thousands of Perl modules.

    49) When does circular reference occur?

    Ans:

    When the two references also contain references to each other, then the case of circular reference occurs.

    50) What happens in dereferencing?

    Ans:

    Value to the location from the reference point is returned.

    51)  What are scalar variables?

    Ans:

    Scalar variables are what many programming languages refer to as simple variables. They hold a single data item, a number, a string, or a perl reference. Scalars are called scalars to differentiate them from constructs that can hold more than one item, like arrays.

    52)Explain about lists?

    Ans:

    A list is a construct that associates data elements together and you can specify a list by enclosing those elements in parenthesis and separating them with commas. They could themselves be arrays, hashes or even other lists. Lists do not have a specific list data type.

    53) Explain about Typeglobs?

    Ans:

    Type globs are another integral type in perl. A typeglob`s prefix derefrencer is *, which is also the wild card character because you can use typeglobs to create an alias for all types associated with a particular name. All kinds of manipulations are possible with typeglobs.

    54) Difference between the variables in which chomp function work ?

    Ans:

    • Scalar: It is denoted by $ symbol. Variable can be a number or a string.
    • Array: Denoted by @ symbol prefix. Arrays are indexed by numbers.

    The namespace for these types of variables is different. For Example: @add, $add. The scalar variables are in one table of names or namespace and it can hold single specific information at a time and array variables are in another table of names or namespace. Scalar variables can be either a number or a string

    55) Create a function that is only available inside the scope where it is defined ?

    Ans:

    • $pvt = Calculation(5,5);
    • print(” result=”$pvt\n");
    • sub Calculation{
    • my ($fstVar, $secndVar) = @_;
    • my $square = sub{
    • return($_[0] ** 2);
    • };
    • return(&$square($fstVar) + &$square($secndVar));
    • };

    Output: Result = 50

    56) Which feature of Perl provides code reusability ? Give any example of that feature.

    Ans:

    Inheritance feature of Perl provides code re usability. In inheritance, the child class can use the methods and property of parent class.

    • Package Parent;
    • Sub foo
    • {
    • print(“Inside A::foo\n”);
    • }
    • package Child;
    • @ISA = (Parent);
    • package main;
    • Child->foo();
    • Child->bar();

    57) In Perl we can show the warnings using some options in order to reduce or avoid the errors. What are that options?

    Ans:

    • Command-line option: It will display the list if warning messages regarding the code.
    • strict pragmatic: It forces the user to declare all variables before they can be used using the my() function.
    • Using the built-in debugger: It allows the user to scroll through the entire program line by line.

    58) Write the program to process a list of numbers.

    Ans:

    The following program would ask the user to enter numbers when executed and the average of the numbers is shown as the output:

    • $sum = 0;
    • $count = 0;
    • print “Enter number: “;
    • $num = <>;
    • chomp($num);
    • while ($num >= 0)
    • {
    • $count++;
    • $sum += $num;
    • print “Enter another number: “;
    • $num = <>;
    • chomp($num);
    • }
    • print “$count numbers were entered\n”;
    • if ($count > 0)
    • {
    • print “The average is “,$sum/$count,”\n”;
    • }
    • exit(0);

    59) Does Perl have objects? If yes, then does it force you to use objects? If no, then why?

    Ans:

    Yes, Perl has objects and it doesn’t force you to use objects. Many object oriented modules can be used without understanding objects. But if the program is too large then it is efficient for the programmer to make it object oriented.

    Course Curriculum

    Get PERL Scripting Certification Courses with Industry Standard Syllabus

    Weekday / Weekend BatchesSee Batch Details

    60) Write a program to concatenate the $firststring and $secondstring and result of these strings should be separated by a single space?

    Ans:

    Syntax:

    $result = $firststring . ” “.$secondstring;

    Program:

    • #!/usr/bin/perl
    • $firststring = “abcd”;
    • $secondstring = “efgh”;
    • $combine = “$firststring $secondstring”;
    • print “$Combine\n”;

    Output:

    abcd efgh

    61) How do I replace every TAB character in a file with a comma?

    Ans:

    perl -pi.bak -e ‘s/\t/,/g’ myfile.txt

    62) In Perl, there are some arguments that are used frequently. What are that arguments and what do they mean?

    Ans:

    • w (argument shows warning)
    • d (use for debug)
    • c (which compile only not run)
    • e (which executes)

    We can also use combination of these like: -wd

    63) How many types of primary data structures in Perl and what do they mean?

    Ans:

    • The scalar: It can hold one specific piece of information at a time (string, integer, or reference). It starts with dollar $ sign followed by the Perl identifier and Perl identifier can contain alphanumeric and underscores. It is not allowed to start with a digit. Arrays are simply a list of scalar variables.
    • Arrays: Arrays begin with @ sign.
    • Example of array: my @arrayvar = (“string a”, “string b “string c”);
    • Associative arrays: It also frequently called hashes, are the third major data type in Perl after scalars and arrays. Hashes are named as such because they work very similarly to a common data structure that programmers use in other languages–hash tables. However, hashes in Perl are actually a direct language supported data type.

    64) Which functions in Perl allows you to include a module file or a module and what is the difference between them?

    Ans:

    “use”: “use” is only for the modules to include .pm type file

    • The objects are certified at the time of collection.
    • We don’t need to specify the file extension.
    • loads the module at compile time.

    “require”:The method is used for both libraries and modules.

    • The included objects are verified at the run time.
    • We need to specify the file Extension.
    • Loads at run-time.

    suppose we have a module file as “Module.pm” use Module; or require “Module.pm”; (Will do the same)

    65) Which guidelines by Perl modules must be followed?

    Ans:

    • Below are guidelines and are not mandatory:
    • The package name should always begin with a capital letter.
    • The entire file name should have the extension “.pm”.
    • In case no object oriented technique is used the package should be derived from the Exporter class.

    Through the @EXPORT and @EXPOR_OK arrays Perl module must spread its functions and variables to the main namespace, if no object oriented performance are used.

    66) For a situation in programming, how can you determine that Perl is a suitable?

    Ans:

    When we need faster execution, then Perl will provide us that requirement. There a lot of flexibility in programming if we want to develop a web based application. We do not need to buy the license for Perl because it is free. We can use Comprehensive Perl Archive Network, which is one of the largest depot of free code in the world.

    67) What is the use of -w, -t and strict in Perl?

    Ans:

    The use –w is, it shows warnings about the possible interpretation errors in the script.
    Strict tells Perl to force checks on the meaning and usage of variables, which made the use strict command. If there are any unsafe commands in the script, this pragma stops the performance of the script instead of giving notice.
    The use –t is, it switches on taint checking and forces Perl to check the origin of variables where outside variables cannot be used in sub shell executions and system calls

    68) Explain about an ivalue?

    Ans:

    An ivalue is an item that can serve as the target of an assignment. The term I value originally meant a “left value”, which is to say a value that appears on the left. An ivalue usually represents a data space in memory and you can store data using the ivalues name. Any variable can serve as an ivalue.

    69) How to use the command shift?

    Ans:

    Shift array function shifts off the first value of the array and returns it, thereby shortening the array by one element and moving everything from one place to the left. If you don’t specify an array to shift, shift uses @ ARGV, the array of command line arguments passed to the script or the array named @-.

    70) What exactly is grooving and shortening of the array?

    Ans:

    You can change the number of elements in an array simply by changing the value of the last index of/in the array $#array. In fact, if you simply refer to a nonexistent element in an array perl extends the array as needed, creating new elements. It also includes new elements in its array.

    71) Is it possible in the Perl to use code again and again? If so, which feature enable user to that?

    Ans:

    Yes, it is possible in Perl. However, there is a limit on usage of the same code in the same program. The users need not to worry about the complexity either as Perl is equipped with a code trimming feature. It automatically guides users on how to keep the code as short as possible. Code re usability is a prime example of this. The feature that enables users to simply keep up the pace towards this is “Inheritance”. The child class in this feature can use the methods of their parent class. 

    72) How can you represent the warning signs in the Perl in case of an error and what are the options through which this task can be performed?

    Ans:

    There is an option in Perl which is known as WCommand Line. All the warning messages can be displayed using this and the pragmatic function simply makes sure that the user can declare the variables during appearance of warning messages. The entire program can be scrolled easily and in fact, in a very short span of time using the in-built debugger. 

    Unix Shell Scripting Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    73) While writing a program, why the code should be as short as possible?

    Ans:

    Complex codes are not always easy to handle. They are not even easy to be reused. Moreover, finding a bug in them is not at all a difficult job. Any software or application if have complex or lengthy code couldn’t work smoothly with the hardware and often have compatibility issues. Generally, they take more time to operate and thus becomes useless or of no preference for most of the users. The short code always makes sure that the project can be made user-friendly and it enables programmers to save a lot of time. 

    74) Can you tell the meaning of the term debugging in the programming?

    Ans:

    Well, every programmer is familiar with this approach. The fact is there are many errors that declare their presence in the programs due to reasons which are not always necessary to be known exactly. Eliminating these errors is very essential for the smooth flow of the tasks. Finding the bugs or the errors is known as debugging. The programming languages can have in-built options for debugging or the programmers are free to consider other options too. 

    75) In Perl, is it possible for the programmers to prefer a dynamic approach when it comes to loading the binary extension?

    Ans:

    Yes, it is possible. The only need for this is the system a programmer is using must support it. The other option is to accomplish this task statically in case the system doesn’t allow the same. Dynamic approach can help users to save time as they are free to perform some basic tasks in their own way.

    76) Tell something about the Associate Arrays in Perl and how they are significant for the programmers?

    Ans:

    It is basically one of the widely used data type in the Perl after Scalar and Array. They are quite similar to that of a hash table and there are a lot of functions which are quite similar to that of the same. 

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free