in reply to html2code.pl

Wouldn't this be a one-liner based on
s/./sprintf('&#%03d', ord($&))/ge;
?

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: html2code.pl
by tlm (Prior) on Aug 21, 2005 at 06:38 UTC

    Nice, but why not use $1 instead, and thus avoid the "evil variable" $& ?

    s/(.)/sprintf('&#%03d;', ord($1))/ge;

    the lowliest monk

      Frankly, because it's a one-liner working on one string. The performance difference isn't going to make up for typing extra parentheses, though it is something that people should be aware of. I could have left out more parens, but I thought it would make a hard-to-read answer.
      perl -pe 's/./sprintf"&#%03d;",ord$&/ge'

      Caution: Contents may have been coded under pressure.