Shell script to convert to Title case

I need a shell script which will convert the given string to Title case.

E.g "hi man"

to "Hi man"

$ echo "hi man" | perl -ane ' foreach $wrd ( @F ) { print ucfirst($wrd)." "; } print "\n" ; '
Hi Man

this is good but pls tell the logic in perl part.

also can it be done purely in shell script (+ awk, sed like tat) without any help from other languages like perl, python, etc?

I have a 2 step process:

a=`echo hi man | cut -c1 | tr [:lower:] [:upper:]`
echo hi man | sed "s/./$a/"

echo "hi man" | perl -ane ' foreach $wrd ( @F ) { print ucfirst($wrd)." "; } print "\n" ; '

a option splits the input with space as separator and assign the splitted words in array F.
Loop through the array and ucfirst function capitalizes the first character of the word

echo "hi man" | awk '{printf("%s%s\n",toupper(substr($0,1,1)),substr($0,2))}'