Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Weird compilation success

by perl_is_fun (Initiate)
on May 25, 2009 at 15:26 UTC ( [id://766054]=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed monks,
I m quite new to perl. I faced a very weird problem today.
I have two modules with circular dependencies, Men.pm and Women.pm. Both use Class::Method maker as follows:
use Class::MethodMaker 'new_with_init' => "new" , 'new_hash_init' => "_hash_init" , 'get_set' => [ qw(method1 ) ];


Hence compilation of both A and B fails.

$perl -wc Men.pm

The component 'new' has already been installed in class -->Men<-- as a HASH(0xa102440) (this time a new) at /usr/local/lib/perl5/5.8.6/i86pc-solaris/Class/MethodMaker.pm line 20
BEGIN failed--compilation aborted at Men.pm line 145.

But when I import this module in any script, script does not fail to get compiled.

Can you please tell me why the scripts which import these modules Men.pm and Women.pm get compiled without errors?

Thanks a lot!

Replies are listed 'Best First'.
Re: Wierd compilation success
by almut (Canon) on May 25, 2009 at 16:10 UTC

    If A.pm and B.pm (bad choice of name, btw) depend on one another, then when syntax checking A.pm with perl -wc A.pm, the the code in A.pm will be compiled/run twice, because A.pm has already been loaded (indirectly via B.pm) — which can cause all kinds of problems. This doesn't happen when you use either module from a script.

Re: Wierd compilation success
by ikegami (Patriarch) on May 25, 2009 at 16:20 UTC
    In few words, A.pm is not a script, so it makes no sense to execute it.
    perl -wc A.pm
    should be
    perl -wce'use A'
Re: Wierd compilation success
by Anonymous Monk on May 25, 2009 at 15:50 UTC
Re: Wierd compilation success
by ELISHEVA (Prior) on May 26, 2009 at 07:12 UTC
    Just to clarify - if A.pm is executed as a script it defines several methods, but does not add A to @INC. This is the root of the problem:
    1. B.pm looks at @INC (via either use or require), sees that A.pm is missing and tries to load it.
    2. A.pm gets loaded (it is not in @INC yet).
    3. Whilst loading A.pm, the perl compiler notices that those methods have already been defined in the namespace of A.
    4. Redefinition warnings are thus triggered.

    See ikegami's post for a better way to pre-load from the command line.

    Best, beth

Re: Wierd compilation success
by Anonymous Monk on May 25, 2009 at 15:46 UTC
    perl comes with B - The Perl Compiler
Re: Wierd compilation success
by helgi (Hermit) on May 26, 2009 at 13:49 UTC
    And "weird" is not spelled "wierd".
    The stupid "i before e except after c rule" is not a rule.

    --
    Regards,
    Helgi Briem
    hbriem AT f-prot DOT com
Re: Weird compilation success
by perl_is_fun (Initiate) on May 27, 2009 at 13:05 UTC
    Hi monks,
    Thanks all for your help.
    Now things become clear to me. One doubt. What role does Class::MethodMaker play here?
    I use Class::MethodMaker as follows in both A.pm and B.pm
    use Class::MethodMaker 'new_with_init' => "new" , 'new_hash_init' => "_hash_init" , 'get_set' => [ qw(method1 ) ];
    Because when I use my own new method without Class::MethodMaker,though I get subroutine redefined warnings, I get 'Perl Sybtax ok' message.
    But with Class::MethodMaker compilation gets aborted.

    Thank you!!
      How did things become clear if you're still using A.pm and B.pm?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://766054]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found