
japhy
Enthusiast
Jun 4, 2000, 7:22 PM
Post #4 of 4
(410 views)
|
|
Re: loading dynamic configuration variables
[In reply to]
|
Can't Post
|
|
The problem with TESTRC1 is that your require()d file declares its variables with my(), which means they're not seen OUTSIDE that file -- not what you'd like. And in your main program, $c was never declared using my(), so strict.pm got upset. By the way, $a and $b are exempt from strict.pm's tyranny, since they're used in sort() subroutines. Variables declared via my() are not entered into the symbol table at all. Only global variables are found in the symbol table. If you want to get the effect of require() at compile-time (I don't know exactly why that's important to you), then wrap it in a BEGIN { } block: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> BEGIN { require "config.pl" } </pre><HR></BLOCKQUOTE> Here is an example that DOESN'T work, and an explanation why: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> # config.pl $this = 10; $that = 20; # main.pl #!/usr/bin/perl -w use strict; my ($this,$that); require "config.pl"; print "$this $that\n"; </pre><HR></BLOCKQUOTE> This will warn you that $this and $that are uninitialized. The reason is because config.pl is setting $main::this and $main::that, while main.pl is using lexically scoped variables $this and $that, not the global variables $main::this and $main::that. Here is a working example: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> # config.pl # this file is free from 'use strict' $this = 10; $that = 20; # main.pl #!/usr/bin/perl -w use strict; use vars qw( $this $that ); require "config.pl"; # or # BEGIN { require "config.pl" } # for compile-time loading print "$this $that"; </pre><HR></BLOCKQUOTE> config.pl is free from main.pl's 'use strict' limitation because the code is executed in a NEW scope, not within main.pl's scope, and strict.pm's effects are scope-based. The variables we define in config.pl are in main::. Then, in main.pl, we tell strict.pm to allow $this and $that without having to give an explicit package name. The vars.pm pragma basically tells Perl that these symbol names are to be assumed to be in the package you're in, so saying: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use strict; use vars '$foo'; $main::foo = 20; print $foo; </pre><HR></BLOCKQUOTE> will print 20. However, it does NOT alias '$foo' to mean '$currentpackage::foo': <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use strict; package Bar; use vars '$this'; # tells Perl that $this in package # Bar means $Bar::this $this = 20; package Foo; $this = 30; # strict complains here </pre><HR></BLOCKQUOTE> You'd need to add another call to vars.pm after you start package Foo: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use strict; package Bar; use vars '$this'; $this = 20; package Foo; use vars '$this'; $this = 30; </pre><HR></BLOCKQUOTE> That's enough for now. I'm tired, and I want to sleep.
|