Question: What are the key features of Perl?
|
Answer: Key features of Perl include powerful text manipulation capabilities through regular expressions,
support for both procedural and object-oriented programming paradigms, platform independence, extensive built-in functions,
and a large ecosystem of modules via CPAN (Comprehensive Perl Archive Network).
|
Question: How can you add comments in Perl?
|
Answer: In Perl, one can use the # character to write comments.
Anything following the # on the same line is considered a comment and is ignored by the Perl interpreter.
|
Question: What is the difference between my, our, and local in Perl?
|
Answer: One can find the difference below:
• 'my' creates a lexically scoped variable that is private to the enclosing block, file, or eval.
• 'our' creates a package variable that can be accessed from anywhere within the current package.
• 'local' creates a temporary value for a global variable, which is local to the current scope until the end of that scope or until the corresponding local declaration is exited.
|
Question: What is a Perl module?
|
Answer: A Perl module is a reusable piece of code that encapsulates functionality.
It typically consists of variables, functions, and classes.
Modules help organize code into logical units, promote code reuse, and simplify maintenance.
|
Question: How do you read from a file in Perl?
|
Answer: Below one can find how a file is read in Perl:
open(my $fh, '<', 'filename.txt') or die "Can't open file: $!";
while (my $line = <$fh>) {
chomp $line; # remove newline character
# do something with $line
}
close $fh;
|
Question: How do you handle errors in Perl?
|
Answer: Error handling in Perl can be done in following ways:
• Using die function to print an error message and terminate the program.
• Using warn function to print a warning message but continue executing the program.
• Additionally, Perl provides eval function for trapping exceptions.
|
Question: How do you install Perl modules from CPAN?
|
Answer: One can install Perl modules from CPAN using the CPAN module itself, or by using tools like cpanm (CPAN Minus)
or by using perl -MCPAN -e 'install Module::Name'.
For example, to install a module named Module::Name, one can run:
cpanm Module::Name
|