Hi Kee,

Resposta a sua mensagem de ter�a-feira, 14 de maio de 2002:

I think that your questions are common to many Embperl developers,
personally I solved this this way:

 - I use EmbperlObject: so you I can build my pages "smarter", and
 reuse UI components over many projects.
 - All the initialization stuff is on a .pm file that I [! use !] on
 base.epl and in other pages that need the symbol table.
 - The module does its initialization on load and on the first
 connection for each process it connects to the database and store the
 handler for future use, and returns the pre-initialized object.
 - All the subroutines that need to be available to the pages are
 declared in the base.epl file or loaded with the:
   Execute ({'isa' => 'methods.epl'});
   The subs are called on the pages like object methods, like:

   [-
   $req = shift;

   $req->do_something($args);
   -]

 - For memory efficiency I also load the module (that loads all other stuff
 needed) in a startup.pl file, maximizing shared memory.

 - Many callbacks, html processing, configs, selects and other
 database statements are "cached" on first use in my module, so after
 a little "warm up" things become really fast.

 [!
 use MyModule.pm;           # Since this is done only one time
 !]                         # is pre-initialized here

 [-
 $req = shift;
 $obj = new MyModule;       # The constructor is very fast returning
                            # just a copy of the pre-initialized
                            # object
 $req->{obj} = $obj;
 ...
 $obj->dbh->prepare("SQL"); # On first call to dbh each object
 -]                         # a fresh DBI->connect object,
 html stuff                 # subsequent calls return the
 [- Execute('*'); -]        # same dbh
 html stuff
                            # The object itself, on using, start
                            # to cache useful things

 Regards,

Luiz Fernando B. Ribeiro
Engenho Solu��es para a Internet
[EMAIL PROTECTED]

Kee> I have some initialization that I need to do on every page of my web
Kee> site.  The site is mostly straight Embperl, but some sections use 
Kee> different EmbperlObject templates.  Fundamentally what I'm trying to 
Kee> do is create a per-process persistent object that has pointers to the 
Kee> database and the like.  And I want to load up any necessary Embperl 
Kee> subroutines and Perl libraries at the same time.  From then on  I 
Kee> would hope that all subsequent page fetches don't have to call the 
Kee> code again.  As always, I'm trying to minimize the amount of Perl 
Kee> code that sits in the front-end HTML pages, which is why I'm 
Kee> obsessing on this.

Kee> Here's the initialization code:

Kee>      use lib "$ENV{DOCUMENT_ROOT}/data/";
Kee>      use lib "$ENV{DOCUMENT_ROOT}/data/SWC";
Kee>      use SWC::Calendar;
Kee>      use SWC::Commons;
Kee>      use SWC::HashChain;

Kee>      Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import => 1});

Kee>      if (!$IMH::Cal) {
Kee>          $IMH::Cal = new SWC::Calendar(dateFormat => '%m/%d/%y',
Kee>                                        timeFormat => '%I:%M %p',
Kee>                                        f_dir => 
Kee> "$ENV{DOCUMENT_ROOT}/data/dynamic");
Kee>      }

Kee>      if (!$IMH::Commons) {
Kee>          $IMH::Commons = new SWC::Commons(database => 'xx',
Kee>                                           username => 'xx',
Kee>                                           password => 'xx');
Kee>      }


Kee> Initially I did the quick and dirty, I simply put that at the 
Kee> beginning of each file.

Kee> Question #1. In that case, I used [! !].  Does that seem correct?

Kee> Question #2. Is there anyway to avoid the $ENV{DOCUMENT_ROOT} in 
Kee> specifying the inputfile?

Kee> Okay, now I want to minimize the code in each module.  So I took this 
Kee> set of code and moved it to a function called "Init" in the IMH.epl 
Kee> module.  Then in each page I put:

Kee> [-
Kee>      Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import => 1});
Kee>      Init();
Kee> -]

Kee> This works.

Kee> Question #3. Any reason not to put that in a [! !]?

Kee> Question #4. Any way to have a function that auto-executes on import? 
Kee> I can't just execute the file because then the routines aren't 
Kee> imported.  (Hmm, maybe I'm answering my own question here, I just put 
Kee> the initialization code in [! !] in the imported file and it seemed 
Kee> to work!)

Kee> Question #5. Is there a way to call the routines in an executed 
Kee> module by name, without having done an import?

Kee> Then I tried to simplify more.

Kee> [-
Kee>      Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import 
=>> 1, sub => 'Init'});
Kee> -]

Kee> This gets me into a very odd loop on my database which in retrospect 
Kee> may have a different cause, but also if I put the same code in my 
Kee> EmbperlObject template I get a rather different error.
Kee>         Call to unknown Embperl macro Init

Kee> Question #6. Exactly how am I supposed to use 'sub =>' or am I 
Kee> looking at the wrong thing?


Kee> I think I've got it down to this.

Kee> Create a .epl file of library functions.  Put the initialization code 
Kee> in it in [! !] brackets.  Then load it from each page with

Kee> [! Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import => 1}) !]

Kee> Does that seem right?

Kee> Finally one unrelated question.  I have a page which does some 
Kee> queries in the body.  For various reasons I'd rather not do them in 
Kee> the header of the page, but I want to set the page *title* to 
Kee> something from the database. I can do this in EmbperlObject using 
Kee> member functions called in the parent template, but I'm not using 
Kee> EmbperlObject in this case.  Is there any to do this with a regular 
Kee> page?  I suspect I'm going to just have to use EmbperlObject.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to