GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  Perl
Go To First  |  Previous Question  |  Next Question 
 Perl  |  Question 2 of 61    Print  
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 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.



  
Total Answers and Comments: 3 Last Update: June 29, 2009     Asked by: Murali Krishnan 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Jay Mathuria
 
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.

Above answer was rated as good by the following members:
udit.17december, borisc, lbsign
October 21, 2005 14:17:31   #1  
Jay Mathuria        

RE: Explain the difference between "my" and "local" va...
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.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
October 09, 2007 16:04:45   #2  
vamshi_maham Member Since: October 2007   Contribution: 2    

RE: Explain the difference between "my" and "local" va...
Perl supports three scoping declarations that enable to create private variables (using my) selectively global variable (using our) and temporary copies of selected global variables (using local) ... my declares a variable to be scoped within the current block and when the block ends the varaible goes out of scope.. "local" is an temporary value to an gobal variable and the value lasts only for the duaration of the block it's just has an temporary value while it's being used within that block...
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
June 29, 2009 09:45:11   #3  
gireeshkumar.kg Member Since: June 2009   Contribution: 5    

RE: Explain the difference between "my" and "local" variable scope declarations. ?
1) My
My creates a new variable and gives a lexical scope for that variable.The variable is not visible outside the block in which it is defined.

2) Local

Local saves the value of a global variable.It never creates a new variable. Here also the variable is not accessible outside the block but it is visible in the subroutine which is called from the original block. My is more preferring than local . But in various places local is useful where my is illegal

1) Changing the meaning of a special variable like $/
2) Localized file handles
3) Globing

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape