Sunday, March 4, 2012

Perl Interview Questions Part 2

Q:What is Perl one-liner? 
A:There are two ways a Perl script can be run:
--from a command line, called one-liner, that means you type and execute immediately on the command line. You'll need the -e option to start like "C:\ %gt perl -e "print \"Hello\";". One-liner doesn't mean one Perl statement. One-liner may contain many statements in one line.
--from a script file, called Perl program.

Q:Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}? 
A:${var} is the lexical variable $var, and ${"var"} is the dynamic variable $var.
Note that because the second is a symbol table lookup, it is disallowed under `use strict "refs"'. The words global, local, package, symbol table, and dynamic all refer to the kind of variables that local() affects, whereas the other sort, those governed by my(), are variously knows as private, lexical, or scoped variable.

Q:What happens when you return a reference to a private variable? 
A:Perl keeps track of your variables, whether dynamic or otherwise, and doesn't free things before you're done using them.

Q:How to turn on Perl warnings? Why is that important? 
A:Perl is very forgiving of strange and sometimes wrong code, which can mean hours spent searching for bugs and weird results. Turning on warnings helps uncover common mistakes and strange places and save a lot of debugging time in the long run. There are various ways of turning on Perl warnings:
For Perl one-liner, use -w option on the command line.
On Unix or Windows, use the -w option in the shebang line (The first # line in the script). Note: Windows Perl interpreter may not require it.
For other systems, choose compiler warnings, or check compiler documentation.

Q:What are scalar data and scalar variables? 
A:Perl has a flexible concept of data types. Scalar means a single thing, like a number or string. So the Java concept of int, float, double and string equals to Perl\'s scalar in concept and the numbers and strings are exchangeable. Scalar variable is a Perl variable that is used to store scalar data. It uses a dollar sign $ and followed by one or more alphanumeric characters or underscores. It is case sensitive.

No comments:

Post a Comment