This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add 5.16 feature bundle
[perl5.git] / lib / feature.pm
index f8a9078..6ab650d 100644 (file)
@@ -1,13 +1,15 @@
 package feature;
 
-our $VERSION = '1.18';
+our $VERSION = '1.23';
 
 # (feature name) => (internal name, used in %^H)
 my %feature = (
+    say             => 'feature_say',
+    state           => 'feature_state',
     switch          => 'feature_switch',
-    say             => "feature_say",
-    state           => "feature_state",
-    unicode_strings => "feature_unicode",
+    evalbytes       => 'feature_evalbytes',
+    unicode_eval    => 'feature_unieval',
+    unicode_strings => 'feature_unicode',
 );
 
 # This gets set (for now) in $^H as well as in %^H,
@@ -18,10 +20,15 @@ our $hint_uni8bit = 0x00000800;
 # NB. the latest bundle must be loaded by the -E switch (see toke.c)
 
 my %feature_bundle = (
-    "5.10" => [qw(switch say state)],
-    "5.11" => [qw(switch say state unicode_strings)],
-    "5.12" => [qw(switch say state unicode_strings)],
-    "5.13" => [qw(switch say state unicode_strings)],
+    "5.10" => [qw(say state switch)],
+    "5.11" => [qw(say state switch unicode_strings)],
+    "5.12" => [qw(say state switch unicode_strings)],
+    "5.13" => [qw(say state switch unicode_strings)],
+    "5.14" => [qw(say state switch unicode_strings)],
+    "5.15" => [qw(say state switch unicode_strings unicode_eval
+                  evalbytes)],
+    "5.16" => [qw(say state switch unicode_strings unicode_eval
+                  evalbytes)],
 );
 
 # special case
@@ -36,7 +43,7 @@ feature - Perl pragma to enable new features
 
 =head1 SYNOPSIS
 
-    use feature qw(switch say);
+    use feature qw(say switch);
     given ($foo) {
        when (1)          { say "\$foo == 1" }
        when ([2,3])      { say "\$foo == 2 || \$foo == 3" }
@@ -53,7 +60,9 @@ It is usually impossible to add new syntax to Perl without breaking
 some existing programs. This pragma provides a way to minimize that
 risk. New syntactic constructs, or new semantic meanings to older
 constructs, can be enabled by C<use feature 'foo'>, and will be parsed
-only when the appropriate feature pragma is in scope.
+only when the appropriate feature pragma is in scope. (Nevertheless, the
+C<CORE::> prefix provides access to all Perl keywords, regardless of this
+pragma.)
 
 =head2 Lexical effect
 
@@ -82,13 +91,6 @@ has lexical effect.
 
 C<no feature> with no features specified will turn off all features.
 
-=head2 The 'switch' feature
-
-C<use feature 'switch'> tells the compiler to enable the Perl 6
-given/when construct.
-
-See L<perlsyn/"Switch statements"> for details.
-
 =head2 The 'say' feature
 
 C<use feature 'say'> tells the compiler to enable the Perl 6
@@ -103,25 +105,100 @@ variables.
 
 See L<perlsub/"Persistent Private Variables"> for details.
 
+=head2 The 'switch' feature
+
+C<use feature 'switch'> tells the compiler to enable the Perl 6
+given/when construct.
+
+See L<perlsyn/"Switch statements"> for details.
+
 =head2 the 'unicode_strings' feature
 
-C<use feature 'unicode_strings'> tells the compiler to treat
-all strings outside of C<use locale> and C<use bytes> as Unicode. It is
-available starting with Perl 5.11.3, but is not fully implemented.
+C<use feature 'unicode_strings'> tells the compiler to use Unicode semantics
+in all string operations executed within its scope (unless they are also
+within the scope of either C<use locale> or C<use bytes>).  The same applies
+to all regular expressions compiled within the scope, even if executed outside
+it.
+
+C<no feature 'unicode_strings'> tells the compiler to use the traditional
+Perl semantics wherein the native character set semantics is used unless it is
+clear to Perl that Unicode is desired.  This can lead to some surprises
+when the behavior suddenly changes.  (See
+L<perlunicode/The "Unicode Bug"> for details.)  For this reason, if you are
+potentially using Unicode in your program, the
+C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
+
+This subpragma is available starting with Perl 5.11.3, but was not fully
+implemented until 5.13.8.
+
+=head2 the 'unicode_eval' and 'evalbytes' features
+
+Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a
+string, will evaluate it as a string of characters, ignoring any
+C<use utf8> declarations.  C<use utf8> exists to declare the encoding of
+the script, which only makes sense for a stream of bytes, not a string of
+characters.  Source filters are forbidden, as they also really only make
+sense on strings of bytes.  Any attempt to activate a source filter will
+result in an error.
+
+The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates
+the argument passed to it as a string of bytes.  It dies if the string
+contains any characters outside the 8-bit range.  Source filters work
+within C<evalbytes>: they apply to the contents of the string being
+evaluated.
+
+Together, these two features are intended to replace the historical C<eval>
+function, which has (at least) two bugs in it, that cannot easily be fixed
+without breaking existing programs:
+
+=over
 
-See L<perlunicode/The "Unicode Bug"> for details.
+=item *
+
+C<eval> behaves differently depending on the internal encoding of the
+string, sometimes treating its argument as a string of bytes, and sometimes
+as a string of characters.
+
+=item *
+
+Source filters activated within C<eval> leak out into whichever I<file>
+scope is currently being compiled.  To give an example with the CPAN module
+L<Semi::Semicolons>:
+
+    BEGIN { eval "use Semi::Semicolons;  # not filtered here " }
+    # filtered here!
+
+C<evalbytes> fixes that to work the way one would expect:
+
+    use feature "evalbytes";
+    BEGIN { evalbytes "use Semi::Semicolons;  # filtered " }
+    # not filtered
+
+=back
+
+These two features are available starting with Perl 5.16.
 
 =head1 FEATURE BUNDLES
 
 It's possible to load a whole slew of features in one go, using
 a I<feature bundle>. The name of a feature bundle is prefixed with
 a colon, to distinguish it from an actual feature. At present, the
-only feature bundle is C<use feature ":5.10"> which is equivalent
-to C<use feature qw(switch say state)>.
+only feature bundles correspond to Perl releases, e.g. C<use feature
+":5.10"> which is equivalent to C<use feature qw(switch say state)>.
+
+By convention, the feature bundle for any given Perl release includes
+the features of previous releases, down to and including 5.10, the
+first official release to provide this facility. Since Perl 5.12
+only provides one new feature, C<unicode_strings>, and Perl 5.14
+provides none, C<use feature ":5.14"> is equivalent to C<use feature
+qw(switch say state unicode_strings)>.
 
-Specifying sub-versions such as the C<0> in C<5.10.0> in feature bundles has
+Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has
 no effect: feature bundles are guaranteed to be the same for all sub-versions.
 
+Note that instead of using release-based feature bundles it is usually
+better, and shorter, to use implicit loading as described below.
+
 =head1 IMPLICIT LOADING
 
 There are two ways to load the C<feature> pragma implicitly :