more complete File::Spec support for Mac and VMS, tests (from
[p5sagit/p5-mst-13.2.git] / lib / charnames.pm
CommitLineData
423cee85 1package charnames;
2
3my $fname = 'unicode/UnicodeData-Latest.txt';
4my $txt;
5
6# This is not optimized in any way yet
7sub charnames {
8 $name = shift;
9 $txt = do "unicode/Name.pl" unless $txt;
10 my @off;
11 if ($^H{charnames_full} and $txt =~ /\t\t$name$/m) {
12 @off = ($-[0], $+[0]);
13 }
14 unless (@off) {
15 if ($^H{charnames_short} and $name =~ /^(.*?):(.*)/s) {
16 my ($script, $cname) = ($1,$2);
17 my $case = ( $cname =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
18 if ($txt =~ m/\t\t\U$script\E (?:$case )?LETTER \U$cname$/m) {
19 @off = ($-[0], $+[0]);
20 }
21 }
22 }
23 unless (@off) {
24 my $case = ( $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
25 for ( @{$^H{charnames_scripts}} ) {
26 (@off = ($-[0], $+[0])), last
27 if $txt =~ m/\t\t$_ (?:$case )?LETTER \U$name$/m;
28 }
29 }
30 die "Unknown charname '$name'" unless @off;
31
423cee85 32 my $ord = hex substr $txt, $off[0] - 4, 4;
9c666405 33 if ($^H & 0x8) { # "use byte" in effect?
d41ff1b8 34 use byte;
35 return chr $ord if $ord <= 255;
36 my $hex = sprintf '%X=0%o', $ord, $ord;
37 my $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2;
38 die "Character 0x$hex with name '$fname' is above 0xFF";
423cee85 39 }
d41ff1b8 40 return chr $ord;
423cee85 41}
42
43sub import {
44 shift;
45 die "No scripts for `use charnames'" unless @_;
46 $^H |= 0x20000;
47 $^H{charnames} = \&charnames ;
48 my %h;
49 @h{@_} = (1) x @_;
50 $^H{charnames_full} = delete $h{':full'};
51 $^H{charnames_short} = delete $h{':short'};
52 $^H{charnames_scripts} = [map uc, keys %h];
53}
54
55
561;
57__END__
58
59=head1 NAME
60
4a2d328f 61charnames - define character names for C<\N{named}> string literal escape.
423cee85 62
63=head1 SYNOPSIS
64
65 use charnames ':full';
4a2d328f 66 print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
423cee85 67
68 use charnames ':short';
4a2d328f 69 print "\N{greek:Sigma} is an upper-case sigma.\n";
423cee85 70
71 use charnames qw(cyrillic greek);
4a2d328f 72 print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";
423cee85 73
74=head1 DESCRIPTION
75
76Pragma C<use charnames> supports arguments C<:full>, C<:short> and
77script names. If C<:full> is present, for expansion of
4a2d328f 78C<\N{CHARNAME}}> string C<CHARNAME> is first looked in the list of
423cee85 79standard Unicode names of chars. If C<:short> is present, and
80C<CHARNAME> has the form C<SCRIPT:CNAME>, then C<CNAME> is looked up
81as a letter in script C<SCRIPT>. If pragma C<use charnames> is used
4a2d328f 82with script name arguments, then for C<\N{CHARNAME}}> the name
423cee85 83C<CHARNAME> is looked up as a letter in the given scripts (in the
84specified order).
85
86For lookup of C<CHARNAME> inside a given script C<SCRIPTNAME>
87F<charcodes.pm> looks for the names
88
89 SCRIPTNAME CAPITAL LETTER CHARNAME
90 SCRIPTNAME SMALL LETTER CHARNAME
91 SCRIPTNAME LETTER CHARNAME
92
93in the table of standard Unicode names. If C<CHARNAME> is lowercase,
94then the C<CAPITAL> variant is ignored, otherwise C<SMALL> variant is
95ignored.
96
97=head1 CUSTOM TRANSLATORS
98
4a2d328f 99The mechanism of translation is C<\N{...}> escapes is general and not
423cee85 100hardwired into F<charnames.pm>. A module can install custom
101translations (inside the scope which C<use>s the module) by the
102following magic incantation:
103
104 sub import {
105 shift;
106 $^H |= 0x20000;
107 $^H{charnames} = \&translator;
108 }
109
110Here translator() is a subroutine which takes C<CHARNAME> as an
111argument, and returns text to insert into the string instead of the
4a2d328f 112C<\N{CHARNAME}> escape. Since the text to insert should be different
423cee85 113in C<utf8> mode and out of it, the function should check the current
114state of C<utf8>-flag as in
115
116 sub translator {
117 if ($^H & 0x8) {
118 return utf_translator(@_);
119 } else {
120 return no_utf_translator(@_);
121 }
122 }
123
124=head1 BUGS
125
126Since evaluation of the translation function happens in a middle of
127compilation (of a string literal), the translation function should not
128do any C<eval>s or C<require>s. This restriction should be lifted in
129a future version of Perl.
130
131=cut
132