• +91 9723535972
  • info@interviewmaterial.com

Perl Interview Questions and Answers

Perl Interview Questions and Answers

Question - 11 : - What happens when you return a reference to a private variable?

Answer - 11 : - Perl keeps track of your variables, whether dynamic or otherwise, and doesn't free things before you're done using them.

Question - 12 : - How to turn on Perl warnings? Why is that important?

Answer - 12 : - Perl is very forgiving of strange and sometimes wrong code, which can mean hours spent searching for bugs and weird results. Turning on warnings helps uncover common mistakes and strange places and save a lot of debugging time in the long run. There are various ways of turning on Perl warnings: For Perl one-liner, use -w option on the command line. On Unix or Windows, use the -w option in the shebang line (The first # line in the script). Note: Windows Perl interpreter may not require it. For other systems, choose compiler warnings, or check compiler documentation.

Question - 13 : - What are scalar data and scalar variables?

Answer - 13 : - Perl has a flexible concept of data types. Scalar means a single thing, like a number or string. So the Java concept of int, float, double and string equals to Perl\'s scalar in concept and the numbers and strings are exchangeable. Scalar variable is a Perl variable that is used to store scalar data. It uses a dollar sign $ and followed by one or more alphanumeric characters or underscores. It is case sensitive.

Question - 14 : - Why should I use the -w argument with my Perl programs?

Answer - 14 : - Many Perl developers use the -w option of the interpreter, especially during the development stages of an application. This warning option turns on many warning messages that can help you understand and debug your applications. To use this option on Unix systems, just include it on the first line of the program, like this: #!/usr/bin/perl -w If you develop Perl apps on a DOS/Windows computer, and you're creating a program named myApp.pl, you can turn on the warning messages when you run your program like this: perl -w myApp.pl Assuming $_ contains HTML, which of the following substitutions will remove all tags in it? 1.s/<.*>//g; 2.s/<.*?>//gs; 3.s/<\/?[A-Z]\w*(?:\s+[A-Z]\w*(?:\s*=\s*(?:(["']).*?\1|[\w-.]+))?)*\s*>//gsix; You can't do that. If it weren't for HTML comments, improperly formatted HTML, and tags with interesting data like < SCRIPT >, you could do this. Alas, you cannot. It takes a lot more smarts, and quite frankly, a real parser. I want users send data by formmail but when they send nothing or call it from web site they will see error. codes in PHP like this: if (isset($HTTP_POST_VARS)){ .......... } else{ echo ("error lalalalal") }

Question - 15 : - How it will look in perl? 

Answer - 15 : - In php it will be like if (isset($HTTP_POST_VARS)){ .... } In perl, tried this. if ($ENV{'REQUEST_METHOD'} eq 'POST'){ ..... }

Question - 16 : - What is the output of the following Perl program? 1 $p1 = "prog1.java"; 2 $p1 =~ s/(.*)\.java/$1.cpp/; 3 print "$p1\n";

Answer - 16 : - prog1.cpp

Question - 17 : - Why aren't Perl's patterns regular expressions?

Answer - 17 : - Because Perl patterns have backreferences. A regular expression by definition must be able to determine the next state in the finite automaton without requiring any extra memory to keep around previous state. A pattern /([ab]+)c\1/ requires the state machine to remember old states, and thus disqualifies such patterns as being regular expressions in the classic sense of the term.

Question - 18 : - What does Perl do if you try to exploit the execve(2) race involving setuid scripts?

Answer - 18 : - Sends mail to root and exits. It has been said that all programs advance to the point of being able to automatically read mail. While not quite at that point (well, without having a module loaded), Perl does at least automatically send it.

Question - 19 : - How do I do < fill-in-the-blank > for each element in a hash?

Answer - 19 : - Here's a simple technique to process each element in a hash: #!/usr/bin/perl -w %days = ( 'Sun' =>'Sunday', 'Mon' => 'Monday', 'Tue' => 'Tuesday', 'Wed' => 'Wednesday', 'Thu' => 'Thursday', 'Fri' => 'Friday', 'Sat' => 'Saturday' ); foreach $key (sort keys %days) { print "The long name for $key is $days{$key}.\n"; }

Question - 20 : - How do I sort a hash by the hash key?

Answer - 20 : - Suppose we have a class of five students. Their names are kim, al, rocky, chrisy, and jane. Here's a test program that prints the contents of the grades hash, sorted by student name: #!/usr/bin/perl -w %grades = ( kim => 96, al => 63, rocky => 87, chrisy => 96, jane => 79, ); print "\n\tGRADES SORTED BY STUDENT NAME:\n"; foreach $key (sort (keys(%grades))) { print "\t\t$key \t\t$grades{$key}\n"; } The output of this program looks like this: GRADES SORTED BY STUDENT NAME: al 63 chrisy 96 jane 79 kim 96 rocky 87 }


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners