Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use, require ?

Status
Not open for further replies.

1yura1

Programmer
Feb 7, 2003
36
0
0
UA
Hi all.

I have the module Vars.pm, that parthes and exports some configuration variables:
#!/usr/bin/perl -w

package Module::Vars;

require 5.004;
require Exporter;

use strict;
use Exporter;

BEGIN
{
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

$VERSION = 0.1;
@ISA = ('Exporter');
@EXPORT = qw(
);
@EXPORT_OK = qw(
$var1
$var2
...
);
}

use vars @EXPORT_OK;

($var1, $var2, ...) = ParthConfig(Some_config_file);

----------------------------------------------------

Then in my script I modefy configuration file [Some_config_file], and need to import from Module::Vars module variables: $var!, $var2. But when I write
require Module::Vars;
Module::Vars->import(qw($var1, $var2, ...));
or
use Module::Vars qw($var1, $var2, ...);
it imports old (not modefied by this script) values of $var1, $var2, ... variables.

I try to use "use autouse" also - the same result.

Thanks.
 
Hi,

Can I ask a quick question? Does the module get loaded before the script modifies the file and then imports the variables? If so, the code you have to set the variables in Module::Vars will only get executed the first time it is loaded. This means if you alter the file after it is loaded and then require or use Module::Vars at a later time, the PathConfig() will not get executed again.

What you probaly want to do is override the import function to check that the config file has not been changed since the Module was last used and re-read the values if it was (or just re-read it every time).

Does this make any sense?

Scotty
 
Hi Scotty

The module get loaded (use, require) after the script modifies the config file, but when I use imported variables, Thay have old (not modefied) values?

Thanks,
Yura
 
Hi Yura,

I am still a little unsure if you have already loaded the module (i.e. from another Module). You can easily check with a quick print statement in the ParseConfig sub to see when it is called.

Otherwise I would still suggest you override the import as follows:

sub import {
# do not modify @_ in this routine!
($var1, $var2) = ParseConfig(...);
Module::Vars->export_to_level(1, @_);
}

check out the docs on Export for more detail.

Kind regards,
Scotty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top