Perl Interview Questions And Answers

Perl Interview Questions And Answers for experienced professionals from Codingcompiler. These Perl interview questions were asked in various interviews conducted by top multinational companies across the globe. We hope that these interview questions on Perl will help you in cracking your job interview. All the best and happy learning.

Perl Interview Questions

  1. What is Perl language?
  2. Why Perl? Explain in brief?
  3. What are the various advantages and disadvantages of Perl?
  4. What are the various uses of Perl?
  5. Can you name the variables in which the chomp works? Also, how they are different from one another?
  6. Explain the execution of a program in Perl.
  7. While writing a program, why the code should be as short as possible?
  8. What are the features of Perl programming?
  9.  Is perl a case sensitive language?
  10. What is a perl identifier?


Perl Interview Questions And Answers

Q1. 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.

Q2. Why Perl? Explain in brief?

Answer: UNIX system administrators and application developers often have to rely on several different languages to accomplish their tasks. This means learning a number of different syntaxes and having to write in multiple languages to accomplish a task. For example, to process a file, a system administrator might have to write a shell script using sh, process a file using awk or grep, and edit the file using sed. For other uses, the administrator may have to create a C program with its longer create/compile/debug development cycle.

It would be better if the administrator could combine many of these tasks into a simple language that is easy to write and develop, and reasonably efficient and complete. Along comes Perl.

In a single language, Perl combines some of the best features of C, sed, awk, and sh. People familiar with these languages have little difficulty being productive in Perl. Perl’s expression syntax is very C-like. Perl uses sophisticated pattern-matching techniques to scan large amounts of data very quickly. Although optimized for scanning text, Perl can also deal with binary data. If you have a problem on which you would ordinarily use sed, awk, or sh, but it exceeds these tools’ capabilities or must run a little faster and you don’t want to write the program in a compiled language such as C, Perl may be the language for you.

Q3. 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.

Q4. 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”.

Q5. Can you name the variables in which the chomp works? Also, how they are different from one another?

Answer:

These are: Scalar and Array

Scalar is generally denoted by symbol $ and it can have a variable which can either be a strong or a number. An array on the other side is denoted by @ symbol. An array is always a number. Both these variables have different namespace. The scalar variables are capable to hold a value of 1 digit while array can have more values. Both of them can be executed in the function whenever there is a need of the same. 

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

#!/usr/bin/perl

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 “hello.pl”. We can execute this program by just giving the following command in the command window:

pl hello.pl

Output: Hello, World!!

Q7. While writing a program, why the code should be as short as possible?

Answer:

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. 

Q8. What are the features of Perl programming?

Answer:

  • Perl takes the best features from other languages, such as C, awk, sed, sh, and BASIC, among others.
  • Perls database integration interface DBI supports third-party databases including Oracle, Sybase, Postgres, MySQL and others.
  • Perl works with HTML, XML, and other mark-up languages.
  • Perl supports Unicode.
  • Perl is Y2K compliant.
  • Perl supports both procedural and object-oriented programming.
  • Perl interfaces with external C/C++ libraries through XS or SWIG.
  • Perl is extensible. There are over 20,000 third party modules available from the Comprehensive Perl Archive Network.
  • The Perl interpreter can be embedded into other systems.

Q9. Is perl a case sensitive language?

Answer: Yes! Perl is a case sensitive programming language

Q10. What is a perl identifier?

Answer: A Perl identifier is a name used to identify a variable, function, class, module, or other object. A Perl variable name starts with either $, @ or % followed by zero or more letters, underscores, and digits (0 to 9).


Frequently Asked Perl Interview Questions And Answers

Q11. What are data types that perl supports?

Answer: Perl has three basic data types − scalars, arrays of scalars, and hashes of scalars, also known as associative arrays.

Q12. What are different data types that Perl supports. Elaborate on them.

Answer: 

PERL has three basic data types. They are:

