Take the 2-minute tour ×

Suppose I have two files: a module file that looks like this:

package myPackage;
use Bio::Seq;

and another file that looks like this:

use lib "path/to/lib";
use myPackage;
use Bio::Seq;

How can i prevent that Bio::Seq is included twice? Thanx

share|improve this question
what actual problem are you having? –  ysth Mar 7 '13 at 18:00
I want to - and here comes the question: prevent that Bio::Seq is included twice!!!! –  sdir Mar 7 '13 at 18:01
It's loaded into memory once and imported twice, once in each package namespace. Your code already does what you want. –  jordanm Mar 7 '13 at 18:04
@sdir: But, as ysth asked, what actual problems are you having? With use and require a *.pm file is read and executed only one per process. What are you trying to fix? –  Borodin Mar 7 '13 at 18:43
@sdir: Dear sdir we try hard to help people, so both ysth and I asked what problem are you trying to solve? You presumably have a problem that you thought including Bio::Seq just once instead of many times would help to fix. Please tell us what problem that is and we will gladly help you. As you now understand, your guess at a solution will not help. –  Borodin Mar 7 '13 at 18:53

4 Answers 4

up vote 2 down vote accepted

use Foo

is mostly equivalent to

# perldoc -f use
    require "Foo.pm";

And require "Foo" is mostly equivalent to

# perldoc -f require
sub require {
  my ($filename) = @_;
  if (exists $INC{$filename}) {
    return 1 if $INC{$filename};
    die "Compilation failed in require";
  # .... find $filename in @INC
  # really load
  return do $realfilename;


No, the code won't be "Loaded" more than once, only "imported" more than once.

If you have code such as

package Bio::Seq;
sub import { 
  # fancy stuff

And you wanted to make sure a library was loaded, but not call import on it,

#perldoc -f use
use Bio::Seq ();
share|improve this answer
@KentFrederic: No! use is It is exactly equivalent to require plus import –  Borodin Mar 7 '13 at 18:46
Yes, but somewhere between 'use' and 'require' ( at least, as far as the documentation is concerned ) your bareword string of the module name is translated into a path via s{::}{/} –  Kent Fredric Mar 7 '13 at 18:54
@KentFrederic: No. require accepts the same format as use for its parameter. The only difference is that use has an implied BEGIN block around it and executes PACKAGE->import. I am being particularly picky because your answer will be found by people searching Stack Overflow for information on this topic. Can you offer a reference that says that require resolves the package name into a file path before it is passed to use? –  Borodin Mar 7 '13 at 19:01
Yes, but the documentation for 'require' doesn't show how that works, the mocked model it has there only compensates for parsing the string, not the bareword form. Also, my name has no 'e' between the 'd' and the 'r'. –  Kent Fredric Mar 7 '13 at 19:05
not quite; the import is skipped if () is explicitly specified for LIST, the call is more like 'Module'->import, and if a version is given, a version check is done before the import: 'Module'->VERSION( VERSION ) –  ysth Mar 7 '13 at 19:39

It won't be included twice. use semantics could be described like that:

  1. require the module
  2. call module's import

As the documentation says, it's equivalent to:

BEGIN { require Module; Module−>import( LIST ); }

require mechanism, on the other hand, assures modules' code is compiled and executed only once, the first time some require it. This mechanism is based on the special variable %INC. You can find further details in the documentation for use, require, and in the perlmod page.

share|improve this answer

Modules aren't "included" in Perl like they are in C. They are "loaded", by which I mean "executed".

A module will only be loaded/executed once, no matter how many use statements specify it.

The only thing that happens for every use of a module is the call to the module's import method. That is typically used to export symbols to the using namespace.

share|improve this answer
"The only thing that happens for each use is the call to the module's import method" is far from true. What happens is that definitions are defined and executable statements are executed. –  Borodin Mar 7 '13 at 19:06
@Borodin, That's completely false. Those two things only happen the first time the module is used. –  ikegami Mar 7 '13 at 19:29
What? Are you saying that, "each use of a module" excludes "the first time the module is used"? You are very wrong here, and that is inexcusable in this environment. –  Borodin Mar 7 '13 at 19:37
@Borodin, No, I don't. It calls import the first time too. –  ikegami Mar 7 '13 at 19:38
Its all about how you rank word precedence =P But I understood him the way he wrote it. –  Kent Fredric Mar 7 '13 at 19:56

I guess, you want to optimize the loading(usage) of Module.

For optimizing, dynamic loading may be helpful.

For dynamically loading a Perl Module, we use Class::Autouse.

For more details you can visit this link.

share|improve this answer

Your Answer


