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

Consider this structure:
Generate[.pl] pm/Foo.pm pm/Bar.pm
Generate is using both Foo and Bar:
use Foo; use Bar;
Foo also uses Bar. If I put 'use Bar;' in Foo, however, I get:
ServerDude-2:/www/cgi/jenny rip$ ./Generate types Subroutine LWXBar redefined at pm/Bar.pm line 73. Undefined subroutine &Foo::LWXBar called at pm/Foo.pm line 116. ServerDude-2:/www/cgi/jenny rip$
So, in one run, it is complaining that the undefined sub has been redefined.

If I leave the 'use Bar;' out of Foo.pm, then it simply dies with the undefined sub &Foo::LWXBar() message.

I can fqualify &Bar::LWXBar() in Foo.pm and it works:
ServerDude-2:/www/cgi/jenny rip$ ./Generate types Undefined subroutine &main::LWXBar called at ./Generate line 89. ServerDude-2:/www/cgi/jenny rip$
As you can see it has now proceeded beyond the &Bar::LWXBar() in Foo, but it's tanked in the main script, again with an undefined LWXBar sub.

If I try to fqualify it in the main script, I get
ServerDude-2:/www/cgi/jenny rip$ ./Generate types Undefined subroutine &Bar::LWXBar called at ./Generate line 89.
Thoughts? Suggestions?

BTW, I have a third module, pm/Baz.pm. It also uses LWXBar -- unqualified -- without any trouble at all.

All three modules use BEGIN blocks to set 'use Exporter ()', as copied (and massaged) from the Perl docs (this is "Foo":
BEGIN { use Exporter (); our ($VERSION, @ISA, @EXPORT); # set the version for version checking $VERSION = "01.f"; @ISA = qw(Exporter); @EXPORT = qw(&GeneratorConfig &Generate &GetWidthFromType &GetWidthFromFormat); }
(MacOS 4.11, perl v5.8.8 darwin-2level)

Am I missing something fundamental?

Thanks for any comments,
rip

Replies are listed 'Best First'.
Re: Perl complains of redefining undefined module export
by ikegami (Patriarch) on May 09, 2009 at 23:51 UTC
Re: Perl complains of redefining undefined module export
by Bloodnok (Vicar) on May 10, 2009 at 00:20 UTC
    I'd be very interested in learning from which perl docs you copied this from - it's not normal to do all the exporting in the BEGIN block, the more usual form is along the lines of...
    package SomePkg; use warnings; use strict; use Exporter; our @ISA = qw/Exporter/; our @EXPORT = qw/&GeneratorConfig &Generate &GetWidthFromType &GetWidt +hFromFormat/; sub GeneratorConfig { ... } sub Generate() { ... } sub GetWidthFromType { ... } sub GetWidthFromFormat { ... } 1;
    The bulk of your problems, I believe, lie in the apparent lack of any sub declaration in scope (at the time that Exporter does its work) - if there are subs declared in the module, they won't have been parsed until after the BEGIN block has run i.e. your use of Exporter in the BEGIN block is far too early in the compilation cycle

    Note that the using strictures might have pointed you in right direction a little earlier.

    Note also that it is extremely unadvisable to export everything by default (via @EXPORT), it is far more preferable to export on demand (using @EXPORT_OK).

    As an aside, I've recently been asked about a similar situation in some extremely legacy code - the workaround (given that there are over 100 warnings if strictures are enabled) was to change use to require.

    Update:

    In the light of ikegamis salient qualification, I thought I ought to further qualify my comment ...it's not normal..., here, by normal, I was alluding to the module template generated by h2xs.

    A user level that continues to overstate my experience :-))

      it's not normal to do all the exporting in the BEGIN block

      I always do. I like for my code to be executed in the order it appears in the file. There's no harm in doing so.

      And it's necessary to do so when you have two exporting modules that use each other. Granted, that's also not a normal thing to do.

      The style looks familiar to me, though I can't say where I have seen it particularly.

      Playing Safe has an example in a very similar style.

      The bulk of your problems, I believe, lie in the apparent lack of any sub declaration in scope (at the time that Exporter does its work) - if there are subs declared in the module, they won't have been parsed until after the BEGIN block has run i.e. your use of Exporter in the BEGIN block is far too early in the compilation cycle
      That isn't a problem. If you take a reference to a not-yet-defined subroutine (essentially what Exporter does), you implicitly are creating a forward declaration which will later be fully defined when the sub body is parsed.
      http://perldoc.perl.org/perlmod.html
      Section "Perl Modules"

      "For example, to start a traditional, non-OO module called Some::Module, create a file called Some/Module.pm and start with this template:"

      The only difference I can see there is that they use "Some::Module", but I don't have "pm::Xyz" even though the module is in the pm directory.

      And there we go.

      Once I had all the directory :: name instead of relying on simply name, it worked...

      If by "strictures" you mean use strict;, yeah, have that (that's default behavior around my house).
      Thanks for your time!

      rip
Re: Perl complains of redefining undefined module export
by ig (Vicar) on May 09, 2009 at 23:37 UTC

    I suggest that you post the code that is generating the errors you posted. Ideally, after removing any code that is not essential to reproducing the errors.

      Right.

      I looked at the perlmod doc (see my reply elsewhere) and returned to the "It's a computer, computers are stupid literal, what's different from the template vs. my code..." concept. The one thing that I could see is the use of
      package Some::Module;
      and the statement that the Module.pm file was in the Some directory. In my case, the modules are in the 'pm' directory. So I added pm:: constructs in my "package" declarations:

      package pm::Foo;
      package pm::Bar;
      package pm::Baz;

      In some cases, it required comparable
      use strict; use warnings; use pm::Bar; use pm::Baz;
      declarations in the modules using the other modules, sometimes not. Still not sure (ie, still haven't really looked at why that might be). But now I get
      ServerDude-2:/www/cgi/jenny rip$ !./ ./Generate types Content-type:text/xml <value> <in>%d</in> <out> <types> <type> <string>pkey</string> <format>%{8}c</format> </type>... </types> </out> </value> ServerDude-2:/www/cgi/jenny rip$

      All is right with the world again.

      Thanks for all your time!

      Regards,
      rip