views:

1212

answers:

2

I.e.:

echo H#97llo | MagicPerlCommand

Stdout:

Hallo

were MagicPerlCommand is something like

perl -pnle "s/#(\d+)/chr(\1)/ge"

(but that doesn't work).

+8  A: 

Change \1 to $1 in your MagicPerlCommand. The \digit backreference style doesn't t work when the replacement expression is evaluated (i.e. s///e).

That worked for me on Windows and Linux.

j_random_hacker
And that makes sense.
secr
+3  A: 

As per the j_random_hacker answer, you must use $1 rather than \1.

This is because using the '/e' modifier to the regex means the right hand half is just another normal Perl expression, and not a regex substitution. Since it's Perl, you've got to use Perl's syntax for the bracket reference, and not the usual regex syntax.

Alnitak
Yes, that makes sense. But it's strange that echo H#97llo | perl -pnle "s/#(\d+)/chr(\1)/ge" actually returned something (and it wasn't an exception).
secr
on my machines (Linux FC10 and Mac OS X) your code produces a warning "wide character in print". It's treating the \1 as a reference to a scalar.
Alnitak
Please note that it produces the same warning in Windows, which makes my delicious comment stupid.
secr