Perl Scripting Interview Questions and Answers
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
2. What are the advantages of programming in Perl?
ANS: Perl is easier to understand due to its simple syntax
-is easier to use due to its flexibility
-is easily readable
3. What factors do you take into consideration to decide if Perl is a suitable programming language for a situation?
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.
4. Explain the different types of data Perl can handle.
ANS: – 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.
5. 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.
6. 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.
7. Explain different types of Perl Operators.
ANS: -Arithmetic operators, +, – ,* etc
-Assignment operators: += , -+, *= etc
-Increment/ decrement operators: ++, —
-String concatenation: ‘.’ operator
-comparison operators: ==, !=, >, < , >= etc
-Logical operators: &&, ||, !
8. 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.
9. 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.
10. 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.
11. 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.
12. 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.
13. Does Perl have reference type?
ANS: Yes. Perl can make a scalar or hash type reference by using backslash operator.
14. What is ‘->’ in Perl?
Ans. it is a symbolic link to link one file name to a new name. so lets say we do it likefile1-> file2, if we read file1, we end up reading file2
15. 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-catenation of the converted values. Typically, each converted values looks like its machine-level repesentation. for example, on 32-bit machines a converted integer may be represented by a sequence of 4 bytes
16. 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.
17. What is Grep used for in Perl?
Ans. Grep is used with regular expression to check if a particular value exist in an array. It returns 0 it the value does not exists, 1 otherwise
18. 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.
19. 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.