Set - 1

Question 1 :

Why do you use Perl?

Answer :

Perl is a powerful free interpreter. 
Perl is portable, flexible and easy to learn.


Question 2 :

How do I set environment variables in Perl programs?

Answer :

you can just do something like this:

$path = $ENV{'PATH'};

As you may remember, "%ENV" is a special hash in Perl that contains the value of all your environment variables.
Because %ENV is a hash, you can set environment variables just as you'd set the value of any Perl hash variable. Here's how you can set your PATH variable to make sure the following four directories are in your path::

$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/home/yourname/bin';

 


Question 3 :

Which of these is a difference between C++ and Perl?

Answer :

Perl can have objects whose data cannot be accessed outside its class, but C++ cannot.
Perl can use closures with unreachable private data as objects, and C++ doesn't support closures. Furthermore, C++ does support pointer arithmetic via `int *ip = (int*)&object', allowing you do look all over the object. Perl doesn't have pointer arithmetic. It also doesn't allow `#define private public' to change access rights to foreign objects. On the other hand, once you start poking around in /dev/mem, no one is safe.


Question 4 :

How to open and read data files with Perl?

Answer :

Data files are opened in Perl using the open() function. When you open a data file, all you have to do is specify (a) a file handle and (b) the name of the file you want to read from.
As an example, suppose you need to read some data from a file named "checkbook.txt". Here's a simple open statement that opens the checkbook file for read access: open (CHECKBOOK, "checkbook.txt"); In this example, the name "CHECKBOOK" is the file handle that you'll use later when reading from the checkbook.txt data file. Any time you want to read data from the checkbook file, just use the file handle named "CHECKBOOK".
Now that we've opened the checkbook file, we'd like to be able to read what's in it. Here's how to read one line of data from the checkbook file:

$record = < CHECKBOOK > ;

After this statement is executed, the variable $record contains the contents of the first line of the checkbook file. The "<>" symbol is called the line reading operator.
To print every record of information from the checkbook file

open (CHECKBOOK, "checkbook.txt") || die "couldn't open the file!";
while ($record = < CHECKBOOK >) {
print $record;
}
close(CHECKBOOK);

 


Question 5 :

How do I do fill_in_the_blank for each file in a directory? 

Answer :

Here's code that just prints a listing of every file in the current directory:

#!/usr/bin/perl -w
opendir(DIR, ".");
@files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
	print "$file\n";
}

 


Question 6 :

How do I do fill_in_the_blank for each file in a directory?

Answer :

Here's code that just prints a listing of every file in the current directory:

#!/usr/bin/perl -w
opendir(DIR, ".");
@files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
	print "$file\n";
}

 


Question 7 :

How do I generate a list of all .html files in a directory?

Answer :

Here's a snippet of code that just prints a listing of every file in the current directory that ends with the extension .html:

#!/usr/bin/perl -w
opendir(DIR, ".");
@files = grep(/\.html$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
	print "$file\n";
}

 


Question 8 :

What is Perl one-liner?

Answer :

There are two ways a Perl script can be run:
--from a command line, called one-liner, that means you type and execute immediately on the command line. You'll need the -e option to start like "C:\ %gt perl -e "print \"Hello\";". One-liner doesn't mean one Perl statement. One-liner may contain many statements in one line.
--from a script file, called Perl program.


Question 9 :

Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}? 

Answer :

${var} is the lexical variable $var, and ${"var"} is the dynamic variable $var.
Note that because the second is a symbol table lookup, it is disallowed under `use strict "refs"'. The words global, local, package, symbol table, and dynamic all refer to the kind of variables that local() affects, whereas the other sort, those governed by my(), are variously knows as private, lexical, or scoped variable.


Question 10 :

What happens when you return a reference to a private variable?

Answer :

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


Question 11 :

How to turn on Perl warnings? Why is that important?

Answer :

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 12 :

What are scalar data and scalar variables?

Answer :

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 13 :

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

Answer :

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

 


Question 14 :

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;

Answer :

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.


Question 15 :

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")
}

How it will look in perl?

Answer :

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?

$p1 = "prog1.java";
$p1 =~ s/(.*)\.java/$1.cpp/;
print "$p1\n";

 

Answer :

prog1.cpp


Question 17 :

Why aren't Perl's patterns regular expressions?

Answer :

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 :

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 :

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 :

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