Re: Own module needs loaded module from main script
From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 08/27/04
- Next message: David K. Wall: "Re: How do you lock a file BEFORE changes are made?"
- Previous message: J. Romano: "How do you lock a file BEFORE changes are made?"
- In reply to: Bart Van der Donck: "Re: Own module needs loaded module from main script"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 27 Aug 2004 23:17:05 +0200
Bart Van der Donck wrote:
> I did some further research and come to odd results.
If you think those results are odd, you haven't spent very long time
with studying "perldoc perlmod", have you? It's good that you
experiment, but you do need to read the docs, too.
Also, you should include
use strict;
use warnings;
in the beginning of every script / module. Doing so will make Perl
help you understand what's going on.
Anyway, I have inserted a couple of comments below.
> -------------------------------------
> This is always present in main script
> -------------------------------------
> use DBI;
> use lib '/path/to/my/own/modules/';
> use DBconf; # own module
That loads the module *and* imports the %CONFIG symbol to package
main, since %CONFIG is included in the @EXPORT array.
> &DBconf::DBvars(); # returns hash with configuration values
A hash with the fully qualified name %DBconf::CONFIG is created, but
the hash is not *returned* by the subroutine.
> use MyModule; # own module
>
> ----------------------
> Case 1: MyModule has:
> ----------------------
> use DBconf;
> &DBconf::DBvars();
>
> Result of case 1: script OK.
>
> ----------------------
> Case 2: MyModule has:
> ----------------------
> &DBconf::DBvars();
>
> Result of case 2: Error: Can't call method "prepare" on an undefined
> value at /some/path/MyModule.pm line 22.
> Line 22 is the first connection string towards MySQL. DBconf::DBvars()
> should have given the connection parameters, but obviously it returned
> empty values. It probably needs "use DBconf;" first, I'ld say.
Not necessarily. But at line 22 you are apparently calling the %CONFIG
hash without using its fully qualified name, while the %CONFIG symbol
has not been imported into package MyModule.
To possible solutions, besides including "use DBconf;" in MyModule.pm:
1) Always call the %CONFIG hash with its fully qualified name, or
2) Include
BEGIN { import DBconf }
in the beginning of MyModule.pm.
> ----------------------
> Case 3: MyModule has:
> ----------------------
> use DBconf;
>
> Result of case 3: script OK
> Odd, I didn't even invoke DBconf::DBvars() !
Why would that be odd? The hash was created when the DBvars() function
was called from the main script. Why would it need to be created again?
-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
- Next message: David K. Wall: "Re: How do you lock a file BEFORE changes are made?"
- Previous message: J. Romano: "How do you lock a file BEFORE changes are made?"
- In reply to: Bart Van der Donck: "Re: Own module needs loaded module from main script"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|