Thursday, January 24, 2013

Perl Interview questions and Answers



Web based & Perl CGI
1.     What do you know about CGI.pm?
 CGI.pm is a large and widely used Perl module for programming Common Gateway Interface (CGI) web applications, providing a consistent API for receiving user input and producing HTML or XHTML output.
2.     How to set sessions and cookies and how to implement in Perl?


you'll define the cookie properties by calling the cookie method. In this example, the name of the cookie is MY_COOKIE, while the value of the cookie is "BEST_COOKIE=chocolatechip". You create a cookie just like this, by specifying the name and value of the cookie when using the cookie method:
$cookie = $query->cookie(-name=>'MY_COOKIE',
    -value=>'BEST_COOKIE=chocolatechip',
    -expires=>'+4h',
    -path=>'/');
get cookies:

$theCookie = $query->cookie('MY_COOKIE');
set session:
$session = new CGI::Session(DSN, SID, DSN_OPTIONS);
get session:
my $first_name  = $session->param("first_name"); 

3.     How to display JSON based output
4.     Why two \n in print “Content-type: text/html \n\n”;
Database oriented
1.     How to connect database using DBI?
2.     What are the ways you know to get records from the table (fetchrow_hashref/arrayref)

1. @row = $conn->fetchrow_array;

2. $row = $conn->fetchrow_arrayref;

3. $row = $conn->fetchrow_hashref;
fetchrow_array:
dbopen.pl

#!/usr/bin/perl
require "dbconfig.pl";
$dbconnect = DBI->connect($dbase,$user,$pass) or die "connection Error:$DBI::errstr\n";
dbconfig.pl
#!/usr/bin/perl
$dbase = 'dbi:mysql:siva';
$user = 'root';
$pass = 'siva123';
dbclose.pl
#!/usr/bin/perl
$dbconnect->disconnect();
Test.pl
#!/usr/bin/perl
use DBI;
require "dbopen.pl";
$sql = "select id,emp_fname,emp_lname,emp_id from stech_emp limit 30,10";
$conn =$dbconnect->prepare($sql);
$conn->execute or die "SQL Error:$DBI::errstr\n";
while (@row = $conn->fetchrow_array) {

print "$row[0]\t$row[1]\t$row[3]\n";


}

$conn->finish();
require "dbclose.pl";

OUTPUT:

root@itadmin-desktop:/var/www/cgi# perl Test.pl
44 Loganayagi 1010
45 Amala 1014
46 Vidhya 1009
47 Nithya 4006
48 Vanitha 1020
49 Saraswathi 1021
50 Maheswari 4009
51 Kalpana 4010
52 Renuga 4017
53 Nisha 4015
fetchrow_arrayref :
dbopen.pl

#!/usr/bin/perl
require "dbconfig.pl";
$dbconnect = DBI->connect($dbase,$user,$pass) or die "connection Error:$DBI::errstr\n";
dbconfig.pl
#!/usr/bin/perl
$dbase = 'dbi:mysql:siva';
$user = 'root';
$pass = 'siva123';
dbclose.pl
#!/usr/bin/perl
$dbconnect->disconnect();
Test1.pl
#!/usr/bin/perl
use DBI;
require "dbopen.pl";
$sql = "select id,emp_fname,emp_lname,emp_id from stech_emp limit 30,10";
$conn =$dbconnect->prepare($sql);
$conn->execute or die "SQL Error:$DBI::errstr\n";
while (@row = $conn->fetchrow_arrayref) {

print "$row->[0]\t$row->[1]\t$row->[3]\n";


}

$conn->finish();
require "dbclose.pl";

OUTPUT:

root@itadmin-desktop:/var/www/cgi# perl Test1.pl
44 Loganayagi 1010
45 Amala 1014
46 Vidhya 1009
47 Nithya 4006
48 Vanitha 1020
49 Saraswathi 1021
50 Maheswari 4009
51 Kalpana 4010
52 Renuga 4017
53 Nisha 4015
fetchrow_hashref :

dbopen.pl

#!/usr/bin/perl
require "dbconfig.pl";
$dbconnect = DBI->connect($dbase,$user,$pass) or die "connection Error:$DBI::errstr\n";
dbconfig.pl
#!/usr/bin/perl
$dbase = 'dbi:mysql:siva';
$user = 'root';
$pass = 'siva123';
dbclose.pl
#!/usr/bin/perl
$dbconnect->disconnect();
Test2.pl
#!/usr/bin/perl
use DBI;
require "dbopen.pl";
$sql = "select id,emp_fname,emp_id from stech_emp limit 30,10";
$conn =$dbconnect->prepare($sql);
$conn->execute or die "SQL Error:$DBI::errstr\n";
while ($row = $conn->fetchrow_hashref) {

print "$row->{id}\t$row->{emp_fname}\t$row->{emp_id}\n";


}

$conn->finish();
require "dbclose.pl";
OUTPUT:

root@itadmin-desktop:/var/www/cgi# perl Test2.pl
44 Loganayagi 1010
45 Amala 1014
46 Vidhya 1009
47 Nithya 4006
48 Vanitha 1020
49 Saraswathi 1021
50 Maheswari 4009
51 Kalpana 4010
52 Renuga 4017
53 Nisha 4015
Object Oriented Perl
1.     Diff b/w module and package
    * Packages are perl files with .pm extn and is considered a separate namespace. So a package is nothing but group of related scalars,arrays,hashes and subroutines for a specific purpose.
    Once a package is included in a .pl file (using "use") and you want to call one of the subroutines of the package, you may have to use the scope resolution operator &package::subroutine1
    * Modules are packages but which has the capabilities of exporting selective subroutines/scalars/arrays/hashes of the package to the namespace of the main package itself. So for the interpreter these look as though the subroutines are part of the main package itself and so there is no need to use the scope resolution operator while calling them.
2.     Diff b/w class and package (rarely asked though )
3.     How do you call any subroutine in object oriented fashion? Hint: bless operator
4.     Use base, EXPORT, EXPORT_OK
5.     Difference between ISA and EXPORT
6.     Use Base related questions
7.     Use of AUTOLOAD function.
Regular Expression in Perl
1.     IP Address validation
2.     Email id Validation
3.     More than one @ is there. Give regex to store username and domain name after encountering last @ (Last @ will be the splitting point and more than one special characters can be there )
4.     What are the uses of different modifiers

5.     Group, range, meta-character etc
6.     m, s, tr
7.     Replace the last ‘x’ in a string with ‘ax’ in one regex. Example : abcxdefgxgaxa should become abcxdefgxgaaxa
8.     Perl regular expression is greedy. Can you explain it with one example?
9.     Can you check palindrome condition using regular expression in perl? (Hint: regex doesn’t support recursion or counting facility )
Perl One Liner
1.     What is Perl one liner and where you will use it?
2.     What are the different options in Perl one Liner. Explain in details
3.     Add a blank line before every line.
4.     Remove blank lines form a file.
5.     Print the total number of lines in a file (emulate wc -l).
6.     Print the number of non-empty lines in a file.
7.     What option you will use to check syntax only without executing it?
8.     Which option is meant to enable all warnings or disable all warning inpite of using use warning or no warnings respectively?

2 comments:

  1. I am happy to see the answers of all my questions that I prepared while going through lots of interview on Perl.
    I feel one should give credit to the original author from where you are copying the questions or contents.

    I collected and prepared in very well manner in 24th April, 2012
    Here is the original source of these questions.
    http://www.aliencoders.com/content/interview-questions-perl-freshers-and-experienced/

    You could answer there as well to avoid any redundancy of same questions everywhere.

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

    ReplyDelete