Re: Other Globals in %once

"Jonathan Swartz" <[email protected]>
Newsgroups gmane.comp.web.mason.devel
Message-ID <[email protected]>
> > However, the $story, $burner, $writer, and $element objects aren't
> > actually assigned. This has never been a problem, because we're not
> > executing the component, just compiling it. Does the %once block
> > actually get executed by make_component()? If so, need it be? And if it
> > must be, well, I guess I'll have to figure out a way to make mock
> > objects to assign to those globals. Which would be a PITA.
>
> Yes, the once block is executed when the component's compiled Perl code is
> loaded/eval'd.  That's how we make sure that it's contents always stick
> around in memory as long as the compiled component itself is in memory.
> That's really the only good way to do it, I think.
>

That is correct. The only way to make sure that the <%once> variables exist
in the lexical closure of the component is by executing the <%once> block,
then returning the component inside of that.

However, there is a simple way to get code to run the first time the
component is _executed_ rather than loaded. Just do this:

    <%once>
    my (@pub, @all_story);
    my $initialized = 0;
    </%once>

    <%init>
    unless ($initialized++) {
        @pub = $burner->get_mode == PUBLISH_MODE ? ( publish_status => 1) :
();
        @all_story = Bric::Biz::Asset::Business::Story->list({ @pub,
           ...
    }

    ... rest of init section ...

You'll still have the benefits of computing these values once. Does that
solve the problem?

This should probably be doc'd / faq'd.

Jon



-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. 
Take an Oracle 10g class now, and we'll give you the exam FREE. 
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.