. 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__
-
8 3629
Once you tell the script to use the config file then you can use the variables as you were. - # This needs to be in your script that is importing the config file if your module is not in the standard location that perl looks.
-
use lib '/path/to/my/lib';
-
use My::Config;
-
-
# Use config hash globally.
-
print $config_var{'db_name'};
--Kevin
I ahve tried what you posted before. The module I am using as my Config is with in the same directory as my perl script.
This is the error that I get.
Global symbol "$config_var" requires explicit package name at main.pl line 14.
Here is my code. -
# Name: Main.pl
-
# Purpose: Handles the excecution of all transdoc
-
use Daemon qw(daemonize);
-
use Utility qw(scrubDoc);
-
use Config;
-
-
-
use strict;
-
-
-
#daemonize();
-
-
print $config_var('dbHost');
-
#while(1){
-
# sleep(20);
-
#}
-
I used $congif_var as an example. You would want to use the name of your hash that stores the values. Also, you still need to use the following line with the proper path to your config file. -
use lib '/path/to/your/module';
-
package My::Config;
-
use base Exporter;
-
-
our @EXPORT = qw(%config_vars);
-
our @EXPORT_OK = qw(%config_vars);
-
our %config_vars = (DBServerName => 'localhost',
-
DBName => 'my_db_name',
-
DBUserName => 'my_db_username',
-
DBPassword => 'my_db_password',
-
);
-
1;
Then in my script I use. - #!/usr/bin/perl -T
-
-
use strict;
-
use warnings;
-
-
use lib '/path/to/my/module';
-
use My::Config;
-
-
....rest of code here....
--Kevin
Sorry to be such a pita about this. It is just a little confusing. One thing I did notice is that you did not use a sub class. I however did. I have since changed this to simply return the config_vars hash.
However, I am still stuck with trying to figure out how to access the hash. I have tried everything I could think of to access and print data from said hash. Even with my updated code, I continue to get the Global symbol $config_vars/%config_vars requires explicit package name at main.pl line 15.
Here is my updated code.
main.pl -
# Name: Main.pl
-
# Purpose: Handles the excecution of all transdoc
-
use Daemon qw(daemonize);
-
use Utility qw(scrubDoc);
-
use lib '/src/perl-bash/docParse/final';
-
use My::Config;
-
-
-
use strict;
-
use warnings;
-
-
-
#daemonize();
-
-
print $config_vars('dbHost');
-
#while(1){
-
# sleep(20);
-
#}
-
My::Config (/src/perl-bash/docParse/final/My/Config.pm): -
# Name: AppConfig.pm
-
# Purpose: Global Config File.
-
-
package My::Config;
-
-
use base qw(Exporter);
-
our @EXPORT = qw();
-
our @EXPORT_OK = qw(%config_var);
-
-
use strict;
-
use Mysql;
-
-
#! 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()){
-
our %config_vars => {
-
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],
-
}
-
}
-
-
1;
-
__END__
-
I believe that the problem is that you are declaring your hash inside of your while loop. Declare the hash outside of the while loop.
--Kevin
After this line: our @EXPORT_OK = qw(%config_var);
add: our qw(%config_var);
and drop "our" in the "while" loop when the hash is built.
After this line: our @EXPORT_OK = qw(%config_var);
add: our qw(%config_var);
and drop "our" in the "while" loop when the hash is built.
I added the line you suggested, and it gives a whole new error.
No such class qw at My/Config.pm line 9, near "our qw"
I added the line you suggested, and it gives a whole new error.
No such class qw at My/Config.pm line 9, near "our qw"
Sorry, I guess you have to drop the qw operator after "our":
our (%config_var);
if you add more stuff to the "our" list use a comma to seperate the list: our (%config_var, %foo, $bar, &function); Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
6 posts
views
Thread by Ruslan |
last post: by
|
6 posts
views
Thread by Andrea Williams |
last post: by
|
3 posts
views
Thread by hansiman |
last post: by
|
41 posts
views
Thread by Miguel Dias Moura |
last post: by
|
12 posts
views
Thread by John M |
last post: by
|
7 posts
views
Thread by Adam |
last post: by
| |
5 posts
views
Thread by =?Utf-8?B?bWNxd2VydHk=?= |
last post: by
|
4 posts
views
Thread by icarus |
last post: by
| | | | | | | | | | |