473,240 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

Creating a Global Config

. 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
8 3783
eWish
971 Expert 512MB
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
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
971 Expert 512MB
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
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
971 Expert 512MB
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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

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

Similar topics

6
by: Ruslan | last post by:
Hello, I have to project: one ASN.NET project and another - Web Service in the same solution. I want to use the same web.config and global.asax files. Does it possible?
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
3
by: hansiman | last post by:
I use Application_Start in global.asax to set some physical folder paths ie.: Application("pdf") = "c:\www\<site>\pdf\" global.asax uses code behind. When I move the project from the dev to...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
12
by: John M | last post by:
Hello, On Microsoft Visual Studio .NET 2003, I want to use some global elements, that can be used in each one of my pages. i.e I put a oleDBConnection on global.asax.vb How can I use it...
7
by: Adam | last post by:
Im trying to add an httphandler for all *.sgf file extensions. I have developed the handler, 1. installed it into the gac 2. added it to the machine.config: <httpHandlers> <add verb="*"...
2
by: arun1985 | last post by:
In the project i am using i am having the following code and when i upload it to the server.Its givig me the following error in the global.cs file. Server Error in '/' Application. ...
5
by: =?Utf-8?B?bWNxd2VydHk=?= | last post by:
Hello, I'm trying to enable an error handling package at the root of my web server by editing this file: C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.config In the httpHandlers...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.