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:

=========================================
package First;

use strict;
use warnings;

BEGIN {
     print "Start compiling First\n";
}
BEGIN {
     print "Before using Second in First\n";
}

# Using Second before we export any method
use Second;

BEGIN {
     print "Just used Second in Data\n";
}

# Exporting methods
use Exporter;
our @EXPORT_OK = qw(first_method);
our @ISA = qw(Exporter);

BEGIN {
     print "Methods are exported from First\n";
}

sub first_method {
     print "Inside First::first_method\n";
}

1;
===================================================
# Second.pm
package Second;

use strict;
use warnings;

BEGIN {
     print "Start compiling Second\n";
}

BEGIN {
     print "Before using First in Second\n";
}

# Importing methods from Data.pm
use First qw(first_method);

BEGIN {
     print "After using First in Second\n";
}

sub second_method {
     print "Inside Second::second_method\n";

   # Calls imported method of First.pm
   first_method();
}

1;
===================================================
# driver.pl
use Second;

Second::second_method();

===================================================


If I compile driver.pl (by perl -wc) the output looks something like:

Start compiling Second
Before using First in Second
Start compiling First
Before using Second in First
Just used Second in Data
Methods are exported from First
After using First in Second
driver.pl syntax OK


If I compile Second.pm (using perl -wc) the output is:


Start compiling Second
Before using First in Second
Start compiling First
Before using Second in First
Start compiling Second
Before using First in Second
After using First in Second
Just used Second in Data
Methods are exported from First
After using First in Second
Subroutine second_method redefined at Second.pm line 21.
Second.pm syntax OK


First question that I have here is :
When I compile Second.pm, the compiler after seeing "use First qw(first_method);" starts compiling First.pm. But why it again compiles Second when it sees "use Second;" in First.pm as it is already compiling it.

The second questing is:
All that "driver" does is using "Second", so when I compile driver the perl compiler should do the same thing what it does when I compile Second.pm. But we both these compilation process are different?

Third question (I have asked this question before to Monks but did not get a satisfying answer):
First.pm uses Second and before it exports first_method. If we follow all compilation steps of Second, then we find that Second gets compiled for the second time, before it's actual compilation completes(weird). As far as I know Exporter's import method is executed at compile time. Strange thing here is that First did not exported any of its methods before using Second. So, in the second compilation of Second, by the time we are at "use First qw(first_method);", First's EXPORT_OK is empty, therefore the compilation should fail with error messages like
"first_method" is not exported by the First module
Can't continue after import errors at Second.pm line 21
BEGIN failed--compilation aborted at Second.pm line 21."


But the Second gets compiled without any such errors. Why?

TIA,
- Praveen.

In reply to Strange problem with Perl Compiler by praveenkumar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.