Perl Interview Questions and Answers
Question - 41 : - How do you match one letter in the current locale?
Answer - 41 : - /[^\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.
Question - 42 : - How do I print the entire contents of an array with Perl?
Answer - 42 : - To answer this question, we first need a sample array. Let's assume that you have an array that contains the name of baseball teams, like this:
@teams = ('cubs', 'reds', 'yankees', 'dodgers');
If you just want to print the array with the array members separated by blank spaces, you can just print the array like this:
@teams = ('cubs', 'reds', 'yankees', 'dodgers');
print "@teams\n";
But that's not usually the case. More often, you want each element printed on a separate line. To achieve this, you can use this code:
@teams = ('cubs', 'reds', 'yankees', 'dodgers');
foreach (@teams) {
print "$_\n";
}
Question - 43 : - Perl uses single or double quotes to surround a zero or more characters. Are the single(' ') or double quotes (" ") identical?
Answer - 43 : - They are not identical. There are several differences between using single quotes and double quotes for strings.
1. The double-quoted string will perform variable interpolation on its contents. That is, any variable references inside the quotes will be replaced by the actual values.
2. The single-quoted string will print just like it is. It doesn't care the dollar signs.
3. The double-quoted string can contain the escape characters like newline, tab, carraige return, etc.
4. The single-quoted string can contain the escape sequences, like single quote, backward slash, etc.
Question - 44 : -
How many ways can we express string in Perl?
Answer - 44 : - Many. For example 'this is a string' can be expressed in:
"this is a string"
qq/this is a string like double-quoted string/
qq^this is a string like double-quoted string^
q/this is a string/
q&this is a string&
q(this is a string)
Question - 45 : - Define Perl Scripting?
Answer - 45 : - In the IT market, Perl scripting is considered as a robust scripting language which is used in various fields. Perl is good at obtaining Regular expressions and in all the fields of application it is unique. Perl is a scripting language which is based on interpreter but not on the languages based on compiler. In all the applications, optimization is used.
Question - 46 : - What Is A Short Circuit Operator?
Answer - 46 : - The C-Style operator, ll, performs a logical (or) operation and you can use it to tie logical clauses together, returning an overall value of true if either clause is true. This operator is called a short-circuit operator because if the left operand is true the right operand is not checked or evaluated.
Question - 47 : - Difference between the variables in which chomp function work ?
Answer - 47 : -
Scalar: It is denoted by $ symbol. Variable can be a number or a string.
Array: Denoted by @ symbol prefix. Arrays are indexed by numbers.
The namespace for these types of variables is different. For Example: @add, $add. The scalar variables are in one table of names or namespace and it can hold single specific information at a time and array variables are in another table of names or namespace. Scalar variables can be either a number or a string
Question - 48 : - Create a function that is only available inside the scope where it is defined ?
Answer - 48 : -
$pvt = Calculation(5,5);
print("Result = $pvt\n");
sub Calculation{
my ($fstVar, $secndVar) = @_;
my $square = sub{
return($_[0] ** 2);
};
return(&$square($fstVar) + &$square($secndVar));
};
Output: Result = 50
Question - 49 : - Which feature of Perl provides code reusability ? Give any example of that feature.
Answer - 49 : -
Inheritance feature of Perl provides code reusability. In inheritance, the child class can use the methods and property of parent class
Package Parent;
Sub foo
{
print("Inside A::foo\n");
}
package Child;
@ISA = (Parent);
package main;
Child->foo();
Child->bar();
Question - 50 : - In Perl we can show the warnings using some options in order to reduce or avoid the errors. What are that options?
Answer - 50 : -
-The -w Command-line option: It will display the list if warning messages regarding the code.
– strict pragma: It forces the user to declare all variables before they can be used using the my() function.
– Using the built-in debugger: It allows the user to scroll through the entire program line by line.