Thursday, June 28, 2012

Perl Interview Questions Part 3

Q:Why should I use the -w argument with my Perl programs?
A: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

Q:Assuming $_ contains HTML, which of the following substitutions will remove all tags in it?
A:1.s/<.*>//g;
2.s/<.*?>//gs;
3.s/</?[A-Z]*(?:+[A-Z]*(?:*=*(?:(["']).*?1|[.]+))?)**>//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.
 
Q:How it will look in perl?

A:In php it will be like
if (isset($HTTP_POST_VARS))
….

In perl, tried this.
if ($ENV‘REQUEST_METHOD’ eq ‘POST’)
…..

Q:What is the output of the following Perl program?
A:1 $p1 = “prog1.java”;
2 $p1 =~ s/(.*).java/$1.cpp/;
3 print “$p1”;
prog1.cpp

Q:Why aren’t Perl’s patterns regular expressions?
A:Because Perl patterns have back references.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]+)c1/ requires the state machine to remember old states, and thus disqualifies such patterns as being regular expressions in the classic sense of the term.

No comments:

Post a Comment