[comp.lang.perl] Detab function

worley@compass.uucp (Dale Worley) (11/21/90)

   X-Name: Stu Donaldson

   I need a detab function written in perl.  [...] How about a one liner? :-)

This isn't even ugly:

perl -pe '$spacing = 8; 1 while s/\t/" " x ($spacing - length($`) % $spacing)/e;'

(Amazing!  The only other language I know of that can do this in one
line is Snobol!)

However, the apparently equivalent program

perl -pe '$spacing = 8; s/\t/" " x ($spacing - length($`) % $spacing)/eg;'

doesn't work, apparently because s///g doesn't work exactly as I
expect.  I expect it to perform each search and replacement, and then
start searching again in the modified string.  It appears (if I insert
a strategic print) that the successive searches are done in the
original string, and all modifications are in a copy of it.  (Is this
what we really want?  Is this documented?)

Dale Worley		Compass, Inc.			worley@compass.com
--
It is easier to get forgiveness than permission.

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/22/90)

In article <1990Nov20.182049.12017@uvaarpa.Virginia.EDU> worley@compass.uucp writes:
: However, the apparently equivalent program
: 
: perl -pe '$spacing = 8; s/\t/" " x ($spacing - length($`) % $spacing)/eg;'
: 
: doesn't work, apparently because s///g doesn't work exactly as I
: expect.  I expect it to perform each search and replacement, and then
: start searching again in the modified string.  It appears (if I insert
: a strategic print) that the successive searches are done in the
: original string, and all modifications are in a copy of it.  (Is this
: what we really want?  Is this documented?)

I don't know if it's what you want, but it's what you get.  :-)

It's documented in The Book, somewhere...

The substitution code is a bit touchy, and if I fiddle with it much
I'll end up rewriting it, which I don't want to do right now.

Larry