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

Discussion in 'Perl Misc' started by Fred, Feb 18, 2007.

  1. Fred

    Fred Guest

    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";

    }
     
    Fred, Feb 18, 2007
    #1
    1. Advertising

  2. Fred wrote:
    >
    > use vars qw($host $VERSION @ISA @EXPORT @EXPORT_OK);
    >
    > sub LoadConfig


    <snip>

    > my $host = $config->host();
    >
    > #####The $host variable is assigned here


    Yes, but that's the lexical variable $host, which is not the same as the
    package global $host you declared via the "use vars" statement.

    Try without "my".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 18, 2007
    #2
    1. Advertising

  3. Fred

    Joe Smith Guest

    Fred wrote:
    > 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.


    True.

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


    This says that $host will be shared unless explicitly overridden.

    > sub LoadConfig
    > {
    > my $host = $config->host();


    By using 'my', you have explicitly declared that, for the rest of the
    sub, $host will _not_ refer to the global variable with the same name.

    Remember, 'my' is exclusive with 'local', 'our', or 'use vars'.

    -Joe
     
    Joe Smith, Feb 19, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,990
    Smokey Grindel
    Dec 2, 2006
  2. Gilles Ganault

    Why no maintained wrapper to Win32?

    Gilles Ganault, Jul 28, 2007, in forum: Python
    Replies:
    12
    Views:
    567
    sturlamolden
    Jul 31, 2007
  3. Replies:
    29
    Views:
    202
  4. Tim S
    Replies:
    3
    Views:
    138
    Brian McCauley
    Jun 1, 2007
  5. Henry Law
    Replies:
    3
    Views:
    118
    Henry Law
    Jan 27, 2008
Loading...

Share This Page