• +91 9971497814
  • info@interviewmaterial.com

Perl Interview Questions Answers

Question 1 : What is PERL?

Answer 1 : Perl is a high-level programming language with an eclectic heritage written by Larry Wall and a cast of thousands. It derives from the ubiquitous C programming language and to a lesser extent from sed, awk, the Unix shell, and at least a dozen other tools and languages. Perl's process, file, and text manipulation facilities make it particularly well-suited for tasks involving quick prototyping, system utilities, software tools, system management tasks, database access, graphical programming, networking, and world wide web programming. These strengths make it especially popular with system administrators and CGI script authors, but mathematicians, geneticists, journalists, and even managers also use Perl. Maybe you should, too.

Question 2 : How can I use Perl interactively?

Answer 2 : The typical approach uses the Perl debugger, described in the perldebug(1) manpage, on an "empty" program, like this: perl -de 42 Now just type in any legal Perl code, and it will be immediately evaluated. You can also examine the symbol table, get stack backtraces, check variable values, set breakpoints, and other operations typically found in symbolic debuggers.

Question 3 : Is there a Perl shell?

Answer 3 : The psh (Perl sh) is currently at version 1.8. The Perl Shell is a shell that combines the interactive nature of a Unix shell with the power of Perl. The goal is a full featured shell that behaves as expected for normal shell activity and uses Perl syntax and functionality for control-flow statements and other things. You can get psh at http://sourceforge.net/projects/psh/ . Zoidberg is a similar project and provides a shell written in perl, configured in perl and operated in perl. It is intended as a login shell and development environment. It can be found at http://pardus-larus.student.utwente.nl/~pardus/projects/zoidberg/ or your local CPAN mirror. The Shell.pm module (distributed with Perl) makes Perl try commands which aren't part of the Perl language as shell commands. perlsh from the source distribution is simplistic and uninteresting, but may still be what you want.

Question 4 : How do I debug my Perl programs?

Answer 4 : Before you do anything else, you can help yourself by ensuring that you let Perl tell you about problem areas in your code. By turning on warnings and strictures, you can head off many problems before they get too big. You can find out more about these in strict and warnings. #!/usr/bin/perl use strict; use warnings; Beyond that, the simplest debugger is the print function. Use it to look at values as you run your program: print STDERR "The value is [$value]\n"; The Data::Dumper module can pretty-print Perl data structures: use Data::Dumper qw( Dumper ); print STDERR "The hash is " . Dumper( \%hash ) . "\n"; Perl comes with an interactive debugger, which you can start with the -d switch. you have Tk, you can use ptkdb. It's on CPAN and available for free. If you need something much more sophisticated and controllable, Leon Brocard's Devel::ebug (which you can call with the -D switch as -Debug) gives you the programmatic hooks into everything you need to write your own (without too much pain and suffering). You can also use a commercial debugger such as Affrus (Mac OS X), Komodo from Activestate (Windows and Mac OS X), or EPIC (most platforms).

Question 5 : Why do you use Perl?

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

Question 6 : How to open and read data files with Perl?

Answer 6 : 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);

Question 7 : How do I do fill_in_the_blank for each file in a directory?

Answer 7 : 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"; }

Question 8 : How do I do fill_in_the_blank for each file in a directory?

Answer 8 : 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"; }

Question 9 : How do I generate a list of all .html files in a directory?

Answer 9 : Here's a snippet of code that just 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"; }

Question 10 : What is Perl one-liner?

Answer 10 : 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. Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}? ${var} is the lexical variable $var, and ${"var"} is the dynamic variable $var. Note that because the second is a symbol table lookup, it is disallowed under `use strict "refs"'. The words global, local, package, symbol table, and dynamic all refer to the kind of variables that local() affects, whereas the other sort, those governed by my(), are variously knows as private, lexical, or scoped variable.


Selected

 

Perl Contributors

Rajeev Katiyar
Sandeep Saxena

Share your email for latest updates

Name:
Email:

Our partners