Tuesday, February 16, 2010

Perl interview questions Part #3

1) What is the difference between die and exit?

Answer:
Die prints out stderr message in the terminal before exiting the program. Die also can evaluate expression before exiting.
Exit just terminates the program

2) Where do you go for perl help?

Answer: perldoc command with -f option is the best. 

3) What is scalar, array and hash in perl?

Answer:
Perl has 3 command data type:

$scalar is the basic variable. It could be letters, digits and underscore characters

$array is a collection of scalar.
e.g. @fruit = ("apple", "bananas", "orange");

foreach $goodfruit (@fruit) {
    if ($goodfruit eq "apple")
{
    print ("found $goodfruit in the list\n");
}
}

%hash: it is like an associative array. It is a collection of scalar data with individual elements selected by some index value which essentially are scalars and called as keys. Each key corresponds to some values.

e.g.

%fruit = ( "apple"=>17,
                "bananas"=>9,
                "oranges"=>"none");

foreach $holder (keys(%fruit))
{
     $numfruit = %fruit{$holder};
    print ("$holder = $numfruit \n");
}

4) What does 'qw()' mean? What is the use of it?

Answer:
qw is a construct which quotes word delimited by spaces.
e.g. @array = qw(1234);   # same as @array=("1","2","3","4");

5) What is the difference between Perl and shell script?

Answer:
Whatever you can do in the shell script, it can be done in Perl. However, Perl gives you an extened advantages of having enormous library. You don't need to write everything from scartch.

6) What is stderr() in Perl?

Answer:
A special file handler to standard error in any package.

7) What is a regular expression?

Answer:
It defines a pattern for a search to match.

Simple String Comparisons

The most basic string comparison is
$string =~ m/sought_text/;
The above returns true if string $string contains substring "sought_text", false otherwise.

If you want only those strings where the sought text appears at the very beginning, you could write the following:
$string =~ m/^sought_text/;

Similarly, the $ operator indicates "end of string". If you wanted to find out if the sought text was the very last text in the string, you could write this:
$string =~ m/sought_text$/;

Now, if you want the comparison to be true only if $string contains the sought text and nothing but the sought text, simply do this:
$string =~ m/^sought_text$/;

Now what if you want the comparison to be case insensitive? All you do is add the letter i after the ending delimiter:
$string =~ m/^sought_text$/i;

Using Simple "Wildcards" and "Repetitions"

Calling these "wildcards" may actually conflict with the theoretical grammar and syntax of Perl, but in fact is the most intuitive way to think of it, and will not lead to any coding mistakes.
.   Match any character
\w  Match "word" character (alphanumeric plus "_")
\W  Match non-word character
\s  Match whitespace character
\S  Match non-whitespace character
\d  Match digit character
\D  Match non-digit character
\t  Match tab
\n  Match newline
\r  Match return
\f  Match formfeed
\a  Match alarm (bell, beep, etc)
\e  Match escape
\021  Match octal char ( in this case 21 octal)
\xf0  Match hex char ( in this case f0 hexidecimal)

You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here's where you start getting some power:
*      Match 0 or more times
+      Match 1 or more times
?      Match 1 or 0 times
{n}    Match exactly n times
{n,}   Match at least n times
{n,m}  Match at least n but not more than m times
Now for some examples:
$string =~ m/\s*rem/i;   #true if the first printable text is rem or REM
$string =~ m/^\S{1,8}\.\S{0,3}/;   # check for DOS 8.3 filename 
                                   #  (note a few illegals can sneak thru
 
 
 

2 comments:

  1. This is really really concise stuff on perl. Thanks Roy...

    ReplyDelete
  2. Perl Interview Questions and Answers
    http://allinterviewquestionsandanswerspdf.blogspot.in/2016/06/top-41-perl-interview-questions-and.html

    ReplyDelete

Search This Blog