From: "Birgit Kellner" <[EMAIL PROTECTED]>
> I have one script that uses global variables - scalars, arrays, and
> hashes - declared with "use qw(...)".
>
> The script requires a configuration file in which these variables are
> assigned values and which also contains a few subroutines.
>
> At some point in the script, I want to switch to a different
> configuration file, use its variable values and the subroutines
> defined therein; then I want to switch back to the original
> configuration file. I thought of doing alternate require-s for this
> purpose, i.e. to first require one configuration file, then the
> second, and then again the first:
require remembers what has been required and doesn't execute the
code again. You want
do "firstsetupfile.cgi";
instead of
require "firstsetupfile.cgi";
> 1) mainscript.cgi
> use vars qw($this $that);
> require ("firstsetupfile.cgi");
> print &create_code; # values and routine from first setup file
> require ("secondsetupfile.cgi");
> print &create_code;# do the same with second configuration file
> require ("firstsetupfile.cgi"); # switch back
> ....
> print "$this\n"; # should print "somevalue"
>
> 2) firstsetupfile.cgi
> my $this = "somevalue";
> my $that = "othervalue";
If you use "my" outside all blocks then the scope of the variable(s)
is the REST OF THE FILE. So if you do this in an include or
module your variables will NOT be accessible to the outside world.
Basicaly the $this in firstsetupfile.cgi and $this in mainscript.cgi
are two UNRELATED variables. If you want to change the global
variables delete the "my".
> ....
> sub create_code {
> my $output;
> $output = "$this and $that";
> return ($output);
> }
Well this is why it seems to work. You define the function in the
scope of existence of the lexical (declared with "my") variables. So
the code accesses those and not the global ones you declared with
use vars qw($this $that);
in mainscript.cgi.
> I've already noticed that arrays and hashes behave differently in the
> process: arrays apparently have to be undef-d, or otherwise, the new
> values are just added to the existing ones and don't replace them.
> With hashes, however, I don't have to undef; I don't really understand
> why.
This depends on how do you set the arrays. If you write
@array = (1,2,3);
then the @array will always contain just the (1,2,3) no matter what
did it contain before. First fix the scoping, use the "do" instead of
"require" and come back if it still does something unexpected.
Jenda
P.S.: My site was down over the holidays (Don't ask me why. It
was not my fault.), but it should be back online on monday. Please
use the mirror at http://www24.brinkster.com/Jenda/ for the time
being if you need something. The address of the PPM repository is
http://www24.brinkster.com/Jenda/perl/
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
--- me
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]