From d12b49d6e531fdc354980af14eb2efa34756c1d8 Mon Sep 17 00:00:00 2001 From: Father Chrysostomos Date: Sun, 17 Apr 2011 16:09:36 -0700 Subject: [PATCH] Make Unicode constants under use utf8 work again MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Because sub lookup (and glob lookup in general) ignores the UTF8 flag, such subs are actually ‘correctly’ stored under the utf8-encoded equivalent of the name, and not the name itself. --- MANIFEST | 1 + dist/constant/lib/constant.pm | 8 +++++++- dist/constant/t/utf8.t | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 dist/constant/t/utf8.t diff --git a/MANIFEST b/MANIFEST index c804dea..5855917 100644 --- a/MANIFEST +++ b/MANIFEST @@ -2869,6 +2869,7 @@ dist/B-Lint/t/lint.t See if B::Lint works dist/B-Lint/t/pluglib/B/Lint/Plugin/Test.pm See if B::Lint works dist/constant/lib/constant.pm For "use constant" dist/constant/t/constant.t See if compile-time constants work +dist/constant/t/utf8.t Test Unicode constants under utf8 pragma dist/Cwd/Cwd.pm Various cwd routines (getcwd, fastcwd, chdir) dist/Cwd/Cwd.xs Cwd extension external subroutines dist/Cwd/lib/File/Spec/Cygwin.pm portable operations on Cygwin file names diff --git a/dist/constant/lib/constant.pm b/dist/constant/lib/constant.pm index 3ee1a6f..22566ce 100644 --- a/dist/constant/lib/constant.pm +++ b/dist/constant/lib/constant.pm @@ -4,7 +4,7 @@ use strict; use warnings::register; use vars qw($VERSION %declared); -$VERSION = '1.20'; +$VERSION = '1.21'; #======================================================================= @@ -116,6 +116,12 @@ sub import { $declared{$full_name}++; if ($multiple || @_ == 1) { my $scalar = $multiple ? $constants->{$name} : $_[0]; + + # Work around perl bug #xxxxx: Sub names (actually glob + # names in general) ignore the UTF8 flag. So we have to + # turn it off to get the "right" symbol table entry. + utf8::is_utf8 $name and utf8::encode $name; + # The constant serves to optimise this entire block out on # 5.8 and earlier. if (_CAN_PCS && $symtab && !exists $symtab->{$name}) { diff --git a/dist/constant/t/utf8.t b/dist/constant/t/utf8.t new file mode 100644 index 0000000..c8830c3 --- /dev/null +++ b/dist/constant/t/utf8.t @@ -0,0 +1,12 @@ +#!./perl -T + +# Tests for constant.pm that require the utf8 pragma + +use utf8; +use Test::More tests => 2; + +use constant π => 4 * atan2 1, 1; + +ok defined π, 'basic scalar constant with funny name'; +is substr(π, 0, 7), '3.14159', ' in substr()'; + -- 2.7.4