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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple problem with global variables

Status
Not open for further replies.

naq2

Programmer
Aug 3, 2004
74
FR
I have some kind of strange problem:

I have a GLOBAL variable declare like:
$url = $CGIHandle->param("url") ; #without any my before

This variable is declared out of any subroutine... but when I want to access to it from one subroutine (in the same file) the value happily changes itself!

Could you help me on that...? Thanks in advance.

Much more questions to come!
 
try to write good code, use globals only when
you have no other chance to make the job :)
the code will be clean.
 
We'll need to see some code to determine scope
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
use strict
my everyting

in your situation it will still be global in scope. If the variable is being changed something has to be changing it.
 
Whao! Answers are so quick in here!

To answer all of you:
1) I know it's not clean code not to pass variables; but I have 15 of them, and if will be very awfull to read if I pass all of them.
2) Here is some code:

my $CGIHandle = new CGI ;
print $CGIHandle->header(-type => "text/html") ;

$url = $CGIHandle->param("url") ;
print "OUT: $selfUrl," ; #prints: OUT:
performRequest() ;

sub performRequest {
print "IN: $selfUrl" ; #prints: IN:<nothing!>

#...
}

3) I also tried with my everything... it doesn't change anything. :'(

Thanks for you help.
 
sorry: in 2) :
$selfUrl = $CGIHandle->param("selfUrl") ;

:)
 
if you try it as simple as that it *should* work,
could you be modifying the variable elsewhere,
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
that is the s..t Q with globals
every module, every object (in c) can
and is allowed to change values of globals:
the alternative are: write clean code (once)
and 'who is changing my value' (a lot of times)
ahah :)
 
please note that globals are allowed,
i used them twice in the past 25 years.
 
ok... thanks! Now I kind of understand...! but snif! It doesn't help much!

Is there any way of knowing who the F**K is changing my variables?

Thanks lots.
 
Print the variable's value at several points within the script to determine where the var changes to an unexpected value.
Keith
 
Bloody gremlins ....

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
What was the problem, just in case a similar soul wanders past?

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
As usual it was the programmer's fault.

But this time I was very stupid: I used undef($maVar) to test if the variable existed or not... and of course... after this the variable won't exist anymore!

Finally the short the best: use ($maVar) instead.

But I would have prefer to hide it! :)
 
It's important to learn, and while you're at, you might as well educate others.

Another question for my arsenal - do you use undef anywhere in your code

Cheers 4 that
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top