jrottman
Programmer
- Jun 17, 2005
- 47
So I think I have made some head way with my perl education. In order to teach my self more. I have started to convert some of my cf scheduled tasks to perl applications.
One area where things are kind of fuzzy is setting up global variables that can be called from any module with in an application.
So far I have created a farily standard module that will act as my global config. This module will store variables that are populated from the database. So that I will not be forced to re-create/query the database anytime I need to use a particular variable or system path.
The area where I am stuck is basically sharing the variables with other modules. I have created a hash stores the variables. Seen below. But I am not sure how to access it from other modules or even verify that the data with in the hash is correct. I have tried print "$cfg_hash{emailHost}"; but this returns Global symbol "%cfg_hash" requires explicit package name at AppConfig.pm line 56.
Config Hash:
while (my @results = $execute->fetchrow()){
cfg_hash => {
dbHost => "$dbHost",
dbUser => "$dbUser",
dbPass => "$dbPass",
datasource => "$datasource",
emailHost => $results[0],
emailUser => $results[1],
emailPass => $results[2],
javaPath => $results[3],
bcReadPath => $results[4],
bcWritePath => $results[5],
storePath => $results[6],
}
}
This is how I am calling the mkConfig method in my main perl script. I have attached the full source below.
my $testVar = mkConfig();
Main.pl:
# Name: Main.pl
# Purpose: Handles the excecution of all transdoc
use Daemon qw(daemonize);
use Utility qw(scrubDoc);
use AppConfig qw(mkConfig);
use strict;
#daemonize();
my $testVar = mkConfig();
#while(1){
# sleep(20);
#}
AppConfig.pm
# Name: AppConfig.pm
# Purpose: Global Config File.
package AppConfig;
use base qw(Exporter);
our @EXPORT = qw();
our @EXPORT_OK = qw(mkConfig);
use strict;
use Mysql;
sub mkConfig {
#! Start MYSQL SERVER CONFIG
my $dbHost = "";
my $dbUser = "";
my $dbPass = "";
my $datasource = $ARGV[0];
# PERL MYSQL CONNECT()
my $connect = Mysql->connect($dbHost, $datasource, $dbUser, $dbPass);
# SELECT DB
$connect->selectDB($datasource);
# DEFINE A MySQL QUERY
my $myquery = "SELECT
fld_system_EmailHost,
fld_system_EmailUser,
fld_system_EmailPass,
fld_system_JavaPath,
fld_system_BarcodeWritePath,
fld_system_BarcodeReadPath,
fld_system_StorePath
FROM
tbl_smartPanel_Paperless_Settings";
# EXECUTE THE QUERY FUNCTION
my $execute = $connect->query($myquery);
while (my @results = $execute->fetchrow()){
cfg_hash => {
dbHost => "$dbHost",
dbUser => "$dbUser",
dbPass => "$dbPass",
datasource => "$datasource",
emailHost => $results[0],
emailUser => $results[1],
emailPass => $results[2],
javaPath => $results[3],
bcReadPath => $results[4],
bcWritePath => $results[5],
storePath => $results[6],
}
}
print cfg_hash{"emailHost"};
}
1;
__END__
One area where things are kind of fuzzy is setting up global variables that can be called from any module with in an application.
So far I have created a farily standard module that will act as my global config. This module will store variables that are populated from the database. So that I will not be forced to re-create/query the database anytime I need to use a particular variable or system path.
The area where I am stuck is basically sharing the variables with other modules. I have created a hash stores the variables. Seen below. But I am not sure how to access it from other modules or even verify that the data with in the hash is correct. I have tried print "$cfg_hash{emailHost}"; but this returns Global symbol "%cfg_hash" requires explicit package name at AppConfig.pm line 56.
Config Hash:
while (my @results = $execute->fetchrow()){
cfg_hash => {
dbHost => "$dbHost",
dbUser => "$dbUser",
dbPass => "$dbPass",
datasource => "$datasource",
emailHost => $results[0],
emailUser => $results[1],
emailPass => $results[2],
javaPath => $results[3],
bcReadPath => $results[4],
bcWritePath => $results[5],
storePath => $results[6],
}
}
This is how I am calling the mkConfig method in my main perl script. I have attached the full source below.
my $testVar = mkConfig();
Main.pl:
# Name: Main.pl
# Purpose: Handles the excecution of all transdoc
use Daemon qw(daemonize);
use Utility qw(scrubDoc);
use AppConfig qw(mkConfig);
use strict;
#daemonize();
my $testVar = mkConfig();
#while(1){
# sleep(20);
#}
AppConfig.pm
# Name: AppConfig.pm
# Purpose: Global Config File.
package AppConfig;
use base qw(Exporter);
our @EXPORT = qw();
our @EXPORT_OK = qw(mkConfig);
use strict;
use Mysql;
sub mkConfig {
#! Start MYSQL SERVER CONFIG
my $dbHost = "";
my $dbUser = "";
my $dbPass = "";
my $datasource = $ARGV[0];
# PERL MYSQL CONNECT()
my $connect = Mysql->connect($dbHost, $datasource, $dbUser, $dbPass);
# SELECT DB
$connect->selectDB($datasource);
# DEFINE A MySQL QUERY
my $myquery = "SELECT
fld_system_EmailHost,
fld_system_EmailUser,
fld_system_EmailPass,
fld_system_JavaPath,
fld_system_BarcodeWritePath,
fld_system_BarcodeReadPath,
fld_system_StorePath
FROM
tbl_smartPanel_Paperless_Settings";
# EXECUTE THE QUERY FUNCTION
my $execute = $connect->query($myquery);
while (my @results = $execute->fetchrow()){
cfg_hash => {
dbHost => "$dbHost",
dbUser => "$dbUser",
dbPass => "$dbPass",
datasource => "$datasource",
emailHost => $results[0],
emailUser => $results[1],
emailPass => $results[2],
javaPath => $results[3],
bcReadPath => $results[4],
bcWritePath => $results[5],
storePath => $results[6],
}
}
print cfg_hash{"emailHost"};
}
1;
__END__