using multiple "use Module" statements

Discussion in 'Perl Misc' started by dszostek@gmail.com, Oct 1, 2005.

  1. Guest

    Hi,

    If I have "use Module;" in a subroutine that may be called more than
    once, does it re-load anything from the module? Is there any
    performance issue with doing this or does it just ignore the statement
    if the module has already been loaded.

    Thanks,
    Dave
    , Oct 1, 2005
    #1
    1. Advertising

  2. writes:

    > If I have "use Module;" in a subroutine that may be called more than
    > once, does it re-load anything from the module?


    It's only loaded once, for two reasons:

    First, "use Module" is executed at *compile* time, and the subroutine is
    compiled only once no matter how many times you call it at run time.

    And second, when a module is included via do, use, or require, Perl checks
    first for an entry for that module in %INC. If an entry is found, nothing
    happens. If none is found, the module is loaded and an entry is created.

    Have a look at:

    perldoc -f use
    perldoc -f do
    perldoc -f require
    perldoc perlvar # Look for %INC

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Oct 1, 2005
    #2
    1. Advertising

  3. Bob Walton Guest

    wrote:
    ....
    > If I have "use Module;" in a subroutine that may be called more than
    > once, does it re-load anything from the module? Is there any
    > performance issue with doing this or does it just ignore the statement
    > if the module has already been loaded.

    ....
    > Dave
    >

    Per
    perldoc -f use
    one sees that require() is the underlying implementation of
    use(). Then from
    perldoc -f require
    one sees that require() "demands that a library file be included
    if it hasn't already been included.". So the module is loaded
    only once. %INC keeps track of this. Note that the module's
    ->import() method will be called for each use() call even if the
    module has already been loaded.

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Oct 1, 2005
    #3
  4. Bob Walton <> writes:

    > wrote:
    > ...
    >> If I have "use Module;" in a subroutine that may be called more than
    >> once, does it re-load anything from the module? Is there any
    >> performance issue with doing this or does it just ignore the statement
    >> if the module has already been loaded.

    > ...
    >> Dave
    >>

    > Per
    > perldoc -f use
    > one sees that require() is the underlying implementation of use().


    One also sees that use() is executed at compile time.

    > %INC keeps track of this. Note that the module's ->import() method
    > will be called for each use() call even if the module has already been
    > loaded.


    That will still only be once, because a subroutine is compiled only once
    no matter how many times it might be called. That includes zero times -
    a subroutine that's never called is still compiled.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Oct 1, 2005
    #4
  5. Bob Walton Guest

    Sherm Pendley wrote:

    > Bob Walton <> writes:

    ....
    >> wrote:
    >>

    ....
    >>>Dave

    ....
    >>%INC keeps track of this. Note that the module's ->import() method
    >>will be called for each use() call even if the module has already been
    >>loaded.

    >
    >
    > That will still only be once, because a subroutine is compiled only once
    > no matter how many times it might be called. That includes zero times -
    > a subroutine that's never called is still compiled.


    Counterexample:

    #File junk552.pm:
    package junk552;
    sub import{
    print "import called:mad:_\n";
    }
    1;

    #File junk552.pl:
    use junk552(a);
    use junk552(b);

    #Results of run:
    d:\junk>perl junk552.pl
    import called:junk552 a
    import called:junk552 b

    d:\junk>

    >
    > sherm--


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Oct 1, 2005
    #5
  6. Bob Walton <> writes:

    > Sherm Pendley wrote:
    >
    >> That will still only be once, because a subroutine is compiled only
    >> once
    >> no matter how many times it might be called. That includes zero times -
    >> a subroutine that's never called is still compiled.

    >
    > Counterexample:
    >
    > #File junk552.pm:
    > package junk552;
    > sub import{
    > print "import called:mad:_\n";
    > }
    > 1;
    >
    > #File junk552.pl:
    > use junk552(a);
    > use junk552(b);
    >
    > #Results of run:
    > d:\junk>perl junk552.pl
    > import called:junk552 a
    > import called:junk552 b


    I thought I'd left enough quotage to make the context clear, but I may have
    oversnipped - sorry 'bout that.

    Anyhow, we were discussing a single use() statement appearing in a subroutine
    that's called many times. For instance:

    sub foo {
    use junk552(a);
    }

    The "use junk552(a);" is evaluated at compile-time, so it's called only once
    no matter how many times you call foo() at run-time.

    Your counter example doesn't negate that point, it simply illustrates something
    entirely different - although a given module is only loaded and compiled once,
    its import() subroutine may be called any number of times. If you really think
    about it, it's obvious why this happens - how else could a package's symbols be
    exported into more than one name space?

    But as your example shows, that happens as a result of multiple use() state-
    ments. It would not happen as a result of additional invocations of foo() in
    the example above.

    If you want to have a use() that's evaluated at run-time, you have to wrap it
    in a string eval().

    sub foo {
    eval 'use junk552(a)';
    }

    One common use of this is to make the use of a module optional by checking for
    its existence at run-time:

    our $junk_exists;

    sub check_junk {
    $junk_exists = eval 'use junk552';
    }

    # ...later...

    if ($junk_exists) {
    my $foo = new junk552();
    }

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Oct 1, 2005
    #6
    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. Neil Zanella
    Replies:
    8
    Views:
    1,181
    mfmehdi
    Oct 20, 2006
  2. Joe via DotNetMonster.com

    Multiple Select Statements in resultset

    Joe via DotNetMonster.com, Mar 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    8,189
    Jim via DotNetMonster.com
    Mar 27, 2005
  3. Harry George
    Replies:
    6
    Views:
    371
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    746
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    259
    John Crichton
    Jul 12, 2010
Loading...

Share This Page