Scalar data type: These are simple variables that are either a number, string or a reference. Scalar data types start with a dollar sign($). Scalar values are by default undefined. A scalar value is interpreted as TRUE in the Boolean sense if it not a null string.

Arrays of scalar: These are the ordered list of scalars that can be accessed by a numeric index and starts with a 0. They start with “at” sign (@). In simpler words, array stores the list of scalar values.

Hashes of scalars: Hashes are also known as associative arrays and are preceded by a percentage sign (%). Hashes are unordered sets of value pairs that can be accessed using the keys as subscripts. It stores associative arrays that use a key-value as an index instead of numerical indexes. It is a third major data type after scalars and arrays.

Q13. What does CPAN means?

Answer:

It stands for Comprehensive Perl Archive Network and is a large collection of all the documents and software related to Perl. The programmers can access the same and can avoid the difficulties they face.  CPAN is of significance use for the programmers and they are free to derive a lot of useful information from the same. 

Q14. What is a list context?

Answer:

Assignment to an array or a hash evaluates the right-hand side in a list context.

Q15. What is boolean context?

Answer: 

Boolean context is simply any place where an expression is being evaluated to see whether it’s true or false.

Q16. Explain the meaning of Perl one-liner.

Answer: Perl one-liners are one line command programs that are used for success of any operation. They may include more than one Perl statements, and one advantage to using it is that the program can be typed and executed from the command line instantly. Example:

The #run program, but with warnings

Perl –w my_file

The #run program under debugger

Perl –d my_file

Q17. What happens when you return a reference to a private variable?

Answer:

Perl keeps track of your variables when we return a reference to a private variable whether it’s dynamic or otherwise. Perl is not going to free things you are done using them.

Q18. Differentiate USE and REQUIRE in Perl?

Answer:

  • USE method is used for modules while REQUIRE method is used for both modules and libraries.
  • The objects which are included are varied at compilation time while in REQUIRE the objects are included are verified at runtime.
  • You are not supposed to give a file extension in USE and REQUIRE.

Q19. Write down the numeric operators in the Perl programming?

Answer: There are various operators in Perl including:

  • Comparison operators
  • Arithmetic operators
  • Bitwise Operators
  • String concatenation:
  • comparison operators

From the above, the athematic operators work from left to right while on the other side the Bitwise operators work from right to left.

Q20. Write down flags or arguments that are used while executing a program in Perl?

Answer: 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.


Advanced Perl Interview Questions And Answers

Q21. How many control keys are there in Perl language?

Answer: In the Perl language, there are three main control keys known as:

  • Redo statement
  • Next statement
  • Last statement

Q22. How to copy a file in Perl?

Answer: To copy the content of one file into another file, read all lines of the first file in a while loop and copy it in another file.

Q23. How to close a file in Perl?

Answer: Closing a file in Perl is not mandatory. However, using close() function will disassociate the file handle from the corresponding file.

Q24. What is the importance of Perl warnings? How do you turn them on?

Answers: 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.

The traditional way of enabling warnings was to use the -w argument on the

command line:

perl -w myscript.pl

You can also supply the option within the “shebang” line:

#/usr/local/bin/perl -w

You can also mention use warnings with all, deprecated and unsafe options.

Eg: use warnings ‘all’;

Q25. Explain ‘->’ in Perl?

Answer: It is a symbolic link which links one file name to a new file name.

For example, in file1 -> file2, if we read file1, we will end up reading file2.

Q26. What is a chop() function in Perl?

Answer: Perl chop() function removes the last character from a string regardless of what that character is. It returns the chopped character.

Q27. What is circular reference?

Answer

A circular reference takes place when two references have a reference to one another. While creating circular references, it is important to proceed carefully since a circular reference can also lead to memory leaks.

Q28. How can you empty an array?

Answer

There are three methods to empty an array that is as follow:

By setting length placing its length to any negative number.

By assigning a null list().

Set an array to undef to clear it.

