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

Hi,
Iam asking a simple question,which u all feel stupid. While writing perl scripts, we will generally use modules.My code goes just like this.

use CGI; use DBI; my $cgi = new CGI; #instance of CGI

I will use this $cgi in my program. Just like this i will create instance for DBI also and I will use it in my rest of program.

if I use this code in every perl script, How the perl interpreter will process this code? My doubt is as Iam calling these things in every script, number of times will it load these modules or only once i.e. at first time only these modules will be loaded???

Second doubt is as Iam using new CGI to create an instance in every script, whether it will create number of instances which will overload the process, or with the available instance only will it work. Sorry, for asking these stupid questions. Iam new to PERL.

thanks
sekhar

2002-04-01 Edit by Corion : Added formatting
title=~s/!//g dvergin 2002-04-01

Replies are listed 'Best First'.
Re: Doubt on perl instances! Plz reply...
by webadept (Pilgrim) on Apr 01, 2002 at 14:23 UTC
    Better to ask than to get trashed. Most of the answers you are looking for are in the Perl Camel book and several places on the web. Be sure to look up that information.

    Perl handles things like memory rather nicely. To the first part of your question, you are loading a module, and then useing the code a lot. Perl isn't going to load the module over and over again, its going to put it in there at the compile state and use it from there.

    Your second question is a good one really and something to think about often when creating aps that will run on the web. Always plan for success, I say, and keep things as small and light as possible. If 2000 users show up and start using your program, then 2000 scripts are being asked to run. Good thing to keep in mind.

    There is lots of information on CGI as well. Good luck to you and keep on keeping on.

    webadept

    My Husband ran off with my shawman, but they love me as I am -- Tori Amos
Re: Doubt on perl instances! Plz reply...
by perrin (Chancellor) on Apr 01, 2002 at 15:46 UTC
    When CGI scripts are run by a web server, each request creates a new Perl interpreter at the beginning and destroys it at the end. The modules you use will have to be loaded and compiled every time because each time you have a brand new interpreter.

    That's the reason people invented things like mod_perl and FastCGI, which can keep Perl interpreters in memory and reuse them. Using CGI the way you are is normal, and will not fill up memory (since the interpreters keep getting destroyed), but it is much slower than mod_perl and friends.

    Regarding your "new CGI" question, you are assigning the instance to a lexically scoped ("my") variable, which means that the instance of CGI will be destroyed when that variable goes out of scope.