Andrew Koebrick wrote:
> I am having trouble creating a modules which splits its functions and
> variables between two files in two separate directories.
>
> I want to have a set of global functions (i.e. open database
> connection...) which would live in the main Perl path, and a local
> configuration file (containing Host name, user name, password...)
> which would live in the user's local directory. When the module was
> called, it would instantiate with the local variables. Here is what
> I have tried:
>
> GLOBAL FUNCTIONS in:
> /usr/lib/perl5/5.8.0/MetaManageGlobal.pm:
>
> package MetaManagerGlobal;
> require Exporter;
>
> our ($VERSION, $dbh);
> our @ISA = qw(Exporter);
> our @EXPORT = qw(connectDb $dbh);
> $VERSION = "1.0.0";
>
> # Content Database connect
> sub connectDb
> {
> require DBI;
> $dbh = DBI->connect("dbi:mysql:$cfg->{dbName}:$cfg->{dbServer}",
> $cfg->{ dbUser}, $cfg->{dbPassword}) || Die $DBI::errstr;
> }
>
>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> LOCAL VARIALBES in:
> /home/httpd/html/admin/MetaManager.pm:
>
> package MetaManager;
> require Exporter;
> require MetaManagerGlobal;
> our ($VERSION, $cfg, $dbh);
> our @ISA = qw(Exporter);
> our @EXPORT = qw(connectDb $dbh $cfg);
> $VERSION = "1.0.0";
>
> $cfg->{dbServer} = "Host Name"; # Database server host
> $cfg->{dbName} = "Database Name"; # Database name
> $cfg->{dbUser} = "User Name"; # Database user
> $cfg->{dbPassword} = "Password"; # Database password
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> From the page (which uses Apache::ASP) I want to be able to simply
> call the function:
>
> <%
> use MetaManager;
> ConnectDb();
> %>
>
> And have my database handle opened to whatever specifications exist
> in the local MetaManager.pm module. When I try this, the ConnectDb
> function is found, but no Db is opened, suggesting to me that the
> variables are not being properly scoped or imported.
MetaManager is exporting $cfg, but because MetaManagerGlobal doesn't use
MetaManager (for obvious reasons), MetaManagerGlobal insn't importing $cfg.
Suggestions:
1) add "use strict" to each module. This would highlight the problem more
clearly.
2) have MetaManagerGlobal export $cfg, and then use (not "require")
MetaManagerGlobal from MetaManager. This will allow your local module to set
the global module's values.
If you have other modules that need to access $cfg, have them "use
MetaManagerGlobal" also.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>