"Anthony" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > > I'm really struggling with my scripts for the moment. > What I have is a config file, for the moment a config.pl > which i'm trying to change to config.ini using Config::IniFiles module. > But here is the header of my programs > > #!/usr/bin/perl > use CGI qw(:standard); > use DBI; > require "/home/sites/prog/www/config.pl"; > use lib '/home/sites/prog/www/Module'; > use AccForm; > use ErrTemp; > use SB_Function; > > Now the modules AccForm, ErrTemp and SB_Function uses config.pl but the > problem is that the variables in config.pl doesn't go into accForm, ErrTemp > and SB_function. > So what i have done is declare config,pl in each module, but then it seems > to cancel out, what i mean is that AccForm will use config.pl but not > ErrTemp and Sb_function, > if I do > use ErrTemp; > use SB_Function; > use AccForm; > then only ErrTemp, will use the varibles. as well as the general script. > > So is there a way in which I can declare one time config.pl and it can go to > all the module??? >
yes but let's explain why this is so. i am not very good at explain stuff so i am going to give you a few examples to look at: #-- #-- A1.pm #-- package A1; use Exporter; @ISA=qw(Exporter); @EXPORT = qw(f1); sub f1{ print "A1->f1->$number\n"; #-- in number.pl below } 1; __END__ #-- #-- A2.pm #-- package A2; use Exporter; @ISA=qw(Exporter); @EXPORT = qw(f2); sub f2{ print "A2->f2->$number\n"; #-- in number.pl below } 1; __END__ #-- #-- finally number.pl: very simple #-- $number = 1234; __END__ #-- #-- your main.pl #-- require "number.pl"; use A1; use A2; f1(); f2(); __END__ nothing is printed out correct? the reason is your 'require "number.pl"' statement is too late for your "use A1;" and "use A2;" statements to pick up. 'use' is done during Perl compile your script and 'require' is done after your script successfully compiles and start running. that's why the 'f1()' function in module 'A1' and the 'f2()' function in module 'A2' don't even know the '$number' variable even exists (in number.pl) when they are compiled. if you do something like that in main.pl: print "$numeber\n"; you will see the number print out because when Perl runs main.pl, it loads number.pl and sees (in fact, $number now belongs to the main package) the $number variables. another interesting observation is to change the above line to: print "$main::number\n"; which also prints out '1234'. this is your solution to the problem (more on that later). You also mention that if you put the 'require' statement in another module (other than your main script), they seems to cancel each other out. let's change A1.pm, A2.pm and main.pl slightly to show this: #-- #-- A1.pm #-- package A1; use Exporter; @ISA=qw(Exporter); @EXPORT = qw(f1); require "number.pl"; #-- this is the only new line sub f1{ print "A1->f1->$number\n"; } 1; __END__ #-- #-- A2.pm #-- #-- #-- A2.pm #-- package A2; use Exporter; @ISA=qw(Exporter); @EXPORT = qw(f2); require "number.pl"; #-- this is the only new line sub f2{ print "A2->f2->$number\n"; } 1; __END__ #-- #-- your main.pl #-- require "number.pl"; use A1; use A2; f1(); f2(); print "$number\n"; #-- new line __END__ now that that print statement in main.pl no longer prints 1234 but you will see this print to screen: A1->f1->1234 what happen is when Perl loads the "number.pl" file, it sees the 'package A1;' statement being the "close" (not the actual technical term but might be easier for you to digest) most currently effective package in that time so it "gives" (again not the actual technical term. for that you will get it from perldoc) $number to A1.pm. now because number.pl is successfully loaded, it's not loaded for the second time so the require statement in A1.pm seems to "cancel" out the require statements in A2.pm and main.pl. you might be wondering how do i make A2.pm and main.pl use the $number varibles then? knowing that $number nows belongs to A1, the solution can simply be prefix $number with '$A1::'. for example, in A2.pm, i can change 'print "A2->f2->$number\n": to: print "A2->f2->$A1::number\n"; and in main.pl, i can change the print statement to: print "$A1::number\n"; now if you run main.pl again, you will see: A1->f1->1234; A2->f2->1234; 1234; you can leave your require statement in any modules / files you want as long as you know how to retrive (knowing which package it belongs to) them. you can leave your require statement in more than one file as well like: #-- #-- main.pl #-- require "number.pl"; require "number.pl"; require "number.pl"; #-- #-- yes nothing else! #-- __END__ #-- #-- number.pl #-- print "hi\n"; __END__ you will only see one "hi" printed to screen when you execute main.pl because after number.pl is successfully loaded for the first time in the first require statement, the following 2 require statements do not load number.pl and thus seems like the first require statement cancels out the next 2. So your solution is: 1. put: require "/home/sites/prog/www/config.pl"; in your main (driver script). 2. change: use AccForm; use ErrTemp; use SB_Function; to use $main::variable_in_config_pl. for example, if you have a variable in config.pl like: $db_server = 'db_server'; AccForm.pm must say: print "$main::db_server\n"; not: print "$db_server\n"; because otherwise, Perl tries to look for $db_server in the AccForm package where $db_server is really "required" into the main package in your driver script. now all of your module can use whatever you define it config.pl :-) generally, i don't recommend after forward with your setup. it's better and safer to 'use' instead. check out the following doc: perldoc perlmod perldoc perlmodlib perldoc perlobj david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]