Dear Monks,

in an ultra-lightweight webserver I have this code:

sub loadCode { my $this = shift; my $scrref = &getFileAsString($this->{filename}); # load into the re +gistry $this->{code} = sub {eval($$scrref);}; # store the script in a +n anonymous subroutine }

The code is then executed as &{$this->{code}};. This works - to a certain extent - well. As already stated in the camel book, the eval EXPR is much slower than the eval BLOCK syntax. And if I do cut&paste code and do a BLOCK after eval (resulting in sub {eval { <verbatim perl-code here...> }; }, execution gets about 18times faster.

My problem is simple: I'm greedy ;-) - that is - I would like to benefit from the speedup, but also be able to initialize these anonymous subroutines at startup of the script (they do not need to be changed at runtime).

I thought about constructing perl source code by another script, but before I'd do such a kludgy solution, I wanted to ask you if there is not a better solution. All in all: mod_perl must do something similar - no?

Thank you for your suggestions.

Bye
 PetaMem
    All Perl:   MT, NLP, NLU


In reply to eval BLOCK with runtime-loaded code? by PetaMem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.