Perl Interview Question
Perl Interview Question
3. What is the use of 'chomp' ? what is the difference between 'chomp' and 'chop'?
Ans. 'chop' functiononly removes the last character completely 'from the scaler, where as
'chomp' function only removes the last character if it is a newline. by default, chomp only
removes what is currently defined as the $INPUT_RECORD_SEPARATOR. whenever
you call 'chomp ', it checks the value of a special variable '$/'. whatever the value of '$/' is
eliminated from the scaler. by default the value of '$/' is 'n'
------------------------------------------------------------------
9. I have a variable named $objref which is defined in main package. I want to make it as
a Object of class XYZ. how could I do it?
Ans. use XYZ my $objref =XYZ -> new() OR, bless $objref, 'XYZ';
---------------------------------------------------------------
16. How to delete an entire directory containing few files in the directory?
Ans. rmtree($dir); OR, you can use CPAN module File::Remove Though it sounds like
deleting file but it can be used also for deleting directories. &File::Removes::remove
(1,$feed-dir,$item_dir);
-----------------------------------------------------------------------------------------------------------
17. What are the arguements we normally use for Perl Interpreter
Ans. -e for Execute, -c to compile, -d to call the debugger on the file specified, -T for
traint mode for security/input checking -W for show all warning mode (or -w to show
less warning)
-----------------------------------------------------------------------------------------------------------
-
47, Assume both a local($var) and a my($var) exist, what's the difference between ${var}
and ${"var"}?
Ans. ${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.
-----------------------------------------------------------------------------------------------------
50. Assuming $_ contains HTML, which of the following substitutions will remove all
tags in it?
Ans. 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.
-----------------------------------------------------------------------------------------------------------
---
51. 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?
Ans. In perl if ($ENV{'REQUEST_METHOD'} eq 'POST'){ ..... }
-----------------------------------------------------------------------------------------------------------
-
54. What does Perl do if you try to exploit the execve(2) race involving setuid scripts?
Ans. 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.
-----------------------------------------------------------------------------------------------------------
--
58. How do you print out the next line from a filehandle with all its bytes reversed?
Ans. print scalar reverse scalar Surprisingly enough, you have to put both the reverse and
the into scalar context separately for this to work.
-----------------------------------------------------------------------------------------------------------
--
65. What does `new $cur->{LINK}' do? (Assume the current package has no new()
function of its own.)
Ans. $cur->new()->{LINK} The indirect object syntax only has a single token
lookahead. That means if new() is a method, it only grabs the very next token, not the
entire following expression. This is why `new $obj[23] arg' does't work, as well as why
`print $fh[23] "stuff\n"' does't work. Mixing notations between the OO and IO notations
is perilous. If you always use arrow syntax for method calls, and nothing else, you'll not
be surprised.
--------------------------------------------------------------------------------------------------------
77. What is the easiest way to download the contents of a URL with Perl?
Ans. Once you have the libwww-perl library, LWP.pm installed, the code is this:
#!/usr/bin/perl use LWP::Simple; $url = get 'http://www.websitename.com/';
---------------------------------------------------------------------------------------------------------
82. Assume that $ref refers to a scalar, an array, a hash or to some nested data structure.
Explain the following statements:
Ans. $$ref; # returns a scalar $$ref[0]; # returns the first element of that array $ref- > [0];
# returns the first element of that array @$ref; # returns the contents of that array, or
number of elements, in scalar context $&$ref; # returns the last index in that array $ref- >
[0][5]; # returns the sixth element in the first row @{$ref- > {key}} # returns the
contents of the array that is the value of the key "key"
------------------------------------------------------------------------------------------------------
84. Perl uses single or double quotes to surround a zero or more characters. Are the
single(' ') or double quotes (" ") identical?
Ans. They are not identical. There are several differences between using single quotes
and double quotes for strings. 1. The double-quoted string will perform variable
interpolation on its contents. That is, any variable references inside the quotes will be
replaced by the actual values. 2. The single-quoted string will print just like it is. It
doesn't care the dollar signs. 3. The double-quoted string can contain the escape
characters like newline, tab, carraige return, etc. 4. The single-quoted string can contain
the escape sequences, like single quote, backward slash, etc.
-----------------------------------------------------------------------------------------------------------
-----------
86. How do you give functions private variables that retain their values between calls?
Ans. Create a scope surrounding that sub that contains lexicals. Only lexical variables are
truly private, and they will persist even when their block exits if something still cares
about them. Thus: { my $i = 0; sub next_i { $i++ } sub last_i { --$i } } creates two
functions that share a private variable. The $i variable will not be deallocated when its
block goes away because next_i and last_i need to be able to access it.
-----------------------------------------------------------------------------------------------------------
---
87. Explain the difference between the following in Perl: $array[3] vs. $array->[3]
Ans. because Perl's basic data structure is all flat, references are the only way to build
complex structures, which means references can be used in very tricky ways. This
question is easy, though. In $array[3], "array" is the (symbolic) name of an array
(@array) and $array[3] refers to the 4th element of this named array. In $array->[3],
"array" is a hard reference to a (possibly anonymous) array, i.e., $array is the reference to
this array, so $array->[3] is the 4th element of this array being referenced.
------------------------------------------------------------------------------------------------------