Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!hellgate.utah.edu!caen!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Capitalizing words in a line. Message-ID: <11543@jpl-devvax.JPL.NASA.GOV> Date: 21 Feb 91 20:01:32 GMT References: Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 32 In article marvit@hplpm.hpl.hp.com (Peter Marvit) writes: : One of the routines leaves every word in the string alone (e.i., all : UPPER CASE) but changes only first word to Mixed Case. Unfortunately, my : first code will cause the perl script to grow in memory usage, apparently : without bound as a function of input data: : : sub capitalizefirstonly { : # Get the string itself : $_ = $_[1]; : # Make the whole first word lower case : s/\b([A-Z]*)/$b=$1,$b=~tr:A-Z:a-z:,$b/e; # <--------- : # Now capitalize the first word : s/\b([a-z])/$b=$1,$b=~tr:a-z:A-Z:,$b/e; : # Return the whole string : return($_); : } : My question? What's wrong with the first method. More straight forward, : but a horrible memory pig. Is this a perl bug (heaven forbid?). Yep. Each tr was dropping a couple of chunks of memory when compiled. This doesn't show up until you put it inside an eval. As mentioned earlier, the way you'd want to do that subroutine under 4.0 is: sub capitalizefirstonly { local($tmp) = @_ $tmp =~ s/\b(\w+)/\L\u$1/; $tmp; } Larry