Click to See Complete Forum and Search --> : Module Export tags


Ultimater
11-02-2005, 09:00 PM
use CGI qw(:standard);

I'm trying to make my own module(s) similar to the way you can export from the CGI module using ":standard".
I have a single module called "MySQLu.pm" that defines functions to make my MySQL database management easier.

Currently this module defines 6 subroutines for each table in my database.

For the "business" table:
sub getTableBusiness
sub addEntryBusiness
sub clearTableBusiness
sub deleteTableBusiness
sub createTableBusiness
sub restartTableBusiness

For the "band" table:
sub getTableBand
sub addEntryBand
sub clearTableBand
sub deleteTableBand
sub createTableBand
sub restartTableBand

Get the picture?

However, I have one PL file to manage each table that will not need any of the subroutines to manage another table. Meaning within the file "band.pl" I will not need subs like "addEntryBusiness" just subs with the name "band" in it like "addEntryBand". However because it becomes a pain-in-the-you-know-where to have to type "band" over and over each time I want to call a MySQL function from within the file "band.pl", I want to be able to remove the table name from my subroutines names.

From within my "band.pl" file I'l like to be able to do:

#band.pl
use MySQLu qw(:band);


From within my "business.pl" file I'l like to be able to do:

#business.pl
use MySQLu qw(:business);

And the MySQLu module will export:
sub getTable
sub addEntry
sub clearTable
sub deleteTable
sub createTable
sub restartTable

How do export tags work?

Jeff Mott
11-02-2005, 09:39 PM
It's all right in here.
http://perldoc.perl.org/Exporter.html

It's a good idea to read the whole thing just so you can be confident that you know everything there is to know about that module, but you could just look under "Specialised Import Lists" for the little bit you're looking for.

Ultimater
11-03-2005, 01:27 AM
I can't get it to work...

My PL file:

#!/usr/bin/perl -w
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use MySQLu qw(:pa);
print header;
specialprint;


Here's what I have so far for the module which I'm sure is far off...

#!/usr/bin/perl -w

package MySQLu;
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
require Exporter;

sub specialprint{
print 'The specialprint method has been called!';
}

our %EXPORT_TAGS = (':pa' => ['specialprint']);
our @EXPORT = qw['specialprint'];
1;

How can I change my module to work with the ':pa' export tag?

Jeff Mott
11-03-2005, 09:08 AM
Red means delete, green means add.#!/usr/bin/perl -w

package MySQLu;
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
require Exporter;
use base Exporter;

sub specialprint{
print 'The specialprint method has been called!';
}

our %EXPORT_TAGS = (':pa' => ['specialprint']);
our @EXPORT = qw['specialprint'];
1;

Ultimater
11-03-2005, 11:56 AM
Still not working:

PL file:

#!/usr/bin/perl -w
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use MySQLu qw(:pa);
print header;
specialprint;

MySQLu.pm


package MySQLu;
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use base Exporter;

sub specialprint{
print 'The specialprint method has been called!';
}

our %EXPORT_TAGS = ('pa)' => ['specialprint']);
our @EXPORT = qw['specialprint'];
1;



Unknown error
Compilation failed in require at cgi-bin/mysql/test.pl line 6.
BEGIN failed--compilation aborted at cgi-bin/mysql/test.pl line 6.

Jeff Mott
11-03-2005, 12:10 PM
There is an extra parenthese.

our %EXPORT_TAGS = ('pa)' => ['specialprint']);

Also,

our @EXPORT = qw['specialprint'];

The qw operator is a quotation itself. The single quote marks shouldn't be there.

our @EXPORT = qw[specialprint];

Ultimater
11-03-2005, 12:58 PM
Same error.


package MySQLu;
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use base Exporter;

sub specialprint{
print 'The specialprint method has been called!';
}

our %EXPORT_TAGS = ('pa' => ['specialprint']);
our @EXPORT = qw[specialprint];
1;

Jeff Mott
11-03-2005, 06:04 PM
Ok, that was my bad.

use base Exporter;

should be

use base qw(Exporter);

or

use base 'Exporter';

or just whatever style you prefer really.

Ultimater
11-03-2005, 06:10 PM
That change did the trick! Thanks a lot Jeff!

fireartist
11-04-2005, 06:57 AM
You only need to

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

in the main package, not in the other modules.

Also,

our @EXPORT = qw[specialprint];

this means specialprint will always be exported, making the : pa redundant.
It should be

our @EXPORT_OK = qw[specialprint];

to make it optional

Ultimater
11-04-2005, 12:33 PM
Ahhh... I have been wondering the difference between EXPORT and EXPORT_OK for some time now. Great! Thanks!