This all applies to the latest Embperl 1.3 revision. It started out
as a bunch of questions, but in the process of testing each case I
may have come up with my own answers, but confirmation would be nice.
I have some initialization that I need to do on every page of my web
site. The site is mostly straight Embperl, but some sections use
different EmbperlObject templates. Fundamentally what I'm trying to
do is create a per-process persistent object that has pointers to the
database and the like. And I want to load up any necessary Embperl
subroutines and Perl libraries at the same time. From then on I
would hope that all subsequent page fetches don't have to call the
code again. As always, I'm trying to minimize the amount of Perl
code that sits in the front-end HTML pages, which is why I'm
obsessing on this.
Here's the initialization code:
use lib "$ENV{DOCUMENT_ROOT}/data/";
use lib "$ENV{DOCUMENT_ROOT}/data/SWC";
use SWC::Calendar;
use SWC::Commons;
use SWC::HashChain;
Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import => 1});
if (!$IMH::Cal) {
$IMH::Cal = new SWC::Calendar(dateFormat => '%m/%d/%y',
timeFormat => '%I:%M %p',
f_dir =>
"$ENV{DOCUMENT_ROOT}/data/dynamic");
}
if (!$IMH::Commons) {
$IMH::Commons = new SWC::Commons(database => 'xx',
username => 'xx',
password => 'xx');
}
Initially I did the quick and dirty, I simply put that at the
beginning of each file.
Question #1. In that case, I used [! !]. Does that seem correct?
Question #2. Is there anyway to avoid the $ENV{DOCUMENT_ROOT} in
specifying the inputfile?
Okay, now I want to minimize the code in each module. So I took this
set of code and moved it to a function called "Init" in the IMH.epl
module. Then in each page I put:
[-
Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import => 1});
Init();
-]
This works.
Question #3. Any reason not to put that in a [! !]?
Question #4. Any way to have a function that auto-executes on import?
I can't just execute the file because then the routines aren't
imported. (Hmm, maybe I'm answering my own question here, I just put
the initialization code in [! !] in the imported file and it seemed
to work!)
Question #5. Is there a way to call the routines in an executed
module by name, without having done an import?
Then I tried to simplify more.
[-
Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import
=> 1, sub => 'Init'});
-]
This gets me into a very odd loop on my database which in retrospect
may have a different cause, but also if I put the same code in my
EmbperlObject template I get a rather different error.
Call to unknown Embperl macro Init
Question #6. Exactly how am I supposed to use 'sub =>' or am I
looking at the wrong thing?
I think I've got it down to this.
Create a .epl file of library functions. Put the initialization code
in it in [! !] brackets. Then load it from each page with
[! Execute({inputfile => "$ENV{DOCUMENT_ROOT}/data/IMH.epl", import => 1}) !]
Does that seem right?
Finally one unrelated question. I have a page which does some
queries in the body. For various reasons I'd rather not do them in
the header of the page, but I want to set the page *title* to
something from the database. I can do this in EmbperlObject using
member functions called in the parent template, but I'm not using
EmbperlObject in this case. Is there any to do this with a regular
page? I suspect I'm going to just have to use EmbperlObject.
--
Kee Hinckley - Somewhere.Com, LLC
http://consulting.somewhere.com/
[EMAIL PROTECTED]
I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]