Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: can we ever compile perl? Message-ID: <110306@convex.convex.com> Date: 11 Dec 90 06:51:33 GMT References: <275E7B47.2EB9@tct.uucp> <9592:Dec920:40:5190@kramden.acf.nyu.edu> Sender: news@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 105 In article <9592:Dec920:40:5190@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: >1. Compile some large subset of the language to portable C code. We usually say "well, but not evals of course." I've a suspicion that this rules out a lot of code. For example, a user guy mailed me recently with a problem that had a quick eval answer, and I'm thinking that saying "no evals in compiled code" really limits a large subset of the language. Here's the problem: I'm still working with the problem I was attempting to describe to you last night. It involves a simple search and replace, but the delimiting of the search string will vary, and I would like the new string to maintain the same variable delimiters. The strings can be internally delimited by a combination of underscores, spaces and newlines, and externally by newlines and commas. I want to replace with the same delimeters. For example: search string: my_search_string new string: this_is_it may be matched by: replace should be: - ------------------ ------------------ my search string this is it my_search_string this_is_it my_search this_is string it my this search string is it and so on. I know there must be some straightforward way to do it, but so far I have not figured it out. I've got the general one word case, and fixed number of words, but not a variable number solution. The code he was trying to use was this: ########################################################################### #!/usr/bin/perl # # gl - global replace for variable format strings $#ARGV == 3 || die "Invalid no. of arguments"; ($infile, $outfile, $oldexp, $newexp) = @ARGV; @old = split(/[ _]/,$oldexp); @new = split(/[ _]/,$newexp); open(in,"$infile") || die "Can't open $infile: $!"; open(out,">$outfile") || die "Can't open $outfile: $!"; $foo = ; while ( ) { $foo .= $_; } #First pass, single line to single line # new expression may contain underscores if (!$#old) { # The following searches for the label making sure it begins # and ends with a space, comma or newline and replaces the # label and whatever separators it found around it. $foo =~ s/(\d\n|,\n|,)([ ]*)$oldexp([ ]*)(,|\n,|\n\d)/\1\2$newexp\3\4/g; print "Finished, output in $outfile.\n"; } # Multi-line to multi line, equal size # Need to parameterize for any size if ($#old) { $test = $foo; $foo =~ s/(\d\n|,\n|,)([ ]*)$old[0]([ _\n])$old[1]([ _\n])$old[2]([ ]*)(,|\n,|\n\d)/\1\2$ new[0]\3$new[1]\4$new[2]\5\6/g; print "Finished 2, output in $outfile.\n"; } print out $foo; ########################################################################### Which I found to be pretty convoluted. My solution was this: #!/usr/local/bin/perl # sanity checks first die "usage: $0 string1 string2 [files ...]" if @ARGV < 2; die "unbalanced underbars" unless ($count = $ARGV[0] =~ tr/_/_/) == ($ARGV[1] =~ tr/_/_/); die "too many underbars" unless $count < 10; ($find = shift) =~ s/[\s_]/([\\s_]+)/g; ($repl = shift) =~ s/[\s_]/'$'.++$i/eg; print STDERR "replacing all ``$find'' with ``$repl''\n"; undef $/; $_ = <>; eval "s/$find/$repl/g"; print; Notice that I've used not one but two evals in this little program. Of course, this is too short to bother wanting to compile (unless someone has other motivations than speed for compilations), but I think it illustrates the problem: evals are just too darn convenient. I don't really want to think about how I might do that if I couldn't have an eval, but I don't know how to compile it with one either. --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "With a kernel dive, all things are possible, but it sure makes it hard to look at yourself in the mirror the next morning." -me