Why isn't variable maintained between subroutines in .pm module?



According to the docs, if you define a variable using the
"use vars" pragma in a perl module, then that variable should
be available across the entire .pm module. Listed below I have
an excerpt from a module named test.pm. The $host variable is
defined with the "usr vars" pragma, but I can't access it from
the subroutine test() below.

Essentially what I want to do is use AppConfig to read values
from a configuration file like host, port, etc., then have
them available to other subroutines when called. I'm trying
to avoid having to read the config file every time a subroutine
is called. Is this possible without using the Storable module?

-Thanks



our @EXPORT = qw(
LoadConfig
);

our $VERSION = '0.01';

use vars qw($host $VERSION @ISA @EXPORT @EXPORT_OK);


sub LoadConfig
{
shift @_;
my ($cfgfile) = @_;

our $config = AppConfig->new(
{
CASE => 1,
PEDANTIC => 0,
CREATE => 1,
ERROR => sub {},
GLOBAL => { ARGCOUNT => ARGCOUNT_ONE }
}
);

$config->file($cfgfile);


my $host = $config->host();

#####The $host variable is assigned here
print "HOST IN LoadConfig: $host\n";

}



sub test
{

######I can't reference $host in different subroutine
print "IN TEST() HOST IS: $host\n";

}

.



Relevant Pages

  • Re: Memory problem with XML::DOM::Parser???
    ... >> # Die Anfrage ist Teil der neuen EPA ... # Modul für die XML-Funktionen des Clients ... # Subroutine, um die XML-Struktur aus dem XML-Rootfile und den ... sub construct_xml { ...
    (comp.lang.perl.misc)
  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • Re: Why does assigning @_ cause subs parameter to be copied?
    ... no use describing sub returns ... that isn't actually returning a hash which can't be done. ... Arguments to a subroutine are accessible inside the subroutine ... HERE you copied the array to a lexical. ...
    (comp.lang.perl.misc)
  • Re: Shared Method Problem With "Global" Storage
    ... Shared Method Problem With "Global" Storage ... declare the Sub as Shared. ... What I don't get is that if I refer to ButtonHasBeenClicked in a subroutine ... code which is unique to mining web site B. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Proper class setup?
    ... > sub init { ... here: AUTOLOAD and lvalue subroutines. ... if a subroutine named AUTOLOAD exists then it will be called whenever ...
    (perl.beginners)