Gossamer Forum
Home : General : Perl Programming :

import vs @EXPORT_OK

Quote Reply
import vs @EXPORT_OK
I'm trying to use sub import in a script so in all my modules I can do:

use Module qw/$foo $bar/;

....but it doesn't seem to be working right and I keep getting errors saying that $foo and $bar need an explicit package name. In sub import I create all the objects and don't get errors about that so that seems to be ok, it's just that I don't seem to be able to use the objects in other modules.


Also what is the difference between using that method and @EXPORT_OK?

Thanks.

Last edited by:

PaulW: Nov 23, 2001, 6:26 AM
Quote Reply
Re: [PaulW] import vs @EXPORT_OK In reply to
Hmm, I've now set it up a little differently and am using:

LoadMe::init(); to create the objects and it seems to work well apart from one module that still keeps dying with "$foo and $bar require explicit package names" even though I have:

use Module qw/$foo $bar/; at the top.

Once I add:

use vars qw/$foo $bar/;

...it works but $foo and $bar are undefined :(

Last edited by:

PaulW: Nov 23, 2001, 6:52 AM
Quote Reply
Re: [PaulW] import vs @EXPORT_OK In reply to
Scrap all that, I just can't get this to work and don't know whether I should start with a different approach...if it helps here is the code:

Code:
package LoadMe;
# ==================================================================

BEGIN {

use strict;
use SQL;
use CGI;
use Html;
use Parser;
use Library;
use Configuration;

use Exporter ();
use vars qw/$IN $DB $CFG $TPL $LIB $HTM $VERSION @ISA @EXPORT_OK %EXPORT_TAGS/;


$VERSION = sprintf "%d.%03d", q$Revision: 1.00 $ =~ /(\d+)\.(\d+)/;

@ISA = qw/Exporter/;
@EXPORT_OK = qw/$IN $DB $CFG $TPL $LIB $HTM/;
%EXPORT_TAGS = ( all => \@EXPORT_OK );

}


use vars @EXPORT_OK;

$IN = new CGI;
$DB = new SQL;
$CFG = new Configuration;
$TPL = new Parser;
$LIB = new Library;
$HTM = new Html;



END { }

Now putting use LoadMe qw/$TPL $CFG/; in another module gives:

Global symbol "$CFG" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 20.
Global symbol "$CFG" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 20.
Global symbol "$CFG" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 20.
Global symbol "$CFG" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 21.
Global symbol "$CFG" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 22.
Global symbol "$TPL" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 22.
Global symbol "$TPL" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 43.
Global symbol "$TPL" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 46.


Last edited by:

PaulW: Nov 23, 2001, 7:28 AM
Quote Reply
Re: [PaulW] import vs @EXPORT_OK In reply to
I've added use vars to the SQL module and the errors went but the vars are still undefined and I get:

Can't connect(DBI::: ), no database driver specified and DBI_DSN env var not set at e:/apache/cgi-bin/wt/Lib/SQL.pm line 23

Ugh, why does it work in my main script but not in the modules? ie...in order to even try to connect to the database I use if ($IN->param('foo')) { ...and that works fine, I don't even have use vars in the main script.

Last edited by:

PaulW: Nov 23, 2001, 7:41 AM
Post deleted by PaulW In reply to
Quote Reply
Re: [PaulW] import vs @EXPORT_OK In reply to
Ok I got desperate and decided to look at how Links SQL does it and so I'm trying this now:

Code:
sub import {
# ------------------------------------------------------------------
#
my $pkg = shift;
my $callpkg = caller;
my @symbols = @_;

# Import requested vars.
foreach my $sym (@symbols) {
no strict 'refs';
CASE: {
($sym eq '$IN') and *{$callpkg . '::IN'} = \$IN, next;
($sym eq '$CFG') and *{$callpkg . '::CFG'} = \$CFG, next;
($sym eq '$DB') and *{$callpkg . '::DB'} = \$DB, next;
($sym eq '$TPL') and *{$callpkg . '::TPL'} = \$TPL, next;
($sym eq '$HTM') and *{$callpkg . '::HTM'} = \$HTM, next;
($sym eq '$LIB') and *{$callpkg . '::LIB'} = \$LIB, next;

die "Invalid symbol: $sym imported by $callpkg";
}
}

# Create variables requested.
my %symbols = map { $_ => 1 } @symbols;
init_vars(\%symbols);
}

Now, in the main script, wt.cgi this works great and I can use $CFG $DB etc....but it won't work in my modules.

Putting this:

use LoadMe qw/$CFG/;

print $CFG->{USER};

...in the main script works....putting the same in a module says "explicit package bla bla"

Any ideas please :(

Last edited by:

PaulW: Nov 23, 2001, 10:06 AM
Quote Reply
Re: [PaulW] import vs @EXPORT_OK In reply to
Hi,

Try this:

1. In your main module:

use strict;
use Exporter;
use vars qw/@EXPORT_OK @ISA $IN $DB $CFG .../;
@EXPORT_OK = qw/$IN $DB $CFG .../;
@ISA = qw/Exporter/;

2. In your main script use:

use MainModule qw/$IN $DB $CFG/;

or whatever globals you need:

use MainModule qw/$IN $CFG/;

to only get two. You can put the same thing in your other libraries.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] import vs @EXPORT_OK In reply to
OOoo thanks!!!!!

I had already tried that but I forgot to create the objects in sub import, I surrounded the code with:

sub import {

}


...and it seems to work!

You've come to my rescue again-thanks!

Last edited by:

PaulW: Nov 23, 2001, 2:13 PM
Quote Reply
Re: [Alex] import vs @EXPORT_OK In reply to
Ewwww I spoke too soon :(

http://213.106.6.135/cgi-bin/wt/wt.cgi
Quote Reply
Re: [PaulW] import vs @EXPORT_OK In reply to
Hi,

No, you don't want to use sub import. Exporter handles it for you. Try it just like the above.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] import vs @EXPORT_OK In reply to
I tried that too.

Here's what I have:

Code:

package LoadMe;
# ==================================================================

use strict;
use SQL;
use CGI;
use Html;
use Parser;
use Library;
use Exporter;
use Configuration;
use vars qw/$IN $DB $CFG $TPL $LIB $HTM $VERSION @ISA @EXPORT_OK/;

$VERSION = sprintf "%d.%03d", q$Revision: 1.00 $ =~ /(\d+)\.(\d+)/;

@ISA = qw/Exporter/;
@EXPORT_OK = qw/$IN $DB $CFG $TPL $LIB $HTM/;

$IN = new CGI;
$DB = new SQL;
$CFG = new Configuration;
$TPL = new Parser;
$LIB = new Library;
$HTM = new Html;



1;


Quote Reply
Re: [PaulW] import vs @EXPORT_OK In reply to
Hmm it is actually dying before it even create the objects in LoadMe.pm ....

Global symbol "$TPL" requires explicit package name at e:/apache/cgi-bin/wt/Lib/SQL.pm line 176.
Compilation failed in require at e:/apache/cgi-bin/wt/Lib/LoadMe.pm line 10.

Line 10 is...

use SQL;

....which is needed to create the object further down.

Last edited by:

PaulW: Nov 24, 2001, 4:30 AM
Quote Reply
Re: [Alex] import vs @EXPORT_OK In reply to
The problem was staring me in the face the whole time.

Of course I can't use $IN $CFG $TPL etc in the modules "use"d in LoadMe.pm as when they are loaded in LoadMe.pm (so the objects can be created) then $IN $CFG etc won't exist yet and thats why I get the error ugh.

So basically I can only use:

use LoadMe qw/$VARS/;

...in my cgi scripts and modules other than those used to create objects in LoadMe.pm

DUHHH

Last edited by:

PaulW: Nov 25, 2001, 4:25 AM