Perl Scripting Interview Questions and Answers
by Pritha Radhakrishnan, on Aug 5, 2023 3:53:30 PM
Q1. What is Perl scripting?
Ans: Perl scripting is the use of the Perl programming language for writing scripts to automate tasks, manipulate text data, and perform system administration.
Q2. What are the basic data types in Perl?
Ans: Perl supports scalar, array, hash, and filehandle as the basic data types.
Q3. How do you declare a variable in Perl?
Ans: Variables in Perl are declared using the '$' symbol for scalars, '@' symbol for arrays, and '%' symbol for hashes.
Q4. Explain the difference between 'my' and 'local' variables in Perl.
Ans: 'my' creates a lexically scoped variable, while 'local' creates a dynamically scoped variable that retains its value until the enclosing block is exited.
Q5. How do you open a file for reading or writing in Perl?
Ans: You can open a file for reading using the 'open' function with the filehandle and file name, or for writing using 'open' with the '>', '>>', or '+>' mode.
Q6. What are regular expressions in Perl?
Ans: Regular expressions are patterns used for matching and manipulating text. Perl has robust regular expression support, allowing powerful text processing capabilities.
Q7. How do you match a pattern in a string using regular expressions in Perl?
Ans: Regular expression pattern matching in Perl is performed using the '=~' operator or the 'm//' operator.
Q8. What is the difference between 'my' and 'our' variables in Perl?
Ans: 'my' declares a variable with a limited scope within the enclosing block, while 'our' declares a variable that is accessible across packages.
Q9. How do you split a string into an array in Perl?
Ans: The 'split' function is used to split a string into an array based on a specified delimiter.
Q10. How do you find the length of an array in Perl?
Ans: In Perl, the 'scalar' function can be used to find the length of an array, returning the number of elements.
Q11. How do you concatenate two strings in Perl?
Ans: Strings can be concatenated using the '.' operator or by simply placing them next to each other.
Q12. What is the purpose of the 'chomp' function in Perl?
Ans: The 'chomp' function is used to remove the trailing newline character from a string read from input.
Q13. How do you use command-line arguments in Perl?
Ans: Command-line arguments can be accessed using the special array '@ARGV' in Perl.
Q14. Explain the concept of references in Perl.
Ans: References in Perl allow you to create and manipulate complex data structures like arrays and hashes, enabling dynamic memory allocation.
Q15. How do you sort an array in Perl?
Ans: The 'sort' function in Perl can be used to sort an array in ascending or descending order.
Q16. How do you replace a substring in a string using Perl?
Ans: The 's///' operator or the 'substitute' function is used to replace a substring with another in a string.
Q17. What is the purpose of the 'use strict' pragma in Perl?
Ans: 'use strict' enforces the use of strict variable declaration and scoping, helping to catch errors and enforce good programming practices.
Q18. How do you read from a file in Perl?
Ans: Reading from a file is typically done using a filehandle and the 'readline' operator '<>' or the 'getline' function.
Q19. What are Perl modules?
Ans: Perl modules are reusable packages of code that provide additional functionality. They can be imported using the 'use' or 'require' statement.
Q20. How do you handle errors or exceptions in Perl?
Ans: Errors or exceptions in Perl can be handled using the 'eval' function and the 'die' function to generate error messages.
Q21. How do you find and replace text in multiple files using Perl?
Ans: This can be accomplished by using the 'File::Find' module along with regular expressions or the 'File::Find::Rule' module.
Q22. Explain the difference between 'split' and 'join' in Perl.
Ans: 'split' is used to divide a string into an array based on a specified delimiter, while 'join' combines elements of an array into a string using a specified separator.
Q23. How do you execute system commands in Perl?
Ans: System commands can be executed in Perl using backticks (
command) or the 'system' function.
Q24. What are the Perl diamond operator ('<>') and its significance?
Ans: The diamond operator reads input either from standard input or from files specified as command-line arguments, allowing for versatile input handling.
Q25. How do you debug Perl scripts?
Ans: Perl scripts can be debugged using tools like 'perl -d', 'perl -w', 'Data::Dumper' for variable inspection, or by incorporating 'warn' and 'print' statements for troubleshooting.