in reply to Globally change ucfirst in mod_perl

Two things:

  1. Redefining symbol table entries requires a leading asterick.
  2. The original ucfirst still exists as CORE::ucfirst.

So that gives us:

BEGIN { *CORE::GLOBAL::ucfirst = sub { join ' ', map CORE::ucfirst, split ' ', shift; } }

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

Replies are listed 'Best First'.
OT - sig needs work ;-)
by cLive ;-) (Prior) on May 20, 2004 at 08:58 UTC

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

    Don't you mean:

    "The first rule of Perl club is - use Perl
    The $i".(int($i%100/10)-1?$i-3?$i-2?$i-1?'th':'st':'nd':'rd':'th' ." rule of Perl club is - follow rule \$i - 1 for \$i > 1"

    :-)

    cLive ;-)

    ps - it's late, and untested :)

Re: Re: Globally change ucfirst in mod_perl
by Roy Johnson (Monsignor) on May 20, 2004 at 13:41 UTC
    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

      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