INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
PERL
DETAILS
Question: Explain the difference between "my" and "local" variable scope declarations. ?
Answer: The variables declared with my() are visible only within the scope of the block which names them. They are not visible outside of this block, not even in routines or blocks that it calls. local() variables, on the other hand, are visible to routines that are called from the block where they are declared. Neither is visible after the end (the final closing curly brace) of the block at all.
|
Question:
Explain the difference between "my" and "local" variable scope declarations. ?
Answer:
The variables declared with my() are visible only within the scope of the block which names them. They are not visible outside of this block, not even in routines or blocks that it calls. local() variables, on the other hand, are visible to routines that are called from the block where they are declared. Neither is visible after the end (the final closing curly brace) of the block at all. Source: CoolInterview.com
'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. Source: CoolInterview.com
Answered by: mak | Date: 3/20/2010
| Contact mak
Both of them are used to declare local variables. The variables declared with "my" can live only within the block it was defined and cannot get its visibility inherited functions called within that block, but one defined with "local" can live within the block and have its visibility in the functions called within that block. Source: CoolInterview.com
Answered by: Bimlesh Sharma | Date: 8/30/2010
| Contact Bimlesh Sharma
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|