You want to convert this:
USER_BATCH_INFO
or
user_batch_info

Into this:
User_Batch_Info

Change the first arg to join to '' and get:
UserBatchInfo

$str = join('_', map { ucfirst(lc($_)) } split ('_', $str));

Replies are listed 'Best First'.
Re: Upper case first letter of each _ delimited word
by btrott (Parson) on Dec 05, 2000 at 02:37 UTC
    Useful. How about wrapping in a sub and letting the caller specify the delim?
    sub ucwords { join $_[1], map ucfirst lc, split /\Q$_[1]\E/, $_[0], -1; }
    Updated to fix the bugs tilly mentions below.

    To be called like:

    print ucwords("BATCH_USER_INFO", "_");
    There may be a faster way, and if there is I'm sure someone'll point it out. :)
      Tsk tsk.
      print ucwords("hello_world_", "_"); print ucwords("hello*world", "*");
      You need to specify the third argument to split and handle meta characters more carefully...
        That's a nice solution, but but I think it is missing a couple features: it doesn't catch the first word in the string; it doesn't lowercase the rest of each word; and it doesn't allow variable-width delimiters. Here's a modified version that addresses those issues.
        sub ucwords { my($new, $delim) = @_; $new =~ s/($delim|^)(.*?)(?=$delim|$)/$1\u\L$2/sg; $new; }
        To match an entire word, from one delimiter to the next, I used a non-greedy match and a positive lookahead.
        That's slick but it doesn't quite work:
        print ucwords("BATCH_USER_INFO", "_"), "\n"; print ucwords("batch_user_info", "_"), "\n";
        produces
        BATCH_USER_INFO batch_User_Info
        In the first case you don't ever lower-case the string; in the second case you don't look for "word" at the beginning of the string, before the delim.
Re: Upper case first letter of each _ delimited word
by Anonymous Monk on Dec 10, 2000 at 04:03 UTC
    $str =~ s/([^_]+)/\u\L$1/gi