Perl Interview Questions & Answers part 1


Q: – Can you suggest a quick way to find a particular string in an array element?

Iterating over the array and checking each element are the usual ways to do so. If you frequently find yourself searching an array to see whether an element is in the array, you probably didn't want to store the data in an array in the first place.A much more efficient structure for random-element access is a hash.

Q: – Why doesn't glob("*.*") match all the files in a directory?

Because "*.*" matches only filenames with a dot in them. To match all the files in a directory, use glob("*"). The glob function's patterns are designed to be portable across many operating systems and thus do not behave the same as the *.* pattern in DOS.

Q: –How do I lock DBM files?

DBM files can be locked using the semaphore locking system. Use the get_lock() and release_lock().

Q: – How to see whether a value only consists of alphabetic characters?

use locale and match against a negated character class.

use locale;
if ($var =~ /^[^\W\d_]+$/) {
print "var is purely alphabetic\n";


if ($var =~ /^[A-Za-z]+$/) {
# it is purely alphabetic

Q: – What's the difference between an associative array and a hash?

Hashes and associative arrays are identical.

Q: – My open statement keeps failing, and I'm not sure why. What's wrong?

First, check the syntax of the open statement. Make sure you're opening the right filename. Print the name before the open if you need to be sure. If you intend to write to the file, make sure you put a > in front of the filename; you need to. Most importantly, did you check the exit status of open by using open() || die "$!"; syntax? The die message might be very important in helping you find your mistake.

Q: – What kinds of data are best suited for hashes?

Lists of key-value pairs

Q: – What value is stored in $c after running the following code?


Submitted By:-Sameer Dahiya            Email-ID: –