In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/c0659f7383f7f1f3ba2523941a04e82c58af56b5?hp=dd90e537d39b34eadaee8ce4df4561bd428f0f9d>
- Log ----------------------------------------------------------------- commit c0659f7383f7f1f3ba2523941a04e82c58af56b5 Author: Karl Williamson <[email protected]> Date: Wed Nov 19 17:02:58 2014 -0700 perldelta for deprecating ${^ENCODING} M pod/perldelta.pod commit a3ee04bad130fd174875daab82a54d8e53d965c8 Author: Karl Williamson <[email protected]> Date: Wed Nov 19 16:49:53 2014 -0700 perlvar: More text about ${^ENCODING}; refer by perldiag M pod/perldiag.pod M pod/perlvar.pod commit 5eef447b12283016e9c57a31362edb829884a7ed Author: Karl Williamson <[email protected]> Date: Tue Nov 18 22:02:21 2014 -0700 toke.c: Consistently upgrade under encoding The documentation says that intermixing above-Latin1 code points with ones that would be otherwise encoded to something else, like Greek, causes the encoding to be foregone. Until this commit, this only happened when the above-latin1 code point came first in the string constant being scanned; meaning string-order was important. This changes things to match the documentation M cpan/Encode/t/encoding.t M toke.c commit d5b4785c1592d741608f5cff697058cc284db4eb Author: Dagfinn Ilmari Mannsåker <[email protected]> Date: Mon Nov 10 23:54:46 2014 +0000 Deprecate setting ${^ENCODING} The commiter added a no warnings in t/op/leaky-magic.t, and made other minor changes because of rebasing issues. M mg.c M pod/perldiag.pod M pod/perlvar.pod M t/lib/warnings/mg M t/op/leaky-magic.t M t/re/pat_re_eval.t M t/uni/chomp.t commit 050e2cbf5b64f9dc68666087f3ade51b110a3194 Author: Karl Williamson <[email protected]> Date: Tue Nov 18 17:23:54 2014 -0700 mg.c: White-space only Indent due to new blocks from previous commit M mg.c commit 3e669301f0a6fa34269f0e1eaf1fbbd72cae498a Author: Karl Williamson <[email protected]> Date: Tue Nov 18 17:03:03 2014 -0700 Make encoding pragma lexical in scope The encoding pragma is deprecated, but in the meantime it causes spooky action at a distance with other modules that it may be combined with. In these modules, operations such as chr(), ord(), and utf8::upgrade() will suddenly start doing the wrong thing. The documentation for 'encoding' has said to call it after loading other modules, but this may be impractical. This is especially bad with anything that auto-loads at first use, like \N{} does now for charnames. There is an issue with combining this with setting the variable ${^ENCODING} directly. The potential for conflicts has always been there, and remains. This commit introduces a shadow hidden variable, subservient to ${^ENCODING} (to preserve backwards compatibility) that has lexical scope validity. The pod for 'encoding' has been revamped to be more concise, clear, use more idiomatic English, and to speak from a modern perspective. M Porting/Maintainers.pl M cpan/Encode/encoding.pm M embedvar.h M gv.c M intrpvar.h M mg.c M perl.h M pod/perldelta.pod M pp_ctl.c M sv.c M t/porting/customized.dat commit ad2de1b2b22db677f46346aac18927b2032d7a68 Author: Karl Williamson <[email protected]> Date: Fri Nov 14 11:18:57 2014 -0700 Make a function to get PL_encoding's value This is in preparation for making the retrieval more complex in future commits than it is now. This is going into mg.c because the value is magical. M embed.fnc M embed.h M mg.c M pad.c M pp.c M pp_hot.c M proto.h M regcomp.c M sv.c M toke.c commit 47e13f24847018e7947377141de64798abfa1ce9 Author: Karl Williamson <[email protected]> Date: Tue Nov 11 22:25:37 2014 -0700 Make testing for PL_encoding into a macro This is in preparation for making the test more complicated. M pad.c M perl.h M pp.c M pp_hot.c M pp_sys.c M regcomp.c M sv.c M toke.c ----------------------------------------------------------------------- Summary of changes: Porting/Maintainers.pl | 1 + cpan/Encode/encoding.pm | 464 ++++++++++++++++++++++++----------------------- cpan/Encode/t/encoding.t | 9 +- embed.fnc | 2 + embed.h | 1 + embedvar.h | 1 + gv.c | 5 +- intrpvar.h | 3 +- mg.c | 88 ++++++++- pad.c | 6 +- perl.h | 4 + pod/perldelta.pod | 14 ++ pod/perldiag.pod | 5 + pod/perlvar.pod | 28 ++- pp.c | 22 +-- pp_ctl.c | 8 - pp_hot.c | 8 +- pp_sys.c | 2 +- proto.h | 3 + regcomp.c | 16 +- sv.c | 19 +- t/lib/warnings/mg | 13 ++ t/op/leaky-magic.t | 1 + t/porting/customized.dat | 1 + t/re/pat_re_eval.t | 2 + t/uni/chomp.t | 2 + toke.c | 34 ++-- 27 files changed, 463 insertions(+), 299 deletions(-) diff --git a/Porting/Maintainers.pl b/Porting/Maintainers.pl index b27f363..748ba73 100755 --- a/Porting/Maintainers.pl +++ b/Porting/Maintainers.pl @@ -392,6 +392,7 @@ use File::Glob qw(:case); 'Encode' => { 'DISTRIBUTION' => 'DANKOGAI/Encode-2.64.tar.gz', 'FILES' => q[cpan/Encode], + 'CUSTOMIZED' => [ qw( encoding.pm ) ], }, 'encoding::warnings' => { diff --git a/cpan/Encode/encoding.pm b/cpan/Encode/encoding.pm index c0bff08..8674cfa 100644 --- a/cpan/Encode/encoding.pm +++ b/cpan/Encode/encoding.pm @@ -131,11 +131,23 @@ sub import { $name = $enc->name; # canonize unless ( $arg{Filter} ) { DEBUG and warn "_exception($name) = ", _exception($name); - _exception($name) or ${^ENCODING} = $enc; + if (! _exception($name)) { + if (!$^V || $^V lt v5.21.7) { + ${^ENCODING} = $enc; + } + else { + # Starting with 5.21.7, this pragma uses a shadow variable + # designed explicitly for it, ${^E_NCODING}, to enforce + # lexical scope; instead of ${^ENCODING}. + $^H{'encoding'} = 1; + ${^E_NCODING} = $enc; + } + } $HAS_PERLIO or return 1; } else { defined( ${^ENCODING} ) and undef ${^ENCODING}; + undef ${^E_NCODING} if $^V && $^V ge v5.21.7; # implicitly 'use utf8' require utf8; # to fetch $utf8::hint_bits; @@ -185,6 +197,7 @@ sub import { sub unimport { no warnings; undef ${^ENCODING}; + undef ${^E_NCODING} if $^V && $^V ge v5.21.7; if ($HAS_PERLIO) { binmode( STDIN, ":raw" ); binmode( STDOUT, ":raw" ); @@ -205,20 +218,12 @@ __END__ =head1 NAME -encoding - allows you to write your script in non-ascii or non-utf8 +encoding - allows you to write your script in non-ASCII and non-UTF-8 =head1 WARNING -This module is deprecated under perl 5.18. It uses a mechanism provided by -perl that is deprecated under 5.18 and higher, and may be removed in a -future version. - -The easiest and the best alternative is to write your script in UTF-8 -and declear: - - use utf8; # not use encoding ':utf8'; - -See L<perluniintro> and L<utf8> for details. +This module has been deprecated since perl v5.18. See L</DESCRIPTION> and +L</BUGS>. =head1 SYNOPSIS @@ -235,68 +240,84 @@ See L<perluniintro> and L<utf8> for details. # A simple euc-cn => utf-8 converter use encoding "euc-cn", STDOUT => "utf8"; while(<>){print}; - # "no encoding;" supported (but not scoped!) + # "no encoding;" supported no encoding; # an alternate way, Filter use encoding "euc-jp", Filter=>1; # now you can use kanji identifiers -- in euc-jp! - # switch on locale - - # note that this probably means that unless you have a complete control - # over the environments the application is ever going to be run, you should - # NOT use the feature of encoding pragma allowing you to write your script - # in any recognized encoding because changing locale settings will wreck - # the script; you can of course still use the other features of the pragma. + # encode based on the current locale - specialized purposes only; + # fraught with danger!! use encoding ':locale'; -=head1 ABSTRACT +=head1 DESCRIPTION -Let's start with a bit of history: Perl 5.6.0 introduced Unicode -support. You could apply C<substr()> and regexes even to complex CJK -characters -- so long as the script was written in UTF-8. But back -then, text editors that supported UTF-8 were still rare and many users -instead chose to write scripts in legacy encodings, giving up a whole -new feature of Perl 5.6. +This pragma is used to enable a Perl script to be written in encodings that +aren't strictly ASCII nor UTF-8. It translates all or portions of the Perl +program script from a given encoding into UTF-8, and changes the PerlIO layers +of C<STDIN> and C<STDOUT> to the encoding specified. -Rewind to the future: starting from perl 5.8.0 with the B<encoding> -pragma, you can write your script in any encoding you like (so long -as the C<Encode> module supports it) and still enjoy Unicode support. -This pragma achieves that by doing the following: +This pragma dates from the days when UTF-8-enabled editors were uncommon. But +that was long ago, and the need for it is greatly diminished. That, coupled +with the fact that it doesn't work with threads, along with other problems, +(see L</BUGS>) have led to its being deprecated. It is planned to remove this +pragma in a future Perl version. New code should be written in UTF-8, and the +C<use utf8> pragma used instead (see L<perluniintro> and L<utf8> for details). +Old code should be converted to UTF-8, via something like the recipe in the +L</SYNOPSIS> (though this simple approach may require manual adjustments +afterwards). -=over +The only legitimate use of this pragma is almost certainly just one per file, +near the top, with file scope, as the file is likely going to only be written +in one encoding. Further restrictions apply in Perls before v5.22 (see +L</Prior to Perl v5.22>). -=item * +There are two basic modes of operation (plus turning if off): -Internally converts all literals (C<q//,qq//,qr//,qw///, qx//>) from -the encoding specified to utf8. In Perl 5.8.1 and later, literals in -C<tr///> and C<DATA> pseudo-filehandle are also converted. +=over 4 -=item * +=item C<use encoding ['I<ENCNAME>'] ;> -Changing PerlIO layers of C<STDIN> and C<STDOUT> to the encoding - specified. +This is the normal operation. It translates various literals encountered in +the Perl source file from the encoding I<ENCNAME> into UTF-8, and similarly +converts character code points. This is used when the script is a combination +of ASCII (for the variable names and punctuation, I<etc>), but the literal +data is in the specified encoding. -=back +I<ENCNAME> is optional. If omitted, the encoding specified in the environment +variable L<C<PERL_ENCODING>|perlrun/PERL_ENCODING> is used. If this isn't +set, or the resolved-to encoding is not known to C<L<Encode>>, the error +C<Unknown encoding 'I<ENCNAME>'> will be thrown. -=head2 Literal Conversions +Starting in Perl v5.8.6 (C<Encode> version 2.0.1), I<ENCNAME> may be the +name C<:locale>. This is for very specialized applications, and is documented +in L</The C<:locale> sub-pragma> below. -You can write code in EUC-JP as follows: +The literals that are converted are C<q//, qq//, qr//, qw///, qx//>, and +starting in v5.8.1, C<tr///>. Operations that do conversions include C<chr>, +C<ord>, C<utf8::upgrade> (but not C<utf8::downgrade>), and C<chomp>. + +Also starting in v5.8.1, the C<DATA> pseudo-filehandle is translated from the +encoding into UTF-8. + +For example, you can write code in EUC-JP as follows: my $Rakuda = "\xF1\xD1\xF1\xCC"; # Camel in Kanji #<-char-><-char-> # 4 octets s/\bCamel\b/$Rakuda/; And with C<use encoding "euc-jp"> in effect, it is the same thing as -the code in UTF-8: +that code in UTF-8: my $Rakuda = "\x{99F1}\x{99DD}"; # two Unicode Characters s/\bCamel\b/$Rakuda/; -=head2 PerlIO layers for C<STD(IN|OUT)> +See L</EXAMPLE> below for a more complete example. -The B<encoding> pragma also modifies the filehandle layers of -STDIN and STDOUT to the specified encoding. Therefore, +Unless C<${^UNICODE}> (available starting in v5.8.2) exists and is non-zero, the +PerlIO layers of C<STDIN> and C<STDOUT> are set to "C<:encoding(I<ENCNAME>)>". +Therefore, use encoding "euc-jp"; my $message = "Camel is the symbol of perl.\n"; @@ -304,183 +325,145 @@ STDIN and STDOUT to the specified encoding. Therefore, $message =~ s/\bCamel\b/$Rakuda/; print $message; -Will print "\xF1\xD1\xF1\xCC is the symbol of perl.\n", -not "\x{99F1}\x{99DD} is the symbol of perl.\n". - -You can override this by giving extra arguments; see below. - -=head2 Implicit upgrading for byte strings - -By default, if strings operating under byte semantics and strings -with Unicode character data are concatenated, the new string will -be created by decoding the byte strings as I<ISO 8859-1 (Latin-1)>. - -The B<encoding> pragma changes this to use the specified encoding -instead. For example: +will print - use encoding 'utf8'; - my $string = chr(20000); # a Unicode string - utf8::encode($string); # now it's a UTF-8 encoded byte string - # concatenate with another Unicode string - print length($string . chr(20000)); + "\xF1\xD1\xF1\xCC is the symbol of perl.\n" -Will print C<2>, because C<$string> is upgraded as UTF-8. Without -C<use encoding 'utf8';>, it will print C<4> instead, since C<$string> -is three octets when interpreted as Latin-1. +not -=head2 Side effects + "\x{99F1}\x{99DD} is the symbol of perl.\n" -If the C<encoding> pragma is in scope then the lengths returned are -calculated from the length of C<$/> in Unicode characters, which is not -always the same as the length of C<$/> in the native encoding. +You can override this by giving extra arguments; see below. -This pragma affects utf8::upgrade, but not utf8::downgrade. +Note that C<STDERR> WILL NOT be changed, regardless. -=head1 FEATURES THAT REQUIRE 5.8.1 +Also note that non-STD file handles remain unaffected. Use C<use +open> or C<binmode> to change the layers of those. -Some of the features offered by this pragma requires perl 5.8.1. Most -of these are done by Inaba Hiroto. Any other features and changes -are good for 5.8.0. +=item C<use encoding I<ENCNAME> Filter=E<gt>1;> -=over +This operates as above, but the C<Filter> argument with a non-zero +value causes the entire script, and not just literals, to be translated from +the encoding into UTF-8. This allows identifiers in the source to be in that +encoding as well. (Problems may occur if the encoding is not a superset of +ASCII; imagine all your semi-colons being translated into something +different.) One can use this form to make -=item "NON-EUC" doublebyte encodings + ${"\x{4eba}"}++ -Because perl needs to parse script before applying this pragma, such -encodings as Shift_JIS and Big-5 that may contain '\' (BACKSLASH; -\x5c) in the second byte fails because the second byte may -accidentally escape the quoting character that follows. Perl 5.8.1 -or later fixes this problem. +work. (This is equivalent to C<$I<human>++>, where I<human> is a single Han +ideograph). -=item tr// +This effectively means that your source code behaves as if it were written in +UTF-8 with C<'use utf8>' in effect. So even if your editor only supports +Shift_JIS, for example, you can still try examples in Chapter 15 of +C<Programming Perl, 3rd Ed.>. -C<tr//> was overlooked by Perl 5 porters when they released perl 5.8.0 -See the section below for details. +This option is significantly slower than the other one. -=item DATA pseudo-filehandle +=item C<no encoding;> -Another feature that was overlooked was C<DATA>. +Unsets the script encoding. The layers of C<STDIN>, C<STDOUT> are +reset to "C<:raw>" (the default unprocessed raw stream of bytes). =back -=head1 USAGE +=head1 OPTIONS -=over 4 +=head2 Setting C<STDIN> and/or C<STDOUT> individually -=item use encoding [I<ENCNAME>] ; +The encodings of C<STDIN> and C<STDOUT> are individually settable by parameters to +the pragma: -Sets the script encoding to I<ENCNAME>. And unless ${^UNICODE} -exists and non-zero, PerlIO layers of STDIN and STDOUT are set to -":encoding(I<ENCNAME>)". + use encoding 'euc-tw', STDIN => 'greek' ...; -Note that STDERR WILL NOT be changed. +In this case, you cannot omit the first I<ENCNAME>. C<< STDIN => undef >> +turns the I/O transcoding completely off for that filehandle. -Also note that non-STD file handles remain unaffected. Use C<use -open> or C<binmode> to change layers of those. +When C<${^UNICODE}> (available starting in v5.8.2) exists and is non-zero, +these options will be completely ignored. See L<perlvar/C<${^UNICODE}>> and +L<"C<-C>" in perlrun|perlrun/-C [numberE<sol>list]> for details. -If no encoding is specified, the environment variable L<PERL_ENCODING> -is consulted. If no encoding can be found, the error C<Unknown encoding -'I<ENCNAME>'> will be thrown. +=head2 The C<:locale> sub-pragma -=item use encoding I<ENCNAME> [ STDIN =E<gt> I<ENCNAME_IN> ...] ; +Starting in v5.8.6, the encoding name may be C<:locale>. This means that the +encoding is taken from the current locale, and not hard-coded by the pragma. +Since a script really can only be encoded in exactly one encoding, this option +is dangerous. It makes sense only if the script itself is written in ASCII, +and all the possible locales that will be in use when the script is executed +are supersets of ASCII. That means that the script itself doesn't get +changed, but the I/O handles have the specified encoding added, and the +operations like C<chr> and C<ord> use that encoding. -You can also individually set encodings of STDIN and STDOUT via the -C<< STDIN => I<ENCNAME> >> form. In this case, you cannot omit the -first I<ENCNAME>. C<< STDIN => undef >> turns the IO transcoding -completely off. +The logic of finding which locale C<:locale> uses is as follows: -When ${^UNICODE} exists and non-zero, these options will completely -ignored. ${^UNICODE} is a variable introduced in perl 5.8.1. See -L<perlrun> see L<perlvar/"${^UNICODE}"> and L<perlrun/"-C"> for -details (perl 5.8.1 and later). +=over 4 -=item use encoding I<ENCNAME> Filter=E<gt>1; +=item 1. -This turns the encoding pragma into a source filter. While the -default approach just decodes interpolated literals (in qq() and -qr()), this will apply a source filter to the entire source code. See -L</"The Filter Option"> below for details. +If the platform supports the C<langinfo(CODESET)> interface, the codeset +returned is used as the default encoding for the open pragma. -=item no encoding; +=item 2. -Unsets the script encoding. The layers of STDIN, STDOUT are -reset to ":raw" (the default unprocessed raw stream of bytes). +If 1. didn't work but we are under the locale pragma, the environment +variables C<LC_ALL> and C<LANG> (in that order) are matched for encodings +(the part after "C<.>", if any), and if any found, that is used +as the default encoding for the open pragma. -=back +=item 3. -=head1 The Filter Option +If 1. and 2. didn't work, the environment variables C<LC_ALL> and C<LANG> +(in that order) are matched for anything looking like UTF-8, and if +any found, C<:utf8> is used as the default encoding for the open +pragma. -The magic of C<use encoding> is not applied to the names of -identifiers. In order to make C<${"\x{4eba}"}++> ($human++, where human -is a single Han ideograph) work, you still need to write your script -in UTF-8 -- or use a source filter. That's what 'Filter=>1' does. +=back -What does this mean? Your source code behaves as if it is written in -UTF-8 with 'use utf8' in effect. So even if your editor only supports -Shift_JIS, for example, you can still try examples in Chapter 15 of -C<Programming Perl, 3rd Ed.>. For instance, you can use UTF-8 -identifiers. +If your locale environment variables (C<LC_ALL>, C<LC_CTYPE>, C<LANG>) +contain the strings 'UTF-8' or 'UTF8' (case-insensitive matching), +the default encoding of your C<STDIN>, C<STDOUT>, and C<STDERR>, and of +B<any subsequent file open>, is UTF-8. -This option is significantly slower and (as of this writing) non-ASCII -identifiers are not very stable WITHOUT this option and with the -source code written in UTF-8. +=head1 CAVEATS -=head2 Filter-related changes at Encode version 1.87 +=head2 SIDE EFFECTS =over =item * -The Filter option now sets STDIN and STDOUT like non-filter options. -And C<< STDIN=>I<ENCODING> >> and C<< STDOUT=>I<ENCODING> >> work like -non-filter version. +If the C<encoding> pragma is in scope then the lengths returned are +calculated from the length of C<$/> in Unicode characters, which is not +always the same as the length of C<$/> in the native encoding. =item * -C<use utf8> is implicitly declared so you no longer have to C<use -utf8> to C<${"\x{4eba}"}++>. - -=back - -=head1 CAVEATS - -=head2 NOT SCOPED - -The pragma is a per script, not a per block lexical. Only the last -C<use encoding> or C<no encoding> matters, and it affects -B<the whole script>. However, the <no encoding> pragma is supported and -B<use encoding> can appear as many times as you want in a given script. -The multiple use of this pragma is discouraged. - -By the same reason, the use this pragma inside modules is also -discouraged (though not as strongly discouraged as the case above. -See below). - -If you still have to write a module with this pragma, be very careful -of the load order. See the codes below; +Without this pragma, if strings operating under byte semantics and strings +with Unicode character data are concatenated, the new string will +be created by decoding the byte strings as I<ISO 8859-1 (Latin-1)>. - # called module - package Module_IN_BAR; - use encoding "bar"; - # stuff in "bar" encoding here - 1; +The B<encoding> pragma changes this to use the specified encoding +instead. For example: - # caller script - use encoding "foo" - use Module_IN_BAR; - # surprise! use encoding "bar" is in effect. + use encoding 'utf8'; + my $string = chr(20000); # a Unicode string + utf8::encode($string); # now it's a UTF-8 encoded byte string + # concatenate with another Unicode string + print length($string . chr(20000)); -The best way to avoid this oddity is to use this pragma RIGHT AFTER -other modules are loaded. i.e. +Will print C<2>, because C<$string> is upgraded as UTF-8. Without +C<use encoding 'utf8';>, it will print C<4> instead, since C<$string> +is three octets when interpreted as Latin-1. - use Module_IN_BAR; - use encoding "foo"; +=back =head2 DO NOT MIX MULTIPLE ENCODINGS Notice that only literals (string or regular expression) having only legacy code points are affected: if you mix data like this + \x{100}\xDF \xDF\x{100} the data is assumed to be in (Latin 1 and) Unicode, not in your native @@ -509,10 +492,70 @@ resort to \x{....} just to spell your name in a native encoding. So feel free to put your strings in your encoding in quotes and regexes. -=head2 tr/// with ranges +=head2 Prior to Perl v5.22 + +The pragma was a per script, not a per block lexical. Only the last +C<use encoding> or C<no encoding> mattered, and it affected +B<the whole script>. However, the C<no encoding> pragma was supported and +C<use encoding> could appear as many times as you want in a given script +(though only the last was effective). + +Since the scope wasn't lexical, other modules' use of C<chr>, C<ord>, I<etc.> +were affected. This leads to spooky, incorrect action at a distance that is +hard to debug. + +This means you would have to be very careful of the load order: + + # called module + package Module_IN_BAR; + use encoding "bar"; + # stuff in "bar" encoding here + 1; + + # caller script + use encoding "foo" + use Module_IN_BAR; + # surprise! use encoding "bar" is in effect. + +The best way to avoid this oddity is to use this pragma RIGHT AFTER +other modules are loaded. i.e. + + use Module_IN_BAR; + use encoding "foo"; + +=head2 Prior to Encode version 1.87 + +=over + +=item * + +C<STDIN> and C<STDOUT> were not set under the filter option. +And C<< STDIN=>I<ENCODING> >> and C<< STDOUT=>I<ENCODING> >> didn't work like +non-filter version. + +=item * + +C<use utf8> wasn't implicitly declared so you have to C<use utf8> to do + + ${"\x{4eba}"}++ + +=back + +=head2 Prior to Perl v5.8.1 + +=over + +=item "NON-EUC" doublebyte encodings + +Because perl needs to parse the script before applying this pragma, such +encodings as Shift_JIS and Big-5 that may contain C<'\'> (BACKSLASH; +C<\x5c>) in the second byte fail because the second byte may +accidentally escape the quoting character that follows. + +=item C<tr///> The B<encoding> pragma works by decoding string literals in -C<q//,qq//,qr//,qw///, qx//> and so forth. In perl 5.8.0, this +C<q//,qq//,qr//,qw///, qx//> and so forth. In perl v5.8.0, this does not apply to C<tr///>. Therefore, use encoding 'euc-jp'; @@ -537,25 +580,21 @@ Does not work as =back -This counterintuitive behavior has been fixed in perl 5.8.1. +This counterintuitive behavior has been fixed in perl v5.8.1. -=head3 workaround to tr///; - -In perl 5.8.0, you can work around as follows; +In perl v5.8.0, you can work around this as follows; use encoding 'euc-jp'; # .... eval qq{ \$kana =~ tr/\xA4\xA1-\xA4\xF3/\xA5\xA1-\xA5\xF3/ }; Note the C<tr//> expression is surrounded by C<qq{}>. The idea behind -is the same as classic idiom that makes C<tr///> 'interpolate'. +this is the same as the classic idiom that makes C<tr///> 'interpolate': tr/$from/$to/; # wrong! eval qq{ tr/$from/$to/ }; # workaround. -Nevertheless, in case of B<encoding> pragma even C<q//> is affected so -C<tr///> not being decoded was obviously against the will of Perl5 -Porters so it has been fixed in Perl 5.8.1 or later. +=back =head1 EXAMPLE - Greekperl @@ -590,10 +629,24 @@ Porters so it has been fixed in Perl 5.8.1 or later. print "zetta\n" if unpack("C", (pack("C", 0xdf))) == 0xdf; -=head1 KNOWN PROBLEMS +=head1 BUGS =over +=item Thread safety + +C<use encoding ...> is not thread-safe (i.e., do not use in threaded +applications). + +=item Can't be used by more than one module in a single program. + +Only one encoding is allowed. If you combine modules in a program that have +different encodings, only one will be actually used. + +=item Other modules using C<STDIN> and C<STDOUT> get the encoded stream + +They may be expecting something completely different. + =item literals in regex that are longer than 127 bytes For native multibyte encodings (either fixed or variable length), @@ -603,13 +656,11 @@ recoding errors for regular expression literals longer than 127 bytes. =item EBCDIC The encoding pragma is not supported on EBCDIC platforms. -(Porters who are willing and able to remove this limitation are -welcome.) -=item format +=item C<format> -This pragma doesn't work well with format because PerlIO does not -get along very well with it. When format contains non-ascii +This pragma doesn't work well with C<format> because PerlIO does not +get along very well with it. When C<format> contains non-ASCII characters it prints funny or gets "wide character warnings". To understand it, try the code below. @@ -628,56 +679,19 @@ To understand it, try the code below. Without binmode this happens to work but without binmode, print() fails instead of write(). -At any rate, the very use of format is questionable when it comes to +At any rate, the very use of C<format> is questionable when it comes to unicode characters since you have to consider such things as character width (i.e. double-width for ideographs) and directions (i.e. BIDI for Arabic and Hebrew). -=item Thread safety - -C<use encoding ...> is not thread-safe (i.e., do not use in threaded -applications). +=item See also L</CAVEATS> =back -=head2 The Logic of :locale - -The logic of C<:locale> is as follows: - -=over 4 - -=item 1. - -If the platform supports the langinfo(CODESET) interface, the codeset -returned is used as the default encoding for the open pragma. - -=item 2. - -If 1. didn't work but we are under the locale pragma, the environment -variables LC_ALL and LANG (in that order) are matched for encodings -(the part after C<.>, if any), and if any found, that is used -as the default encoding for the open pragma. - -=item 3. - -If 1. and 2. didn't work, the environment variables LC_ALL and LANG -(in that order) are matched for anything looking like UTF-8, and if -any found, C<:utf8> is used as the default encoding for the open -pragma. - -=back - -If your locale environment variables (LC_ALL, LC_CTYPE, LANG) -contain the strings 'UTF-8' or 'UTF8' (case-insensitive matching), -the default encoding of your STDIN, STDOUT, and STDERR, and of -B<any subsequent file open>, is UTF-8. - =head1 HISTORY -This pragma first appeared in Perl 5.8.0. For features that require -5.8.1 and better, see above. - -The C<:locale> subpragma was implemented in 2.01, or Perl 5.8.6. +This pragma first appeared in Perl v5.8.0. It has been enhanced in later +releases as specified above. =head1 SEE ALSO diff --git a/cpan/Encode/t/encoding.t b/cpan/Encode/t/encoding.t index 8c7f253..847c26f 100644 --- a/cpan/Encode/t/encoding.t +++ b/cpan/Encode/t/encoding.t @@ -14,7 +14,7 @@ BEGIN { } } -print "1..31\n"; +print "1..33\n"; no warnings "deprecated"; use encoding "latin1"; # ignored (overwritten by the next line) @@ -202,3 +202,10 @@ print "ok 28\n"; print $h1{"\x{3af}"} == 41 ? "ok 30\n" : "not ok 30\n"; print $h2{"\xdf"} == 42 ? "ok 31\n" : "not ok 31\n"; } + +# Order of finding the above-Latin1 code point should not matter: both should +# assume Latin1/Unicode encoding +print "not " if "\xDF\x{100}" =~ /\x{3af}\x{100}/; +print "ok 32\n"; +print "not " if "\x{100}\xDF" =~ /\x{100}\x{3af}/; +print "ok 33\n"; diff --git a/embed.fnc b/embed.fnc index 77ceca8..23f8e47 100644 --- a/embed.fnc +++ b/embed.fnc @@ -2774,4 +2774,6 @@ Ei |STRLEN |sv_or_pv_pos_u2b|NN SV *sv|NN const char *pv|STRLEN pos \ |NULLOK STRLEN *lenp #endif +EMpPX |SV* |_get_encoding + : ex: set ts=8 sts=4 sw=4 noet: diff --git a/embed.h b/embed.h index 7fddb5d..b9d6d8d 100644 --- a/embed.h +++ b/embed.h @@ -889,6 +889,7 @@ #define do_spawn_nowait(a) Perl_do_spawn_nowait(aTHX_ a) #endif #if defined(PERL_CORE) || defined(PERL_EXT) +#define _get_encoding() Perl__get_encoding(aTHX) #define av_reify(a) Perl_av_reify(aTHX_ a) #define current_re_engine() Perl_current_re_engine(aTHX) #define cv_ckproto_len_flags(a,b,c,d,e) Perl_cv_ckproto_len_flags(aTHX_ a,b,c,d,e) diff --git a/embedvar.h b/embedvar.h index 60c897b..2a3ebdc 100644 --- a/embedvar.h +++ b/embedvar.h @@ -179,6 +179,7 @@ #define PL_lastgotoprobe (vTHX->Ilastgotoprobe) #define PL_laststatval (vTHX->Ilaststatval) #define PL_laststype (vTHX->Ilaststype) +#define PL_lex_encoding (vTHX->Ilex_encoding) #define PL_localizing (vTHX->Ilocalizing) #define PL_localpatches (vTHX->Ilocalpatches) #define PL_lockhook (vTHX->Ilockhook) diff --git a/gv.c b/gv.c index eaf9d21..4f072d7 100644 --- a/gv.c +++ b/gv.c @@ -1846,7 +1846,7 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, } else #endif { - const char * const name2 = name + 1; + const char * name2 = name + 1; switch (*name) { case 'A': if (strEQ(name2, "RGV")) { @@ -1905,6 +1905,9 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, goto magicalize; break; case '\005': /* $^ENCODING */ + if (*name2 == '_') { + name2++; + } if (strEQ(name2, "NCODING")) goto magicalize; break; diff --git a/intrpvar.h b/intrpvar.h index df8ce8b..56bb5c4 100644 --- a/intrpvar.h +++ b/intrpvar.h @@ -672,7 +672,8 @@ PERLVARI(I, known_layers, PerlIO_list_t *, NULL) PERLVARI(I, def_layerlist, PerlIO_list_t *, NULL) #endif -PERLVARI(I, encoding, SV *, NULL) /* character encoding */ +PERLVARI(I, encoding, SV *, NULL) /* $^ENCODING */ +PERLVARI(I, lex_encoding, SV *, NULL) /* encoding pragma */ PERLVAR(I, utf8_idstart, SV *) PERLVAR(I, utf8_idcont, SV *) diff --git a/mg.c b/mg.c index c49b146..679b309 100644 --- a/mg.c +++ b/mg.c @@ -767,6 +767,45 @@ S_fixup_errno_string(pTHX_ SV* sv) } } +SV* +Perl__get_encoding(pTHX) +{ + /* For core Perl use only: Returns the $^ENCODING or 'use encoding' in + * effect; NULL if none. + * + * $^ENCODING maps to PL_encoding, and is the old way to do things, and is + * retained for backwards compatibility. Now, there is a shadow variable + * ${^E_NCODING} set only by the encoding pragma, used to give this pragma + * lexical scope, unlike the global scope it (shudder) used to have. This + * variable maps to PL_lex_encoding. Again for backwards compatibility, + * PL_encoding has precedence over PL_lex_encoding. The hints hash is used + * to determine if PL_lex_encoding is in scope, and hence valid. The hints + * hash only accepts simple values, so we can't put an Encode object into + * it, so we put the object into the global, and put a simple boolean into + * the hints hash giving whether the global is valid or not */ + + SV *is_encoding; + + if (PL_encoding) { + return PL_encoding; + } + + if (! PL_lex_encoding) { + return NULL; + } + + is_encoding = cop_hints_fetch_pvs(PL_curcop, "encoding", 0); + if ( is_encoding + && is_encoding != &PL_sv_placeholder + && SvIOK(is_encoding) + && SvIV(is_encoding)) /* non-zero mean valid */ + { + return PL_lex_encoding; + } + + return NULL; +} + #ifdef VMS #include <descrip.h> #include <starlet.h> @@ -816,8 +855,11 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg) break; case '\005': /* ^E */ if (nextchar != '\0') { + /* We shouldn't be trying to retrieve this shadow variable */ + assert(strNE(remaining, "_NCODING")); + if (strEQ(remaining, "NCODING")) - sv_setsv(sv, PL_encoding); + sv_setsv(sv, _get_encoding()); break; } @@ -2601,15 +2643,41 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg) # endif #endif } - else if (strEQ(mg->mg_ptr+1, "NCODING")) { - SvREFCNT_dec(PL_encoding); - if (SvOK(sv) || SvGMAGICAL(sv)) { - PL_encoding = newSVsv(sv); - } - else { - PL_encoding = NULL; - } - } + else { + unsigned int offset = 1; + bool lex = FALSE; + + /* It may be the shadow variable ${E_NCODING} which has lexical + * scope. See comments at Perl__get_encoding in this file */ + if (*(mg->mg_ptr + 1) == '_') { + lex = TRUE; + offset++; + } + if (strEQ(mg->mg_ptr + offset, "NCODING")) { + if (lex) { /* Use the shadow global */ + SvREFCNT_dec(PL_lex_encoding); + if (SvOK(sv) || SvGMAGICAL(sv)) { + PL_lex_encoding = newSVsv(sv); + } + else { + PL_lex_encoding = NULL; + } + } + else { /* Use the regular global */ + SvREFCNT_dec(PL_encoding); + if (SvOK(sv) || SvGMAGICAL(sv)) { + if (PL_localizing != 2) { + Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), + "Setting ${^ENCODING} is deprecated"); + } + PL_encoding = newSVsv(sv); + } + else { + PL_encoding = NULL; + } + } + } + } break; case '\006': /* ^F */ PL_maxsysfd = SvIV(sv); diff --git a/pad.c b/pad.c index c9e16e5..e5fa3b4 100644 --- a/pad.c +++ b/pad.c @@ -167,16 +167,16 @@ sv_eq_pvn_flags(pTHX_ const SV *sv, const char* pv, const STRLEN pvlen, const U3 STRLEN cur1 = SvCUR(sv); const char *pv2 = pv; STRLEN cur2 = pvlen; - if (PL_encoding) { + if (IN_ENCODING) { SV* svrecode = NULL; if (SvUTF8(sv)) { svrecode = newSVpvn(pv2, cur2); - sv_recode_to_utf8(svrecode, PL_encoding); + sv_recode_to_utf8(svrecode, _get_encoding()); pv2 = SvPV_const(svrecode, cur2); } else { svrecode = newSVpvn(pv1, cur1); - sv_recode_to_utf8(svrecode, PL_encoding); + sv_recode_to_utf8(svrecode, _get_encoding()); pv1 = SvPV_const(svrecode, cur1); } SvREFCNT_dec_NN(svrecode); diff --git a/perl.h b/perl.h index 580ad6a..4344b76 100644 --- a/perl.h +++ b/perl.h @@ -767,6 +767,10 @@ # endif #endif /* !NO_LOCALE && HAS_SETLOCALE */ +/* Is $^ENCODING set, or are we under the encoding pragma? */ +#define IN_ENCODING UNLIKELY(PL_encoding \ + || (PL_lex_encoding && _get_encoding() != NULL)) + #include <setjmp.h> #ifdef I_SYS_PARAM diff --git a/pod/perldelta.pod b/pod/perldelta.pod index cb4e09f..3d16f8d 100644 --- a/pod/perldelta.pod +++ b/pod/perldelta.pod @@ -78,6 +78,14 @@ as an updated module in the L</Modules and Pragmata> section. [ List each other deprecation as a =head2 entry ] +=head2 Setting C<${^ENCODING}> to anything but C<undef> + +This variable allows Perl scripts to be written in a non-ASCII, +non-UTF-8 encoding. However, it affects all modules globally, leading +to wrong answers and segmentation faults. New scripts should be written +in UTF-8; old scripts should be converted to UTF-8, which is easily done +with the L<encoding> pragma. + =head1 Performance Enhancements XXX Changes which enhance performance without changing behaviour go here. @@ -340,6 +348,12 @@ files in F<ext/> and F<lib/> are best summarized in L</Modules and Pragmata>. XXX +=item * + +The L<encoding> pragma's effect is now limited to lexical scope. This +pragma is deprecated, but in the meantime, it could adversely affect +unrelated modules that are included in the same program. + =back =head1 Known Problems diff --git a/pod/perldiag.pod b/pod/perldiag.pod index d9dd692..67e39dc 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -5243,6 +5243,11 @@ a positive integer, where the integer was the address of the reference. As of Perl 5.20.0 this is a fatal error, to allow future versions of Perl to use non-integer refs for more interesting purposes. +=item Setting ${^ENCODING} is deprecated + +(D deprecated) You assiged a non-C<undef> value to C<${^ENCODING}>. +This is deprecated, see C<L<perlvar/${^ENCODING}>> for details. + =item shift on reference is experimental (S experimental::autoderef) C<shift> with a scalar argument is experimental diff --git a/pod/perlvar.pod b/pod/perlvar.pod index fe44c9d..5b9f433 100644 --- a/pod/perlvar.pod +++ b/pod/perlvar.pod @@ -1871,10 +1871,34 @@ Mnemonic: value of B<-D> switch. =item ${^ENCODING} X<${^ENCODING}> +DEPRECATED!!! + The I<object reference> to the C<Encode> object that is used to convert the source code to Unicode. Thanks to this variable your Perl script -does not have to be written in UTF-8. Default is I<undef>. The direct -manipulation of this variable is highly discouraged. +does not have to be written in UTF-8. Default is C<undef>. + +Setting this variable to any other value than C<undef> is deprecated due +to fundamental defects in its design and implementation. It is planned +to remove it from a future Perl version. Its purpose was to allow your +non-ASCII Perl scripts to not have to be written in UTF-8; this was +useful before editors that worked on UTF-8 encoded text were common, but +that was long ago. It causes problems, such as affecting the operation +of other modules that aren't expecting it, causing general mayhem. Its +use can lead to segfaults. + +If you need something like this functionality, you should use the +L<encoding> pragma, which is also deprecated, but has fewer nasty side +effects. + +If you are coming here because code of yours is being adversely affected +by someone's use of this variable, you can usually work around it by +doing this: + + local ${^ENCODING}; + +near the beginning of the functions that are getting broken. This +undefines the variable during the scope of execution of the including +function. This variable was added in Perl 5.8.2. diff --git a/pp.c b/pp.c index fc2714b..e39d66d 100644 --- a/pp.c +++ b/pp.c @@ -809,14 +809,14 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping) Perl_croak_no_modify(); } - if (PL_encoding) { + if (IN_ENCODING) { if (!SvUTF8(sv)) { /* XXX, here sv is utf8-ized as a side-effect! If encoding.pm is used properly, almost string-generating operations, including literal strings, chr(), input data, etc. should have been utf8-ized already, right? */ - sv_recode_to_utf8(sv, PL_encoding); + sv_recode_to_utf8(sv, _get_encoding()); } } @@ -861,11 +861,11 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping) } rsptr = temp_buffer; } - else if (PL_encoding) { + else if (IN_ENCODING) { /* RS is 8 bit, encoding.pm is used. * Do not recode PL_rs as a side-effect. */ svrecode = newSVpvn(rsptr, rslen); - sv_recode_to_utf8(svrecode, PL_encoding); + sv_recode_to_utf8(svrecode, _get_encoding()); rsptr = SvPV_const(svrecode, rslen); rs_charlen = sv_len_utf8(svrecode); } @@ -3262,7 +3262,7 @@ PP(pp_index) little_utf8 = DO_UTF8(little); if (big_utf8 ^ little_utf8) { /* One needs to be upgraded. */ - if (little_utf8 && !PL_encoding) { + if (little_utf8 && !IN_ENCODING) { /* Well, maybe instead we might be able to downgrade the small string? */ char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen, @@ -3284,8 +3284,8 @@ PP(pp_index) temp = little_utf8 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen); - if (PL_encoding) { - sv_recode_to_utf8(temp, PL_encoding); + if (IN_ENCODING) { + sv_recode_to_utf8(temp, _get_encoding()); } else { sv_utf8_upgrade(temp); } @@ -3370,9 +3370,9 @@ PP(pp_ord) STRLEN len; const U8 *s = (U8*)SvPV_const(argsv, len); - if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) { + if (IN_ENCODING && SvPOK(argsv) && !DO_UTF8(argsv)) { SV * const tmpsv = sv_2mortal(newSVsv(argsv)); - s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding); + s = (U8*)sv_recode_to_utf8(tmpsv, _get_encoding()); len = UTF8SKIP(s); /* Should be well-formed; so this is its length */ argsv = tmpsv; } @@ -3435,8 +3435,8 @@ PP(pp_chr) *tmps = '\0'; (void)SvPOK_only(TARG); - if (PL_encoding && !IN_BYTES) { - sv_recode_to_utf8(TARG, PL_encoding); + if (IN_ENCODING && !IN_BYTES) { + sv_recode_to_utf8(TARG, _get_encoding()); tmps = SvPVX(TARG); if (SvCUR(TARG) == 0 || ! is_utf8_string((U8*)tmps, SvCUR(TARG)) diff --git a/pp_ctl.c b/pp_ctl.c index 59ad06e..4b16e14 100644 --- a/pp_ctl.c +++ b/pp_ctl.c @@ -3691,7 +3691,6 @@ PP(pp_require) SV *filter_state = NULL; SV *filter_sub = NULL; SV *hook_sv = NULL; - SV *encoding; OP *op; int saved_errno; bool path_searchable; @@ -4138,18 +4137,11 @@ PP(pp_require) PUTBACK; - /* Store and reset encoding. */ - encoding = PL_encoding; - PL_encoding = NULL; - if (doeval(gimme, NULL, PL_curcop->cop_seq, NULL)) op = DOCATCH(PL_eval_start); else op = PL_op->op_next; - /* Restore encoding. */ - PL_encoding = encoding; - LOADED_FILE_PROBE(unixname); return op; diff --git a/pp_hot.c b/pp_hot.c index 55e2c97..4908525 100644 --- a/pp_hot.c +++ b/pp_hot.c @@ -2186,8 +2186,8 @@ PP(pp_subst) if (DO_UTF8(TARG) && !doutf8) { nsv = sv_newmortal(); SvSetSV(nsv, dstr); - if (PL_encoding) - sv_recode_to_utf8(nsv, PL_encoding); + if (IN_ENCODING) + sv_recode_to_utf8(nsv, _get_encoding()); else sv_utf8_upgrade(nsv); c = SvPV_const(nsv, clen); @@ -2367,10 +2367,10 @@ PP(pp_subst) first = FALSE; } else { - if (PL_encoding) { + if (IN_ENCODING) { if (!nsv) nsv = sv_newmortal(); sv_copypv(nsv, repl); - if (!DO_UTF8(nsv)) sv_recode_to_utf8(nsv, PL_encoding); + if (!DO_UTF8(nsv)) sv_recode_to_utf8(nsv, _get_encoding()); sv_catsv(dstr, nsv); } else sv_catsv(dstr, repl); diff --git a/pp_sys.c b/pp_sys.c index 831bf26..b371469 100644 --- a/pp_sys.c +++ b/pp_sys.c @@ -1756,7 +1756,7 @@ PP(pp_sysread) bytes from a byte file handle into a UTF8 buffer, but it won't harm us unduly. (should be 2 * length + offset + 1, or possibly something longer if - PL_encoding is true) */ + IN_ENCODING Is true) */ buffer = SvGROW(bufsv, (STRLEN)(length+offset+1)); if (offset > 0 && offset > (SSize_t)orig_size) { /* Zero any newly allocated space */ Zero(buffer+orig_size, offset-orig_size, char); diff --git a/proto.h b/proto.h index 656d6b4..76c47d5 100644 --- a/proto.h +++ b/proto.h @@ -42,6 +42,9 @@ PERL_CALLCONV void Perl_Slab_Free(pTHX_ void *op) #define PERL_ARGS_ASSERT_SLAB_FREE \ assert(op) +PERL_CALLCONV SV* Perl__get_encoding(pTHX) + __attribute__pure__; + PERL_CALLCONV bool Perl__is_in_locale_category(pTHX_ const bool compiling, const int category); PERL_CALLCONV bool Perl__is_uni_FOO(pTHX_ const U8 classnum, const UV c) __attribute__warn_unused_result__; diff --git a/regcomp.c b/regcomp.c index 12c3678..442d0ba 100644 --- a/regcomp.c +++ b/regcomp.c @@ -12242,7 +12242,7 @@ tryagain: vFAIL(error_msg); } ender = result; - if (PL_encoding && ender < 0x100) { + if (IN_ENCODING && ender < 0x100) { goto recode_encoding; } if (ender > 0xff) { @@ -12272,7 +12272,7 @@ tryagain: } ender = result; - if (PL_encoding && ender < 0x100) { + if (IN_ENCODING && ender < 0x100) { goto recode_encoding; } if (ender > 0xff) { @@ -12331,12 +12331,12 @@ tryagain: form_short_octal_warning(p, numlen)); } } - if (PL_encoding && ender < 0x100) + if (IN_ENCODING && ender < 0x100) goto recode_encoding; break; recode_encoding: if (! RExC_override_recoding) { - SV* enc = PL_encoding; + SV* enc = _get_encoding(); ender = reg_recode((const char)(U8)ender, &enc); if (!enc && PASS2) ckWARNreg(p, "Invalid escape in the specified encoding"); @@ -14147,7 +14147,7 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth, vFAIL(error_msg); } } - if (PL_encoding && value < 0x100) { + if (IN_ENCODING && value < 0x100) { goto recode_encoding; } break; @@ -14166,7 +14166,7 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth, vFAIL(error_msg); } } - if (PL_encoding && value < 0x100) + if (IN_ENCODING && value < 0x100) goto recode_encoding; break; case 'c': @@ -14198,13 +14198,13 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth, (void)ReREFCNT_inc(RExC_rx_sv); } } - if (PL_encoding && value < 0x100) + if (IN_ENCODING && value < 0x100) goto recode_encoding; break; } recode_encoding: if (! RExC_override_recoding) { - SV* enc = PL_encoding; + SV* enc = _get_encoding(); value = reg_recode((const char)(U8)value, &enc); if (!enc) { if (strict) { diff --git a/sv.c b/sv.c index a82350f..c94a529 100644 --- a/sv.c +++ b/sv.c @@ -3453,8 +3453,8 @@ Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extr S_sv_uncow(aTHX_ sv, 0); } - if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) { - sv_recode_to_utf8(sv, PL_encoding); + if (IN_ENCODING && !(flags & SV_UTF8_NO_ENCODING)) { + sv_recode_to_utf8(sv, _get_encoding()); if (extra) SvGROW(sv, SvCUR(sv) + extra); return SvCUR(sv); } @@ -7744,15 +7744,15 @@ Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags) if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) { /* Differing utf8ness. * Do not UTF8size the comparands as a side-effect. */ - if (PL_encoding) { + if (IN_ENCODING) { if (SvUTF8(sv1)) { svrecode = newSVpvn(pv2, cur2); - sv_recode_to_utf8(svrecode, PL_encoding); + sv_recode_to_utf8(svrecode, _get_encoding()); pv2 = SvPV_const(svrecode, cur2); } else { svrecode = newSVpvn(pv1, cur1); - sv_recode_to_utf8(svrecode, PL_encoding); + sv_recode_to_utf8(svrecode, _get_encoding()); pv1 = SvPV_const(svrecode, cur1); } /* Now both are in UTF-8. */ @@ -7835,9 +7835,9 @@ Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2, /* Differing utf8ness. * Do not UTF8size the comparands as a side-effect. */ if (SvUTF8(sv1)) { - if (PL_encoding) { + if (IN_ENCODING) { svrecode = newSVpvn(pv2, cur2); - sv_recode_to_utf8(svrecode, PL_encoding); + sv_recode_to_utf8(svrecode, _get_encoding()); pv2 = SvPV_const(svrecode, cur2); } else { @@ -7847,9 +7847,9 @@ Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2, } } else { - if (PL_encoding) { + if (IN_ENCODING) { svrecode = newSVpvn(pv1, cur1); - sv_recode_to_utf8(svrecode, PL_encoding); + sv_recode_to_utf8(svrecode, _get_encoding()); pv1 = SvPV_const(svrecode, cur1); } else { @@ -14626,6 +14626,7 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags, /* magical thingies */ PL_encoding = sv_dup(proto_perl->Iencoding, param); + PL_lex_encoding = sv_dup(proto_perl->Ilex_encoding, param); sv_setpvs(PERL_DEBUG_PAD(0), ""); /* For regex debugging. */ sv_setpvs(PERL_DEBUG_PAD(1), ""); /* ext/re needs these */ diff --git a/t/lib/warnings/mg b/t/lib/warnings/mg index 348f9b2..6bd6c3a 100644 --- a/t/lib/warnings/mg +++ b/t/lib/warnings/mg @@ -99,3 +99,16 @@ Use of uninitialized value $3 in oct at - line 3. use warnings 'uninitialized'; $ENV{FOO} = undef; # should not warn EXPECT +######## +${^ENCODING} = 42; +{ local ${^ENCODING}; } +${^ENCODING} = undef; +{ local ${^ENCODING} = 37; } +no warnings 'deprecated'; +${^ENCODING} = 42; +{ local ${^ENCODING}; } +${^ENCODING} = undef; +{ local ${^ENCODING} = 37; } +EXPECT +Setting ${^ENCODING} is deprecated at - line 1. +Setting ${^ENCODING} is deprecated at - line 4. diff --git a/t/op/leaky-magic.t b/t/op/leaky-magic.t index 371f3ac..dd94d30 100644 --- a/t/op/leaky-magic.t +++ b/t/op/leaky-magic.t @@ -34,6 +34,7 @@ ok !scalar keys %foo::SIG, "%foo::SIG"; use tests 4; # rw ${^LETTERS} variables for(qw< CHILD_ERROR_NATIVE ENCODING UTF8CACHE WARNING_BITS >) { + no warnings 'deprecated'; # ENCODING is deprecated; my $name = s/./"qq|\\c$&|"/ere; local $$name = 'swit'; diff --git a/t/porting/customized.dat b/t/porting/customized.dat index eb849a1..407f0ab 100644 --- a/t/porting/customized.dat +++ b/t/porting/customized.dat @@ -1,5 +1,6 @@ DB_File cpan/DB_File/DB_File.xs f364b661bbb0df075b0c425b104577e7176fe82b Digest::MD5 cpan/Digest-MD5/t/files.t bdbe05b705d9da305fedce7a9f4b6ba63250c7cf +Encode cpan/Encode/encoding.pm 506ec84f1fbbff189c3f4f47b92aff5afc95b98e ExtUtils::MakeMaker cpan/ExtUtils-MakeMaker/lib/ExtUtils/MM_Unix.pm 793813932194c12e62c6046f62ea6f1fb65de80b IO::Socket::IP cpan/IO-Socket-IP/t/22timeout.t 0a65d4ca44663f70eea3c7f1ccb5faed2940611f PerlIO::via::QuotedPrint cpan/PerlIO-via-QuotedPrint/t/QuotedPrint.t ca39f0146e89de02c746e199c45dcb3e5edad691 diff --git a/t/re/pat_re_eval.t b/t/re/pat_re_eval.t index c77439a..16ecf35 100644 --- a/t/re/pat_re_eval.t +++ b/t/re/pat_re_eval.t @@ -744,7 +744,9 @@ sub run_tests { # Poor man's "use encoding 'ascii'". # This causes a different code path in S_const_str() # to be used + no warnings 'deprecated'; local ${^ENCODING} = $enc; + use warnings 'deprecated'; use re 'eval'; ok($ss =~ /^$cc/, fmt("encode $u->[2]", $ss, $cc)); } diff --git a/t/uni/chomp.t b/t/uni/chomp.t index 4730736..c78c3e1 100644 --- a/t/uni/chomp.t +++ b/t/uni/chomp.t @@ -33,7 +33,9 @@ our %mbchars = ( plan tests => 2 * (4 ** 3 + 4 + 1) * (keys %mbchars); for my $enc (sort keys %mbchars) { + no warnings 'deprecated'; local ${^ENCODING} = find_encoding($enc); + use warnings 'deprecated'; my @char = (sort(keys %{ $mbchars{$enc} }), sort(values %{ $mbchars{$enc} })); diff --git a/toke.c b/toke.c index b6da013..e20c93f 100644 --- a/toke.c +++ b/toke.c @@ -3189,9 +3189,13 @@ S_scan_const(pTHX_ char *start) SvPOK_on(sv); *d = '\0'; /* See Note on sizing above. */ - sv_utf8_upgrade_flags_grow(sv, - SV_GMAGIC|SV_FORCE_UTF8_UPGRADE, - UNISKIP(uv) + (STRLEN)(send - s) + 1); + sv_utf8_upgrade_flags_grow( + sv, + SV_GMAGIC|SV_FORCE_UTF8_UPGRADE + /* Above-latin1 in string + * implies no encoding */ + |SV_UTF8_NO_ENCODING, + UNISKIP(uv) + (STRLEN)(send - s) + 1); d = SvPVX(sv) + SvCUR(sv); has_utf8 = TRUE; } @@ -3588,8 +3592,8 @@ S_scan_const(pTHX_ char *start) " >= %"UVuf, (UV)SvCUR(sv), (UV)SvLEN(sv)); SvPOK_on(sv); - if (PL_encoding && !has_utf8) { - sv_recode_to_utf8(sv, PL_encoding); + if (IN_ENCODING && !has_utf8) { + sv_recode_to_utf8(sv, _get_encoding()); if (SvUTF8(sv)) has_utf8 = TRUE; } @@ -6920,13 +6924,13 @@ Perl_yylex(pTHX) if (!IN_BYTES) { if (UTF) PerlIO_apply_layers(aTHX_ PL_rsfp, NULL, ":utf8"); - else if (PL_encoding) { + else if (IN_ENCODING) { SV *name; dSP; ENTER; SAVETMPS; PUSHMARK(sp); - XPUSHs(PL_encoding); + XPUSHs(_get_encoding()); PUTBACK; call_method("name", G_SCALAR); SPAGAIN; @@ -9334,8 +9338,8 @@ S_scan_heredoc(pTHX_ char *s) if (!IN_BYTES) { if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr))) SvUTF8_on(tmpstr); - else if (PL_encoding) - sv_recode_to_utf8(tmpstr, PL_encoding); + else if (IN_ENCODING) + sv_recode_to_utf8(tmpstr, _get_encoding()); } PL_lex_stuff = tmpstr; pl_yylval.ival = op_type; @@ -9625,12 +9629,12 @@ S_scan_str(pTHX_ char *start, int keep_bracketed_quoted, int keep_delims, int re sv_catpvn(sv, s, termlen); s += termlen; for (;;) { - if (PL_encoding && !UTF && !re_reparse) { + if (IN_ENCODING && !UTF && !re_reparse) { bool cont = TRUE; while (cont) { int offset = s - SvPVX_const(PL_linestr); - const bool found = sv_cat_decode(sv, PL_encoding, PL_linestr, + const bool found = sv_cat_decode(sv, _get_encoding(), PL_linestr, &offset, (char*)termstr, termlen); const char *ns; char *svlast; @@ -9843,13 +9847,13 @@ S_scan_str(pTHX_ char *start, int keep_bracketed_quoted, int keep_delims, int re /* at this point, we have successfully read the delimited string */ - if (!PL_encoding || UTF || re_reparse) { + if (!IN_ENCODING || UTF || re_reparse) { if (keep_delims) sv_catpvn(sv, s, termlen); s += termlen; } - if (has_utf8 || (PL_encoding && !re_reparse)) + if (has_utf8 || (IN_ENCODING && !re_reparse)) SvUTF8_on(sv); PL_multi_end = CopLINE(PL_curcop); @@ -10519,8 +10523,8 @@ S_scan_formline(pTHX_ char *s) if (!IN_BYTES) { if (UTF && is_utf8_string((U8*)SvPVX_const(stuff), SvCUR(stuff))) SvUTF8_on(stuff); - else if (PL_encoding) - sv_recode_to_utf8(stuff, PL_encoding); + else if (IN_ENCODING) + sv_recode_to_utf8(stuff, _get_encoding()); } NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0, stuff); force_next(THING); -- Perl5 Master Repository
