Reputation: 1989
I have following PL and PM files
Start.PL, ConfigReader.PM, BL.PM, Logger.PM
Code extract from ConfigReader.PM
Package ConfigReader
use Config::Simple;
use Logger;
our $configIni;
sub OpenConfigIni()
{
my ($cfg_ini_path)=@_;
&Logger::LogMsg("**** OpenConfigIni starts ****","info");
&Logger::LogMsg($cfg_ini_path,"info");
$configIni = new Config::Simple($cfg_ini_path);
&Logger::LogMsg("**** OpenConfigIni ends ****","info");
}
Where Logger.PM is a custom module I have written using Log4Perl.
Code extract from BL.PM
Package BL
use strict;
use Logger;
use ConfigReader;
my %chash = %{$ConfigReader::configIni->param( -block => "UserDetails")};
Code extract from Start.PL
Start.PL is the entry point to my application which initiates Logger, ConfigReader
use strict;
use ConfigReader;
use Logger;
use BL;
&Logger::InitiateLogger();
&ConfigReader::OpenConfigIni("My_Config.ini");
my %qhash = %{$ConfigReader::configIni->param( -block => "ABC")};
Problem is
When i run Start.PL, the code fails with following error
Can't call method "param" on an undefined value at BL.pm line 9
Compilation failed in require at Start.pl line 19.
BEGIN failed--compilation aborted at Start.pl line 19.
(Ignore Line numbers)
It is pointing to following in BL.pm
my %chash = %{$ConfigReader::configIni->param( -block => "UserDetails")};
If i comment use Bl
in start.Pl, the script works fine that means
my %qhash = %{$ConfigReader::configIni->param( -block => "ABC")};
works fine in Start.PL
what i am trying to achieve here is that
1. Use the ConfigReader.PM for handling the configuration related
2. Make use of the our $configIni in all modules
3. Initialing the Logger and ConfigReader form start.pl whioch is the entry point here
What could be the problem here?
Upvotes: 2
Views: 294
Reputation: 41085
The initialization of %chash
happens when BL.pm is loaded. Which happens as part of the use BL;
in Start.PL. As you can see, execution of Start.PL at that point has not yet reached the call to OpenConfigIni
, so the $ConfigReader::configIni
variable has not yet been initialized.
The %qhash
initialization in Start.pl happens after the config initialization call, so that works.
As you have discovered, initializing %chash
from a function works. Initializing your various modules explicitly through initialization functions is IMO better style than relying on load order to get the initializations right. And mixing and matching doesn't work that well, as you've seen.
Upvotes: 2