By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,760 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,760 IT Pros & Developers. It's quick & easy.

Creating a Global Config

P: 46
. 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:
Expand|Select|Wrap|Line Numbers
  1.             while (my @results = $execute->fetchrow()){
  2.                 cfg_hash => {
  3.                             dbHost => "$dbHost",
  4.                             dbUser => "$dbUser",
  5.                             dbPass => "$dbPass",
  6.                             datasource => "$datasource",
  7.                             emailHost => $results[0],
  8.                             emailUser => $results[1],
  9.                             emailPass => $results[2],
  10.                             javaPath => $results[3],
  11.                             bcReadPath => $results[4],
  12.                             bcWritePath => $results[5],
  13.                             storePath => $results[6],
  14.                             }
  15.             }
  16.  
  17.  
  18.  

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:
Expand|Select|Wrap|Line Numbers
  1. # Name:    Main.pl
  2. # Purpose: Handles the excecution of all transdoc
  3. use Daemon qw(daemonize);
  4. use Utility qw(scrubDoc);
  5. use AppConfig qw(mkConfig);
  6.  
  7.  
  8. use strict;
  9.  
  10.  
  11. #daemonize();
  12.  
  13.     my $testVar = mkConfig();
  14.     #while(1){
  15.     #   sleep(20);
  16.     #}
  17.  

AppConfig.pm

Expand|Select|Wrap|Line Numbers
  1. # Name: AppConfig.pm
  2. # Purpose:  Global Config File.
  3.  
  4. package AppConfig;
  5.  
  6.     use base qw(Exporter);
  7.     our @EXPORT = qw();
  8.     our @EXPORT_OK = qw(mkConfig);
  9.  
  10.     use strict;
  11.     use Mysql;
  12.  
  13.         sub mkConfig {
  14.  
  15.             #! Start MYSQL SERVER CONFIG
  16.             my $dbHost = "";
  17.             my $dbUser = "";
  18.             my $dbPass = "";
  19.             my $datasource = $ARGV[0];
  20.  
  21.             # PERL MYSQL CONNECT()
  22.             my $connect = Mysql->connect($dbHost, $datasource, $dbUser, $dbPass);
  23.  
  24.             # SELECT DB
  25.             $connect->selectDB($datasource);
  26.  
  27.             # DEFINE A MySQL QUERY
  28.             my $myquery = "SELECT
  29.                                 fld_system_EmailHost,
  30.                                 fld_system_EmailUser,
  31.                                 fld_system_EmailPass,
  32.                                 fld_system_JavaPath,
  33.                                 fld_system_BarcodeWritePath,
  34.                                 fld_system_BarcodeReadPath,
  35.                                 fld_system_StorePath                            
  36.                            FROM
  37.                                 tbl_smartPanel_Paperless_Settings";
  38.             # EXECUTE THE QUERY FUNCTION
  39.             my $execute = $connect->query($myquery);
  40.  
  41.             while (my @results = $execute->fetchrow()){
  42.                 cfg_hash => {
  43.                             dbHost => "$dbHost",
  44.                             dbUser => "$dbUser",
  45.                             dbPass => "$dbPass",
  46.                             datasource => "$datasource",
  47.                             emailHost => $results[0],
  48.                             emailUser => $results[1],
  49.                             emailPass => $results[2],
  50.                             javaPath => $results[3],
  51.                             bcReadPath => $results[4],
  52.                             bcWritePath => $results[5],
  53.                             storePath => $results[6],
  54.                             }
  55.             }
  56.             print cfg_hash{"emailHost"};
  57.         }
  58. 1;
  59. __END__
  60.  
Jan 14 '08 #1
Share this Question
Share on Google+
8 Replies


eWish
Expert 100+
P: 971
Once you tell the script to use the config file then you can use the variables as you were.

Expand|Select|Wrap|Line Numbers
  1. # 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.
  2. use lib '/path/to/my/lib';
  3. use My::Config;
  4.  
  5. # Use config hash globally.
  6. print $config_var{'db_name'};
--Kevin
Jan 14 '08 #2

P: 46
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.

