mjd-perl-pm on Mon, 6 Mar 2000 22:51:01 -0500 (EST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Funky code thing


Here's the thing I wanted to show, but I couldn't remember what it
was:

        sub handle_parens {
          my ($t, $code, $o, $c) = @_;
          $o = '(' unless defined $o;
          $c = ')' unless defined $c;
        
          while ('true') {
            last if (my $cp = index  $t, $c)      < 0;
            last if (my $op = rindex $t, $o, $cp) < 0;
            $code->(substr($t, $op, $cp-$op+1));
          }
        
          return $t;
        }

The thing I actually wanted to show was the

        last if (my ... )

in the middle, but when I got home and saw the context, I thought
folks might be interested to see the whole thing.  

There are at least four features of note:

1. This shows the new method for handling strings with nested
   delimiters that I mentioned this evening.  If you give it the
   string 

        (1 (2) (3 (4)) 5)

   it will invoke $code on (2), then (4), then (3 ...), then (1 ... 5),
   in that order.  Short descrition:  The parentheses groups get
   handled in left-to-right order of their *close* parentheses.

2. last if (my ... ) < 0 ;

   Clever innovation or horrid obfuscation?

3. while ('true') { ... }

   Idiomatic or idiotic?

4. Notice the way that $t gets modified.  $code is supposed to look
   something like this:

        sub { $_[0] = '...' }

   The $_[0] is aliased to the actual argument, which is a substr()
   call.  Thus assigning to $_[0] down in the $code actually modifies
   $t back in handle_parens.  What do people think of this?  I found
   it rather convenient to use, and had the idea that maybe other
   people would be able to use it easily without having to know how it
   was implemented.  What verdict do you folks have?  Do you find it
   sweet, sour, bitter, or salty?

I believe Mr. R. Geoffrey said this evening that he was planning to
pick on me, and this looks like as good an opportunity as any, so
bring it on!

**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**