Perl Interview Questions and Answers
We have listed of Perl Interview Questions and Answers that have been designed for Perl professionals who are preparing interviews on Perl Programming. Perl is sometimes called the “Practical Extraction and Report Language,” although it has also been called a “Pathologically Eclectic Rubbish Lister,” among other expan‐ sions. It’s actually a backronym, not an acronym—Larry Wall, Perl’s creator, came up with the name first and the expansion later.
Best Perl Interview Questions and Answers
Perl is easy to use, but sometimes hard to learn. This is a generalization, of course. In designing Perl, Larry made many trade-offs. When he’s had the chance to make something easier for the programmer at the expense of being more difficult for the student, he’s decided in the programmer’s favor nearly every time. Here are a few Perl interview questions that will help you crack an interview for getting dream job.
Why do you use Perl?
Perl is a powerful free interpreter. Perl is portable, flexible and easy to learn.
What is Perl one-liner?
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.
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.
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.
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.
Why should I use the -w argument with my Perl programs?
Many Perl developers use the -w option of the interpreter, especially during the development stages of an application. This warning option turns on many warning messages that can help you understand and debug your applications.
To use this option on Unix systems, just include it on the first line of the program, like this: #!/usr/bin/perl -w
If you develop Perl apps on a DOS/Windows computer, and you’re creating a program named myApp.pl, you can turn on the warning messages when you run your program like this:
perl -w myApp.pl
Which of these is a difference between C++ and Perl?
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.
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”.
How do you print out the next line from a filehandle with all its bytes reversed?
print scalar reverse scalar <FH>
Surprisingly enough, you have to put both the reverse and the <FH> into scalar context separately for this to work.
How do I send e-mail from a Perl/CGI program on a Unix system?
Sending e-mail from a Perl/CGI program on a Unix computer system is usually pretty simple. Most Perl programs directly invoke the Unix sendmail program. We’ll go through a quick example here. Assuming that you’ve already have e-mail information you need, such as the send-to address and subject, you can use these next steps to generate and send the e-mail message:
# the rest of your program is up here …
open(MAIL, "1/usr/lib/sendmail -t"); print MAIL "To: $sendToAddress\n"; print MAIL "From: $myEmailAddress\n"; print MAIL "Subject: $subject\n"; print MAIL "This is the message body.\n"; print MAIL "Put your message here in the body.\n"; close (MAIL);
How to read from a pipeline with Perl
Example 1:
To run the date command from a Perl program, and read the output of the command, all you need are a few lines of code like this:
open(DATE, "dater); $theDate = <DATE>; clo se(DATE);
The open() function runs the external date command, then opens a file handle DATE to the output of the date command.
Next, the output of the date command is read into the variable $theDate through the file handle DATE.
What value is returned by a lone ‘return;’ statement?
The undefined value in scalar context, and the empty list value 0 in list context.
This way functions that wish to return failure can just use a simple return without worrying about the context in which they were called.
What’s the difference between /^Foo/s and /^Foo/?
The second would match Foo other than at the start of the record if $* were set.
The deprecated $* flag does double duty, filling the roles of both /s and /m. By using /s, you suppress any settings of that spooky variable, and force your carets and dollars to match only at the ends of the string and not at ends of line as well — just as they would if $* weren’t set at all.
How to dereference a reference?
There are a number of ways to dereference a reference.
Using two dollar signs to dereference a scalar.
$original = $$strref; Using @ sign to dereference an array. @list = @$arrayref; Similar for hashes.
What does length(%HASH) produce if you have thirty-seven random keys in newly created hash?
5 length() is a built-in prototyped as sub length($), and a scalar prototype silently changes aggregates into radically different forms. The scalar sense of a hash is false (0) if it’s empty, otherwise it’s a string representing the fullness of the buckets, like “18/32” or “39/64”. The length of that string is likely to be 5. Likewise, length(@a)’ would be 2 if there were 37 elements in @a.
When would ‘local $_’ in a function ruin your day?
When your caller was in the middle for a while(m//g) loop
The /g state on a global variable is not protected by running local on it. That’ll teach you to stop using locals. Too bad $_ can’t be the target of a my() — yet.
What happens to objects lost in “unreachable” memory, such as the object returned by Ob>new() in ‘{ my $ap; $ap = [ Ob->new(), \Sap ]; }’ ?
Their destructors are called when that interpreter thread shuts down.
When the interpreter exits, it first does an exhaustive search looking for anything that it allocated. This allows Perl to be used in embedded and multithreaded applications safely, and furthermore guarantees correctness of object code.
Assume that $ref refers to scalar, an array, a hash or to some nested data structure. Explain the following statements:
$$ref; # returns a scalar $$ref[0]; # returns the first element of that array $ref- > [0]; # returns the first element of that array @$ref; # returns the contents of that array, or number of elements, in scalar context $&$ref; # returns the last index in that array $ref- > [0][5]; # returns the sixth element in the first row @{$ref- > {key} } # returns the contents of the array that is the value of the key "key"
How do you match one letter in the current locale?
We don’t have full POSIX regexps, so you can’t get at the isalpha() <ctype.h> macro save indirectly. You ask for one byte which is neither a non-alphanumunder, nor an under, nor a numeric. That leaves just the alphas, which is what you want.
How do I print the entire contents of an array with Perl?
To answer this question, we first need a sample array. Let’s assume that you have an array that contains the name of baseball teams, like this:
@teams = (‘cubs’, ‘reds’, ‘yankees’, ‘dodgers’);
If you just want to print the array with the array members separated by blank spaces, you can just print the array like this:
@teams = ('cubs', 'reds', 'yankees', 'dodgers'); print "@teams\n";
But that’s not usually the case. More often, you want each element printed on a separate line. To
achieve this, you can use this code:
@teams = ('cubs', 'reds', 'yankees', 'dodgers'); foreach (@teams) {
Thus, the compilation of these questions includes some of the major points that can be asked during an Perl interview. The interviewers generally check the base knowledge of a candidate, which can be gained by practicing these questions prior interview. We have prepared simple Perl Interview Questions and Answers that helps you in getting your dream career as Perl expert.