This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
charnames.pm: Change variable name
[perl5.git] / lib / charnames.pm
index ba580f8..b8d1593 100644 (file)
@@ -2,9 +2,9 @@ package charnames;
 use strict;
 use warnings;
 use File::Spec;
-our $VERSION = '1.11';
+our $VERSION = '1.15';
 
-use bytes ();          # for $bytes::hint_bits
+use bytes ();          # for $bytes::hint_bits
 
 my %system_aliases = (
                 # Icky 3.2 names with parentheses.
@@ -399,18 +399,38 @@ my %deprecated_aliases = (
                 'REVERSE INDEX'           => 0x8D, # REVERSE LINE FEED
             );
 
-my %user_name_aliases = (
-                # User defined aliases. Even more convenient :)
-                # These are the ones that resolved to names
-            );
 
-my %user_numeric_aliases = (
-                # And these resolve directly to code points.
-            );
-my %inverse_user_aliases = (
-                # Map from code point to name
-            );
-my $txt;
+my $txt;  # The table of official character names
+
+my %full_names_cache; # Holds already-looked-up names, so don't have to
+# re-look them up again.  The previous versions of charnames had scoping
+# bugs.  For example if we use script A in one scope and find and cache
+# what Z resolves to, we can't use that cache in a different scope that
+# uses script B instead of A, as Z might be an entirely different letter
+# there; or there might be different aliases in effect in different
+# scopes, or :short may be in effect or not effect in different scopes,
+# or various combinations thereof.  This was solved in this version
+# mostly by moving things to %^H.  But some things couldn't be moved
+# there.  One of them was the cache of runtime looked-up names, in part
+# because %^H is read-only at runtime.  I (khw) don't know why the cache
+# was run-time only in the previous versions: perhaps oversight; perhaps
+# that compile time looking doesn't happen in a loop so didn't think it
+# was worthwhile; perhaps not wanting to make the cache too large.  But
+# I decided to make it compile time as well; this could easily be
+# changed.
+# Anyway, this hash is not scoped, and is added to at runtime.  It
+# doesn't have scoping problems because the data in it is restricted to
+# official names, which are always invariant, and we only set it and
+# look at it at during :full lookups, so is unaffected by any other
+# scoped options.  I put this in to maintain parity with the older
+# version.  If desired, a %short_names cache could also be made, as well
+# as one for each script, say in %script_names_cache, with each key
+# being a hash for a script named in a 'use charnames' statement.  I
+# decided not to do that for now, just because it's added complication,
+# and because I'm just trying to maintain parity, not extend it.
+
+# Designed so that test decimal first, and then hex.  Leading zeros
+# imply non-decimal, as do non-[0-9]
 my $decimal_qr = qr/^[1-9]\d*$/;
 
 # Returns the hex number in $1.
@@ -426,31 +446,40 @@ sub carp
   require Carp; goto &Carp::carp;
 } # carp
 
-sub alias (@)
+sub alias (@) # Set up a single alias
 {
   my $alias = ref $_[0] ? $_[0] : { @_ };
   foreach my $name (keys %$alias) {
     my $value = $alias->{$name};
+    next unless defined $value;          # Omit if screwed up.
+
+    # Is slightly slower to just after this statement see if it is
+    # decimal, since we already know it is after having converted from
+    # hex, but makes the code easier to maintain, and is called
+    # infrequently, only at compile-time
+    if ($value !~ $decimal_qr && $value =~ $hex_qr) {
+      $value = CORE::hex $1;
+    }
     if ($value =~ $decimal_qr) {
-        $user_numeric_aliases{$name} = $value;
+        $^H{charnames_ord_aliases}{$name} = $value;
 
         # Use a canonical form.
-        $inverse_user_aliases{sprintf("%04X", $value)} = $name;
-    }
-    elsif ($value =~ $hex_qr) {
-        my $decimal = CORE::hex $1;
-        $user_numeric_aliases{$name} = $decimal;
-
-        # Must convert to decimal and back to guarantee canonical form
-        $inverse_user_aliases{sprintf("%04X", $decimal)} = $name;
+        $^H{charnames_inverse_ords}{sprintf("%05X", $value)} = $name;
     }
     else {
-        $user_name_aliases{$name} = $value;
+        # XXX validate syntax when deprecation cycle complete. ie. start
+        # with an alpha only, etc.
+        $^H{charnames_name_aliases}{$name} = $value;
     }
   }
 } # alias
 
