On 11/15/05, John Doe <[EMAIL PROTECTED]> wrote: > Jeremy Nixon am Dienstag, 15. November 2005 08.06: > > Peter1 Alvin <[EMAIL PROTECTED]> wrote: > > > Please tell me I can do this! > > > > > > Using mod_perl, how do you keep Perl objects in RAM from page to page? > [...] > > As an example, I have a handler that needs to read a directory listing. > > Most of the time it will be looking at the same directory over and over, > > so I decide that it's okay to have to restart the server if the listing > > will change (the listing won't change much) and do something like: > > > > { # start a lexical scope > > my %d_cache; > > sub handler { > > # stuff > > if (not defined($d_cache{$foo})) { > > # pull in directory listing and store it in $d_cache{$foo} > > } > > # proceed to use $d_cache{$foo} information > > } > > } end lexical scope > > > > This way, a directory is only actually read once (at most) per server > > child process. > > Hi Jeremy > > Hope it's not a stupid question, but are you sure %d_cache survives a request? > Maybe I'm totally misunderstanding something but I thought after the point > > } end lexical scope > > %d_cache gets destroyed (if not still referenced from somewhere else). > > I would have left out the scope-{} to keep %d_cache at file level. > Would that be wrong? And why?
The handler() sub stays in scope - it's basically a global variable, and it holds a reference to %d_cache. So %d_cache goes out of scope, but doesn't get destroyed. It hangs around until the next time handler() is called, at which point you're back in the same scope and can access %d_cache again. d.