Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: loading modules using 'use'

by jethro (Monsignor)
on Jun 30, 2010 at 11:54 UTC ( [id://847306]=note: print w/replies, xml ) Need Help??


in reply to loading modules using 'use'

There is not a universal compile-time in perl. If you execute an eval($somestring) for example, the code inside the eval has a compile-time long after the main program executes.

use Module is equivalent to the sequence BEGIN { require Module; Module->import( LIST ); }, you probably have already read about this. The BEGIN is the statement that moves any code into "compile-time" to be executed as soon as possible). When you 'use A', your "require Exporter" is already inside a BEGIN and therefore executed at compile-time of your main module. And because the import is after the require (so there is a strict order) in the same BEGIN, the import comes after the require

Replies are listed 'Best First'.
Re^2: loading modules using 'use'
by angshuman (Novice) on Jun 30, 2010 at 12:21 UTC
    Thanks again... What does happen in case of circular inheritance ? Lets say a.pl uses A.pm and B.pm, while A.pm and B.pm in turn uses each other. How can in case of circular dependency/inheritance one of the module may not be loaded completely, that too from another module ? What I mean to say that while 'sub abc'(say) in A.pm is accessible fine from a.pl, it may not be accessible from B.pm. Is there a package based loading going on here ?

      This is what "Programming Perl", the O'Reilly book, has to say about it:

      "When Perl searches for a method, it makes sure that you haven't created a circular inheritance hierarchy. This could happen if two classes inherit from one another, even indirectly through other classes. Trying to be your own great-grandfather is too paradoxical even for Perl, so the attempt raises an exception."

      perl only loads a module once. If you use a Module that is already loaded, it's import function is called, but it is not loaded again.

        by 'load'-ing a module, we do essentially mean that the functions existent in the EXPORT list are placed such that the script having the 'use' statement can call them.(am I right?).

        The why in case of a circular inheritance/depencency, (script abc.pl uses A.pm and B.pm, while A.pm and B.pm have a circular inheritance among them) the script(abc.pl) has acccess to a certain function inside A.pm, while B.pm does not have access to the same function ?

        Am bringing in the circular dependency example, just to know how perl loads in a module, as I saw understanding this sceanrio will help me understand how perl calls 'use' in general.

        Thanks to all Monks for the quick replies...

        Perl learning has never been more exciting for me with a so many monks to consult with....!!!!!

      You know you shouldn't use B as a module name, because there's a B module that comes with perl, right? (It's best to also avoid A because it makes you want to name the next module B.) If you really want to name a throwaway module with a single letter, call it X (or any other letter except B and O).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://847306]
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: (6)
As of 2024-03-28 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found