Perl interview questions part 1 /Perl Interview Questions and Answers for Freshers & Experienced

What does the’$_’ symbol mean?

The ‘$_’ is a default variable in Perl and $_ is known as the “default input and pattern matching space.

What is use of ‘->’ symbol?

The infix dereference operator in Perl is the ‘->’ symbol. The left-hand side must be a reference if the right-hand side is an array subscript, hash key, or function.

@array = qw/ abcde/; # array
print "n",$array->[0]; # it is wrong
print "n",$array[0]; #it is correct , @array is an array

What are the advantages of C over Perl?

C has a larger number of development tools than PERL. PERL programmes are slower to execute than C programmes. Although Perl looks to be an interpreted language, the code is generated dynamically. In contrast to C, if you don’t want anyone to use your Perl code, you must hide it in some way. It is difficult to create a Perl programme executable without the use of extra tools.

Describe Returning Values From Subroutines?

The value of last expression which is evaluated is the return value of subroutine or explicitly, a returned statement can be used to exit subroutine which specifies return value. This return value is evaluated in perfect content based on content of subroutine call.

Tell the Associate Arrays in Perl something and how important they are to the programmers?

It is essentially one of Perl’s most frequently used data type after Scalar and Array. They are very like a hash table and there are a lot of functions that are quite like the same one.

What is the use of -n and -p options?

Scripts are wrapped inside loops using the -n and -p parameters. The -n option instructs Perl to run the script within the loop. The -p option employed the same loop as the -n option, but it also used to continue. When both the -n and -p options are used at the same time, the -p option takes precedence.

Write syntax to use grep function?

grep BLOCK LIST
grep (EXPR, LIST)

Explain Different Types Of Eval Statements?

In general, there are two types of eval statements they are:
• Eval BLOCK and
• Eval EXPR
An expression is executed by eval EXPR and BLOCK is executed by eval BLOCK. Entire block is executed by eval block, BLOCK. When you want your code passed in expression then first one is used and to parse code in the block, second one is used.

How Shift Command Is Used?

The first value of an array shifted using shift array function and it is returned, which results in array shortening by one element and moves everything from a place to left. If an array is not specified to shift, shift uses @ ARGV, the command line arguments of an array is passed to script or to an array named @.

Can you add a module file in Perl and what are the functions that simply enable you to do so?

Yes, it is possible and there are “Require” “Or” and “Use”

How are parameters passed to subroutines in Perl?

In Perl, all input or actual parameters of the subroutine are stored in an array ‘@_’. In other words, array @_ is used as an alias for subroutine arguments.

Let’s demonstrate this with an example:

print &sum(1..4),”
”;
sub sum{
my $sum = 0;
for my $i(@_){
$sum += $i;
}
return $sum;
}
In this example, we are calculating the sum of elements 1 to 4. We pass these elements as a range to a subroutine. In the subroutine code, @_ that contains parameters is iterated to find the sum and then the sum is returned.

In Perl, Name Different Forms Of Goto And Explain?


In Perl, there are three different forms for goto, they are:
• goto name
• goto label
• goto expr
goto name is used along with subroutines, it is used only when it is required as it creates destruction in programs. It is the second form of label where Execution is transferred to a statement labeled LABEL using goto LABEL. The last label form is goto EXPR which expects EXPR to evaluate label.

Demonstrate subroutines in Perl with a simple example.

Let’s take an example of a subroutine to print “Hello,World!” string.

Sub print_str{
Print “Hello,World!”;
}
We can call this subroutine using the following statements:

print_str();

Output: Hello,World!

What is Subroutine in Perl?

Subroutine is a block of code that can be reused by a program either internally or externally.

A general representation of subroutine is as follows:
sub NAME PROTOTYPE ATTRIBUTES BLOCK
Here, the sub is a keyword followed by the subroutine name NAME.
PROTOTYPE represents the parameters for a subroutine.

ATTRIBUTES give additional semantics about subroutine. Value of attribute can be either be “locked”, ”method” or “lvalue”.

BLOCK is a block of code for the subroutine.
Once subroutine is defined, we can call it using the statement,

