First, the problem with the code. You have, in control.cgi:
Then in required.cgi, you have:my $TestVar = "Test";
Your problem is that the var in control.cgi isn't in package main. my variables aren't in any package; their scope is lexical to the file or the surrouding block. Which means that your code in required.cgi has *no way* of accessing the $TestVar in control.cgi.my ($Test2Var) = $main::TestVar;
So right there, you have a problem: there's no way you can get that to work if you're using lexical variables in control.cgi and trying to access them in required.cgi.
And this comes back to your implementation: this is really not the best way to do this. If the code in required.cgi is to be used by a lot of scripts, you should package it up into either a library of exportable variables and functions or make some sort of OO interface (but *only if that makes sense*). From your message it sounds like what you may want most is the former, a library of exportable functions and variables. Take a look at perlmod and Exporter for that.
In reply to Re: Scope of variables using strict
by btrott
in thread Scope of variables using strict
by dfog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |