Re: Own module needs loaded module from main script

From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 08/27/04


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


Relevant Pages

  • Re: Newbie needs help...
    ... and I have some questions regarding a script I am ... Not quite, no. $config is a reference to a hash, not an actual hash. ...
    (comp.lang.perl.misc)
  • Re: Simple script that locks up my box with recent kernels
    ... I've been using the this very simple script for a while to do test ... builds of the kernel: ... cp .config config.$ ... compiler output and time are recorded into the destination ...
    (Linux-Kernel)
  • Re: reversing hash ?
    ... I have found one web site out there that uses a very similar script to ... digit of the second hash from the "encrypted" (I really don't want to use ... the first character of both are changed by the same ... For example, the first URL is 6 letters long, so ...
    (sci.crypt)
  • Re: diet-kconfig: a script to trim unneeded kconfigs
    ... unneeded kernel configs automatically to reduce the compile time. ... I already wrote such a script during the last SUSE hack ... You can specify the config file via option, as default, it reads from ... most of the hardware hardware. ...
    (Linux-Kernel)
  • RE: Problem with LWP::UserAgent
    ... have an xml file that contains a list of URLs. ... hash and pass them as a parameter into the function that does LWP get. ... > assume your script is named net.pl, you can involve the debugger with: ...
    (perl.beginners)