Scripts & Modules

Discussion in 'Perl Misc' started by Bigus, Dec 3, 2009.

  1. Bigus

    Bigus Guest

    Hello

    I would just like some clarification on "use"ing modules. Say we have the
    following files on our system:

    script1.cgi
    module1.pm [contains sub - mod1function1()]
    module2.pm [contains subs - mod2function1() and mod2function2()]

    sciprt1.cgi has "use module1;" in it because it needs to call
    mod1function1().

    However, mod1function1() needs to call mod2function1(), so "use module2;" is
    placed inside mod1function1().

    At a later date script1.cgi needs to use mod2function2(). in addition to
    mod1function1(), so "use module2;" is placed directly into script1.cgi.

    So we now have a scenario where script1.cgi is loading both modules but when
    mod1function1() is called it loads module1.pm again (or does it?)

    That is a simple case but in reality our system has a dozen modules and
    scores of scripts so it can be difficult keeping track of what modules are
    loaded at any one time.

    Is there a "best practise" in this kind of situation, when it comes to
    loading modules?

    Regards
    Bigus
     
    Bigus, Dec 3, 2009
    #1
    1. Advertising

  2. On Thu, 03 Dec 2009 18:53:26 +0100, Bigus <>
    wrote:

    > Is there a "best practise" in this kind of situation, when it comes to
    > loading modules?


    http://www.google.de/search?q=perldoc use

    "The ... makes sure the module is loaded into memory if it hasn't been
    yet."

    So, don't worry, just use "use" as much as you want to.

    And, the usual way is to put the "use" statements at the top of the file,
    not inside a function. Not that it matters, it just makes it easier to
    read.
     
    Jochen Lehmeier, Dec 3, 2009
    #2
    1. Advertising

  3. Bigus

    Bigus Guest

    "Jochen Lehmeier" <> wrote in message
    news:eek:p.u4di82eqmk9oye@frodo...
    > On Thu, 03 Dec 2009 18:53:26 +0100, Bigus <>
    > wrote:
    >
    >> Is there a "best practise" in this kind of situation, when it comes to
    >> loading modules?

    >
    > http://www.google.de/search?q=perldoc use
    >
    > "The ... makes sure the module is loaded into memory if it hasn't been
    > yet."
    >
    > So, don't worry, just use "use" as much as you want to.
    >
    > And, the usual way is to put the "use" statements at the top of the file,
    > not inside a function. Not that it matters, it just makes it easier to
    > read.


    Blimey, yes it's clicked now.. that perldoc doesn't make it all that clear
    to me.

    However, I created a test script with the lines:

    use MyModule;
    print time2str("%d %m %Y",time);

    Then created MyModule.pm with these lines:

    sub func1 {
    use Date::Format;
    }
    1;

    and ran the test script and it worked! So, basically modules loaded via
    "use" have a kind of a global scope :)

    Thanks
    Bigus
     
    Bigus, Dec 3, 2009
    #3
  4. "Bigus" <> wrote:
    >sub func1 {
    > use Date::Format;


    I consider this to be poor style.

    'use' is executed at compile time while placing the 'use' statement
    inside of a function erroniously indicates that the 'use' would be
    executed at runtime of this function.

    Why do you want to confuse people like that?

    jue
     
    Jürgen Exner, Dec 4, 2009
    #4
  5. Jochen Lehmeier wrote:
    > On Thu, 03 Dec 2009 18:53:26 +0100, Bigus <>
    > wrote:
    >
    >> Is there a "best practise" in this kind of situation, when it comes to
    >> loading modules?

    >
    > http://www.google.de/search?q=perldoc use
    >
    > "The ... makes sure the module is loaded into memory if it hasn't been
    > yet."
    >
    > So, don't worry, just use "use" as much as you want to.
    >
    > And, the usual way is to put the "use" statements at the top of the
    > file, not inside a function. Not that it matters, it just makes it
    > easier to read.


    If I am fairly confident that nothing in the main script other than this
    specific function is ever going to want that module, then I put the
    "use" of that module inside that function. It keeps the dependency
    together.

    Xho
     
    Xho Jingleheimerschmidt, Dec 4, 2009
    #5
  6. Bigus wrote:
    ....

    >
    > However, mod1function1() needs to call mod2function1(), so "use module2;" is
    > placed inside mod1function1().
    >
    > At a later date script1.cgi needs to use mod2function2(). in addition to
    > mod1function1(), so "use module2;" is placed directly into script1.cgi.
    >
    > So we now have a scenario where script1.cgi is loading both modules but when
    > mod1function1() is called it loads module1.pm again (or does it?)


    No, for two reasons. One is that the "use" inside the function happens
    when the function is compiled, not when it is called.

    And second, the module is not loaded twice because perl keeps track of
    such things. It's "import" subroutine, if any, is executed twice, however.


    Xho
     
    Xho Jingleheimerschmidt, Dec 4, 2009
    #6
  7. Bigus

    sreservoir Guest

    Bigus wrote:
    > Hello
    >
    > I would just like some clarification on "use"ing modules. Say we have the
    > following files on our system:
    >
    > script1.cgi
    > module1.pm [contains sub - mod1function1()]
    > module2.pm [contains subs - mod2function1() and mod2function2()]


    if this is actually happening, you should rewrite the thing so that it
    has module1::function1, module2::function1, module2::function2. module
    code being compiled in main:: is usually considered bad style.

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
     
    sreservoir, Dec 4, 2009
    #7
  8. Bigus

    Bigus Guest

    "Tad McClellan" <> wrote in message
    news:...
    > Bigus <> wrote:
    >> "Jochen Lehmeier" <> wrote in message
    >> news:eek:p.u4di82eqmk9oye@frodo...
    >>> On Thu, 03 Dec 2009 18:53:26 +0100, Bigus <>
    >>> wrote:

    >
    > [ "use"ing a module multiple times ]
    >
    >>> http://www.google.de/search?q=perldoc use
    >>>
    >>> "The ... makes sure the module is loaded into memory if it hasn't been
    >>> yet."
    >>>
    >>> So, don't worry, just use "use" as much as you want to.

    >
    >> that perldoc doesn't make it all that clear
    >> to me.

    >
    >
    > See also the description for %INC in
    >
    > perldoc perlvar
    >
    > which is how perl knows whether or not it has already loaded a module.


    Ahh, thanks - a list of all the modules loaded at any one point :)

    Regards
    Bigus
     
    Bigus, Dec 4, 2009
    #8
  9. Ben Morrow wrote:
    > Quoth Xho Jingleheimerschmidt <>:
    >> Bigus wrote:
    >> ...
    >>
    >>> However, mod1function1() needs to call mod2function1(), so "use module2;" is
    >>> placed inside mod1function1().
    >>>
    >>> At a later date script1.cgi needs to use mod2function2(). in addition to
    >>> mod1function1(), so "use module2;" is placed directly into script1.cgi.
    >>>
    >>> So we now have a scenario where script1.cgi is loading both modules but when
    >>> mod1function1() is called it loads module1.pm again (or does it?)

    >> No, for two reasons. One is that the "use" inside the function happens
    >> when the function is compiled, not when it is called.
    >>
    >> And second, the module is not loaded twice because perl keeps track of
    >> such things. It's "import" subroutine, if any, is executed twice, however.

    >
    > No, since the file is (normally) only compiled once. If you 'do' a file
    > that 'use's a module twice, import will get called twice ('do' updates
    > %INC but doesn't check it); similarly if you eval a 'use' twice.


    I'm not sure what you are trying to correct here, but if you "use" a
    module from more than one place, which is what the OP was talking about,
    its import sub certainly does get called more than once.

    Xho
     
    Xho Jingleheimerschmidt, Dec 13, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Danielsson
    Replies:
    8
    Views:
    634
    Mike Meyer
    Jul 22, 2005
  2. Jp Calderone
    Replies:
    0
    Views:
    464
    Jp Calderone
    Jul 21, 2005
  3. davidj411
    Replies:
    0
    Views:
    530
    davidj411
    Jun 27, 2008
  4. Stef Mientki
    Replies:
    1
    Views:
    352
    Steven D'Aprano
    Oct 11, 2009
  5. Replies:
    13
    Views:
    540
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page