[comp.lang.perl] Perl Neophyte Needs Help

Andrew.Vignaux@comp.vuw.ac.nz (Andrew Vignaux) (06/27/90)

In article <8504@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax.JPL.NASA.GOV
(Larry Wall) writes:
> The only other tricky thing we might do here is to put the whole loop
> inside an eval so that the pattern gets compiled before the optimizer
> runs.  The eval is just using the here-is syntax to quote the evaluated
> bit of program.
> [...]
> $pattern = shift;	# get first argument
> [...]
> eval <<"EOF";		# to avoid recompile pattern each time
>     while (<>) {
> 	print if /$pattern/;
>     }
> EOF

I've always been a little wary about using the "eval-the-loop" trick
because it will probably mean that the whole perl compiler/interpreter
will get sucked into my executable when (not if :-) a "perl to C
translator" comes along.  [Note: ispell thinks that "eval" is "evil"
-- so it's not just my opinion :-]

Of course this doesn't stop me using it.

I think the only time *I* every really want to do this is for building
the regexp test(s) based on the script arguments i.e. a grep-like
script (just like this example).  Does/could perl notice that $pattern
is invariant in the original program and arrange to compile the regexp
only once?

Comments?

Andrew
-- 
Domain address: Andrew.Vignaux@comp.vuw.ac.nz

PS: While I was looking at the Wishlist, I noticed that "built-in cpp"
    was there.  Well, I'd be really keen on at least "#include".  I've
    also been worried about the distinction between "#include" and
    "do".  I often want to include a subroutine in multiple scripts:
    I'd rather not have the longer cpp startup time when I'm
    interpreting the script, or the runtime compile of "do" for the
    compiled script.
    Should I be using the pat.SH / package.pl (from dist) approach?

PPS: ispell suggests that "perl" is "peril", but we already knew that :-)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (06/28/90)

In article <1990Jun27.101219.11357@comp.vuw.ac.nz> Andrew.Vignaux@comp.vuw.ac.nz (Andrew Vignaux) writes:
: I think the only time *I* every really want to do this is for building
: the regexp test(s) based on the script arguments i.e. a grep-like
: script (just like this example).  Does/could perl notice that $pattern
: is invariant in the original program and arrange to compile the regexp
: only once?

To recognize that invariance of a variable over a loop is not trivial.  That's
why I put in the o modifier.  The basic problem is that the o doesn't have
any effect until after the optimizer is run, since the pattern is not
compiled until the first time the loop is executed.

The solution will be to make the o modifier set things up so that the
pattern can be optimized at that point to run as fast as if the pattern
were known at compile time.  I think this is doable.  Then you won't
have to use the eval trick on loops to squeeze that last bit of efficiency
out.

I won't guarantee it in the next patch, however.

Larry

sean@ms.uky.edu (Sean Casey) (06/28/90)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:

|eval <<"EOF";		# to avoid recompile pattern each time
|    while (<>) {
|	print if /$pattern/;
|    }
|EOF

Thanks for posting that info. It reminds me one of the problems I have
using perl, which is making things efficient.

I've got a few scripts that munch on some data, a lot of data, and I never
would have thought of a trick like that. That's one of the things I like
about perl: there's a lot of ways to do things. Unfortunately, that's one
of the things that bothers me most about perl: I don't usually pick the
most efficient way.

It would be kind of nice to get some speedup tips published. Some of the
not-so-obvious ways of making scripts run faster. My script takes 15-20
minutes to munch the data. Something tells me with some info like that,
it might be running in 5 minutes.

Sean