Perl Interview Questions & Answers Freshers

Perl Interview Questions & Answers Freshers

revanth1. How to open and read data files with Perl?

 Ans: Data files are opened in Perl using the open () function. When you open a data file, all you have to do is specify (a) a file handle and (b) the name of the file you want to read from.

As an example, suppose you need to read some data from a file named “checkbook.txt”. Here’s a simple open statement that opens the checkbook file for read access: open (CHECKBOOK, “checkbook.txt”); In this example, the name “CHECKBOOK” is the file handle that you’ll use later when reading from the checkbook.txt data file. Any time you want to read data from the checkbook file, just use the file handle named “CHECKBOOK”.

Now that we’ve opened the checkbook file, we’d like to be able to read what’s in it. Here’s how to read one line of data from the checkbook file:

$record = < CHECKBOOK > ;

After this statement is executed, the variable $record contains the contents of the first line of the checkbook file. The “<>” symbol is called the line reading operator.

To print every record of information from the checkbook file.

 Open (CHECKBOOK, “checkbook.txt”) || die “couldn’t open the file!”;

While ($record = < CHECKBOOK >) {

Print $record;

}

Close (CHECKBOOK);

 2. What is the use of command “use strict”?

 Ans: Use strict command calls the strict pragma and is used to force checks on definition and usage of variables, references and other bare words used in the script. If unsafe or ambiguous statements are used, this command stops the execution of the script instead of just providing warnings.

Explain the arguments for Perl Interpreter:

-a – automatically splits a group of input files

-c – checks the syntax of the script without executing it

-d – invokes the PERL debugger after the script is compiled

-d- module – script is compiled and control is transferred to the module specified.

-d – The command line is interpreted as single line script

-S – uses the $PATH env variable to locate the script

-T – switches on Taint mode

-v – prints the version and path level of the interpreter

-w – prints warnings

 3. What would happen if you prefixed some variables with following symbols ?

 Ans:

i.) $ – The variable becomes a scalar variable which can hold one value only

ii.) @ – The variable becomes an array variable which can hold a list of scalar variables

iii.) % – The variable becomes a hash variable which stores values as key-value pairs.

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

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

 6. How do I do fill_in_the_blank for each file in a directory?

 Ans: Here’s code that just prints a listing of every file in the current directory:

#!/usr/bin/perl -w

opendir(DIR, “.”);

@files = readdir(DIR);

closedir(DIR);

foreach $file (@files) {

print “$file\n”;

}.

 7. What are the advantages of programming in Perl?

 Ans: Advantages of programming in Perl:

 As mentioned above, PERL

-is easier to understand due to its simple syntax

-is easier to use due to its flexibility

-supports OOP

-is easily readable.

 8. What are prefix dereference? List them.

 Ans: When we dereference a variable using particular prefix, they are called prefix dereference.

 (i) $-Scalar variables

(ii) %-Hash variables

(iii) @-arrays

(iv) &-subroutines

(v) Type globs-*myvar stands for @myvar, %myvar.

 Explain “grep” function:The grep function evaluates an expr or a block for each element of the List. For each statement that returns true, it adds that element to the list of returning values.

 Differentiate between C++ and Perl:

-C++ does not allow closures while Perl does

-C++ allows pointer arithmetic but Perl does not allow pointer arithmetic

 Explain: Chomp, Chop, CPAN, TK.

 Chomp: A Chomp function removes the last character from an expert or each element of list if it matches the value of $/. This is considered to be safer than Chop as this removes only if there is a match.

 Chop: Chop function removes the last character from EXPR, each element of list.

 CPAN: CPAN is the Comprehensive Perl Archive Network, a large collection of Perl software and documentation. CPAN.pm is also a module in Perl which is used to download and install Perl software from the CPAN archive.

 TK:TK is an open source tool kit that is used to build web based applications using Perl.

 9.  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 thens tart reading till the end.

 10. Can we load binary extension dynamically?

 Ans: Yes, we can load binary extension dynamically but your system supports that. If it doesn’t support, then you can statically compile the extension.

Save