Dru has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I read the Exporter docs, and this appears correct to me, but my %lists sub does not seem to be getting imported into tmp.pl file. I can print out the sub when not importing it into another file, so I know the syntax is correct.
My Test::Parser.pm file:
package Test::Parser; use strict; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); sub test1 { print "New message for test1\n"; } sub test2 { print "New message for test2\n"; } my %lists = ( 'test1' => \&test1, 'test2' => \&test2 ); @ISA = qw(Exporter); $VERSION = 0.1; @EXPORT = qw(%lists); # What's allowed to be exported @EXPORT_OK = qw(%lists); 1;


and my tmp.pl file that uses Test::Parser
#!/usr/bin/perl -w # # tmp.pl # use strict; use Test::Parser; for my $key (keys %lists) { $lists{$key}->(); }

Replies are listed 'Best First'.
Re: First Time Creating a Package, Am I doing this Right?
by Fletch (Bishop) on Oct 05, 2004 at 15:59 UTC

    Lexicals (declared with my) can't be exported since they don't live in the symbol table. You need to declare it with our or use vars qw( %lists ), or fully qualify the name %Test::Parser::lists = ( ... ).

      Ah, ok, that was it. Is one way preferred over the other in the community or just personal preference?
        I think it's mostly personal preference. That said our is not avalable in older versions of perl, so if you plan to distribute your code it may be wise to not use it unless you have good reason to. Fully qualifed names get a bit long if you use them more than a few times, especialy if there is more than one in the same statment. Generaly I think use vars is the cleanest and safest way to do it.
Re: First Time Creating a Package, Am I doing this Right?
by revdiablo (Prior) on Oct 05, 2004 at 16:28 UTC
    my %lists sub does not seem to be getting imported . . . I can print out the sub

    It may seem like I'm picking nits, but in this case I think it's a fairly big nit. %lists is not a subroutine, it is a variable. Perhaps you simply misspoke, but you did so twice. I think it needs to be pointed out, as there is a big difference between a variable and a subroutine.

Re: First Time Creating a Package, Am I doing this Right?
by johnnywang (Priest) on Oct 05, 2004 at 18:23 UTC
    You don't need both of these two lines:
    @EXPORT = qw(%lists); # What's allowed to be exported @EXPORT_OK = qw(%lists);
    Usually it's advisable to use the second line:
    @EXPORT = qw(); @EXPORT_OK = qw(%lists);
    Then you need to use the following in your tmp.pl
    use Test::parser qw(%lists);
Re: First Time Creating a Package, Am I doing this Right?
by tachyon (Chancellor) on Oct 06, 2004 at 07:17 UTC