David Wheeler <[EMAIL PROTECTED]> wrote:
> Now, this seemed rather silly to me. I couldn't imagine that
> it was efficient, and I generally like to look for reasons to
> lose POSIX and its bloat. This is what I came up with:
>
> $str =~ s/([^ -~])/'\\' . sprintf("%03o", ord($1))/ge;
If you're serious about submitting a patch, you might note that
perlre says
Note also that the whole range idea is rather unportable
between character sets--and even within character sets
they may cause results you probably didn't expect. A sound
principle is to use only ranges that begin from and end at
either alphabets of equal case ([a-e], [A-E]), or digits
([0-9]). Anything else is unsafe. If in doubt, spell out
the character sets in full.
Your character class works only in ASCII. So for correctness
it's better to use the POSIX character class syntax. The
character class for nonprintable characters can be written as
either [[:^print:]] or [^[:print:]] (see perlre).
Unfortunately, it seems that that syntax requires Perl 5.6.1,
so it might not be the most friendly thing to use in a module.
I assume that's why the authors went with POSIX.pm.
If you're interested only in golf, and don't care about those
issues, you can eliminate several bytes easily:
$str=~s/[^ -~]/sprintf'\%03o',ord$&/ge;
--
Keith C. Ivey <[EMAIL PROTECTED]>
Washington, DC