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.
Showing 5 of 25 questions
21. How do I replace every character in a file with a comma?
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/';
22. 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.
23. What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), \$ap ]; }' ?
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.
24. How do you match one letter in the current locale?
/[^\W_\d]/
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.
25. How do you give functions private variables that retain their values between calls?
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