Mailing List Archive

subs only evaluated once?
Hi,
I have a sub on my embperl page. It is executed like this:
my $line = lineStatus();
The return value of the sub depends on the value of a system
call. I have the suspicion that the sub is only executed once per
Apache child, since I sometimes get different values in $line
after reloading the page, values that obviously got cached from a
previous request (And no, it's not the Netscape cache...). A
workaround for this of course is to include the full code of the
sub every time I would call it, but it would be nicer to know why
that caching happens or how to circumvent it cleanly... I
encountered that behaviour at least in three different scripts
until now, so I either gravely misunderstood something and wrote
completely wrong code or Embperl really behaves like I described.

Does Embperl cache the return value of a sub, and why does it do
so if it does?

modperl 1.21 static
Embperl 1.3b2

Andre
RE: subs only evaluated once? [ In reply to ]
Hi,
> I have a sub on my embperl page. It is executed like this:
> my $line = lineStatus();
>[..]

>
> Does Embperl cache the return value of a sub, and why does it do
> so if it does?
>

No, Embperl does _not_ cache the value. I guess your running into a closure
problem, so not Embperl, but youself caches the result.

You have to know that Embperl compiles all of your code into subroutines, so
you have an outer subroutine around your code (which is made by Embperl) and
a inner subroutine, that is your own. With this knowledge you should read
the section "my() Scoped Variable in Nested Subroutines" in the mod_perl
guide
(http://perl.apache.org/guide/perl.html#my_Scoped_Variable_in_Nested_S) .

Solution is to either pass values as argumentes to your subroutine or to use
locals. If this is not your problem, please post a short example script.

Gerald


-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925151
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------
Re: subs only evaluated once? [ In reply to ]
On Fri, May 26, 2000 at 10:38:00PM +0200, Gerald Richter wrote:
> > Does Embperl cache the return value of a sub, and why does it do
> > so if it does?
> >
>
> No, Embperl does _not_ cache the value. I guess your running into a closure
> problem, so not Embperl, but youself caches the result.

Ah, thanks! I really misunderstood closures it seems, now it
works! Thanks very much again!

Andre