Expand|Select|Wrap|Line Numbers
  1. # Name:    Main.pl
  2. # Purpose: Handles the excecution of all transdoc 
  3. use Daemon qw(daemonize);
  4. use Utility qw(scrubDoc);
  5. use Config;
  6.  
  7.  
  8. use strict;
  9.  
  10.  
  11. #daemonize();
  12.  
  13.     print $config_var('dbHost');
  14.     #while(1){
  15.     #   sleep(20);
  16.     #}
  17.  
Jan 14 '08 #3

eWish
Expert 100+
P: 971
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.
Expand|Select|Wrap|Line Numbers
  1. use lib '/path/to/your/module';
Expand|Select|Wrap|Line Numbers
  1. package My::Config;
  2. use base Exporter; 
  3.  
  4. our @EXPORT = qw(%config_vars);
  5. our @EXPORT_OK = qw(%config_vars);
  6. our %config_vars = (DBServerName => 'localhost',
  7.                     DBName             => 'my_db_name',
  8.                     DBUserName         => 'my_db_username',
  9.                     DBPassword         => 'my_db_password',
  10.            );
  11. 1;
Then in my script I use.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use lib '/path/to/my/module';
  7. use My::Config;
  8.  
  9. ....rest of code here....
--Kevin
Jan 14 '08 #4

P: 46
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

Expand|Select|Wrap|Line Numbers
  1. # Name:    Main.pl
  2. # Purpose: Handles the excecution of all transdoc 
  3. use Daemon qw(daemonize);
  4. use Utility qw(scrubDoc);
  5. use lib '/src/perl-bash/docParse/final';
  6. use My::Config;
  7.  
  8.  
  9. use strict;
  10. use warnings;
  11.  
  12.  
  13. #daemonize();
  14.  
  15.     print $config_vars('dbHost');
  16.     #while(1){
  17.     #   sleep(20);
  18.     #}
  19.  
My::Config (/src/perl-bash/docParse/final/My/Config.pm):
Expand|Select|Wrap|Line Numbers
  1. # Name: AppConfig.pm
  2. # Purpose:  Global Config File.
  3.  
  4. package My::Config;
  5.  
  6.     use base qw(Exporter);
  7.     our @EXPORT = qw();
  8.     our @EXPORT_OK = qw(%config_var);
  9.  
  10.     use strict;
  11.     use Mysql;
  12.  
  13.         #! Start MYSQL SERVER CONFIG
  14.         my $dbHost = "";
  15.         my $dbUser = "";
  16.         my $dbPass = "";
  17.         my $datasource = $ARGV[0];
  18.  
  19.         # PERL MYSQL CONNECT()
  20.         my $connect = Mysql->connect($dbHost, $datasource, $dbUser, $dbPass);
  21.  
  22.         # SELECT DB
  23.         $connect->selectDB($datasource);
  24.  
  25.         # DEFINE A MySQL QUERY
  26.         my $myquery = "SELECT 
  27.                             fld_system_EmailHost,
  28.                             fld_system_EmailUser,
  29.                             fld_system_EmailPass,
  30.                             fld_system_JavaPath,
  31.                             fld_system_BarcodeWritePath,
  32.                             fld_system_BarcodeReadPath,
  33.                             fld_system_StorePath                            
  34.                        FROM 
  35.                             tbl_smartPanel_Paperless_Settings";
  36.         # EXECUTE THE QUERY FUNCTION
  37.         my $execute = $connect->query($myquery);
  38.  
  39.         while (my @results = $execute->fetchrow()){
  40.             our %config_vars => {
  41.                             dbHost => "$dbHost",
  42.                             dbUser => "$dbUser",
  43.                             dbPass => "$dbPass",
  44.                             datasource => "$datasource",
  45.                             emailHost => $results[0],
  46.                             emailUser => $results[1],
  47.                             emailPass => $results[2],
  48.                             javaPath => $results[3],
  49.                             bcReadPath => $results[4],
  50.                             bcWritePath => $results[5],
  51.                             storePath => $results[6],
  52.                             }
  53.         }
  54.  
  55. 1;
  56. __END__
  57.  
Jan 14 '08 #5

eWish
Expert 100+
P: 971
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
Jan 14 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
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.
Jan 14 '08 #7

P: 46
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"
Jan 14 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
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);
Jan 15 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.