Wednesday, April 19, 2017

PERL Interview Questions and Answers

PERL Interview Questions and Answers


 1. What is Perl ?:

Perl, sometimes said as Practical Extraction and Reporting Language, is an interpreted programming language with a huge number of uses, libraries and resources.

Arguably one of the most discussed and used
Questions you may use
on the internet, it is often referred to as the swiss army knife, of the web.

2. Who created it ?:

Perl was first brought into being by Larry Wall circa 1987 as a general purpose Unix scripting language to make his programming work simpler.

Although it has far surpassed his original creation, Larry Wall still oversees development of the core language, and the newest version, Perl 6.

3. Why do you use Perl ?

Perl is a powerful free interpreter.

Perl is portable, flexible and very easy to learn.

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

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.

5. How to connect to SQL server through Perl ?

We use the DBI(Database Independent Interface) module to connect to any database.

use DBI; $dh = DBI->connect("dbi:mysql:database=DBname","username","password"); $sth = $dh-> prepare("select name, symbol from table");

$sth->execute(); while(@row = $sth->fetchrow-array()){ print "name =$row[0].symbol= $row[1]; } $dh->disconnect $dh=DBI->connect("dbi:mysql")





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

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



7. How to implement stack in Perl ?

Through push() and shift() function.

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

8. How do I generate a list of all .html files in a directory ?

Following are the snippet of code which prints a listing of every file in the current directory that ends with the extension .html:

#!/usr/bin/perl -w

opendir(DIR, ".");

@files = grep(/\.html$/,readdir(DIR));

closedir(DIR);

foreach $file (@files) {

print "$file\n";

}

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

Perl keeps track of your variables, whether dynamic or otherwise, and doesn't free things before you're done using them.

10. Explain the different data types used in Perl ?.

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

11. How to turn on Perl warnings? Why is that important ?

Perl is very forgiving of strange and sometimes wrong code, which can mean hours spent searching for bugs and weird results.

Turning on warnings helps uncover common mistakes and strange places and save a lot of debugging time in the long run.

There are various ways of turning on Perl warnings:

For Perl one-liner, use -w option on the command line.

On Unix or Windows, use the -w option in the shebang line (The first # line in the script). Note: Windows Perl interpreter may not require it.

For other systems, choose compiler warnings, or check compiler documentation.

12. What are scalar data and scalar variables ?

Perl has a flexible concept of data types. Scalar means a single thing, like a number or string.

So the Java concept of int, float, double and string equals to Perl\'s scalar in concept and the numbers and strings are exchangeable.

Scalar variable is a Perl variable that is used to store scalar data.

It uses a dollar sign $ and followed by one or more alphanumeric characters or underscores. It is case sensitive.

Jobs in india

IT / Software Jobs,Core Technical Jobs, Government Jobs,Defence Jobs,Research Jobs,BPO Jobs,Bank Jobs, Tech Support Jobs,Health Care Job...