Perl Interview Questions and Answers for 5 years experience
-
What is Perl?
- Answer: Perl is a high-level, general-purpose, interpreted, dynamic programming language. It's known for its powerful text processing capabilities, regular expressions, and its ability to glue different systems together.
-
Explain the difference between `my`, `local`, and `our` variables.
- Answer: `my` creates lexically scoped variables, visible only within the current block. `local` creates dynamically scoped variables, affecting only the current subroutine and its callees. `our` declares package variables, accessible throughout the package.
-
What are references in Perl?
- Answer: References are pointers to data structures. They allow you to create complex data structures like linked lists, trees, and hash of hashes. They are created using a backslash before a variable.
-
How do you handle errors in Perl?
- Answer: Perl uses the `eval` block to handle exceptions. You can also check the value of `$?` after system calls to check for errors, and use `die` to terminate execution with an error message. Modules like Try::Tiny provide more structured exception handling.
-
Explain the use of regular expressions in Perl.
- Answer: Perl's regular expression engine is very powerful. It's used for pattern matching and text manipulation. The `m//` operator performs pattern matching, and various modifiers control the matching behavior (e.g., `i` for case-insensitive matching, `g` for global matching).
-
What is the difference between `grep` and `map`?
- Answer: `grep` filters a list, returning only elements that match a given condition. `map` applies a function to each element of a list and returns a new list with the transformed elements.
-
Explain the concept of modules in Perl.
- Answer: Modules are reusable code units organized into separate files. They are used with the `use` statement and provide a way to organize and share code. They enhance code reusability and maintainability.
-
How do you create and use a custom module in Perl?
- Answer: A custom module is created by placing Perl code in a file named `ModuleName.pm`. The module's functions are typically placed within a package declaration (`package ModuleName;`). It's then used in other scripts with `use ModuleName;`.
-
What is the purpose of the `use strict` pragma?
- Answer: `use strict` enforces stricter coding practices. It requires explicit variable declarations (using `my`), prevents the use of barewords as identifiers, and helps to catch common errors early.
-
What are file handles in Perl?
- Answer: File handles are symbolic names that refer to open files. They are used to read from or write to files. They are typically opened using the `open` function and closed using the `close` function.
-
Explain different ways to read a file in Perl.
- Answer: Files can be read line by line using a `while` loop and the `<>` operator, or using `readline`. The entire file can be read into an array using `@lines =
;` or slurped into a single scalar using `$content = do { local $/; };`.
- Answer: Files can be read line by line using a `while` loop and the `<>` operator, or using `readline`. The entire file can be read into an array using `@lines =
-
How do you write to a file in Perl?
- Answer: Data is written to a file using the `print` function with a filehandle. For example: `print FILEHANDLE "My data\n";`.
-
What are hashes in Perl?
- Answer: Hashes are associative arrays, where data is accessed using keys instead of numerical indices. They are declared using curly braces `%hash = ('key1' => 'value1', 'key2' => 'value2');`.
-
How do you iterate through a hash in Perl?
- Answer: You can iterate using `foreach` with keys: `foreach my $key (keys %hash) { print "$key => $hash{$key}\n"; }` or using `each` for key-value pairs.
-
Explain the concept of object-oriented programming (OOP) in Perl.
- Answer: Perl supports OOP through packages and blessings. A class is essentially a package, and objects are created by blessing a reference into a package.
-
How do you create a class and objects in Perl?
- Answer: A class is defined by creating a package. Objects are created by blessing a reference to a hash or an array into the package using `bless`.
-
Explain inheritance in Perl.
- Answer: Inheritance is achieved by having one class inherit methods and attributes from another using the `@ISA` array.
-
What are autoloading and method overloading?
- Answer: Autoloading allows methods to be loaded only when they are called. Method overloading allows different methods to be called based on the number or type of arguments.
-
What are some common Perl modules you've used?
- Answer: (This will vary depending on experience, but examples include: `DBI`, `DBD::Pg`, `LWP::UserAgent`, `CGI`, `Moose`, `Test::More`, etc.)
Thank you for reading our blog post on 'Perl Interview Questions and Answers for 5 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!