- Explain the difference between 'my' and 'local' variable scope declarations?
Both of them are used to declare local variables.
The variables declared with 'my' can live only within the block and cannot gets its visibility inherited fucntions called within that block, but one defined as 'local' canlive within the block and have its visibility in the functions called within that block.
'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 ";
print "In block, $::a = $::a ";
}
print "Outside block, $a = $a ";
print "Outside block, $::a = $::a ";
# This outputs
In block, $a = 3
In block, $::a = 3
Outside block, $a = 3.14159
Outside block, $::a = 3.14159
[download]
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 ";
print "In block, $::a = $::a ";
}
print "Outside block, $a = $a ";
print "Outside block, $::a = $::a ";
# This outputs
In block, $a = 3
In block, $::a = 3.14159
Outside block, $a = 3.14159
Outside block, $::a = 3.14159
[download]
ie, 'my' has no effect on the global $a, even inside the block.
http://www.coolinterview.com/interview/6205/
- what is meant by a 'pack' in perl?
Pack Converts a list into a binary representation. Takes an array or list of values and packs it into a binary structure
http://discuss.itacumens.com/index.php?topic=37847.0
- What are scalar data and scalar variables?
Scalar means a single thing, like a number or string
http://www.toppersarena.com/index.php?option=com_content&view=article&id=217:perl-interview-questions-and-answers&catid=36:blog-section&Itemid=49
- What is a hash?
conventions remain the same - any combination of letters, numbers and the underscore characte
%special_events
A hash is also called an associative array, or an array where each value is associated with a corresponding key
%contactInfo = ('name', 'Bob', 'phone', '111-111-1111');
http://www.techinterviews.com/perl-interview-questions-and-answers
http://www.onesmartclick.com/interviews/perl-programming-interview-questions-answers.html
What does the command ‘use strict’ do and why should you use it?
#!/usr/local/bin/perl -w
use strict;
This will help you (really, it will force you) to write better, cleaner code.
Adding the -w switch to the perl interpreter will cause it to spit out warnings on uninitialized variables
'use strict' turns on the 'strict' pragma which will make you declare your variables with the my keyword
'use strict' also tosses up errors if your code contains any barewords that can't be interpreted in their current context.
http://perl.about.com/b/2006/09/21/why-you-should-use-strict.htm
- "Use strict" is one of the pragma used for checking the variable scope and syntax
Eg: You can write a program like this:
$x=100
print $x
But if you use strict pragma, then you have to be very
specific while using variables
use strict;
my $x=100; ##look at the usage of scope like my,local,our
print $x;
http://www.allinterview.com/showanswers/16873.html
- Explain the difference between use and require.
The use statement works at compile time, require at run time. So if you have a module that does a lot in a begin block and you don't really need it in all cases, then it's clever to "require" it there where you want to use it but don't "use" it. So you don't have to wait for all that initializations of that module in case you don't need it.
The differences are many and often subtle:
use only expects a bareword, require can take a bareword or an expression
use is evaluated at compile-time, require at run-time
use implicitly calls the import method of the module being loaded, require does not
use excepts arguments in addition to the bareword (to be passed to import), require does not
use does not behave like a function (i.e can't be called with parens, can't be used in an expression, etc), whereas require does
So they behave differently but achieve the same goal. Then there's a list of cultural differences in addition to the technical differences, but they're not so hard and fast.
http://www.perlmonks.org/?node_id=412860
- What purpose does each of the following serve: -w, strict, - T ?
-w enables the warnings mode in perl-T is enables the Taint mode it performs some checks howyour program is using the data passed to itTurn on strict mode to make Perl check for common mistakes.
http://www.scribd.com/doc/1184228/Perl-Interview
-w:Enables warnings.Turning on warnings will make Perl yelp and complain at a huge variety of things that are almost always sources of bugs in your programs.
-t:Perl offers a mechanism called taint that marks any variable that the user can possibly control as being insecure. This includes user input, file input and environment variables.
Strict: It makes you declare all your variables (``strict vars''), and it makes it harder for Perl to mistake your intentions when you are using subs (``strict subs'').
- Difference between for & foreach ?
For and foreach both are for looping the code but if you want to use array in place of scalar for loop, you have to use foreach keyword
- What is a subroutine?
subroutine is a reusable piece of code.
- Why we use Perl?
1.Perl is a powerful free interpreter.
2.Perl is portable, flexible and easy to learn.
-For shell scripting
-For CGI
-Tons of scripts are available.
-Easy development
-Enormous big support script archive like CPAN
-No one starts to write a Perl scripts from scratch, you choose one from an archive and modify that.
-It is a "mature" scripting language.
-You may find Perl interpreter on every mission critical environment
-Easy to learn
- what's hash?
Hash in basically used to comment the script line.
A hash is and unordered set of key/value pairs that you access using strings (keys) as subscripts, to look up the scalar value corresponding to a given key.
- Name all the prefix dereferencer in perl?
The symbol that starts all scalar variables is called a prefix dereferencer. The different types of dereferencer are.
(i) $-Scalar variables
(ii) %-Hash variables
(iii) @-arrays
(iv) &-subroutines
(v) Type globs-*myvar stands for @myvar, %myvar.
- What's the difference between grep and map in Perl?
grep returns those elements of the original list that match the expression, while map returns the result of the expression applied to each element of the original list.
http://perlknowledge.blogspot.com/2010/12/perl-basic-interview-question-and.html
No comments:
Post a Comment