Latest Perl Interview Questions Part – 3
1) What is Perl ?
Perl stands for Practical Extraction and Reporting Language. It is a high level programming language written by Larry Wall, especially designed for processing text. It supports object oriented programming and its syntax is quite similar to C language. It is much more flexible to use. Due to its strong text processing abilities, it has become one of the most popular languages for writing CGI scripts.
2) Perl is Compiler or Interpreter?
Perl is said to be both compiler and interpreter. It reads the source code, convert the program into byte code before execution and then run it. Hence, Perl is sometimes called an interpreter/compiler.
3) What is CPAN in Perl?
CPAN stands for Comprehensive Perl Archive Network. It is a repository which contains thousands of Perl modules.
4) What are the features of Perl language.
Feature of Perl:
It has a very simple Object-oriented programming syntax.
It is easily extendible as it supports 25,000 open source modules.
It supports Unicode.
Includes powerful tools to process text to make it compatible with mark-up languages like HTML, XML.
It supports third party database including Oracle, MySQL and many others.
It is embeddable in other systems such as web servers and database servers.
It is open source software licensed under GNU.
Many frameworks are written in perl.
It can handle encrypted web data including e-commerce transactions.
It is a cross platform language.
It offers a regular expression engine which is able to transform any type of text.
For more information: click here
5) What are the advantages and disadvantages of Perl language?
Perl advantages:
Its syntax is simple, which makes it easy to understand.
It supports OOP concepts.
Perl programs runs easily on the system.
It is more flexible to use.
It supports all platforms and is much more portable.
It has a rich set of Perl modules and free software.
It is much more efficient to work on text and string manipulation as it is a scripting language.
Perl combines features of many other languages which make it easy to understand.
Perl disadvantages
A Perl program containing CPAN modules will not run on another system which doesn’t have CPAN modules installed.
It is an interpretative language, so it is slower in comparison with other languages.
It has untidy and unreadable codes.
It starts creating problems when code is larger than 200 lines.
It is not portable.
6) Define print() function in Perl?
The Perl print() function prints anything it gets as its argument.
7) Define say() function in Perl?
The Perl say() function is not supported by older Perl versions. It is like Perl print() function with only one difference that it automatically adds a new line at the end.
8) How many data types are there in Perl?
Perl has three data types:
Scalars
Arrays
Hashes
9) What are Perl variables?
A variable is a place to store values reserving some memory space. Perl treats same variables differently based on context.
There are three types of Perl variables:
Scalars
Arrays
Hashes
10) What are scalars?
A scalar contains a single unit of data. They are preceded with a ($) sign. A scalar contains a number, character, reference or a string. A reference is the address of the variable.
11) What are arrays in Perl?
An array contains an ordered list of scalar values. It is preceded with (@) sign. To access a single element in a Perl array ($) sign is used.
12) How to find length of an array in Perl?
Size of an array is determined with scalar context on the array. Array length will always be one greater than its largest index.
Perl size = $#arrayName +
Where, $#arrayName is the maximum index of the array.
13) What are Perl array functions?
Perl array functions are used to add or remove some elements in an array.
There are four types Perl array functions:
Push
Pop
Shift
Unshift
14) What is Perl push array function?
The Perl push array function appends a new element at the end of an array.
15) What is Perl pop array function?
The Perl pop array function removes the last element of an array.
16) What is Perl shift array function?
The Perl shift array function removes the left most element from the array shortening array by 1.
17) What is Perl unshift array function?
The Perl shift array function adds a new element at the start of an array.
18) How to replace Perl array elements?
The Perl splice array function removes elements and replaces them with the specified list of elements.
19) How to convert strings into array in Perl?
The Perl split array function splits a string into array of strings. Thus converting strings into array.
20) How to convert arrays into string in Perl?
The Perl join array function combines more than one array into single string. thus converting arrays into string.
21) How to merge two arrays in Perl?
The Perl merged array function merges two arrays into single array by removing all the commas in between them.
22) How to sort arrays in Perl?
The Perl sort array function sorts all the elements of an array according to the ASCII standard.
23) What are hashes?
A Perl hash is a group of unordered key-value pairs. The keys are unique strings and values are scalar values. It is preceded with (%) sign. They can be accessed using their key values.
24) How to know whether a key exists or not in Perl?
Using Perl exists function, you can check whether a key exists or not in a hash. It returns true if the key exists.
25) How to add elements in a hash in Perl?
To add new key-value pair in hash, declare them as a single variable in the hash variable.
26) What does delete function do in Perl?
To remove a hash element, use delete function. It removes both key and value element from hash.
27) What does undef function in Perl?
The undef function removes the value from hash but its key remains there.
28) What is the difference between Perl array and Perl hash?
Perl array: They are ordered list of elements, positioned by index number. It is denoted with @ sign.
Perl hash: They are unordered list of elements, positioned by their key values. It is denoted with % sign.
29) What is the difference between Perl list and Perl array?
Perl list
Perl list is a method to organize data in the Perl source code. It is a fixed collection of scalars. They are always one dimensional.
Perl array
Perl array is a method to store data in form of variables. They are multi dimensional.
30) What is the difference between use and require in Perl?
Use:
It is used only for the Perl modules.
The included modules are verified at the time of compilation.
It does not need file extension.
Require:
It is used for both Perl modules and libraries.
The included objects are verified at run time.
It does need file extension.
31) How many loop control keywords are there in Perl?
There are three types of loop control statement:
Next
Last
Redo
32) What does next statement do in Perl?
Perl next statement is like continue statement in C. It lets you to move on to the next element of your array or hash skipping all elements in between.
33) What does last statement do in Perl?
Perl next statement is like break statement in C. It exist the loop immediately skipping remaining codes.
34) What does redo statement do in Perl?
Perl redo statement restarts the current loop without evaluation of the control statement.
35) Define operators used in Perl?
A Perl operator is a series of symbols like +, -, =, <, >, etc. It uses its operands as arguments.
Pattern matching operator : (=~, !~)
Shifting operator : (>>, <<)
Comparison operator : (==, !=, <=, >=, <=>)
Logical operator : &&, ||
36) What is the importance of Perl warnings?
Perl warnings help us to check errors in our code by giving warnings.
To enable them use -w :
perl -w scriptName.pl
Or, you can also supply it within the “shebang” line:
#/usr/local/bin/perl -w
37) Why do we use “use strict” in Perl?
The “use strict” command in Perl calls strict pragma. These pragmatas help to catch some bugs or errors in our script and stops the program execution.
38) What are Perl strings?
Strings are an essential part in Perl. They are scalars so they start with $ sign. Strings can be placed inside single or double quote.
Two types of string operators are there:
Concatenation (.)
Repetition (x)
39) What is interpolation in Perl?
Interpolation means inserting something with different nature. It can be defined as replacing a variable with its value.
40) What is the difference between single (‘) and double (“) quote in string in Perl?
In single quote, the value is printed as it is given inside the string without interpolation.
In double quote, the value is printed with interpolation given inside the string.
41) Explain substr function in Perl?
The substr function is used to truncate string. String will be truncated till the offset value we provide.
42) How to compare two strings in Perl?
To compare two strings in Perl eq is used instead of (==). It checks whether two strings are equal or not.
43) How to determine strings length in Perl?
String length can be determined with length() function.
44) How to print escaping characters inside a string in Perl?
Escaping characters are the special characters like @, \, /, &, $, “, etc. To print escaping characters put a backslash (\) before escaping characters.
45) What is qq (double q)operator in Perl?
The qq operator replaces double quote surrounding a string by its parentheses. You can use qq instead of (“”).
46) What is q (single q) operator in Perl?
The q operator replaces single quote surrounding a string by its parentheses. You can use q instead of (‘).
47) What is STDIN in Perl?
The STDIN stands for standard input. Using this we can get input from the standard console. It can be abbreviated as <>.
48) What is goto statement in Perl?
The Perl goto statement is the jump statement. It transfers control by jumping to other label inside a loop.
There are three goto forms:
goto LABEL
goto EXPR
goto &NAME
49) How to do comment in Perl?
Like other languages, Perl also provides comment facility in its code.
There is single line and multi line comment.
For single line comment : use # before the line you want to comment.
For multi line comment : use =begin and =cut statement before and after the lines respectively you want to comment.
50) Explain regular expression in Perl?
A regular expression is a string of characters that defines a specific pattern.
There are three regular expression operators inside Perl:
Matching regular expression operator, m//
Substitute regular expression operator, s///
Transliterate regular expression operator, tr///
51) Explain split function in Perl?
Perl split function splits a string at specified delimiter pattern like -, /, :, etc. By default, white space is assumed as delimiter pattern if nothing is specified.
52) Explain join function in Perl?
Perl join function joins symbol or character in between or afterwards each elements of an array.
53) Explain subroutine in Perl?
Perl subroutine lets you reuse a code in your program. They accept arguments, perform their operation and return the values. A subroutine is declared with ‘sub’ keyword before its name. In Perl, function and subroutine are used interchangeably.
54) How to access parameters passed to a subroutine in Perl?
Parameters are accessed inside a subroutine using special array @_. Hence, the arguments will start with $_[0], $_[1], $_[2], $_[3] and so on.
55) Explain use of ‘my’ keyword in Perl?
The ‘my’ keyword restricts a variable to a particular region in which it can be used and accessed. Outside this region, this variable can’t be used.
56) Explain the difference between declarations of ‘my’ and ‘local’ variable scope in Perl?
Variables declared with ‘my’ keyword live within a code block and can’t get its visibility inherited in the functions called within that block.
Variables declared with ‘local’ keyword live within a code block and get its visibility in the functions called within that block.
57) Explain default scope of variables in Perl?
By default, all variables in Perl are global variables unless they are locally defined. They can be accessed from anywhere in a program.
58) What is lexical variable in Perl?
Lexical variables are created using ‘my’ keyword in Perl. They are private variables.
59) How will you create a file in Perl?
To create a file in Perl, ‘>’ sign is used before file name. It will create a new file.
60) How will you open a file in read-only mode in Perl?
To open a file in read-only mode, ‘<‘ sign is used. You can only read this file and can’t write anything.
61) How will you open a file in write-only mode in Perl?
To open a file in write-only mode, ‘>’ sign is used. The file you open will be emptied or truncated if it already exists, if not, a new file will be created.
62) How to prevent file truncation in Perl?
Opening a file in write-only mode truncates data of the file. To prevent it, use sign ‘+>’. This will prevent your data and you can append new data in the last of the file.
63) What is the use of ‘>>’ in Perl?
The ‘>>’ sign opens a file with appending purpose. It places the pointer at the end of the file where you can add new data.
64) How to read a single line from a file in Perl?
Taking $row = <$fh> as a variable will print a single line from the file.
65) How to read multi lines from a file in Perl?
Taking $row = <$fh> as a variable in a while loop will print all lines from the file.
66) How to close a file in Perl?
Closing a file in Perl is not mandatory. However, using close() function will disassociate file handle from the corresponding file.
67) How to copy a file in Perl?
To copy the content of one file into other file, read all lines of first file in a while loop and copy it in another file.
68) Explain ‘->’ in Perl?
It is a symbolic link which links one file name to a new file name.
For example, in file1 -> file2, if we read file1, we will end up reading file2.
69) Explain tell function in Perl?
The tell function finds your position within a file. This is the first thing you need to do while file handling.
70) What does file test operators do in Perl?
File test operators check miscellaneous information about a file. Like type of file, file byte size, its UID or GID, etc.
71) How to open a directory in Perl?
To open a directory in Perl, opendir function is used. It returns true on success or false on failure.
72) How to create a directory in Perl?
To create a directory in Perl, mkdir function is used. You need required permission to create a directory.
73) How to read a directory in Perl?
To read a directory in Perl, readdir function is used. In scalar context, it will return each item one by one. In list context, it will return all the content of directory in one statement. So list context uses more memory than scalar context.
74) How to remove a directory in Perl?
To remove a directory in Perl, rmdir function is used. The directory which you want to remove should be empty before removing it.
75) How to change a directory in Perl?
To change a directory in Perl, chdir function is used. To change a directory and go inside a new directory you need required permission.
76) How to close a directory in Perl?
To close a directory in Perl, closedir function is used. This function officially shut down the connection between directory handle and directory.
77) What is chop() function in Perl?
Perl chop() function removes last character from a string regardless of what that character is. It returns the chopped character.
78) What is chomp() function in Perl?
Perl chomp() function removes any new line character from end of the string. It returns the number of characters removed from the string.
For more information: click here
79) What does die() function do in Perl?
Perl die() function gives us a proper error message. It immediately terminates the script on encountering an error.
80) Explain the difference between die and exit in Perl?
The die function prints the standard error message then exits the program. Whereas, the exit function terminates the program without giving any error message.
81) What $! in Perl?
The $! Is a built-in error reporting variable in Perl. It tells us the reason for error and print it. It prints what it is told by operating system.
82) What is warn function in Perl?
The warn function gives the warning on encountering an error but does not exit the script. Script keeps on running.
83) What is confess function in Perl?
The confess function is used within the Carp standard library for error handling.
84) What is eval function in Perl?
The eval function is a built-in function in Perl which is used to detect normal fatal error. It is supplied with a code block instead of passing into string.
85) What is Perl DBI?
DBI stands for Database Independent Interface. In Perl, database is accessed using DBI module. It is a third party module provided by CPAN. It supports all the major database systems. It provides an abstraction layer between Perl code and database.
86) What does ‘do’ statement do in Perl?
The do statement is used in Perl database. It is a shortcut to perform all the CRUD operation. If it is succeeded, it returns true, otherwise false.
87) What is ‘commit’ command in Perl?
Once you have given Perl commit command, you can’t retrieve back the changes you made.
Syntax:
$dbh->commit or die $dbh->errstr;
88) What is ‘rollback’ command in Perl?
If you want to revert the changes made during the operation, call rollback command.
Syntax:
$dbh->rollback or die $dbh->errstr;
89) What is automatic error handling in Perl?
By specifying RaiseError option, your errors will be handled automatically by Perl. Your program will be aborted on encountering an error rather than running a failure code. Its value can be either 1 or 0.
90) What are some common methods to all handles in Perl?
err
errstr
trace
rows
91) What does localtime() do in Perl?
The localtime() function if used without any argument, returns the current date and time according to the system.
92) What is the use of now constructor in Perl?
The now constructor creates DateTime object displaying current date and time using DateTime module.
93) Explain gmtime() function in Perl?
This function works similar to localtime() with only one difference that returned value is localized for the standard Greenwich time zone.
94) What is epoch time Perl?
The epoch time refers to the number of seconds after a specific date and time. This specific date and time varies for different operating systems. For example, for Unix it is January 1, 1970.
95) What is POSIX in Perl?
POSIX – Perl interface to IEEE Std 1003.1
The POSIX module allows you to access all the standard POSIX identifiers. This module provides much more functions than any other module.
96) Explain strftime() function in Perl?
The Perl POSIX strftime() function is used to format date and time with the specifiers preceded with (%) sign.
97) Explain socket programming in Perl?
The socket is a procedure which helps to establish a virtual connection between different processes over a network.
In socket programming, client and server side script is made which communicate through each other over TCP/IP protocol.