Professional Documents
Culture Documents
com Perl
Interview Questions
Perl
Interview Questions and Answers
1 of 13
www.sybaserays.com Perl
Interview Questions
Example:
if( defined $opts{'f'} ) {
$Force_Rerun = $opts{'f'};
} else {
$Force_Rerun = "N";
}
Note: MUST be first line of the file.. no comments, no spaces prior to the shebang
Standard: #!/usr/bin/perl
Note: The pound sign "#" is the symbol for comment entry. There is no multiline comment entry , so
you have to use repeated # for each line.
IMPORTANT NOTES:
Quoting Strings:
With ' (apostrophe): Everything is interpreted literally
Single quotation marks are used to enclose data you want taken literally. Just as the <code></code>
tags here at the Monastery make all text they enclose literally rendered, whitespace and all, so too
does a set of single-quotes in Perl ensure that what they enclose is used literally:
#!/usr/bin/perl -w
use strict;
my $foo;
my $bar;
$foo = 7;
$bar = 'it is worth $foo';
print $bar;
#!/usr/bin/perl -w
use strict;
my $foo;
my $bar;
$foo = 7;
$bar = "it is worth $foo";
print $bar;
Note: Double-quotes interpolate scalar and array variables, but not hashes. On the other hand, you
can use double-quotes to interpolate slices of both arrays and hashes.
With ` (backtick):
The text is executed as a separate process, and the output of the command is returned as the value of
the string
Quote
quoting without quotes:
3 of 13
www.sybaserays.com Perl
Interview Questions
In Perl, you can use methods other than quotation marks to "quote" a string. This functionality makes
using strings that contain quotation marks much easier sometimes, since those quotation marks no
longer need to be escaped. There are three simple methods of doing this with the letter q.
q - singly quote a string :
The first way to quote without quotes is to use q() notation. Instead of using quotation marks, you
would use parentheses with a q preceding them:
#!/usr/bin/perl -w
use strict;
my $foo;
my $bar;
$foo = 7;
$bar = q(it is 'worth' $foo);
print $bar;
it is 'worth' $foo
it is "worth" 7
5 of 13
www.sybaserays.com Perl
Interview Questions
7 of 13
www.sybaserays.com Perl
Interview Questions
eq equal to ==
ne not equal to !=
Operator Operation
||, or logical or
Operator Operation
www.sybaserays.com Perl
Interview Questions
. string concatenation
Function Calls:
$Name = &getname();
The symbol & should precede the function name in any function call.
Parameters of Functions:
We can pass parameter to the function as a list .
The parameter is taken in as a list which is denoted by @_ inside the function.
So if you pass only one parameter the size of @_ list will only be one variable. If you pass two
parameters then the @_ size will be two and the two parameters can be accessed by
$_[0],$_[1] ....
Q. What is regex?
A.
A regular expression (or regex) is a simple, rather mindless way of matching a series of
symbols to a pattern you have in mind.
The basic ideas of regex are simple, but powerful
We want to know whether a text string matches a pattern.
Example:
$mystring=~/blahblah/ ;
Matching Anything
/sh[ou]t/
9 of 13
www.sybaserays.com Perl
Interview Questions
/sh.t/
Matching several characters
/sh.+t/
/sh.*t/
Escaping confusion
/a \+ b/
/a \.b/
/a \* b/
Case iNseNSItivITy
/sensitive/
/sensitive/ I
s - Treat the whole string as one line, so that even /./ will match a "newline" character.
More matching tricks
? - matches zero or one of the preceding character
{n} - matches n copies of the preceding character!
{n,m} - matches at least n but not more than m copies of the preceding character
{n,} - matches at least n copies of the preceding character
IMPORTANT NOTES:
ANCHORS:
^ Match string start (or line, if /m is used)
$ Match string end (or line, if /m is used) or before newline
\b Match word boundary (between \w and \W)
\B Match except at word boundary (between \w and \w or \W and \W)
\A Match string start (regardless of /m)
\Z Match string end (before optional newline)
QUANTIFIERS
Match 0 or more times (*)
Match 0 or 1 time (?)
Match 1 or more times (+)
Match exactly n times ({n} )
Match at least n times {n,}
Match at least n, but not more than m, times
{n,m}
VARIABLES :
$` Everything prior to matched string
$& Entire matched string
$ Post match
$1, $2 ... hold the Xth captured expr
11 of 13
www.sybaserays.com Perl
Interview Questions
File Processing:
Reading (< $file&path)
Writing and overwriting (> $file&path)
Writing and Appending (>> $file&path )
Read and Write only, no file creation/appending. (+<$file&path )
Read, Write, Create, overwrites rather than appending (+>$file&path )
Read, Write, Create, Append, no overwriting. (+>>$file&path )
File Slurping
File Locking
use Fcntl qw(:flock);
open(DAT,"$sitedata") || die("Cannot Open File");
flock(DAT, LOCK_EX);
close(DAT);
FILE VALIDATION:
-e part is the existence test
Readable: -r
Writable: -w
Executable: -x
Text File: -T
Binary File: -B
Entry is a socket : -S
Entry is a directory: -d
Entry is a plain file: -f
Entry is a symbolic link: -l
File exists and has zero size (always false for directories) : -z
Error Handling:
Warn
Die ($!)
Installing CPAN Module:
perl -MCPAN -e shell
install MODULE::NAME
www.sybaserays.com Perl
Interview Questions
Or single line
perl -MCPAN -e 'install HTML::Template'
Module Usage:
Use Module
- The method is used only for the modules(only to include .pm type file)
- The included objects are varified at the time of compilation
- No Need to give file extension.
Require Module
- The method is used for both libraries and modules.
- The included objects are varified at the run time.
- Need to give file Extension.
13 of 13