I am trying to write a module which exports functions from another module as well as a number of functions of it's own. For example, given the code below, I would like a script calling
[tt]use CGI::MyWidgets;[/tt]
to have access to all the functions exported by CGI as implied by the [tt]use CGI[/tt] line as well as those in @EXPORT.
[tt]package CGI::MyWidgets;
BEGIN {
use strict;
use CGI qw/:standard :html3 start_table font shortcuts/;
use CGI::Carp qw/ fatalsToBrowser warningsToBrowser /;
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 0.01;
@ISA = qw{Exporter}; # I is a exporter ?!?
@EXPORT = qw{ wt wd wn wm wx wc }; # always exported
%EXPORT_TAGS = (); # eg TAG=>[qw! name1 name2 !],
@EXPORT_OK = (); # exportable package globals and subs
}
# Function Definitions...
sub wt {
....[/tt]
If I list the CGI functions individually in @EXPORT then everything works as I wish but: (i) there are many of them and alarm bells go off in my head every time I find I'm copy-typing; and (ii) they may change and I would like my code to be robust enough to cope with this.
I have read about the [tt]export_to_level[/tt] method of the Exporter module but think that although this could be used by CGI to export functions up through my module, it could not be used by my module to push the CGI functions up to main.
I have tried code like this in my module
[tt]package main;
use CGI qw/:standard :html3 start_table font shortcuts/;
use CGI::Carp qw/ fatalsToBrowser warningsToBrowser /;
package CGI::MyWidgets;
BEGIN {
use strict;
use CGI qw/:standard :html3 start_table font shortcuts/;
use CGI::Carp qw/ fatalsToBrowser warningsToBrowser /;
....
[/tt]
This works but looks ugly and is (probably) inefficient: does CGI.pm get parsed twice?
The only solution that I have found that looks vaguely reasonable is to grabs CGI's symbol table entries directly and include a line like
[tt]push @EXPORT, keys %CGI::;[/tt]
but I can't help feeling that there is an elegant solution just around the corner....
Yours, "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
[tt]use CGI::MyWidgets;[/tt]
to have access to all the functions exported by CGI as implied by the [tt]use CGI[/tt] line as well as those in @EXPORT.
[tt]package CGI::MyWidgets;
BEGIN {
use strict;
use CGI qw/:standard :html3 start_table font shortcuts/;
use CGI::Carp qw/ fatalsToBrowser warningsToBrowser /;
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 0.01;
@ISA = qw{Exporter}; # I is a exporter ?!?
@EXPORT = qw{ wt wd wn wm wx wc }; # always exported
%EXPORT_TAGS = (); # eg TAG=>[qw! name1 name2 !],
@EXPORT_OK = (); # exportable package globals and subs
}
# Function Definitions...
sub wt {
....[/tt]
If I list the CGI functions individually in @EXPORT then everything works as I wish but: (i) there are many of them and alarm bells go off in my head every time I find I'm copy-typing; and (ii) they may change and I would like my code to be robust enough to cope with this.
I have read about the [tt]export_to_level[/tt] method of the Exporter module but think that although this could be used by CGI to export functions up through my module, it could not be used by my module to push the CGI functions up to main.
I have tried code like this in my module
[tt]package main;
use CGI qw/:standard :html3 start_table font shortcuts/;
use CGI::Carp qw/ fatalsToBrowser warningsToBrowser /;
package CGI::MyWidgets;
BEGIN {
use strict;
use CGI qw/:standard :html3 start_table font shortcuts/;
use CGI::Carp qw/ fatalsToBrowser warningsToBrowser /;
....
[/tt]
This works but looks ugly and is (probably) inefficient: does CGI.pm get parsed twice?
The only solution that I have found that looks vaguely reasonable is to grabs CGI's symbol table entries directly and include a line like
[tt]push @EXPORT, keys %CGI::;[/tt]
but I can't help feeling that there is an elegant solution just around the corner....
Yours, "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk