PERL technical interview questions and answers are useful for candidates applying for scripting, automation, and system administration roles. PERL is widely used in text processing, network programming, automation scripts, and backend development, making it a common interview topic in IT companies. Recruiters from TCS, Wipro, Infosys, Cognizant, Capgemini, and Accenture often test candidates on PERL basics, syntax, regular expressions, arrays, hashes, file handling, subroutines, and modules. This guide provides clear explanations and examples for the most important PERL interview questions. Whether you are preparing for campus placements or experienced-level interviews, this resource will help you strengthen your scripting knowledge. You can also download PERL interview PDFs and practice mock questions to improve your performance.
1. Why do you use Perl?
Answer: Perl is a powerful free interpreter.
Perl is portable, flexible and easy to learn.
Show Answer
Hide Answer
2. How do I set environment variables in Perl programs?
Answer: 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'} = '/bin:/usr/bin:/usr/local/bin:/home/yourname/bin';
Show Answer
Hide Answer
3. Which of these is a difference between C++ and Perl?
Answer: 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
Show Answer
Hide Answer
4. What is Perl one-liner?
Answer: 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.
Show Answer
Hide Answer
5. Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?
Answer: ${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.
Show Answer
Hide Answer
6. What happens when you return a reference to a private variable?
Answer: Perl keeps track of your variables, whether dynamic or otherwise, and doesn't free things before you're done using them.
Show Answer
Hide Answer
7. What are scalar data and scalar variables?
Answer: 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.
Show Answer
Hide Answer
8. Why aren't Perl's patterns regular expressions?
Answer: Because Perl patterns have backreferences.
A regular expression by definition must be able to determine the next state in the finite automaton without requiring any extra memory to keep around previous state. A pattern /([ab]+)c\1/ requires the state machine to remember old states, and thus disqualifies such patterns as being regular expressions in the classic sense of the term.
Show Answer
Hide Answer
9. What does Perl do if you try to exploit the execve(2) race involving setuid scripts?
Answer: Sends mail to root and exits.
It has been said that all programs advance to the point of being able to automatically read mail. While not quite at that point (well, without having a module loaded), Perl does at least automatically send it.
Show Answer
Hide Answer
10. How do you print out the next line from a filehandle with all its bytes reversed?
print scalar reverse scalar
Answer: Surprisingly enough, you have to put both the reverse and the into scalar context separately for this to work.
Show Answer
Hide Answer
11. Why is it hard to call this function: sub y { "because" }
Answer: Because y is a kind of quoting operator.
The y/// operator is the sed-savvy synonym for tr///. That means y(3) would be like tr(), which would be looking for a second string, as in tr/a-z/A-Z/, tr(a-z)(A-Z), or tr[a-z][A-Z].
Show Answer
Hide Answer
12. What does read() return at end of file?
Answer: A defined (but false) 0 value is the proper indication of the end of file for read() and sysread().
Show Answer
Hide Answer
13.
What does `new $cur->{LINK}' do? (Assume the current package has no new() function of its own.)
Answer: $cur->new()->{LINK}
The indirect object syntax only has a single token lookahead. That means if new() is a method, it only grabs the very next token, not the entire following expression.
This is why `new $obj[23] arg' does't work, as well as why `print $fh[23] "stuff\n"' does't work. Mixing notations between the OO and IO notations is perilous. If you always use arrow syntax for method calls, and nothing else, you'll not be surprised.
Show Answer
Hide Answer
15. What value is returned by a lone `return;' statement?
Answer: The undefined value in scalar context, and the empty list value () 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.
Show Answer
Hide Answer
16. What's the difference between /^Foo/s and /^Foo/?
Answer: 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.
Show Answer
Hide Answer
17. How to dereference a reference?
Answer: 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
Show Answer
Hide Answer
18. What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash?
Answer: 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.
Show Answer
Hide Answer
19. If EXPR is an arbitrary expression, what is the difference between $Foo::{EXPR} and *{"Foo::".EXPR}?
Answer:
The second is disallowed under `use strict "refs"'.
Dereferencing a string with *{"STR"} is disallowed under the refs stricture, although *{STR} would not be. This is similar in spirit to the way ${"STR"} is always the symbol table variable, while ${STR} may be the lexical variable. If it's not a bareword, you're playing with the symbol table in a particular dynamic fashion.
Show Answer
Hide Answer
20. How do I do < fill-in-the-blank > for each element in an array?
Answer: #!/usr/bin/perl -w
@homeRunHitters = ('McGwire', 'Sosa', 'Maris', 'Ruth');
foreach (@homeRunHitters) {
print "$_ hit a lot of home runs in one year\n";
}
Show Answer
Hide Answer
21. How do I replace every character in a file with a comma?
Answer: perl -pi.bak -e 's/\t/,/g' myfile.txt
What is the easiest way to download the contents of a URL with Perl?
Once you have the libwww-perl library, LWP.pm installed, the code is this:
#!/usr/bin/perl
use LWP::Simple;
$url = get 'http://www.websitename.com/';
Show Answer
Hide Answer
22. When would `local $_' in a function ruin your day?
Answer: 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.
Show Answer
Hide Answer
23. What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), \$ap ]; }' ?
Answer: 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.
Show Answer
Hide Answer
24. How do you match one letter in the current locale?
/[^\W_\d]/
Answer: We don't have full POSIX regexps, so you can't get at the isalpha() 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.
Show Answer
Hide Answer
25. How do you give functions private variables that retain their values between calls?
Answer: Create a scope surrounding that sub that contains lexicals.
Only lexical variables are truly private, and they will persist even when their block exits if something still cares about them. Thus:
{ my $i = 0; sub next_i { $i++ } sub last_i { --$i } }
creates two functions that share a private variable. The $i variable will not be deallocated when its block goes away because next_i and last_i need to be able to access it
Show Answer
Hide Answer