Dacav
Dacav

Reputation: 14078

Getting a Unicode code point ("character") by name

Perl makes it very convenient to get some symbol.

$ perl -CO -E 'say "\N{FAMILY}"'
👪

Is there a way to evaluate the symbol with "\N{}" via interpolation? Something similar to:

perl -CO -E 'my $what = "FAMILY"; say "\N{$what}"'

(which does not work, but gives you the flavour).

Besides feasibility, is there any drawback or possible threat in such evaluation? E.g. say the $what string is user-defined, I would never eval $what. Would I \N{$what} safely?

Ops...

After getting the answers, I noticed how this information clearly stated in perlunicook. Sorry for the lack of RTFM. I guess this is a good thing to have on stackoverflow anyway.

Upvotes: 4

Views: 205

Answers (2)

choroba
choroba

Reputation: 241958

Use the vianame function from charnames:

perl -CO -Mcharnames=:full -wE 'say chr charnames::vianame("PILE OF POO")'
💩

Upvotes: 10

ysth
ysth

Reputation: 98398

use charnames ();
my $what = "FAMILY";
say charnames::string_vianame($what);

Upvotes: 5

Related Questions