PERL FAQ'S :
Click on the question to view answerAns : Easy to use and fast execution since perl script undergoes only two phases like compile phase and run phase.
Ans : Chop removes last character.
Chomp removes new line character.
Ans : It is array having attributes and values.
Ans : By using Translation operator (tr///). Number of replacements can be obtained by assigning whole expression to scalar variable.
Ans : open HANDLER, “< filename” or die $!;
Ans : Only syntax differs.
Ans : List out the warnings if any in perl program.
Ans : "use" the controler will go to that file during run time and come back to original program at the same time if we consider the "require" the whole file will get copied at the place where it is required.
Ans : The variables declared with "my" can live only within the block it was defined.
The variables declared with "local" can live within the block and have its visibility in the functions called within that block.
Ans : #! /usr/bin/perl
Ans : Compiler phase and Run phase.
Ans : $_
Ans : #! /usr/bin/perl -c
Ans : #! /usr/bin/perl -v
Ans : sprintf
Ans : chomp();
Ans : Ord();
Ans : QW is quote word which mainly used to avoid complexity in lists.
Ans : print ( map lc ,A,B,C);
Ans : Array is obtained by assigning list to variable.
Ans : Last index of an array.
Ans : By assigning an array to any scalar variables.
a. push b. pop c. shift d. unshift
Ans :
a. push = updates the values at the end of an array.
b. pop = takes out the last element of an array.
c. shift =takes out the first element of an array.
d. unshift = updates the values at the beginning of an array.
Ans : @num = (1,2,3,4,5);
@val = (6,7);
splice (@num ,4,0,@val);
Ans : Using {}
Ans : Using key words Keys and Values.
Ans : undef
Ans : delete
Ans : Bitwise and
Ans “ =~”
a. last b. next c. redo
Ans :
a. last : used to exit from the statement block.
b. next : used to skip the rest of the statements block and start the next iteration.
c. redo : causes perl to restart the current statement block.
Ans : m// , s/// ,tr///
Ans : Through variable @_.
Ans : @_
Ans : Last successfully executed regular expression is used.
Ans : SEARCHLIST
Ans : By assigning this expression to another variable like
$count=($value=~tr/SEARCHLIST/REPLACEMENTLIST/)
Ans : Using c modofier
Ans : m/\metacharacter/
Ans : Matches any digit.
a. [a-z] b. [a-zA-Z] c. [^0-9a-zA-Z] d. [0-9]
Ans : C
Ans : \s
Ans : \u changes next character to uppercase.
\U changes following characters to uppercase.
$var=~/w[^aoi]nder/ & $var=~/^w[aoi]nder/
Ans : first one will look for W followed by something that is none of 'a' , 'o' , or 'i'
second one will matches at the begining of the line.
$txt , $1 , $2 , $3
Ans : $1 = Testing, $2=T, $3=ing
Ans : Quantifiers says how many times something may match.
a. * b. + c. ? d. {}
Ans : a
Ans : Assigning @ARGV to any scalar variables.
Ans : There we don't have a much difference between PERL and shell script.
As of my knowledge both are good in there own way of work environment.
As consern to speed & performance the PERL is a head of shell.
---Perl is very good at text processing like we have regular
expressions.
---Shell scripts are mainly inteded for sys-admin tasks.
Ans : By writing linux command in `` operater.
Ans : $var = `setenv $VAR path `;
Ans : foreach (< HANDLER >) {
command1;
command2;
.......
}
Ans : chdir(dir name);
$var = `find . -type d -name “*” | tee list `;
Ans: @num= qw(10 25 2 7 15);
Ascending order :
@result1 = sort {$a <=> $b} @num;
Descending order :
@result2 = sort {$b <=> $a} @num;
Ans. #! usr/bin/perl
use warnings;
$input = “file-path”;
open IN, $input or die “Error in Opening file:$!”;
while (<IN>)
{
print “line numbers $. is $_”;
}
Ans. #! /usr/bin/perl
open IN, ">out.txt" or die "ERROR IN OPENING :$!";
for $i (1..1000)
{
print IN "$i :: hello the time is ",
scalar (localtime), "\n";
}
Ans.
last : If it appears in the body of loop, will cause PERL immediately to exit the loop.
Next : When executed the remaining statements in the loop will be skipped and the next iteration will begin.
Ans. Because datatypes are not assigned to variables
Ans: Variable interpolation is in effect.
And: Using the (.) operator.
Ex: $newstring = $a.$b.$c;
Ans: The number gets added to ASCII value.
Ans: unshift(@XYZ,10);
Ans: Basically a method to specify string.
Refers to the pattern that follows the rules of syntax.
Powerful way to specify string patterns.
Ans. Consider we want to search for word. Some times we may need to search at beginning of the string, at end of the string and sometimes at word boundary.
Anchors are used for this.
^ => beginning of the strings
$ => End of the strings
\b => To a word boundary
Ans. It reads names of files from the command line and opens them all(read line by line).
Ans :#!usr/bin/perl
opendir (DIR.".");
@files =grep {/\.pl$/} readdir DIR;
closedir (DIR);
foreach (@files)
{
print "$_\n";
}
Ans. my $str = 'Hiii Heere';
$str =~ tr///cs;
print “$str”;
Ans. /(\w\w\w)\s\1/;
