views:

38

answers:

1

I have a module something like this, in "lib", called Fool.pm, which is based on the source code of CGI.pm (since that was the first module I thought of when I was thinking about exporting tags):

package Fool;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw/raspberry/;
%EXPORT_TAGS = (
    ':all' => \@EXPORT_OK,
);
1;

and a test script like this:

use lib 'lib';
use Fool qw/:all/;

I try to run the script and get the following:

perl fool.pl
"all" is not defined in %Fool::EXPORT_TAGS at fool.pl line 2
  main::BEGIN() called at lib/Fool.pm line 2
  eval {...} called at lib/Fool.pm line 2
Can't continue after import errors at fool.pl line 2
BEGIN failed--compilation aborted at fool.pl line 2.

I can't see what the mistake is here, can anyone help?

+3  A: 

There shouldn't be a colon in your key. Also, I think the variables must be declared our in order to have it be available to Exporter:

our @ISA = qw(Exporter);
our @EXPORT_OK = qw/raspberry/;

our %EXPORT_TAGS = (
    'all' => \@EXPORT_OK,
);
amphetamachine
Thanks, you are right. I actually copied this from [CGI.pm](http://cpansearch.perl.org/src/LDS/CGI.pm-3.49/lib/CGI.pm), where there **is** a colon. I wonder how that error doesn't happen there.
Kinopiko
@Kinopiko: I looked at the source code for CGI.pm and it looks like they implement a number of overloaded methods that handle the exporting process. My only guess is that the colons in the keys of original definition of `%EXPORT_TAGS` are there specifically to make `Exporter` __not__ handle them.
amphetamachine
OK, I noticed there is some other stuff underneath there. Thanks very much for your help. I appreciate it.
Kinopiko
No, `our` just (lexically) declares use of a global variable; without it the variables are still global (but will give errors under `use strict`)
ysth
@ytsh: Ah. That's where I was confused; I only ever think of Perl code in terms of `use strict`.
amphetamachine