Shorten text with Run Length Encoding Shorten (or not) text using run length encoding

Input: 

heeeello
woooorld


Output: 

1h4e2l1o
1w4o1r1l1d


Read lines from stdin.
Print to stdout.
Stderr is of course discarded. 
Assume there are hidden testcases (no embedding of the output)
Input/Output as ASCII
Any language is accepted
Perl: 46 → 36 or 27 characters

perl -pe's|((.)\2*)|@x=split//,$1;@x.$x[0]|eg' 

All hail @ardnew for coming up with the idea of using the tr///c operator to count the number of characters in the matched string instead of splitting:

perl -pe's|((.)\2*)|$1=~y///c.$2|eg'


Degolfed:

while(defined($_ = &lt;&gt;)) {
  $_ =~ s{((.)\2*)}           # match 1 or more consecutive identical non-newlines
         {
           ($1 =~ y///c )     # count the number of characters in $1
           .                  # and concatenate it
           $2                 # with the first matched character
         }eg;                 # execute substitution, match "global"
  print $_;                   # print the modified line
}


Usage:

$ perl -pe's|((.)\2*)|$1=~y///c.$2|eg' infile


or via STDIN

$ perl -pe's|((.)\2*)|$1=~y///c.$2|eg'
heeeello


prints

1h4e2l1o