Perl Interview Questions and Answers
Question - 61 : - Where the command line arguments are stored and if you want to read command-line arguments with Perl, how would you do that?
Answer - 61 : -
The command line arguments in Perl are stored in an array @ARGV.
$ARGV[0] (the first argument)
$ARGV[1] (the second argument) and so on.
$#ARGV is the subscript of the last element of the @ARGV array, so the number of arguments on the command line is $#ARGV + 1
Question - 62 : - Suppose an array contains @arraycontent=(‘ab’, ‘cd’, ‘ef’, ‘gh’). How to print all the contents of the given array?
Answer - 62 : -
@arraycontent=('ab', 'cd', 'ef', 'gh')
foreach (@arraycontent)
{
print "$_\n";
}
Question - 63 : - What is the use of -w, -t and strict in Perl?
Answer - 63 : -
When we use –w, it gives warnings about the possible interpretation errors in the script.
Strict tells Perl to force checks on the definition and usage of variables. This can be invoked using the use strict command. If there are any unsafe or ambiguous commands in the script, this pragma stops the execution of the script instead of just giving warnings.
When used –t, it switches on taint checking. It forces Perl to check the origin of variables where outside variables cannot be used in sub shell executions and system calls
Question - 64 : - Which has the highest precedence, List or Terms? Explain?
Answer - 64 : -
Terms have the highest precedence in Perl. Terms include variables, quotes, expressions in parenthesis etc. List operators have the same level of precedence as terms. Specifically, these operators have very strong left word precedence.
Question - 65 : - List the data types that Perl can handle?
Answer - 65 : -
Scalars ($): It stores a single value.
Arrays (@): It stores a list of scalar values.
Hashes (%): It stores associative arrays which use a key value as index instead of numerical indexes
Question - 66 : - Write syntax to use grep function?
Answer - 66 : -
grep BLOCK LIST
grep (EXPR, LIST)
Question - 67 : - What is the use of -n and -p options?
Answer - 67 : -
The -n and -p options are used to wrap scripts inside Loops. The -n option makes the Perl execute the script inside the loop. The -p option also used the same loop as -n loop but in addition to it, it uses continue. If both the -n and -p options are used together the -p option is given the preference.
Question - 68 : - What is the usage of -i and 0s options?
Answer - 68 : -
The -i option is used to modify the files in-place. This implies that Perl will rename the input file automatically and the output file is opened using the original name. If the -i option is used alone then no backup of the file would be created. Instead -i.bak causes the option to create a backup of the file.
Question - 69 : - Why Perl aliases are considered to be faster than references?
Answer - 69 : -
In Perl, aliases are considered to be faster than references because they do not require any dereferencing.
Question - 70 : - What do you mean by context of a subroutine?
Answer - 70 : -
It is defined as the type of return value that is expected. You can use a single function that returns different values.