Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Strict and my $var 1

Status
Not open for further replies.

perl21

Programmer
Jul 13, 2005
22
US
Hey everyone.

I was chasing down a bug in my mod_perl site and thought initially a variable was being undesirably cached because of my use of the my $var style code with use strict;

Instead of saying

my %Cookies = $standardLibrary->&GetCookies('CookieName');

I used,

my %Cookies;
undef(%Cookies);
%Cookies = $standardLibrary->&GetCookies('CookieName');

I thought this would solve my problem with the cookies of another person's browser being used when someone else visits the site.

It turns out that after doing this it didn't help, and was more likely because the %Cookies var wasn't being undefined inside my library within the GetCookies sub. It appears to have corrected the problem. I just tested it again and would have been doing it by now.

My question is, why use strict? and was it contributing to the problem? My guess is that it wasn't causing the problem at all but now I wonder why use it at all.

Thanks,
Tony
 
if you are declaring all your variables properly with "my", just like you would when using strict, then you don't have to use strict, it will have the same effect: making the variables scope local to the block of code it's declared in. But you shouyd still use strict, otherwise a single error in scoping a variable might cause problems that will be very hard if not impossible to track down.

Did you try:

Code:
my %Cookies = ();#<-- should now be empty of data
%Cookies = $standardLibrary->&GetCookies('CookieName');
 

Hey Kevin, Thanks.

If I use strict then I have to declare all variables with my, is there a reason I would want to have a variable not local to the block it was declared in?

And when you say local, I assume that if I declare a variable inside a sub routine or library it's not shared with the code outside of the sub or library.

If otherwise please let me know. I will also do more reading on this at the week end.

Thanks,
Tony
 
If I use strict then I have to declare all variables with my, is there a reason I would want to have a variable not local to the block it was declared in?

And when you say local, I assume that if I declare a variable inside a sub routine or library it's not shared with the code outside of the sub or library.


Thats pretty much it. Why you would want a variable to be global is up to you, but I think most experienced coders will tell you to avoid global variables if at all possible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top