ucwords functionality in Perl

pradeep's Avatar author of ucwords functionality in Perl
This is an article on ucwords functionality in Perl in Perl.
There is a function ucwords() in PHP which capitalizes the first letter of each word.

PHP Code:
<?
 $str = "programming forums";
 $str = ucwords($str);
 // now $str = "Programming Forums"
 ?>
I have written a sub-routine in Perl, which gives the same functionality.

Code: Perl
sub ucwords
 {
   $str = shift;
   $str = lc($str);
   $str =~ s/\b(\w)/\u$1/g;
   return $str;
 }
 
 $str = "programming forums";
 $str = &ucwords($str);
 # now $str = "Programming Forums"
Newbie Member
10Oct2007,10:57   #2
#anna's Avatar
No bad code, useful to me, Thanks.