package charnames; my $fname = 'unicode/UnicodeData-Latest.txt'; my $txt; # This is not optimized in any way yet sub charnames { $name = shift; $txt = do "unicode/Name.pl" unless $txt; my @off; if ($^H{charnames_full} and $txt =~ /\t\t$name$/m) { @off = ($-[0], $+[0]); } unless (@off) { if ($^H{charnames_short} and $name =~ /^(.*?):(.*)/s) { my ($script, $cname) = ($1,$2); my $case = ( $cname =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL"); if ($txt =~ m/\t\t\U$script\E (?:$case )?LETTER \U$cname$/m) { @off = ($-[0], $+[0]); } } } unless (@off) { my $case = ( $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL"); for ( @{$^H{charnames_scripts}} ) { (@off = ($-[0], $+[0])), last if $txt =~ m/\t\t$_ (?:$case )?LETTER \U$name$/m; } } die "Unknown charname '$name'" unless @off; # use caller 'encoding'; # Does not work at compile time? my $ord = hex substr $txt, $off[0] - 4, 4; if ($^H & 0x8) { use utf8; return chr $ord; } return chr $ord if $ord <= 255; my $hex = sprintf '%X=0%o', $ord, $ord; my $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2; die "Character 0x$hex with name '$fname' is above 0xFF"; } sub import { shift; die "No scripts for `use charnames'" unless @_; $^H |= 0x20000; $^H{charnames} = \&charnames ; my %h; @h{@_} = (1) x @_; $^H{charnames_full} = delete $h{':full'}; $^H{charnames_short} = delete $h{':short'}; $^H{charnames_scripts} = [map uc, keys %h]; } 1; __END__ =head1 NAME charnames - define character names for C<\C{named}> string literal escape. =head1 SYNOPSIS use charnames ':full'; print "\C{GREEK SMALL LETTER SIGMA} is called sigma.\n"; use charnames ':short'; print "\C{greek:Sigma} is an upper-case sigma.\n"; use charnames qw(cyrillic greek); print "\C{sigma} is Greek sigma, and \C{be} is Cyrillic b.\n"; =head1 DESCRIPTION Pragma C supports arguments C<:full>, C<:short> and script names. If C<:full> is present, for expansion of C<\C{CHARNAME}}> string C is first looked in the list of standard Unicode names of chars. If C<:short> is present, and C has the form C, then C is looked up as a letter in script C