-sub alias_file ($)
+sub not_legal_use_bytes_msg {
+  my ($name, $ord) = @_;
+  return sprintf("Character 0x%04x with name '$name' is above 0xFF with 'use bytes' in effect", $ord);
+}
+
+sub alias_file ($)  # Reads a file containing alias definitions
 {
   my ($arg, $file) = @_;
   if (-f $arg && File::Spec->file_name_is_absolute ($arg)) {
@@ -473,111 +502,167 @@ sub alias_file ($)
   0;
 } # alias_file
 
+# For use when don't import anything.  This structure must be kept in
+# sync with the one that import() fills up.
+my %dummy_H = (
+                charnames_stringified_names => "",
+                charnames_stringified_ords => "",
+                charnames_scripts => "",
+                charnames_full => 1,
+                charnames_short => 0,
+              );
 
-sub lookup_name {
-  my $name = shift;
-  my $runtime = shift;  # compile vs run time
+
+sub lookup_name ($;$) {
 
   # Finds the ordinal of a character name, first in the aliases, then in
-  # the large table.  If not found, returns undef if runtime; complains
-  # and returns the Unicode replacement if compile.
-  # This is not optimized in any way yet
+  # the large table.  If not found, returns undef if runtime; if
+  # compile, complains and returns the Unicode replacement character.
+
+  my $runtime = (@_ > 1);  # compile vs run time
+
+  my ($name, $hints_ref) = @_;
+
+  my $utf8;
+  my $save_input;
 
-  my $ord;
+  if ($runtime) {
+
+    # If we didn't import anything (which happens with 'use charnames ()',
+    # substitute a dummy structure.
+    $hints_ref = \%dummy_H if ! defined $hints_ref
+                              || ! defined $hints_ref->{charnames_full};
+
+    # At runtime, but currently not at compile time, $^H gets
+    # stringified, so un-stringify back to the original data structures.
+    # These get thrown away by perl before the next invocation
+    # Also fill in the hash with the non-stringified data.
+    # N.B.  New fields must be also added to %dummy_H
+
+    %{$^H{charnames_name_aliases}} = split ',',
+                                      $hints_ref->{charnames_stringified_names};
+    %{$^H{charnames_ord_aliases}} = split ',',
+                                      $hints_ref->{charnames_stringified_ords};
+    $^H{charnames_scripts} = $hints_ref->{charnames_scripts};
+    $^H{charnames_full} = $hints_ref->{charnames_full};
+    $^H{charnames_short} = $hints_ref->{charnames_short};
+  }
 
   # User alias should be checked first or else can't override ours, and if we
   # add any, could conflict with theirs.
-  if (exists $user_numeric_aliases{$name}) {
-    $ord = $user_numeric_aliases{$name};
+  if (exists $^H{charnames_ord_aliases}{$name}) {
+    $utf8 = $^H{charnames_ord_aliases}{$name};
   }
-  elsif (exists $user_name_aliases{$name}) {
-    $name = $user_name_aliases{$name};
+  elsif (exists $^H{charnames_name_aliases}{$name}) {
+    $name = $^H{charnames_name_aliases}{$name};
+    $save_input = $name;  # Cache the result for any error message
   }
   elsif (exists $system_aliases{$name}) {
-    $ord = $system_aliases{$name};
+    $utf8 = $system_aliases{$name};
   }
   elsif (exists $deprecated_aliases{$name}) {
     require warnings;
     warnings::warnif('deprecated', "Unicode character name \"$name\" is deprecated, use \"" . viacode($deprecated_aliases{$name}) . "\" instead");
-    $ord = $deprecated_aliases{$name};
+    $utf8 = $deprecated_aliases{$name};
   }
 
   my @off;
 
-  if (! defined $ord) {
-    ## Suck in the code/name list as a big string.
-    ## Lines look like:
-    ##     "0052\t\tLATIN CAPITAL LETTER R\n"
-    $txt = do "unicore/Name.pl" unless $txt;
-
-    ## @off will hold the index into the code/name string of the start and
-    ## end of the name as we find it.
+  if (! defined $utf8) {
 
-    ## If :full, look for the name exactly; runtime implies full
-    if (($runtime || $^H{charnames_full}) && $txt =~ /\t\t\Q$name\E$/m) {
-      @off = ($-[0] + 2, $+[0]);    # The 2 is for the 2 tabs
+    # See if has looked this up earlier.
+    if ($^H{charnames_full} && exists $full_names_cache{$name}) {
+      $utf8 = $full_names_cache{$name};
     }
+    else {
 
-    ## If we didn't get above, and :short allowed, look for the short name.
-    ## The short name is like "greek:Sigma"
-    unless (@off) {
-      if (($runtime || $^H{charnames_short}) && $name =~ /^(.+?):(.+)/s) {
-       my ($script, $cname) = ($1, $2);
-       my $case = $cname =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL";
-       if ($txt =~ m/\t\t\U$script\E (?:$case )?LETTER \U\Q$cname\E$/m) {
-         @off = ($-[0] + 2, $+[0]);
-       }
+      ## Suck in the code/name list as a big string.
+      ## Lines look like:
+      ##     "00052\tLATIN CAPITAL LETTER R\n"
+      $txt = do "unicore/Name.pl" unless $txt;
+
+      ## @off will hold the index into the code/name string of the start and
+      ## end of the name as we find it.
+
+      ## If :full, look for the name exactly; runtime implies full
+      my $found_full_in_table = 0;  # Tells us if can cache the result
+      if ($^H{charnames_full}) {
+
+        # See if the name is one which is algorithmically determinable.
+        # The subroutine is included in Name.pl.  The table contained in
+        # $txt doesn't contain these.  Experiments show that checking
+        # for these before checking for the regular names has no
+        # noticeable impact on performance for the regular names, but
+        # the other way around slows down finding these immensely.
+        # Algorithmically determinables are not placed in the cache (that
+        # $found_full_in_table indicates) because that uses up memory,
+        # and finding these again is fast.
+        if (! defined ($utf8 = name_to_code_point_special($name))) {
+
+          # Not algorthmically determinable; look up in the table.
+          if ($txt =~ /\t\Q$name\E$/m) {
+            @off = ($-[0] + 1, $+[0]);    # The 1 is for the tab
+            $found_full_in_table = 1;
+          }
+        }
       }
-    }
 
-    ## If we still don't have it, check for the name among the loaded
-    ## scripts.
-    if (! $runtime && not @off) {
-      my $case = $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL";
-      for my $script (@{$^H{charnames_scripts}}) {
-        if ($txt =~ m/\t\t$script (?:$case )?LETTER \U\Q$name\E$/m) {
-          @off = ($-[0] + 2, $+[0]);
-          last;
+      # If we didn't get it above, keep looking
+      if (! $found_full_in_table && ! defined $utf8) {
+
+        # If :short is allowed, see if input is like "greek:Sigma".
+        my $scripts_trie;
+        if (($^H{charnames_short})
+            && $name =~ /^ \s* (.+?) \s* : \s* (.+?) \s* $ /xs)
+        {
+            $scripts_trie = "\U\Q$1";
+            $name = $2;
+        }
+        else {
+            $scripts_trie = $^H{charnames_scripts};
+        }
+
+        my $case = $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL";
+        if ($txt !~
+            /\t (?: $scripts_trie ) \ (?:$case\ )? LETTER \ \U\Q$name\E $/xm)
+        {
+          # Here we still don't have it, give up.
+          return if $runtime;
+
+          # May have zapped input name, get it again.
+          $name = (defined $save_input) ? $save_input : $_[0];
+          carp "Unknown charname '$name'";
+          return 0xFFFD;
         }
+
+        @off = ($-[0] + 1, $+[0]);  # The 1 is for the tab
       }
-    }
 
-    ## If we don't have it by now, give up.
-    unless (@off) {
-      return if $runtime;
-      carp "Unknown charname '$name'";
-      return "\x{FFFD}";
-    }
+      if (! defined $utf8) {
+
+        # Now know where in the string the name starts.
+        # The code, 5 hex digits long (and a tab), is before that.
+        $utf8 = CORE::hex substr($txt, $off[0] - 6, 5);
+      }
 
-    # Get the official name in case need to output a message
-    $name = substr($txt, $off[0], $off[1] - $off[0]);
-
-    ##
-    ## Now know where in the string the name starts.
-    ## The code, in hex, is before that.
-    ##
-    ## The code can be 4-6 characters long, so we've got to sort of
-    ## go look for it, just after the newline that comes before $off[0].
-    ##
-    ## This would be much easier if unicore/Name.pl had info in
-    ## a name/code order, instead of code/name order.
-    ##
-    ## The +1 after the rindex() is to skip past the newline we're finding,
-    ## or, if the rindex() fails, to put us to an offset of zero.
-    ##
-    my $hexstart = rindex($txt, "\n", $off[0]) + 1;
-
-    ## we know where it starts, so turn into number -
-    ## the ordinal for the char.
-    $ord = CORE::hex substr($txt, $hexstart, $off[0] - 2 - $hexstart);
+      # Cache the input so as to not have to search the large table
+      # again, but only if it came from the one search that we cache.
+      $full_names_cache{$name} = $utf8 if $found_full_in_table;
+    }
   }
 
-  return $ord if $runtime || $ord <= 255 || ! ($^H & $bytes::hint_bits);
+  return $utf8 if $runtime || $utf8 <= 255 || ! ($^H & $bytes::hint_bits);
 
   # Here is compile time, "use bytes" is in effect, and the character
   # won't fit in a byte
-
-  croak sprintf("Character 0x%04x with name '$name' is above 0xFF", $ord);
+  # Prefer any official name over the input one.
+  if (@off) {
+    $name = substr($txt, $off[0], $off[1] - $off[0]) if @off;
+  }
+  else {
+    $name = (defined $save_input) ? $save_input : $_[0];
+  }
+  croak not_legal_use_bytes_msg($name, $utf8);
 } # lookup_name
 
 sub charnames {
@@ -586,8 +671,8 @@ sub charnames {
   # For \N{...}.  Looks up the character name and returns its ordinal if
   # found, undef otherwise.  If not in 'use bytes', forces into utf8
 
-  my $ord = lookup_name($name, 0); # 0 means compile-time
-  return unless defined $ord;
+  my $ord = lookup_name($name);
+  return if ! defined $ord;
   return chr $ord if $^H & $bytes::hint_bits;
 
   no warnings 'utf8'; # allow even illegal characters
@@ -602,6 +687,11 @@ sub import
     carp("`use charnames' needs explicit imports list");
   }
   $^H{charnames} = \&charnames ;
+  $^H{charnames_ord_aliases} = {};
+  $^H{charnames_name_aliases} = {};
+  $^H{charnames_inverse_ords} = {};
+  # New fields must be added to %dummy_H, and the code in lookup_name()
+  # that copies fields from the runtime structure
 
   ##
   ## fill %h keys with our @_ args.
@@ -610,19 +700,19 @@ sub import
   while (my $arg = shift) {
     if ($arg eq ":alias") {
       @_ or
-       croak ":alias needs an argument in charnames";
+        croak ":alias needs an argument in charnames";
       my $alias = shift;
       if (ref $alias) {
-       ref $alias eq "HASH" or
-         croak "Only HASH reference supported as argument to :alias";
-       alias ($alias);
-       next;
+        ref $alias eq "HASH" or
+          croak "Only HASH reference supported as argument to :alias";
+        alias ($alias);
+        next;
       }
       if ($alias =~ m{:(\w+)$}) {
-       $1 eq "full" || $1 eq "short" and
-         croak ":alias cannot use existing pragma :$1 (reversed order?)";
-       alias_file ($1) and $promote = 1;
-       next;
+        $1 eq "full" || $1 eq "short" and
+          croak ":alias cannot use existing pragma :$1 (reversed order?)";
+        alias_file ($1) and $promote = 1;
+        next;
       }
       alias_file ($alias);
       next;
@@ -636,26 +726,39 @@ sub import
   @args == 0 && $promote and @args = (":full");
   @h{@args} = (1) x @args;
 
-  $^H{charnames_full} = delete $h{':full'};
-  $^H{charnames_short} = delete $h{':short'};
-  $^H{charnames_scripts} = [map uc, keys %h];
+  $^H{charnames_full} = delete $h{':full'} || 0;  # Don't leave undefined,
+                                                  # as tested for in
+                                                  # lookup_names
+  $^H{charnames_short} = delete $h{':short'} || 0;
+  my @scripts = map uc, keys %h;
 
   ##
   ## If utf8? warnings are enabled, and some scripts were given,
-  ## see if at least we can find one letter of each script.
+  ## see if at least we can find one letter from each script.
   ##
-  if (warnings::enabled('utf8') && @{$^H{charnames_scripts}}) {
+  if (warnings::enabled('utf8') && @scripts) {
     $txt = do "unicore/Name.pl" unless $txt;
 
-    for my $script (@{$^H{charnames_scripts}}) {
-      if (not $txt =~ m/\t\t$script (?:CAPITAL |SMALL )?LETTER /) {
-       warnings::warn('utf8',  "No such script: '$script'");
+    for my $script (@scripts) {
+      if (not $txt =~ m/\t$script (?:CAPITAL |SMALL )?LETTER /) {
+        warnings::warn('utf8',  "No such script: '$script'");
+        $script = quotemeta $script;  # Escape it, for use in the re.
       }
     }
   }
+
+  # %^H gets stringified, so serialize it ourselves so can extract the
+  # real data back later.
+  $^H{charnames_stringified_ords} = join ",", %{$^H{charnames_ord_aliases}};
+  $^H{charnames_stringified_names} = join ",", %{$^H{charnames_name_aliases}};
+  $^H{charnames_stringified_inverse_ords} = join ",", %{$^H{charnames_inverse_ords}};
+  $^H{charnames_scripts} = join "|", @scripts;  # Stringifiy them as a trie
 } # import
 
-my %viacode;    # Cache of already-found codes
+# Cache of already looked-up values.  This is set to only contain
+# official values, and user aliases can't override them, so scoping is
+# not an issue.
+my %viacode;
 
 sub viacode {
 
@@ -672,42 +775,63 @@ sub viacode {
   # function _getcode(), but here it makes sure that even a hex argument
   # has the proper number of leading zeros, which is critical in
   # matching against $txt below
+  # Must check if decimal first; see comments at that definition
   my $hex;
   if ($arg =~ $decimal_qr) {
-    $hex = sprintf "%04X", $arg;
+    $hex = sprintf "%05X", $arg;
   } elsif ($arg =~ $hex_qr) {
     # Below is the line that differs from the _getcode() source
-    $hex = sprintf "%04X", hex $1;
+    $hex = sprintf "%05X", hex $1;
   } else {
     carp("unexpected arg \"$arg\" to charnames::viacode()");
     return;
   }
 
-  # checking the length first is slightly faster
-  if (length($hex) > 5 && CORE::hex($hex) > 0x10FFFF) {
-    carp "Unicode characters only allocated up to U+10FFFF (you asked for U+$hex)";
-    return;
-  }
-
   return $viacode{$hex} if exists $viacode{$hex};
 
-  $txt = do "unicore/Name.pl" unless $txt;
+  # If the code point is above the max in the table, there's no point
+  # looking through it.  Checking the length first is slightly faster
+  if (length($hex) <= 5 || CORE::hex($hex) <= 0x10FFFF) {
+    $txt = do "unicore/Name.pl" unless $txt;
+
+    # See if the name is algorithmically determinable.
+    my $algorithmic = code_point_to_name_special(CORE::hex $hex);
+    if (defined $algorithmic) {
+      $viacode{$hex} = $algorithmic;
+      return $algorithmic;
+    }
 
-  # Return the official name, if exists
-  if ($txt =~ m/^$hex\t\t(.+)/m) {
-    $viacode{$hex} = $1;
-    return $1;
+    # Return the official name, if exists.  It's unclear to me (khw) at
+    # this juncture if it is better to return a user-defined override, so
+    # leaving it as is for now.
+    if ($txt =~ m/^$hex\t/m) {
+
+        # The name starts with the next character and goes up to the
+        # next new-line.  Using capturing parentheses above instead of
+        # @+ more than doubles the execution time in Perl 5.13
+        $viacode{$hex} = substr($txt, $+[0], index($txt, "\n", $+[0]) - $+[0]);
+        return $viacode{$hex};
+    }
   }
 
   # See if there is a user name for it, before giving up completely.
-  return if ! exists $inverse_user_aliases{$hex};
+  # First get the scoped aliases, give up if have none.
+  my $H_ref = (caller(0))[10];
+  return if ! defined $H_ref
+            || ! exists $H_ref->{charnames_stringified_inverse_ords};
+
+  my %code_point_aliases = split ',',
+                          $H_ref->{charnames_stringified_inverse_ords};
+  if (! exists $code_point_aliases{$hex}) {
+    if (CORE::hex($hex) > 0x10FFFF) {
+        carp "Unicode characters only allocated up to U+10FFFF (you asked for U+$hex)";
+    }
+    return;
+  }
 
-  $viacode{$hex} = $inverse_user_aliases{$hex};
-  return $inverse_user_aliases{$hex};
+  return $code_point_aliases{$hex};
 } # viacode
 
-my %vianame;    # Cache of already-found names
-
 sub vianame
 {
   if (@_ != 1) {
@@ -723,16 +847,14 @@ sub vianame
   if ($arg =~ /^U\+([0-9a-fA-F]+)$/) {
 
     # khw claims that this is bad.  The function should return either a
-    # an ord or a chr for all inputs; not be bipolar.  Also, under 'use
-    # bytes', can create a chr above 255.
-    return chr CORE::hex $1;
-  }
-
-  if (! exists $vianame{$arg}) {
-    $vianame{$arg} = lookup_name($arg, 1); # 1 means run-time
+    # an ord or a chr for all inputs; not be bipolar.
+    my $ord = CORE::hex $1;
+    return chr $ord if $ord <= 255 || ! ((caller 0)[8] & $bytes::hint_bits);
+    carp not_legal_use_bytes_msg($arg, $ord);
+    return;
   }
 
-  return $vianame{$arg};
+  return lookup_name($arg, (caller(0))[10]);
 } # vianame
 
 
@@ -741,7 +863,7 @@ __END__
 
 =head1 NAME
 
-charnames - define character names for C<\N{named}> string literal escapes
+charnames - access to Unicode character names; define character names for C<\N{named}> string literal escapes
 
 =head1 SYNOPSIS
 
@@ -759,7 +881,7 @@ charnames - define character names for C<\N{named}> string literal escapes
     mychar => 0xE8000,  # Private use area
   };
   print "\N{e_ACUTE} is a small letter e with an acute.\n";
-  print "\\N{mychar} allows me to name and use private use characters.\n";
+  print "\\N{mychar} allows me to name private use characters.\n";
 
   use charnames ();
   print charnames::viacode(0x1234); # prints "ETHIOPIC SYLLABLE SEE"
@@ -768,47 +890,61 @@ charnames - define character names for C<\N{named}> string literal escapes
 
 =head1 DESCRIPTION
 
-Pragma C<use charnames> enables the use of C<\N{CHARNAME}> sequences to
-insert a Unicode character into a string based on its name.  (However,
-you don't need this pragma to use C<\N{U+...}> where the C<...> is a
-hexadecimal ordinal number.)
-
-The pragma supports arguments C<:full>, C<:short>, script names and
-customized aliases.  If C<:full> is present, for expansion of
-C<\N{CHARNAME}>, the string C<CHARNAME> is first looked up in the list of
+Pragma C<use charnames> is used to gain access to the names of the
+Unicode characters, and to allow you to define your own character names.
+
+All forms of the pragma enable use of the
+L</charnames::vianame(I<name>)> function for run-time lookup of a
+character name to get its ordinal (code point), and the inverse
+function, L</charnames::viacode(I<code>)>.
+
+Forms other than C<S<"use charnames ();">> enable the use of of
+C<\N{I<CHARNAME>}> sequences to compile a Unicode character into a
+string based on its name.
+
+Note that C<\N{U+I<...>}>, where the I<...> is a hexadecimal number,
+also inserts a character into a string, but doesn't require the use of
+this pragma.  The character it inserts is the one whose code point
+(ordinal value) is equal to the number.  For example, C<"\N{U+263a}"> is
+the Unicode (white background, black foreground) smiley face; it doesn't
+require this pragma, whereas the equivalent, C<"\N{WHITE SMILING FACE}">
+does.
+Also, C<\N{I<...>}> can mean a regex quantifier instead of a character
+name, when the I<...> is a number (or comma separated pair of numbers;
+see L<perlreref/QUANTIFIERS>), and is not related to this pragma.
+
+The C<charnames> pragma supports arguments C<:full>, C<:short>, script
+names and customized aliases.  If C<:full> is present, for expansion of
+C<\N{I<CHARNAME>}>, the string I<CHARNAME> is first looked up in the list of
 standard Unicode character names.  If C<:short> is present, and
-C<CHARNAME> has the form C<SCRIPT:CNAME>, then C<CNAME> is looked up
-as a letter in script C<SCRIPT>.  If pragma C<use charnames> is used
-with script name arguments, then for C<\N{CHARNAME}> the name
-C<CHARNAME> is looked up as a letter in the given scripts (in the
+I<CHARNAME> has the form C<I<SCRIPT>:I<CNAME>>, then I<CNAME> is looked up
+as a letter in script I<SCRIPT>.  If C<use charnames> is used
+with script name arguments, then for C<\N{I<CHARNAME>}> the name
+I<CHARNAME> is looked up as a letter in the given scripts (in the
 specified order). Customized aliases can override these, and are explained in
 L</CUSTOM ALIASES>.
 
-For lookup of C<CHARNAME> inside a given script C<SCRIPTNAME>
+For lookup of I<CHARNAME> inside a given script I<SCRIPTNAME>
 this pragma looks for the names
 
   SCRIPTNAME CAPITAL LETTER CHARNAME
   SCRIPTNAME SMALL LETTER CHARNAME
   SCRIPTNAME LETTER CHARNAME
 
-in the table of standard Unicode names.  If C<CHARNAME> is lowercase,
+in the table of standard Unicode names.  If I<CHARNAME> is lowercase,
 then the C<CAPITAL> variant is ignored, otherwise the C<SMALL> variant
 is ignored.
 
-Note that C<\N{...}> is compile-time, it's a special form of string
-constant used inside double-quoted strings: in other words, you cannot
+Note that C<\N{...}> is compile-time; it's a special form of string
+constant used inside double-quotish strings; this means that you cannot
 use variables inside the C<\N{...}>.  If you want similar run-time
-functionality, use charnames::vianame().
+functionality, use L<charnames::vianame()|/charnames::vianame(I<name>)>.
 
 For the C0 and C1 control characters (U+0000..U+001F, U+0080..U+009F)
-as of Unicode 3.1, there are no official Unicode names but you can use
-instead the ISO 6429 names (LINE FEED, ESCAPE, and so forth, and their
-abbreviations, LF, ESC, ...).  In
-Unicode 3.2 (as of Perl 5.8) some naming changes take place ISO 6429
-has been updated, see L</ALIASES>.
-
-Since the Unicode standard uses "U+HHHH", so can you: "\N{U+263a}"
-is the Unicode smiley face, or "\N{WHITE SMILING FACE}".
+there are no official Unicode names but you can use instead the ISO 6429
+names (LINE FEED, ESCAPE, and so forth, and their abbreviations, LF,
+ESC, ...).  In Unicode 3.2 (as of Perl 5.8) some naming changes took
+place, and ISO 6429 was updated, see L</ALIASES>.
 
 If the input name is unknown, C<\N{NAME}> raises a warning and
 substitutes the Unicode REPLACEMENT CHARACTER (U+FFFD).
@@ -817,6 +953,10 @@ It is a fatal error if C<use bytes> is in effect and the input name is
 that of a character that won't fit into a byte (i.e., whose ordinal is
 above 255).
 
+Otherwise, any string that includes a C<\N{I<charname>}> or
+C<S<\N{U+I<code point>}>> will automatically have Unicode semantics (see
+L<perlunicode/Byte and Character Semantics>).
+
 =head1 ALIASES
 
 A few aliases have been defined for convenience: instead of having
@@ -901,28 +1041,29 @@ will also give a warning about being deprecated.
 And finally, certain published variants are usable, including some for
 controls that have no Unicode names:
 
-    END OF PROTECTED AREA
-    HIGH OCTET PRESET
-    HOP
-    IND
-    INDEX
-    PAD
-    PADDING CHARACTER
-    PRIVATE USE 1
-    PRIVATE USE 2
-    SGC
-    SINGLE GRAPHIC CHARACTER INTRODUCER
-    SINGLE-SHIFT 2
-    SINGLE-SHIFT 3
-    START OF PROTECTED AREA
+    name                                   character
+
+    END OF PROTECTED AREA                  END OF GUARDED AREA, U+0097
+    HIGH OCTET PRESET                      U+0081
+    HOP                                    U+0081
+    IND                                    U+0084
+    INDEX                                  U+0084
+    PAD                                    U+0080
+    PADDING CHARACTER                      U+0080
+    PRIVATE USE 1                          PRIVATE USE ONE, U+0091
+    PRIVATE USE 2                          PRIVATE USE TWO, U+0092
+    SGC                                    U+0099
+    SINGLE GRAPHIC CHARACTER INTRODUCER    U+0099
+    SINGLE-SHIFT 2                         SINGLE SHIFT TWO, U+008E
+    SINGLE-SHIFT 3                         SINGLE SHIFT THREE, U+008F
+    START OF PROTECTED AREA                START OF GUARDED AREA, U+0096
 
 =head1 CUSTOM ALIASES
 
-This version of charnames supports three mechanisms of adding local
-or customized aliases to standard Unicode naming conventions (:full).
-The aliases override any standard definitions, so, if you're twisted
-enough, you can change C<"\N{LATIN CAPITAL LETTER A}"> to mean C<"B">,
-etc.
+You can add customized aliases to standard (C<:full>) Unicode naming
+conventions.  The aliases override any standard definitions, so, if
+you're twisted enough, you can change C<"\N{LATIN CAPITAL LETTER A}"> to
+mean C<"B">, etc.
 
 Note that an alias should not be something that is a legal curly
 brace-enclosed quantifier (see L<perlreref/QUANTIFIERS>).  For example
@@ -935,23 +1076,27 @@ Currently they must be ASCII.
 An alias can map to either an official Unicode character name or to a
 numeric code point (ordinal).  The latter is useful for assigning names
 to code points in Unicode private use areas such as U+E800 through
-U+F8FF.  The number must look like an unsigned decimal integer, or a
-hexadecimal constant beginning with C<0x>, or C<U+>.
+U+F8FF.
+A numeric code point must be a non-negative integer or a string beginning
+with C<"U+"> or C<"0x"> with the remainder considered to be a
+hexadecimal integer.  A literal numeric constant must be unsigned; it
+will be interpreted as hex if it has a leading zero or contains
+non-decimal hex digits; otherwise it will be interpreted as decimal.
 
-=head2 Anonymous hashes
+Aliases are added either by the use of anonymous hashes:
 
-    use charnames ":full", ":alias" => {
+    use charnames ":alias" => {
         e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
         mychar1 => 0xE8000,
         };
     my $str = "\N{e_ACUTE}";
 
-=head2 Alias file
+or by using a file containing aliases:
 
-    use charnames ":full", ":alias" => "pro";
+    use charnames ":alias" => "pro";
 
-    will try to read "unicore/pro_alias.pl" from the @INC path. This
-    file should return a list in plain perl:
+will try to read C<"unicore/pro_alias.pl"> from the C<@INC> path. This
+file should return a list in plain perl:
 
     (
     A_GRAVE         => "LATIN CAPITAL LETTER A WITH GRAVE",
@@ -961,50 +1106,72 @@ hexadecimal constant beginning with C<0x>, or C<U+>.
     A_BREVE         => "LATIN CAPITAL LETTER A WITH BREVE",
     A_RING          => "LATIN CAPITAL LETTER A WITH RING ABOVE",
     A_MACRON        => "LATIN CAPITAL LETTER A WITH MACRON",
-    mychar2         => U+E8001,
+    mychar2         => "U+E8001",
     );
 
-=head2 Alias shortcut
+Both these methods insert C<":full"> automatically as the first argument (if no
+other argument is given), and you can give the C<":full"> explicitly as
+well, like
 
-    use charnames ":alias" => ":pro";
-
-works exactly the same as the alias pairs, only this time,
-":full" is inserted automatically as the first argument (if no
-other argument is given).
+    use charnames ":full", ":alias" => "pro";
 
-=head1 charnames::viacode(code)
+=head1 charnames::viacode(I<code>)
 
 Returns the full name of the character indicated by the numeric code.
-The example
+For example,
 
     print charnames::viacode(0x2722);
 
 prints "FOUR TEARDROP-SPOKED ASTERISK".
 
-Returns undef if no name is known for the code.
-
 The name returned is the official name for the code point, if
 available, otherwise your custom alias for it.  This means that your
 alias will only be returned for code points that don't have an official
 Unicode name (nor Unicode version 1 name), such as private use code
 points, and the 4 control characters U+0080, U+0081, U+0084, and U+0099.
+If you define more than one name for the code point, it is indeterminate
+which one will be returned.
+
+The function returns C<undef> if no name is known for the code point.
+In Unicode the proper name of these is the empty string, which
+C<undef> stringifies to.  (If you ask for a code point past the legal
+Unicode maximum of U+10FFFF that you haven't assigned an alias to, you
+get C<undef> plus a warning.)
+
+The input number must be a non-negative integer or a string beginning
+with C<"U+"> or C<"0x"> with the remainder considered to be a
+hexadecimal integer.  A literal numeric constant must be unsigned; it
+will be interpreted as hex if it has a leading zero or contains
+non-decimal hex digits; otherwise it will be interpreted as decimal.
 
 Notice that the name returned for of U+FEFF is "ZERO WIDTH NO-BREAK
 SPACE", not "BYTE ORDER MARK".
 
-=head1 charnames::vianame(name)
+=head1 charnames::vianame(I<name>)
 
 Returns the code point indicated by the name.
-The example
+For example,
 
     printf "%04X", charnames::vianame("FOUR TEARDROP-SPOKED ASTERISK");
 
 prints "2722".
 
-Returns undef if the name is unknown.
+C<vianame> takes the identical inputs that C<\N{...}> does under the
+L<C<:full> option|/DESCRIPTION> to C<charnames>.  In addition, any other
+options for the controlling C<"use charnames"> in the same scope apply,
+like any L<script list, C<:short> option|/DESCRIPTION>, or L<custom
+aliases|/CUSTOM ALIASES> you may have defined.
+
+There are just a few differences.  The main one is that under
+most (see L</BUGS> for the others) circumstances, vianame returns
+an ord, whereas C<\\N{...}> is seamlessly placed as a chr into the
+string in which it appears.  This leads to a second difference.
+Since an ord is returned, it can be that of any character, even one
+that isn't legal under the C<S<use bytes>> pragma.
 
-This works only for the standard names, and does not yet apply
-to custom translators.
+The final difference is that if the input name is unknown C<vianame>
+returns C<undef> instead of the REPLACEMENT CHARACTER, and it does not
+raise a warning message.
 
 =head1 CUSTOM TRANSLATORS
 
@@ -1014,47 +1181,38 @@ translations (inside the scope which C<use>s the module) with the
 following magic incantation:
 
     sub import {
-       shift;
-       $^H{charnames} = \&translator;
+        shift;
+        $^H{charnames} = \&translator;
     }
 
-Here translator() is a subroutine which takes C<CHARNAME> as an
+Here translator() is a subroutine which takes I<CHARNAME> as an
 argument, and returns text to insert into the string instead of the
-C<\N{CHARNAME}> escape.  Since the text to insert should be different
+C<\N{I<CHARNAME>}> escape.  Since the text to insert should be different
 in C<bytes> mode and out of it, the function should check the current
 state of C<bytes>-flag as in:
 
-    use bytes ();                      # for $bytes::hint_bits
+    use bytes ();                      # for $bytes::hint_bits
     sub translator {
-       if ($^H & $bytes::hint_bits) {
-           return bytes_translator(@_);
-       }
-       else {
-           return utf8_translator(@_);
-       }
+        if ($^H & $bytes::hint_bits) {
+            return bytes_translator(@_);
+        }
+        else {
+            return utf8_translator(@_);
+        }
     }
 
-See L</CUSTOM ALIASES> above for restrictions on C<CHARNAME>.
+See L</CUSTOM ALIASES> above for restrictions on I<CHARNAME>.
 
-=head1 ILLEGAL CHARACTERS
-
-If you ask by name for a character that does not exist, a warning is given and
-the Unicode I<replacement character> "\x{FFFD}" is returned.
-
-If you ask by code (C<charnames::viacode()>) for a character that is
-unassigned, no warning is given and C<undef> is returned.  In Unicode
-the proper name of these is the empty string, which C<undef> stringifies
-to.  (If you ask for a code point past the legal Unicode maximum of
-U+10FFFF you do get C<undef> and a warning.)
+Of course, C<vianame> and C<viacode> would need to be overridden as
+well.
 
 =head1 BUGS
 
 vianame returns a chr if the input name is of the form C<U+...>, and an ord
 otherwise.  It is proposed to change this to always return an ord.  Send email
-to C<perl5-porters@perl.org> to comment on this proposal.
-
-None of the functions work on almost all the Hangul syllable and CJK Unicode
-characters that have their code points as part of their names.
+to C<perl5-porters@perl.org> to comment on this proposal.  If S<C<use
+bytes>> is in effect when a chr is returned, and if that chr won't fit
+into a byte, C<undef> is returned instead.
 
 Names must be ASCII characters only, which means that you are out of luck if
 you want to create aliases in a language where some or all the characters of
@@ -1065,9 +1223,12 @@ C<LATIN CAPITAL LETTER A WITH MACRON AND GRAVE>
 (which should mean C<LATIN CAPITAL LETTER A WITH MACRON> with an additional
 C<COMBINING GRAVE ACCENT>).
 
-Since evaluation of the translation function happens in the middle of
-compilation (of a string literal), the translation function should not
-do any C<eval>s or C<require>s.  This restriction should be lifted in
+Since evaluation of the translation function (see L</CUSTOM
+TRANSLATORS>) happens in the middle of compilation (of a string
+literal), the translation function should not do any C<eval>s or
+C<require>s.  This restriction should be lifted (but is low priority) in
 a future version of Perl.
 
 =cut
+
+# ex: set ts=8 sts=2 sw=2 et: