in reply to Re: Globally change ucfirst in mod_perl
in thread Globally change ucfirst in mod_perl

A couple of nits:
  1. Should default to $_
  2. using split ' ' replaces newlines and multiple spaces with single spaces
Update: Brain-fart code deleted. See reply below for correct code.
Update: Just discovered you don't even have to specify CORE:: on ucfirst. Perl knows:
BEGIN { *CORE::GLOBAL::ucfirst = sub { join ' ', map ucfirst, split / /, @_ ? $_[0] : $_ } }

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re: Re: Globally change ucfirst in mod_perl
by antirice (Priest) on May 20, 2004 at 14:25 UTC

    A couple of points:

    1. There's a slight bug as is shown in the code below:
      BEGIN { *CORE::GLOBAL::ucfirst = sub { join ' ', map CORE::ucfirst, split / /, defined $_[0] ? $_[0] : $ +_; } } $_ = "I shouldn't be printing this, but I am anyway."; my $var = undef; print ucfirst $var;

      Perhaps you should be checking for the number of parameters passed instead?

      BEGIN { *CORE::GLOBAL::ucfirst = sub { join ' ', map CORE::ucfirst, split / /, @_ ? $_[0] : $_; } }
    2. The reason I used split ' ' was because the OP used it. Perhaps it's the behavior he wanted. Maybe not? Either way, there are benefits in both approaches.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1