Sunday, December 01, 2013

Perl - Quick Recap

Link to a source on the web: http://careerride.com/perl-interview-questions.aspx

Differentiate between Use and Require, My and Local, For and Foreach and Exec and System

Use and Require

Both the Use and Require statements are used while importing modules.

A require statement imports functions only within their packages. The use statement imports functions with a global scope so that their functions and objects can be accessed directly.

Eg. Require module;
Var = module::method(); //method called with the module reference

Eg: use module;
Var = method(); //method can be called directly


-Use statements are interpreted and are executed during the parsing whereas the require statements are executed during run time thereby supporting dynamic selection of modules.


My and Local


'my' creates a new variable, 'local' temporarily amends the value of a variable

There is a subtle difference.
In the example below, $::a refers to $a in the 'global' namespace.

$a = 3.14159; { local $a = 3; print "In block, \$a = $a\n"; print "In block, \$::a = $::a\n"; } print "Outside block, \$a = $a\n"; print "Outside block, \$::a = $::a\n"; # This outputs In block, $a = 3 In block, $::a = 3 Outside block, $a = 3.14159 Outside block, $::a = 3.14159
ie, 'local' temporarily changes the value of the variable, but only within the scope it exists in.

so how does that differ from 'my'? 'my' creates a variable that does not appear in the symbol table, and does not exist outside of the scope that it appears in. So using similar code:
$a = 3.14159; { my $a = 3; print "In block, \$a = $a\n"; print "In block, \$::a = $::a\n"; } print "Outside block, \$a = $a\n"; print "Outside block, \$::a = $::a\n"; # This outputs In block, $a = 3 In block, $::a = 3.14159 Outside block, $a = 3.14159 Outside block, $::a = 3.14159


For and Foreach

The for statement has an initialization, condition check and increment expressions in its body and is used for general iterations performing operations involving a loop.

The foreach statement is particularly used to iterate through arrays and runs for the length of the array.

Exec and System

The exec command stops the execution of the current process and starts the execution of the new process and does not return back to the stopped process. 

But the system command, holds the execution of the current process, forks a new process and continues with the execution of the command specified and returns back to the process on hold to continue execution.

What is the use of command "use strict"?

Use strict command calls the strict pragma and is used to force checks on definition and usage of variables, references and other barewords used in the script. If unsafe or ambiguous statements are used, this command stops the execution of the script instead of just providing warnings.

What would happen if you prefixed some variables with following symbols?

i.) $ - The variable becomes a scalar variable which can hold one value only
ii.) @ - The variable becomes an array variable which can hold a list of scalar variables
iii.) % - The variable becomes a hash variable which stores values as key-value pairs

What is the use of following?

i.) –w
When used gives out warnings about the possible interpretation errors in the script.

ii.) Strict
Strict is a pragma which is used to force checks on the definition and usage of variables, references and other barewords used in the script. This can be invoked using the use strict command. If there are any unsafe or ambiguous commands in the script, this pragma stops the execution of the script instead of just giving warnings.

iii.) -T.
When used, switches on taint checking which forces Perl to check the origin of variables where outside variables cannot be used in system calls and subshell executions.



What is chomp Function:
Chomp is used to remove newline character "\n" from the end of a string, or from end of every element of an array, or from every value of a hash.

What is Chop function

chop function removes the last character of a string variable and returns the chopped character, it doesn’t matter what it is