Q29. Is the length of Perl code is same as in C++ and Java code?

Answer

Perl code is less as a comparison to Java and C++ language since Perl code is the one-fifth size of the C++ code, we need less to maintain, write and debug.

Q30. Name the options that can be used to wrap scripts inside loops?

Answer:

-n or -p options can be used.


Top Perl Interview Questions for Experienced

Q31. 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.

Example

$str1 = “abc”;

$str2 = “def”;

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

Q32. What is “grep” function in Perl?

Answer:

The grep function in Perl 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:

#!/usr/bin/perl@list =(“foo”,10,0,”bar”,20);

@has_string = grep( /\s/,@list );

Print “@has_string\n”;

Output: foo bar

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

Q33. How many types of operators are used in Perl?

Answer

Arithmetic operators: +,–,*

Assignment operators: += , -+, *=

Increment/ decrement operators: ++, – –

String concatenation: ‘.’ operator

comparison operators: ==, !=, >, < , >=

Logical operators: &&, ||, !

Q34. What does the q{ } operator do?

Answer :

The operator encloses a string in the single quotes.

Q35. Why Perl aliases are considered to be faster than references?

Answer:

Perl aliases are faster than references because they do not require any dereferencing.

Q36. When would ‘local $_’ in a function ruin your day?

Answer:

When your caller was in the middle for a while(m//g) loop

The /g state on a global variable is not protected by running local on it. That’ll teach you to stop using locals. Too bad $_ can’t be the target of a my() — yet.

Q37. How do I do < fill-in-the-blank > for each element in an array?

Answer:

#!/usr/bin/perl -w
@homeRunHitters = (‘McGwire’, ‘Sosa’, ‘Maris’, ‘Ruth’);
foreach (@homeRunHitters) {
print “$_ hit a lot of home runs in one yearn”;
}

Q38. Why is it hard to call this function: sub y { “because” }

Answer:

Because y is a kind of quoting operator.

The y/// operator is the sed-savvy synonym for tr///. That means y(3) would be like tr(), which would be looking for a second string, as in tr/a-z/A-Z/, tr(a-z)(A-Z), or tr[a-z][A-Z].

Q39. How do you print out the next line from a filehandle with all its bytes reversed?

Answer:

print scalar reverse scalar <FH>

Surprisingly enough, you have to put both the reverse and the <FH> into scalar context separately for this to work.

Q40. How do I do < fill-in-the-blank > for each element in a hash?

Answer:

Here’s a simple technique to process each element in a hash:

#!/usr/bin/perl -w
%days = (
‘Sun’ =>’Sunday’,
‘Mon’ => ‘Monday’,
‘Tue’ => ‘Tuesday’,
‘Wed’ => ‘Wednesday’,
‘Thu’ => ‘Thursday’,
‘Fri’ => ‘Friday’,
‘Sat’ => ‘Saturday’ );
foreach $key (sort keys %days) {
print “The long name for $key is $days{$key}.n”;
}

Related Interview Questions

  1. SAP HANA Interview Questions
  2. Core Java Interview Questions
  3. JSF Interview Questions
  4. JSP Interview Questions
  5. JPA Interview Questions
  6. Spring Framework Interview Questions
  7. Spring Boot Interview Questions
  8. Core Java Multiple Choice Questions
  9. 60 Java MCQ Questions And Answers
  10. Aricent Java Interview Questions
  11. Accenture Java Interview Questions
  12. Advanced Java Interview Questions For 5 8 10 Years Experienced
  13. Core Java Interview Questions For Experienced
  14. GIT Interview Questions And Answers
  15. Network Security Interview Questions
  16. CheckPoint Interview Questions
  17. Page Object Model Interview Questions
  18. Apache Pig Interview Questions
  19. Python Interview Questions And Answers
  20. Peoplesoft Integration Broker Interview Questions
  21. PeopleSoft Application Engine Interview Questions


Leave a Comment