This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[Fwd: [PATCH] Pod::Find should ignore SCM files and dirs]
[perl5.git] / lib / charnames.pm
index 6037ea8..4f7fdeb 100644 (file)
@@ -2,10 +2,11 @@ package charnames;
 use strict;
 use warnings;
 use Carp;
-our $VERSION = '1.02';
+use File::Spec;
+our $VERSION = '1.03';
 
 use bytes ();          # for $bytes::hint_bits
-$charnames::hint_bits = 0x20000;
+$charnames::hint_bits = 0x20000; # HINT_LOCALIZE_HH
 
 my %alias1 = (
                # Icky 3.2 names with parentheses.
@@ -52,9 +53,21 @@ sub alias (@)
 
 sub alias_file ($)
 {
-  my $arg = shift;
-  my $file = -f $arg ? $arg : "unicore/${arg}_alias.pl";
+  my ($arg, $file) = @_;
+  if (-f $arg && File::Spec->file_name_is_absolute ($arg)) {
+    $file = $arg;
+  }
+  elsif ($arg =~ m/^\w+$/) {
+    $file = "unicore/${arg}_alias.pl";
+  }
+  else {
+    croak "Charnames alias files can only have identifier characters";
+  }
   if (my @alias = do $file) {
+    @alias == 1 && !defined $alias[0] and
+      croak "$file cannot be used as alias file for charnames";
+    @alias % 2 and
+      croak "$file did not return a (valid) list of alias pairs";
     alias (@alias);
     return (1);
   }
@@ -94,7 +107,7 @@ sub charnames
     ## @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 the name exactly
+    ## If :full, look for the name exactly
     if ($^H{charnames_full} and $txt =~ /\t\t\Q$name\E$/m) {
       @off = ($-[0], $+[0]);
     }
@@ -178,18 +191,28 @@ sub import
   ##
   my ($promote, %h, @args) = (0);
   while (@_ and $_ = shift) {
-    if ($_ eq ":alias" && @_) {
+    if ($_ eq ":alias") {
+      @_ or
+       croak ":alias needs an argument in charnames";
       my $alias = shift;
       if (ref $alias) {
        ref $alias eq "HASH" or
-         die "Only HASH reference supported as argument to :alias";
+         croak "Only HASH reference supported as argument to :alias";
        alias ($alias);
        next;
       }
-      if ($alias =~ m{:(\w+)$} and $1 ne "full" && $1 ne "short") {
-       alias_file ($1) and $promote = 1, 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;
       }
-      alias_file ($alias) and next;
+      alias_file ($alias);
+      next;
+    }
+    if (m/^:/ and ! ($_ eq ":full" || $_ eq ":short")) {
+      warn "unsupported special '$_' in charnames";
+      next;
     }
     push @args, $_;
   }
@@ -215,7 +238,19 @@ sub import
   }
 } # import
 
-require Unicode::UCD; # for Unicode::UCD::_getcode()
+# this comes actually from Unicode::UCD, but it avoids the
+# overhead of loading it
+sub _getcode {
+    my $arg = shift;
+
+    if ($arg =~ /^[1-9]\d*$/) {
+       return $arg;
+    } elsif ($arg =~ /^(?:[Uu]\+|0[xX])?([[:xdigit:]]+)$/) {
+       return hex($1);
+    }
+
+    return;
+}
 
 my %viacode;
 
@@ -227,7 +262,7 @@ sub viacode
   }
 
   my $arg = shift;
-  my $code = Unicode::UCD::_getcode($arg);
+  my $code = _getcode($arg);
 
   my $hex;
 
@@ -310,9 +345,10 @@ charnames - define character names for C<\N{named}> string literal escapes
 
   use charnames ":full", ":alias" => {
     e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
-    };
+  };
   print "\N{e_ACUTE} is a small letter e with an acute.\n";
 
+  use charnames ();
   print charnames::viacode(0x1234); # prints "ETHIOPIC SYLLABLE SEE"
   printf "%04X", charnames::vianame("GOTHIC LETTER AHSA"); # prints "10330"
 
@@ -320,8 +356,8 @@ charnames - define character names for C<\N{named}> string literal escapes
 
 Pragma C<use charnames> supports arguments C<:full>, C<:short>, script
 names and customized aliases.  If C<:full> is present, for expansion of
-C<\N{CHARNAME}> string C<CHARNAME> is first looked in the list of
-standard Unicode names of chars.  If C<:short> is present, and
+C<\N{CHARNAME}>, the string C<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