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

Dear Monks,
I am facing some problems in compiling two modules which use one another and one of the module imports few methods exported by the other. You can find both modules below:
#======================================= # First.pm package First; use strict; use warnings; use Second; use Exporter; our @EXPORT_OK = qw(first_method); sub first_method { print "Inside first_method\n"; } #=================================================== # Second.pm package Second; use strict; use warnings; use First qw(first_method); sub second_method { print "Inside second_method\n"; first_method(); } #===================================================


When I compile First.pm (perl -wc First.pm), the compiler after looking at the line "use Second" should start compiling Second.pm. By the time the compiler compiles the line "use First qw(first_method)", First was not compiled yet and therefore First's @EXPORT_OK will be empty. So, the compilation should fail saying that first_method not exported by First. But, the compilation succeeds. Why?

Regards,
- Praveen.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: Problem in using Exporter
by mscharrer (Hermit) on Apr 21, 2008 at 14:34 UTC
    Hi, I'm not sure where your "problem" is. You are missing an error not getting one.

    The solution is, I think, that Perl looks up the function names at run-time not at compile-time! The call to first_method() is compiled in but where/what this function exactly is is only interpreted at run-time, which isn't executed when using the -c switch.

    Also your code is incomplete because you are missing a main file which uses this two modules:

    #!/usr/bin/perl use warnings; use strict; use Second; Second::second_method();
    When I run this script I get the following error:
    Inside second_method Undefined subroutine &Second::first_method called at Second.pm line 11 +.
    But I figured out that you missed the following line in First.pm:
    our @ISA = qw(Exporter);
    Otherwise nothing will be exported.
      The import() method in Exporter class is executed at the compile-time by the use compiler directive. So, when the compiler sees use First qw(first_method) the Exporter class should look at EXPORT or EXPORT_OK of First and check for the method first_method. If it is not there in any of the EXPORT lists then it should throw an error. But it is not....