July 30, 2021
Top 10 perl interview questions and answers

Top 10 perl interview questions and answers

Top 10 perl interview questions and answers

srihitha1.Why do you use Perl ?

Ans: Perl is portable, flexible and easy to learn.
Perl is a powerful free interpreter.

2. what is the difference between ‘use’ and ‘require’ function ?

Ans: 1. the method is used only for modules (only to include .pm type file)
2. the included object are verified at the time of compilation. 3. No Need to give  file extentsion.
1. The method is used for both libraries ( package ) and modules
2. The include objects are varified at the run time. 3. Need to give file


3. How can we create Perl programs in UNIX, Windows NT, Macintosh and OS/2 ?

Ans: “Emacs” or “vi” can be used in UNIX and in Windows NT we can use “notepad”. In Macintosh we can use MacPerl’s text editor or any other text editor and in OS/2, e or epm can be used.

4. How do I set environment variables in Perl programs ?

Ans: you can just do something like this:$path = $ENV{‘PATH’};
As you may remember, “%ENV” is a special hash in Perl that contains the value of all your environment variables.

Because %ENV is a hash, you can set environment variables just as you’d set the value of any Perl hash variable. Here’s how you can set your PATH variable to make sure the following four directories are in your path: $ENV{‘PATH’} =

5. What is the use of ‘chomp’ ? what is the difference between ‘chomp’ and ‘chop’?

Ans: ‘chop’ functiononly removes the last character completely ‘from the scaler, where as ‘chomp’ function only removes the last character if it is a newline. by default, chomp only removes what is currently defined as the $INPUT_RECORD_SEPARATOR. whenever you call ‘chomp ‘, it checks the value of a special variable ‘$/’. whatever the value of ‘$/’ is eliminated from the scaler.

6.  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 = <>;
while ($num >= 0)
$sum += $num;
print “Enter another number: “;
$num = <>;
print “$count numbers were entered\n”;
if ($count > 0)
print “The average is “,$sum/$count,”\n”;

7. Which of these is a difference between C++ and Perl ?

Ans: Perl can have objects whose data cannot be accessed outside its class, but C++ cannot.Perl can use closures with unreachable private data as objects, and C++ doesn’t support closures. Furthermore, C++ does support pointer arithmetic via

`int *ip = (int*)&object’,

allowing you do look all over the object. Perl doesn’t have pointer arithmetic. It also doesn’t allow `#define private public’ to change access rights to foreign objects. On the other hand, once you start poking around in /dev/mem, no one is safe.

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

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’;
Differentiate between Use and Require, My and Local, For and Foreach and Exec and System

Use and Require:
Both the Use and Require statements are used while importing modules.
A require statement imports functions only within their packages. The use statement imports functions with a global scope so that their functions and objects can be accessed directly.

Eg. Require module;
Var = module::method(); //method called with the module reference

Eg: use module;
Var = method(); //method can be called directly

-Use statements are interpreted and are executed during the parsing whereas the require statements are executed during run time thereby supporting dynamic selection of modules.

My and Local:
A variable declared with the My statement is scoped within the current block. The variable and its value goes out of scope outside the block whereas a local statement is used to temporarily assign a value to the global variable inside the block. The variable used with local statement still has global accessibility but the value lasts only as long as the control is inside the block.

For and Foreach:
The for statement has an initialization, condition check and increment expressions in its body and is used for general iterations performing operations involving a loop. The foreach statement is particularly used to iterate through arrays and runs for the length of the array.

Exec and System:
Exec command is used to execute a system command directly which does not return to the calling script unless if the command specified does not exist and System command is used to run a subcommand as part of a Perl script.

i.e The exec command stops the execution of the current process and starts the

execution of the new process and does not return back to the stopped process. But the system command, holds the execution of the current process, forks a new process and continues with the execution of the command specified and returns back to the process on hold to continue execution.

9. 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 contaings 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 representated by a sequence of 4 bytes.

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

Ans: The -w Command-line option: It will display the list if warning messages regarding the code.

– strict pragma: 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.