[kw-pm] Free (as in beer) Advanced Perl

Eric - fishbot eric at uc.org
Tue Jul 26 08:02:12 PDT 2005


For various reasons, I have ended up with three copies of
Advanced Perl (from O'Reilly).  These copies are in addition to
the copy that is going into the kwpm library.  Soo... quick
contest, the winner gets a free copy.

(You have to attend the /pub/social on Thursday (the 28th) to
collect the prize.  Feel free to send me a solution if you just
like solving things, though.)

I won one of the copies in a contest on Perlcast.com.  The
challenge was to come up with the most number of ways to Title
Case all the words in a string, forcing all the rest to
lowercase.

One of my more obscure solutions was as follows:

=code
sub ucfirst_fsm {
   my @chars = unpack 'C*', shift;
   my $bound = 1; # initial boundary

   for ( @chars )
   {
      $bound = 1, next if (( $_ == 0x20 ) || ( $_ == 0x09 ));
      if ( $bound )
      {
         $_ &= 0xDF if (( $_ >= 0x61 ) && ( $_ <= 0x7A ));
         $bound = 0;
         next;
      }
      $_ |= 0x20 if (( $_ >= 0x41 ) && ( $_ <= 0x5A ));
   }
   return pack( 'C*', @chars );
}
=cut

As required, this translates "a stRing TO fOrMaT" to "A String To
Format".  However, this approach quickly breaks.

The first person who sends me a testcase where this code fails,
wins a copy of Advanced Perl, 2nd Edition.

Incidentally, the book is pretty good.

Looking forward to see everyone on Thursday,
Eric


More information about the kw-pm mailing list