Important alert: (current site time 7/15/2013 9:04:00 PM EDT)
 

article

Access a dynamic variable while a similarly named lexical is in scope

Email
Submitted on: 7/29/2000 10:54:00 PM
By: Found on the World Wide Web 
Level: Beginner
User Rating: By 2 Users
Compatibility: 5.0 (all versions), 4.0 (all versions)
Views: 7838
 
     How can I access a dynamic variable while a similarly named lexical is in scope? You can do this via symbolic references, provided you haven't set use strict "refs". So instead of $var, use ${'var'}.

 
				
sub lexical {
my $var = 'private';# new private variable, $var
visible(); # (invisible outside of sub scope)
}

 

$var = 'global';

 

visible(); # prints global
dynamic(); # prints local
lexical(); # prints global

Notice how at no point does the value ``private'' get printed. That's because $var only has that value within the block of the lexical() function, and it is hidden from called subroutine.

In summary, local() doesn't make what you think of as private, local variables. It gives a global variable a temporary value. my() is what you're looking for if you want private variables.


Other 100 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/12/2002 4:26:54 AMChris Stratford

Hey,
I have a question thats been bugging me, if you can help me, please mail me the solution @ neester_@hotmail.com...

My question is, Is it possible to make a Perl/CGI script that Saves the content of a URL to a file... ie.
If the user navigates to:

http://mysite.com/cgi-bin/logger.pl?I+WANT+TO+BE+ABLE+TO+SAVE+THIS+TEXT
is
that possible?
if it is, i am guessing it wont be too hard, i have been researching for the last hour, but to no avail...

I came upon a lot of your submissions, and you look like a pro, so yeah. can you help me? please!
thank you!
(If this comment was disrespectful, please report it.)

 
2/19/2003 9:26:46 AMAlan Darwish

Have ever seen wordcrucnher
I was wondering if you can emmulate it for me as a contract on rentacoder?
Thanks
ldapguru@yahoo.com
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.