&subroutine_name;
The ampersand(&) is optional unless we are using references that refer to a subroutine name.

Subroutines in Perl can also be called as follows:
subroutine_name();

Differences between DIE and EXIT.

DIE and EXIT are two library functions in Perl to exit the program. The difference between DIE and EXIT is that DIE exits the program and prints a specified message. Exit simply exits the program.

Example

open(myfile,filename) ||DIE(“File cannot be opened
”);

The above line of code will print a message “File cannot be opened” in case if open fails and then exits the program.

Tell something about the Associate Arrays in Perl and how they are significant for the programmers?

It is basically one of the widely used data types in Perl after Scalar and Array. They are quite similar to that of a hash table and there are a lot of functions that are quite similar to that of the same.

In Perl, is it possible for the programmers to prefer a dynamic approach when it comes to loading the binary extension?

Yes, it is possible. The only need for this is the system a programmer is using must support it. The other option is to accomplish this task statically in case the system doesn’t allow the same. The dynamic approach can help users to save time as they are free to perform some basic tasks in their own way.

Differentiate between Arrays and List in Perl.

Both list and array can be defined as a set of elements. The main difference between a list and an array in Perl is that a list is immutable i.e. it cannot be altered directly.

In Perl, a list is an array without a name. Hence, most of the times array and list are used interchangeably. An array is mutable and its contents can grow, shrink in size, etc.

Thus in order to change the contents of a list, we can store the list as an array. An array is a variable that provides dynamic storage for a list.

What is chomp() operator/function?

chomp() operator removes the last character of a string and returns the number of characters removed.

chomp() operator is useful while reading input data from the console where it can be used to remove a newline (
) character.

For Example,

$str = <STDIN>; #enter hello through standard input and press Enter.
chomp($str);
This will chomp the ‘
’ character that was entered after hello.

What are the different string manipulation operators in Perl?

Perl provides two different operators to manipulate strings.

Concatenation Operator (.): Combines two strings to form a result string.
Repetition Operator (x): Repeats string for a specified number of times.

Example

$str1 = “abc”;
$str2 = “def”;
$str3 = $str1.$str2; #concatenates the string and str3 has value ‘abcdef’

How many types of operators are used in the Perl?

Arithmetic operators

+, – ,*

Assignment operators:

+= , -+, *=

Increment/ decrement operators:

++, —

String concatenation:

‘.’ operator

comparison operators:

==, !=, >, < , >=

Logical operators:

&&, ||, !

Write syntax to add two arrays together in perl?

@arrayvar = (@array1,@array2);
To accomplish the same, we can also use the push function.

For a situation in programming, how can you determine that Perl is a suitable?

If you need faster execution the Perl will provide you that requirement. There a lot of flexibility in programming if you want to develop a web based application. We do not need to buy the license for Perl because it is free. We can use CPAN (Comprehensive Perl Archive Network), which is one of the largest repositories of free code in the world.

How the interpreter is used in Perl?

Every Perl program must be passed through the Perl interpreter in order to execute. The first line in many Perl programs is something like:

#!/usr/bin/perl
The interpreter compiles the program internally into a parse tree. Any words, spaces, or marks after a pound symbol will be ignored by the program interpreter. After converting into parse tree, interpreter executes it immediately. Perl is commonly known as an interpreted language, is not strictly true. Since the interpreter actually does convert the program into byte code before executing it, it is sometimes called an interpreter/compiler. Although the compiled form is not stored as a file.

Which guidelines by Perl modules must be followed?

Below are guidelines and are not mandatory

The name of the package should always begin with a capital letter.

The entire file name should have the extension “.pm”.

In case no object oriented technique is used the package should be derived from the Exporter class.

Also if no object oriented techniques are used the module should export its functions and variables to the main namespace using the @EXPORT and @EXPOR_OK arrays (the use directive is used to load the modules).

Can we load binary extension dynamically?

Yes, we can load binary extension dynamically but your system supports that. If it doesn’t support, then you can statically compile the extension.

Comment on the scope of variables in Perl.

By default, all the variables in Perl are global in scope. This means that a variable can be used for a reminder of the program from the point of its declaration.

You can use ‘my’ keyword for a variable and this makes a variable to have local scope.

Example: my $x=10;

Does Perl have objects? If yes, then does it force you to use objects? If no, then why?

Yes, Perl has objects and it doesn’t force you to use objects. Many object oriented modules can be used without understanding objects. But if the program is too large then it is efficient for the programmer to make it object oriented.

Define A Short Circuit Operator?

The C-style operator ll carries out logical operation which is used to tie logical clauses, overall value of true is returned if either clause is true. This operator is known as short-circuit operator because you need not check or evaluate right operand if the left operand is true.

What are “Require” and “Use” statement in Perl and when it is used?

It is considered when it comes to importing the functions in a way that they can be accessed directly during the program. The users are free to get the results in case the substatements are not accurate. On the other side, the use statement is generally executed during parsing.

Can you tell the meaning of the term debugging in the programming?

Well, every programmer is familiar with this approach. The fact is many errors declare their presence in the programs due to reasons which are not always necessary to be known exactly. Eliminating these errors is very essential for the smooth flow of the tasks. Finding bugs or errors is known as debugging. The programming languages can have in-built options for debugging or the programmers are free to consider other options too.

Which Has Highest Precedence In Between List And Terms? Explain?


In Perl, the highest precedence is for Perl. Quotes, variables, expressions in parenthesis are included in the Terms. The same level of precedence as Terms is for List operators. Especially, these operators have strong left word precedence.

Why -w Argument Is Used With Perl Programs?


-w option of the interpreter is used by most of the Perl developers especially in the development stage of an application. It is warning option to turn on multiple warning messages that are useful in understanding and debugging the application.

Define Scalar Data And Variables?

The concept of data types is flexible, which is present in Perl. Scalar is a single thing such as a string or a number. The java concepts such as int, float, string, and double are similar to scalar concept of Perl. Strings and numbers are exchangeable. Scalar variable is nothing but a Perl variable which is used to store scalar data. A dollar sign $ is used by it which is followed by underscores or alphanumeric characters. It is a case sensitive.

What Happens If A Reference Is Returned To Private Variable?

Your variables are kept on track by the Perl, whether dynamic or else, and does not free things before you use them.

Why Perl Patterns Are Not Regular Expressions?

Perl patterns have back references
By the definition, a regular expression should determine next state infinite automation without extra money to keep in previous state. State machine is required by the pattern / ([ab] +) c1/ to remember old states. Such patterns are disqualified as being regular expressions in the term’s classic sense.

Distinguish My And Local?

The variables which are declared using “my” lives only in that particular block ion which they are declared and inherited functions do not have a visibility that are called in that block. The variables which are defined as “local” are visible in that block and they have a visibility in functions which are called in that particular block.

How the interpreter is used in Perl?

In order to run, every Perl programme must go via the Perl interpreter. In many Perl applications, the opening line is something like:

#!/usr/bin/perl

Internally, the interpreter compiles the programme into a parse tree. The programme interpreter will ignore any words, spaces, or marks after the pound symbol. The interpreter immediately executes it after transforming it to a parse tree. Although Perl is usually referred to as an interpreted language, it is not.

This is absolutely correct. Because the interpreter converts the programme into byte code before it is executed, It is frequently referred to as an interpreter/compiler because it executes it.

While writing a program, why the code should be as short as possible?

Complex codes are not always easy to handle. They are not even easy to be reused. Moreover, finding a bug in them is not at all a difficult job. Any software or application if has complex or lengthy code couldn’t work smoothly with the hardware and often have compatibility issues. Generally, they take more time to operate and thus become useless or of no preference for most of the users. The shortcode always makes sure that the project can be made user-friendly and it enables programmers to save a lot of time.

How Perl Warnings Are Turn On And Why Is It Important?


In general, Perl excuse strange and also wrong code sometimes. Thus, the time spent for searching weird results and bugs in very high. You can identify common mistakes and strange places in the code easily when warnings are turned on. In the long run, the time required for debugging is saved a lot. There are numerous ways to turn on the warnings of Perl:

