Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!swrinde!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: global pattern matching question Message-ID: <1991May6.233109.12524@jpl-devvax.jpl.nasa.gov> Date: 6 May 91 23:31:09 GMT References: <1991May4.010307.11792@jpl-devvax.jpl.nasa.gov> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: comp Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 53 In article jes@mbio.med.upenn.edu (Joe Smith) writes: : The book says that $` and $' aren't generated unless they're : mentioned. In an expression like length $`, does perl actually : generate a copy of this piece of the original string just to determine : the match position, or is the length just calculated from the match : position? Unfortunately, "length $`" counts as a mention currently. I've wanted to optimize that out for some time, but haven't collected enough of those little round tuits. : > An option to s/// not to do replacement would be interesting, though : : And to think I've always wondered how such wonderfully : convoluted language design happens... Yah. : > there might be a better way--maybe an initial offset for m//, or : > allowing patterns in index(). : : This seems more direct, but the ability to bind an action to the match : is also very important. Perhaps access to a primitive pattern : matching function (the way splice is related to push, pop, etc.) would : be reasonable: : : $options = "oi"; : : for ($pos = 0; : $matched = regexp( $pattern, $seq, $pos, $options ); : $pos += $matched ) { : push( @positions, $pos ); : push( @sites, subst($seq, $pos, $matched )); : } : : I think you could replicate the match and substitute capabilities with : that (subst($seq, $pos, $matched ) = "your replacement here..."), with : arbitrary actions on a match and without a lot of extra string copies. : It'll never fit on one line, but I could live with that. I'm thinking more along the lines of the g// operator in ed: while ($foo =~ g/pat/) { push(@positions, length $`); push(@sites, $&); } g// would have to have its own built in state. I'm not sure what it should do if $foo changes... Perhaps it ought to be m/pat/g instead. Larry