Table of Contents
1. What is Perl?
Perl expansion is Practical Extraction and Reporting Language. It is a cross-platform scripting language that we can use for web development, GUI design, system administration, etc. It supports object-oriented concepts and is recommended for CGI scripts due to its strong text processing capabilities.
2. What are the features of Perl?
Below are the main features of Perl:
- Supports object-oriented programming
- Cross-platform
- Open-source
- Supports third party databases
- Extendable
- Case-sensitive
- Supports high-level intrinsic operations like stack push/pop
3. Is Perl an interpreter or compiler?
Perl is both interpreter and compiler since it converts the source code into bytecode before execution and then runs it.
4. What is CPAN in Perl?
CPAN stands for Comprehensive Perl Archive Network which is a repository that contains many Perl modules.
5. What are the advantages and disadvantages of Perl?
Below are the advantages of Perl:
- Easy to use and understand
- Portable and cross-platform
- Strong text and string manipulation scripting language
- Supports OOPs concept
- Free and open source
Below are the disadvantages of Perl:
- It is slower when compared to other languages since it is an interpreter language
- When code contains more than 200 lines of code, it becomes more difficult and hence does not support complex code.
- Perl contains exhaustive features which makes it difficult for programmers.
6. How do you execute a Perl program?
We can either use a Perl IDE or write the code in a notepad and save it with .pl extension. We can then execute this code using the command prompt in the following way:
pl hello.pl
7. What are the different arguments that we can use while executing a Perl program?
We can use the below arguments while executing a Perl program:
- w – warning
- d – debugging
- c – compile
- e – execute
It is possible to use a combination of arguments also.
8. What are the different data types or variables in Perl?
By default, the variables do not have a defined data type in Perl. It depends on the values that we assign to a variable.
- Scalar – linear data types like integer, string, or float. Uses prefix $.
- Arrays – a ordered collection of scalar values. Uses prefix @
- Hashes – unordered collection of key-value pairs. Uses prefix %
9. Define the scope of a variable in Perl.
By default, variables in Perl have global scope. We can use the $my keyword to define a local variable.
10. What are the different types of operators in Perl?
- Numeric operator: +, -, *, /
- String operator
- Assignment operator
- Bitwise operator: AND, OR, XOR, NOT, SHIFT LEFT, SHIFT RIGHT
- Logical operator
- Special operator
- Comparison operator: >, <, >=, <=, ==, !=, <=>
11. What are the different string manipulation operators?
- Concatenation operator: Concatenates 2 strings using the dot(.) operator
- Repetition operator: Repeats the string for a specified number of times using the (x) operator.
12. What is the use of chomp() function?
The chomp()
operator removes the last character of the string and returns the number of characters removed.
13. What are the different array functions in Perl?
There are 4 types of array functions in Perl that we can use to add or remove elements in an array:
- Shift
- Unshift
- Push
- Pop
14. What is Array slicing and Range operator?
Array slicing returns more than one element at a time. In other words, we can retrieve part of the elements from an array. We can also use the List-range operator to retrieve the range of elements in an array.
15. Write a simple Perl program to print “Hello world”.
print "Hello World";
16. What are the different types of scalar functions used in Perl?
- Defined
- Undef
- Ref
- Reverse
- Chomp
- Chop
- Index
- Return
17. What are the different Hash functions used in Perl?
- Exists
- Delete
- Keys
- Values
- Each
- HashLength
- Context
- Sort
18. What are hashes in Perl?
Hash is a variable type in Perl that represents an unordered collection of elements in the form of key-value pairs. Hashes are denoted by a % percentage symbol.
19. What is dereferencing?
Dereferencing returns the value of the reference point. It is represented by $, @ or %.
20. What are the different prefixes of dereferencing?
$: scalar variables
@: arrays
&: subroutine
%: Hash variables
21. Which keyword is used to call a subroutine?
We can use &myvariable
to call a subroutine.
22. Define say() function in Perl
The say()
function is similar to print()
with the on;y difference that it adds a new line character automatically at the end of the string. It is not supported by older Perl versions.
23. What is dynamic scoping?
Dynamic scoping is the assignment of temporary values to global variables.
24. What are lexical variables?
The variables that are created using the my
operator and that is private in scope are lexical variables.
25. Define circular reference.
A circular reference occurs when two references contains references of each other.
26. What is ‘ne’ operator?
The ne
operator compares the left string with the right string value and returns true if they are not equal.
27. What is the use of q{}, qq{} and qx{} operator?
The q{} operator encloses the string within a single quote and qq{} encloses the string in double-quotes. The qx{} operator encloses the string in inverted quotes.
28. How to replace elements in Perl?
We can use the Perl array slice operation to remove the array elements and replace them with specific elements.
29. How to convert string to an array vice versa?
We can use the split()
function to convert a string to an array whereas the join()
method converts an array to a string.
30. What is the use of undef() function?
The undef()
method removes the values from the hash but retains the keys.
31. What is the difference between use and require keywords in Perl?
use keyword is used for Perl modules where it verifies these modules during compile time.
require keyword is used for both Perl modules and libraries and verifies them during runtime.
32. What are the different loop control keywords in Perl?
- next: Moves to the next element in the array or hash and is similar to the continue statement in C.
- last: Exits the current loop execution and is similar to the break statement in C.
- redo: Restarts the current loop without evaluating the loop condition
33. What is the use of “use strict” in Perl?
The use strict command helps to identify and catch bugs or errors in the code and stop the program execution.
34. How to print escaping characters in Perl?
To print the escaping characters like @,\,/,$,&, we can use the backslash(\) before these characters.
35. How to comment in Perl?
For single-line comments, use # before the comment line.
For multi-line comments, use = at the start and end of the comments.
36. What is the split function in Perl?
The split function splits a string based on the specified delimiter like –, /,;, etc. By default, it considers white space as a delimiter.
37. What is a subroutine in Perl?
A subroutine is a piece of reusable code in a Perl program and is denoted by the sub keyword. It accepts arguments, performs operations, and also returns values.
38. How to open a file in a read-only or write-only mode in Perl?
To open a file in read-only mode, use the ‘<‘ symbol, and to open a file in write-only mode, use the ‘>’ symbol.
39. What is the use of the ‘->’ symbol in Perl?
The -> symbol is used to link one file to another. For example, when we use file1->file2, it starts reading from file1 and ends up reading in file2.
40. What is tell function in Perl?
Tell function identifies the position in the file. This is an important step in file handling.
41. Explain the below directory related operations
mkdir – Creates a new directory
opendir – Opens a directory
readdir – Reads the directory
rmdir – Removes the directory
chdir – Changes the directory
closedir – Closes the directory
42. What is the difference between die and exit in Perl?
The die function returns an error message and terminates the program execution while the exit function just terminates the program execution and does not return any error message.
43. What is Perl DBI?
DBI expansion is Database independent Interface. Perl DBI is used to access the database which is a third party module provided by CPAN. It supports all major database systems.
44. Which feature in Perl provides code reusability?
Inheritance is the feature in Perl that provides code reusability. Using inheritance, the child class can access all the methods and variables of the parent class.
45. How is the interpreter used in Perl?
An interpreter compiles the program and converts to a bytecode before it executes. Every Perl program must pass through an interpreter. Generally, below line must be the first line in any Perl program. Perl is both an interpreter and a compiler.
#!/usr/bin/perl
46. What is the name of the Perl match where the regular expressions match the longest string possible?
It is called the greedy match.
47. How to identify and call a subroutine?
We can call a subroutine by using &myvariable and identify a subroutine using &.
48. What is the use of grep function in Perl and write its syntax?
The grep function filters and lists only those elements that match the criteria.
grep BLOCKLIST grep(EXPR,LIST)
49. How to retrieve the size of an array?
We can retrieve the size of an array using the scalar context on the array. It returns the number of elements in an array.
@arrnumber = (10,20,30); print "Size of array: ", scalar @arrnumber, "\n";
50. What is the difference between localtime and gmtime functions?
localtime function returns the current local time of the system where the script runs and gmtime function returns the universal Greenwhich Mean Time(GMT).