Perl Interview Questions & Answers part 2


Q: – The pattern /\W(\w)+\W/ doesn't seem to match all the words on the line, just the ones in the middle. Why?

You're looking for word characters surrounded by nonword characters. The first word of the line—assuming it starts at the beginning of the line—doesn't have a nonword character in front of it. It doesn't have a character in front of it at all.

Q: – What's the difference between m// and //?

There's almost no difference at all. The only difference is that if you decide to specify a pattern delimiter other than /, you can do so only if you precede the pattern with an m—for example, m!pattern!.

Q: – Concatenation can be performed only with the concatenation operator (.), is it true or false ?


Q: – How many for (while, if) blocks can I nest inside each other?

As many as you like, within memory restrictions of your system. Usually, however, if you have deeply nested loops, it a sign that you should approach the problem differently.

Q: – What's an efficient way to swap the values contained in two scalar variables $a and $b?

($a,$b)=($b, $a);
$c=$a; $a=$b; $b=$c;
b – ($a,$b)=($b, $a);

Q: – What does the statement $a=@array; assign to the variable $a?

The number of elements in @array
The index of the last element of @array
That syntax is not valid
The number of elements in @array

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: – Can you suggest a convenient way to write my hash into a file?

Yes. Modules such as Data::Dumper or Storable can reformat data types such as hashes and arrays into easily storable scalar values, which can be written to text files. These modules also have functions that take those formatted scalars and re-create the original structure you stored.

Q: – I found my program under Windows using the Explorer and double-clicked on it. A DOS window opened, printed something, and then immediately closed. What's wrong?

Nothing! The Perl interpreter is associated with the .pl extension as a DOS-mode program. When you clicked on it, a DOS window was opened and the interpreter ran your program and then immediately exited. (It was a short program.) If you actually need to see your Perl program run, open a window and run it manually.

Submitted By:-Sameer Dahiya            Email-ID: –