How to convert sequence U + XXXXX to character in perl?

it

perl -CO -E 'say "\N{U+00E1}"'

      

prints

รก

      

How to achieve to get the same thing with the following:

echo "U+00E1" | perl -CO -lnE 'say what_here($_)'

      

+3


source to share


4 answers


Use charnames :

echo "U+00E1" | perl -Mcharnames=short -CO -lnE 'say charnames::vianame($_)'

      



You can also use eval

if you can control what comes in as input:

echo "U+00E1" | perl -CO -lnE 'say eval qq("\\N{$_}")'

      

+4


source


Pure basic Perl solution:

echo "U+00E1" | perl -C -nE 'say chr(hex) if s/U\+//'

      



Please don't use unsafe ( eval

) solutions like:

echo "U+00E1" | perl -C -nE 's/U\+/0x/;eval qq{say chr($_)}'

      

+2


source


Using pack

echo U+0041 U+0042 U+0043 U+00E1 | perl -ne"print pack 'U', hex for /\p{hex}+/g"

      

Output

ABCรก

      

+1


source


echo "U+00E1" | perl -CO -lnE 'eval "\$a=\"\\N{$_}\";"; say $a;'

      

0


source







All Articles