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

i want to make a module, for cpan, that provides some functionality, one part of which depends on CGI.pm. Can someone explain how I can use this without loading cgi.pm multiple times? for exampel if soeone uses my module as well as cgi, will it load twice? this cant be good for recources. are there any examples of modules which used something from cgi.pm which demo it working in this way?
  • Comment on modules using things from other modules

Replies are listed 'Best First'.
Re: modules using things from other modules
by FunkyMonk (Bishop) on Sep 01, 2008 at 18:35 UTC
    Modules are only read from disc and stored in memeory once, no matter how many times a program or module uses them. This is tracked by the special variable %INC (documented in perlvar).

    For example...

    use Foo; #%INC now contains ( 'Foo.pm' => '/absolute/path/to/Foo.pm' ) use Foo; #perl "sees" that Foo has been loaded from that entry in %INC and proc +eeds directly to import()

    Also, see use for more details about how it works, specifically it's relationship to require

      that method and module is just one example of what I need use. So what you guys saying is that even if i use a module in my module, if it is alredy loaded it done not get loaded again? thats great news

        If it was loaded via require or use, yes, it is only compiled and executed the first time. It's import method will be called each time, allowing symbols to be exported into multiple namespaces.

        # script.pl use Module1; use Module2;
        # Module1.pm package Module1; BEGIN { print("Compiling Module1\n"); } use Module2; sub import { print("Module1 exporting to ". caller() . "\n"); } print("Executing Module1\n"); 1;
        # Module2.pm package Module2; BEGIN { print("Compiling Module2\n"); } sub import { print("Module2 exporting to ". caller() . "\n"); } print("Executing Module2\n"); 1;
        >perl script.pl Compiling Module1 Compiling Module2 Executing Module2 Module2 exporting to Module1 Executing Module1 Module1 exporting to main Module2 exporting to main
        >perl script.pl | sort Compiling Module1 --> once Compiling Module2 --> once Executing Module1 --> once Executing Module2 --> once Module1 exporting to main --> once per "use" Module2 exporting to main \ Module2 exporting to Module1 --> once per "use"
Re: modules using things from other modules
by CountZero (Bishop) on Sep 01, 2008 at 18:38 UTC
    Have no fear: perl keeps a hash of modules already loaded in %INC and skips use-ing a module that is already loaded.

    Try:

    perl -MData::Dumper -e "print Dumper(\%INC);"

    which gives:

    $VAR1 = { 'warnings/register.pm' => 'C:/data/Perl/lib/warnings/registe +r.pm', 'bytes.pm' => 'C:/data/Perl/lib/bytes.pm', 'XSLoader.pm' => 'C:/data/Perl/lib/XSLoader.pm', 'Carp.pm' => 'C:/data/Perl/lib/Carp.pm', 'Exporter.pm' => 'C:/data/Perl/lib/Exporter.pm', 'warnings.pm' => 'C:/data/Perl/lib/warnings.pm', 'C:/data/Perl/site/lib/sitecustomize.pl' => 'C:/data/Perl/si +te/lib/sitecustomize.pl', 'overload.pm' => 'C:/data/Perl/lib/overload.pm', 'Data/Dumper.pm' => 'C:/data/Perl/lib/Data/Dumper.pm' };

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      wait a sec, can you show me how I could craft a module that would would be able to use an existing CGi.pm (or make new if not exist) to access the raw_cookie() method mentioned in teh docs?

        Sure. Here you go-

        package MyPackage; use CGI (); sub get_raw_cookie { return CGI::raw_cookie(); }

        You do not need to create a CGI object to use its functions. raw_cookie() is not likely to be what you want either unless you're passing it to another cookie parser. The cookie handling that comes with CGI is probably better.