Gossamer Forum
Home : General : Perl Programming :

"require" and variable scoping across multiple files

Quote Reply
"require" and variable scoping across multiple files
Near the beginning of db.cgi, I have the following code:

eval {
...
require "html.pl";

}
...

I would like certain variables - especially %in - to be available in both db.cgi and html.pl, throughout the entire code. How and where should I declare them?
It doesn't seem to be sufficient to declare "my %in" prior to the eval block in db.cgi, but if I use "use vars" in both files I run into problems as well.

Help will be greatly appreciated,

kellner
Post deleted by RedRum In reply to
Quote Reply
Re: [kellner] "require" and variable scoping across multiple files In reply to
I think you can already use %in in html.pl and db.cgi
Quote Reply
Re: [RedRum] "require" and variable scoping across multiple files In reply to
Not if I want to use dbman with use strict.
Then I need to declare lots of variables expressly, and this also involves addressing use of variables in "required" scripts.

kellner

Last edited by:

kellner: Oct 27, 2001, 7:05 PM
Quote Reply
Re: [kellner] "require" and variable scoping across multiple files In reply to
But why would you want to use strict?

Hmmm you could do it by making html.pl into html.pm and "use"ing it. Seems like too much hard work though.

You could always just pass %in into the subroutines in html.pl from db.cgi

Last edited by:

RedRum: Oct 28, 2001, 3:23 AM
Quote Reply
Re: [RedRum] "require" and variable scoping across multiple files In reply to
Maybe I'm obsessive, but I'm always using strict on my scripts. I've also heard that at least from perl 6 onwards use strict will be enabled by default, so writing scripts which comply with it might be a good idea in the long run.
I thought there would be an easy answer to my question, but it seems that doing some module recoding might be the best way. - Not just with html.pl, but also some of the routines in db.cgi. I think the only thing you really need in db.cgi within the present setup is the very beginning and sub main, everything else could go into a module and swiftly be exported/imported with export tags.
kellner
Quote Reply
Re: [kellner] "require" and variable scoping across multiple files In reply to
Yes it could be done like that but Im still not sure why you are doing it.

Fair enough, using strict in your scripts is good practice but it doesn't need to be done with DBMAN.
Quote Reply
Re: [kellner] "require" and variable scoping across multiple files In reply to
Or you could simply upgrade to DBMAN SQL, which uses strict and also is module-based.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Chewbaca] "require" and variable scoping across multiple files In reply to
If I could afford it, *and* if I could persuade my sysadmin that they shouldn't just install MySQL on that other server that runs PHP, but should also install the DBD module for mysql on the server that runs perl - well, then I most certainly would :-)

Erm, purchasing options and dbman-peculiarities aside, I think I understand now that making variables available to other scripts is a task for modules & "use".
There remains one thing that I don't yet understand though:

use vars qw($db_setup $db_script_path);
...
if ($in{'db'}) { $db_setup = $in{'db'};}
eval {
...
require "$db_setup.cfg";
}

print "$db_script_path\n"; # does $db_script_path now have the value assigned to it in $db_setup.cfg?

Could I also use "our" instead of "use vars" in this code? What would be the difference?
kellner
Quote Reply
Re: [RedRum] "require" and variable scoping across multiple files In reply to
And why is that?
kellner
Quote Reply
Re: [kellner] "require" and variable scoping across multiple files In reply to
Hi,

I looked at this a while back. Making DBMan use strict/-w compilant is quite an undertaking and will either involve excessive use of exporter, or a long list of use vars.

I really wouldn't recommend it for any reason except a learning experience.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [RedRum] "require" and variable scoping across multiple files In reply to
In Reply To:
But why would you want to use strict?

Because not using strict would be a bad thing. It is good coding practice to ALWAYS use strict, and while developing use the -w directive too.

The two make debugging a hell of a lot easier, and forces you to pay attention to what you are doing.

--mark


Quote Reply
Re: [Mark Badolato] "require" and variable scoping across multiple files In reply to
Sorry you misunderstood, I did say this above:

Quote:
Fair enough, using strict in your scripts is good practice

When I said why use strict I was referring to DBMAN
Quote Reply
Re: [RedRum] "require" and variable scoping across multiple files In reply to
Ah my bad :)
Quote Reply
Re: [Mark Badolato] "require" and variable scoping across multiple files In reply to
No worries, it was easy to misinterpret
Quote Reply
Re: [Alex] "require" and variable scoping across multiple files In reply to
Thanks for the info, Alex.
Yes, I embarked on that "learning experience" a few months ago, though with a rather crude approach restricted to use of "require" and, as you say, a LOT of "use vars".
It wasn't too bad and forced me to clean up a lot of code that I had, over several years of using dbman, simply added wherever it seemed fit, without bearing in mind the larger picture, and with next to no knowledge of what I was doing :-)
I spent today's lazy afternoon, where my head wasn't fit to do much else, rewriting dbman into a module, and it's not too difficult, with a few exceptions.
One is the availability of subroutines in "require"-d files within (my new module) DBMAN.pm.

I have html_view_success subroutines in individual database configuration files whose name is stored in $db_configfile and need to make them available in DBMAN.pm. So I thought this would work:
package DBMAN;
our ($db_configfile);
...
eval {
require "$db_setup.cfg"; # in this file we set $db_configfile to "whateveritsfilename.pl"
require "$db_configfile";# contains subroutine html_view_success

}
...
&html_view_success;
...

It doesn't however; it is automatically assumed that html_view_success belongs to the package DBMAN.
Any suggestions how to fix this?
kellner
Quote Reply
Re: [kellner] "require" and variable scoping across multiple files In reply to
If you are going to do that, you may as well make the config files into modules as well and do...

use config;

Then to called html_view_success you can do...

&config::html_view_success;

Or the OO way....

my $cfg = new config;

$cfg->html_view_success;

Last edited by:

RedRum: Oct 28, 2001, 5:40 PM