Top 50 Perl Scripting Interview Questions
1. How many type of variable in perl
Perl has three built in variable types
Scalar
Array
Hash
2. What is the different between array and hash in perl
Array is an order list of values position by index.
Hash is an unordered list of values position by keys.
3. What is the difference between a list and an array?
A list is a fixed collection of scalars. An array is a variable that holds a variable collection of scalars.
4. what is the difference between use and require in perl
Use :
1. The method is used only for the modules(only to include .pm type file)
2. The included objects are varified at the time of compilation.
3. No Need to give file extension.
Require:
1. The method is used for both libraries and modules.
2. The included objects are varified at the run time.
3. Need to give file Extension.
5. How to Debug Perl Programs
Start perl manually with the perl command and use the -d switch, followed by your script and any arguments you wish to pass to your script:
“perl -d myscript.pl arg1 arg2”
6. What is a subroutine?
A subroutine is like a function called upon to execute a task.
subroutine is a reusable piece of code.
7. what does this mean ‘$^0’? tell briefly
$^ – Holds the name of the default heading format for the default file handle. Normally, it is equal to the file handle’s name with _TOP appended to it.
8. What is the difference between die and exit in perl?
1) die is used to throw an exception
exit is used to exit the process.
2) die will set the error code based on $! or $? if the exception is uncaught.
exit will set the error code based on its argument.
3) die outputs a message
exit does not.
9. How to merge two array?
@a=(1, 2, 3, 4);
@b=(5, 6, 7, 8);
@c=(@a, @b);
print “@c”;
10. Adding and Removing Elements in Array
Use the following functions to add/remove and elements:
push(): adds an element to the end of an array.
unshift(): adds an element to the beginning of an array.
pop(): removes the last element of an array.
shift() : removes the first element of an array.
11. How to get the hash size
%ages = (‘Martin’ => 28, ‘Sharon’ => 35, ‘Rikke’ => 29);
print “Hash size: “,scalar keys %ages,”\n”;
12. Add & Remove Elements in Hashes
%ages = (‘Martin’ => 28, ‘Sharon’ => 35, ‘Rikke’ => 29);
# Add one more element in the hash
$age{‘John’} = 40;
# Remove one element from the hash
delete( $age{‘Sharon’} );
13. PERL Conditional Statements
The conditional statements are if and unless
14. Perl supports four main loop types:
While, for, until, foreach
15. There are three loop control keywords: next, last, and redo.
The next keyword skips the remainder of the code block, forcing the loop to proceed to the next value in the loop.
The last keyword ends the loop entirely, skipping the remaining statements in the code block, as well as dropping out of the loop.
The redo keyword reexecutes the code block without reevaluating the conditional statement for the loop.
16. Renaming a file
rename (“/usr/test/file1.txt”, “/usr/test/file2.txt” );
17. Deleting an existing file
unlink (“/usr/test/file1.txt”);
18. Explain tell Function
The first requirement is to find your position within a file, which you do using the tell function:
tell FILEHANDLE
tell
19. Perl Regular Expression
A regular expression is a string of characters that define the pattern
There are three regular expression operators within Perl
Match Regular Expression – m//
Substitute Regular Expression – s///
Transliterate Regular Expression – tr///
20. What is the difference between chop & chomp functions in perl?
chop is used remove last character, chomp function removes only line endings.
21. Email address validation – perl
if ($email_address =~ /^(\w¦\-¦\_¦\.)+\@((\w¦\-¦\_)+\.)+[a-zA-Z]{2,}$/)
{
print “$email_address is valid”;
}
else {
print “$email_address is invalid”;
}
22. Why we use Perl?
1.Perl is a powerful free interpreter.
2.Perl is portable, flexible and easy to learn.
23. Given a file, count the word occurrence (case insensitive)
open(FILE,”filename”);
@array= ;
$wor=”word to be found”;
$count=0;
foreach $line (@array)
{
@arr=split (/s+/,$line);
foreach $word (@arr)
{
if ($word =~ /s*$wors*/i)
$count=$count+1;
}
}
print “The word occurs $count times”;
24. Name all the prefix dereferencer in perl?
The symbol that starts all scalar variables is called a prefix dereferencer.
The different types of dereferencer are.
(i) $-Scalar variables
(ii) %-Hash variables
(iii) @-arrays
(iv) &-subroutines
(v) Type globs-*myvar stands for @myvar, %myvar.
25. What is the Use of Symbolic Reference in PERL?
$name = “bam”;
$$name = 1; # Sets $bam
${$name} = 2; # Sets $bam
${$name x 2} = 3; # Sets $bambam
$name->[0] = 4; # Sets $bam[0]
symbolic reference means using a string as a reference.
25. What is the difference between for & foreach, exec & system?
Both Perl’s exec() function and system() function execute a system shell command. The big difference is that system() creates a fork process and waits to see if the command succeeds or fails – returning a value. exec() does not return anything, it simply executes the command. Neither of these commands should be used to capture the output of a system call. If your goal is to capture output, you should use the $result = system(PROGRAM);
exec(PROGRAM);