<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package charnames;
use bytes ();		# for $bytes::hint_bits
use warnings();
$charnames::hint_bits = 0x20000;

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;

  my $hexlen = 4; # Unicode guarantees 4-, 5-, or 6-digit format
  $hexlen++ while
      $hexlen &lt; 6 &amp;&amp; substr($txt, $off[0] - $hexlen - 1, 1) =~ /[0-9a-f]/;
  my $ord = hex substr $txt, $off[0] - $hexlen, $hexlen;
  if ($^H &amp; $bytes::hint_bits) {	# "use bytes" in effect?
    use bytes;
    return chr $ord if $ord &lt;= 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";
  }
  return chr $ord;
}

sub import {
  shift;
  die "`use charnames' needs explicit imports list" unless @_;
  $^H |= $charnames::hint_bits;
  $^H{charnames} = \&amp;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];
  if (warnings::enabled('utf8') &amp;&amp; @{$^H{charnames_scripts}}) {
	$txt = do "unicode/Name.pl" unless $txt;
    for (@{$^H{charnames_scripts}}) {
        warnings::warn('utf8',  "No such script: '$_'") unless
	    $txt =~ m/\t\t$_ (?:CAPITAL |SMALL )?LETTER /;
	}
  }
}


1;
__END__

=head1 NAME

charnames - define character names for C&lt;\N{named}&gt; string literal escape.

=head1 SYNOPSIS

  use charnames ':full';
  print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";

  use charnames ':short';
  print "\N{greek:Sigma} is an upper-case sigma.\n";

  use charnames qw(cyrillic greek);
  print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";

=head1 DESCRIPTION

Pragma C&lt;use charnames&gt; supports arguments C&lt;:full&gt;, C&lt;:short&gt; and
script names.  If C&lt;:full&gt; is present, for expansion of
C&lt;\N{CHARNAME}}&gt; string C&lt;CHARNAME&gt; is first looked in the list of
standard Unicode names of chars.  If C&lt;:short&gt; is present, and
C&lt;CHARNAME&gt; has the form C&lt;SCRIPT:CNAME&gt;, then C&lt;CNAME&gt; is looked up
as a letter in script C&lt;SCRIPT&gt;.  If pragma C&lt;use charnames&gt; is used
with script name arguments, then for C&lt;\N{CHARNAME}}&gt; the name
C&lt;CHARNAME&gt; is looked up as a letter in the given scripts (in the
specified order).

For lookup of C&lt;CHARNAME&gt; inside a given script C&lt;SCRIPTNAME&gt;
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&lt;CHARNAME&gt; is lowercase,
then the C&lt;CAPITAL&gt; variant is ignored, otherwise the C&lt;SMALL&gt; variant is
ignored.

=head1 CUSTOM TRANSLATORS

The mechanism of translation of C&lt;\N{...}&gt; escapes is general and not
hardwired into F&lt;charnames.pm&gt;.  A module can install custom
translations (inside the scope which C&lt;use&gt;s the module) with the
following magic incantation:

    use charnames ();		# for $charnames::hint_bits
    sub import {
	shift;
	$^H |= $charnames::hint_bits;
	$^H{charnames} = \&amp;translator;
    }

Here translator() is a subroutine which takes C&lt;CHARNAME&gt; as an
argument, and returns text to insert into the string instead of the
C&lt;\N{CHARNAME}&gt; escape.  Since the text to insert should be different
in C&lt;bytes&gt; mode and out of it, the function should check the current
state of C&lt;bytes&gt;-flag as in:

    use bytes ();			# for $bytes::hint_bits
    sub translator {
	if ($^H &amp; $bytes::hint_bits) {
	    return bytes_translator(@_);
	}
	else {
	    return utf8_translator(@_);
	}
    }

=head1 BUGS

Since evaluation of the translation function happens in a middle of
compilation (of a string literal), the translation function should not
do any C&lt;eval&gt;s or C&lt;require&gt;s.  This restriction should be lifted in
a future version of Perl.

=cut
</pre></body></html>