>> -w option is used on the command line for Perl one-liner
>> -w option on shebang line is used on windows or UNIX. Windows Perl interpreter do not require it.
>> For other systems, compiler documentation is checked or compiler warnings are selected.

Which Is Your Favorite Module And Why It Is?

CGI.pm is my favorite module and it handles several tasks such as printing the headers, parsing the form input and it handles sessions and cookies effectively.

Why To Use Perl Scripting?

Perl scripting is mainly used in functional concepts as well as regular expressions, you can also design own policies to obtain generalized pattern using regular expression. Perl is compatible or supports more than 76 operating systems and 3000 modules and it is known as Comprehensive Perl Archive Network modules.

How can you represent the warning signs in the Perl in case of an error and what are the options through which this task can be performed?

There is an option in Perl which is known as the Command-Line. All the warning messages can be displayed using this and the pragma function simply makes sure that the user can declare the variables during the appearance of warning messages. The entire program can be scrolled easily and in fact, in a very short span of time using the in-built debugger.

Is it possible in Perl to use code again and again? If so, which feature enables the user to that?

Yes, it is possible in Perl. However, there is a limit on the usage of the same code in the same program. The users need not worry about the complexity either as Perl is equipped with a code trimming feature. It automatically guides users on how to keep the code as short as possible. Code reusability is a prime example of this. The feature that enables users to simply keep up the pace towards this is “Inheritance”. The child class in this feature can use the methods of their parent class.

Can you name the variables in which the chomp works? Also, how they are different from one another?

These are: Scalar and Array

Scalar is generally denoted by the symbol $ and it can have a variable that can either be a string or a number. An array on the other side is denoted by @ symbol. An array is always a number. Both these variables have a different namespace. The scalar variables are capable to hold a value of 1 digit while the array can have more values. Both of them can be executed in the function whenever there is a need for the same.

What are the various flags/arguments that can be used while executing a Perl program?

The following arguments can be used while executing a Perl program.

<> w – argument shows a warning.
<> d – used for debugging.
<> c – compiles only do not run.
<> e – execute.

Explain the execution of a program in Perl.

Perl is portable and Perl programs can be executed on any platform. Though having a Perl IDE is useful, we can even write the Perl code in a notepad and then execute the program using the command prompt.

For Example, consider the following simple program to print “Hello, World!!”

#!/usr/bin/perl
Print(“Hello, World!!”);
In this code, the first line “#!/usr/bin/perl”, is the path to the Perl interpreter.

Let’s name this file as “hello.pl”. We can execute this program by just giving the following command in the command window:

pl hello.pl
Output: Hello, World!!

Which feature of Perl provides code reusability ? Give any example of that feature.

Inheritance feature of Perl provides code reusability. In inheritance, the child class can use the methods and property of parent class

Package Parent;

Sub foo

{

print("Inside A::foo
");

}

package Child;

@ISA = (Parent);

package main;

Child->foo();

Child->bar();

What are the various advantages and disadvantages of Perl?

Advantages of Perl include:

<> Perl is efficient and is easy-to-use.
<> It is an Interpreted language i.e. Perl program is interpreted on a statement-by-statement basis.
<> Perl is portable and cross-platform. Currently, it can run on more than 100 platforms.
<> Perl is extendable. We can include various open-source packages and modules in a Perl program for any additional functionality. For example, we can import CPAN modules for database support in the Perl program.

Explain what is Perl language?

Perl stands for “Practical Extraction and Reporting Language”. It’s a powerful scripting language and is rich in features. Using Perl, we can write powerful and efficient code that can be used in mission-critical projects.

Search
R4R Team
R4R provides Perl Freshers questions and answers (Perl Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Perl interview questions part 1 ,Perl Freshers & Experienced Interview Questions and Answers,Perl Objetive choice questions and answers,Perl Multiple choice questions and answers,Perl objective, Perl questions , Perl answers,Perl MCQs questions and answers Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for Perl fresher interview questions ,Perl Experienced interview questions,Perl fresher interview questions and answers ,Perl Experienced interview questions and answers,tricky Perl queries for interview pdf,complex Perl for practice with answers,Perl for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .