1# -*- buffer-read-only: t -*- 2# !!!!!!! DO NOT EDIT THIS FILE !!!!!!! 3# This file is built by regen/feature.pl. 4# Any changes made here will be lost! 5 6package feature; 7 8our $VERSION = '1.36_01'; 9 10our %feature = ( 11 fc => 'feature_fc', 12 say => 'feature_say', 13 state => 'feature_state', 14 switch => 'feature_switch', 15 evalbytes => 'feature_evalbytes', 16 postderef => 'feature_postderef', 17 array_base => 'feature_arybase', 18 signatures => 'feature_signatures', 19 current_sub => 'feature___SUB__', 20 lexical_subs => 'feature_lexsubs', 21 postderef_qq => 'feature_postderef_qq', 22 unicode_eval => 'feature_unieval', 23 unicode_strings => 'feature_unicode', 24); 25 26our %feature_bundle = ( 27 "5.10" => [qw(array_base say state switch)], 28 "5.11" => [qw(array_base say state switch unicode_strings)], 29 "5.15" => [qw(current_sub evalbytes fc say state switch unicode_eval unicode_strings)], 30 "all" => [qw(array_base current_sub evalbytes fc lexical_subs postderef postderef_qq say signatures state switch unicode_eval unicode_strings)], 31 "default" => [qw(array_base)], 32); 33 34$feature_bundle{"5.12"} = $feature_bundle{"5.11"}; 35$feature_bundle{"5.13"} = $feature_bundle{"5.11"}; 36$feature_bundle{"5.14"} = $feature_bundle{"5.11"}; 37$feature_bundle{"5.16"} = $feature_bundle{"5.15"}; 38$feature_bundle{"5.17"} = $feature_bundle{"5.15"}; 39$feature_bundle{"5.18"} = $feature_bundle{"5.15"}; 40$feature_bundle{"5.19"} = $feature_bundle{"5.15"}; 41$feature_bundle{"5.20"} = $feature_bundle{"5.15"}; 42$feature_bundle{"5.9.5"} = $feature_bundle{"5.10"}; 43 44our $hint_shift = 26; 45our $hint_mask = 0x1c000000; 46our @hint_bundles = qw( default 5.10 5.11 5.15 ); 47 48# This gets set (for now) in $^H as well as in %^H, 49# for runtime speed of the uc/lc/ucfirst/lcfirst functions. 50# See HINT_UNI_8_BIT in perl.h. 51our $hint_uni8bit = 0x00000800; 52 53# TODO: 54# - think about versioned features (use feature switch => 2) 55 56=head1 NAME 57 58feature - Perl pragma to enable new features 59 60=head1 SYNOPSIS 61 62 use feature qw(say switch); 63 given ($foo) { 64 when (1) { say "\$foo == 1" } 65 when ([2,3]) { say "\$foo == 2 || \$foo == 3" } 66 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" } 67 when ($_ > 100) { say "\$foo > 100" } 68 default { say "None of the above" } 69 } 70 71 use feature ':5.10'; # loads all features available in perl 5.10 72 73 use v5.10; # implicitly loads :5.10 feature bundle 74 75=head1 DESCRIPTION 76 77It is usually impossible to add new syntax to Perl without breaking 78some existing programs. This pragma provides a way to minimize that 79risk. New syntactic constructs, or new semantic meanings to older 80constructs, can be enabled by C<use feature 'foo'>, and will be parsed 81only when the appropriate feature pragma is in scope. (Nevertheless, the 82C<CORE::> prefix provides access to all Perl keywords, regardless of this 83pragma.) 84 85=head2 Lexical effect 86 87Like other pragmas (C<use strict>, for example), features have a lexical 88effect. C<use feature qw(foo)> will only make the feature "foo" available 89from that point to the end of the enclosing block. 90 91 { 92 use feature 'say'; 93 say "say is available here"; 94 } 95 print "But not here.\n"; 96 97=head2 C<no feature> 98 99Features can also be turned off by using C<no feature "foo">. This too 100has lexical effect. 101 102 use feature 'say'; 103 say "say is available here"; 104 { 105 no feature 'say'; 106 print "But not here.\n"; 107 } 108 say "Yet it is here."; 109 110C<no feature> with no features specified will reset to the default group. To 111disable I<all> features (an unusual request!) use C<no feature ':all'>. 112 113=head1 AVAILABLE FEATURES 114 115=head2 The 'say' feature 116 117C<use feature 'say'> tells the compiler to enable the Perl 6 style 118C<say> function. 119 120See L<perlfunc/say> for details. 121 122This feature is available starting with Perl 5.10. 123 124=head2 The 'state' feature 125 126C<use feature 'state'> tells the compiler to enable C<state> 127variables. 128 129See L<perlsub/"Persistent Private Variables"> for details. 130 131This feature is available starting with Perl 5.10. 132 133=head2 The 'switch' feature 134 135C<use feature 'switch'> tells the compiler to enable the Perl 6 136given/when construct. 137 138See L<perlsyn/"Switch Statements"> for details. 139 140This feature is available starting with Perl 5.10. 141 142=head2 The 'unicode_strings' feature 143 144C<use feature 'unicode_strings'> tells the compiler to use Unicode rules 145in all string operations executed within its scope (unless they are also 146within the scope of either C<use locale> or C<use bytes>). The same applies 147to all regular expressions compiled within the scope, even if executed outside 148it. It does not change the internal representation of strings, but only how 149they are interpreted. 150 151C<no feature 'unicode_strings'> tells the compiler to use the traditional 152Perl rules wherein the native character set rules is used unless it is 153clear to Perl that Unicode is desired. This can lead to some surprises 154when the behavior suddenly changes. (See 155L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are 156potentially using Unicode in your program, the 157C<use feature 'unicode_strings'> subpragma is B<strongly> recommended. 158 159This feature is available starting with Perl 5.12; was almost fully 160implemented in Perl 5.14; and extended in Perl 5.16 to cover C<quotemeta>. 161 162=head2 The 'unicode_eval' and 'evalbytes' features 163 164Under the C<unicode_eval> feature, Perl's C<eval> function, when passed a 165string, will evaluate it as a string of characters, ignoring any 166C<use utf8> declarations. C<use utf8> exists to declare the encoding of 167the script, which only makes sense for a stream of bytes, not a string of 168characters. Source filters are forbidden, as they also really only make 169sense on strings of bytes. Any attempt to activate a source filter will 170result in an error. 171 172The C<evalbytes> feature enables the C<evalbytes> keyword, which evaluates 173the argument passed to it as a string of bytes. It dies if the string 174contains any characters outside the 8-bit range. Source filters work 175within C<evalbytes>: they apply to the contents of the string being 176evaluated. 177 178Together, these two features are intended to replace the historical C<eval> 179function, which has (at least) two bugs in it, that cannot easily be fixed 180without breaking existing programs: 181 182=over 183 184=item * 185 186C<eval> behaves differently depending on the internal encoding of the 187string, sometimes treating its argument as a string of bytes, and sometimes 188as a string of characters. 189 190=item * 191 192Source filters activated within C<eval> leak out into whichever I<file> 193scope is currently being compiled. To give an example with the CPAN module 194L<Semi::Semicolons>: 195 196 BEGIN { eval "use Semi::Semicolons; # not filtered here " } 197 # filtered here! 198 199C<evalbytes> fixes that to work the way one would expect: 200 201 use feature "evalbytes"; 202 BEGIN { evalbytes "use Semi::Semicolons; # filtered " } 203 # not filtered 204 205=back 206 207These two features are available starting with Perl 5.16. 208 209=head2 The 'current_sub' feature 210 211This provides the C<__SUB__> token that returns a reference to the current 212subroutine or C<undef> outside of a subroutine. 213 214This feature is available starting with Perl 5.16. 215 216=head2 The 'array_base' feature 217 218This feature supports the legacy C<$[> variable. See L<perlvar/$[> and 219L<arybase>. It is on by default but disabled under C<use v5.16> (see 220L</IMPLICIT LOADING>, below). 221 222This feature is available under this name starting with Perl 5.16. In 223previous versions, it was simply on all the time, and this pragma knew 224nothing about it. 225 226=head2 The 'fc' feature 227 228C<use feature 'fc'> tells the compiler to enable the C<fc> function, 229which implements Unicode casefolding. 230 231See L<perlfunc/fc> for details. 232 233This feature is available from Perl 5.16 onwards. 234 235=head2 The 'lexical_subs' feature 236 237B<WARNING>: This feature is still experimental and the implementation may 238change in future versions of Perl. For this reason, Perl will 239warn when you use the feature, unless you have explicitly disabled the 240warning: 241 242 no warnings "experimental::lexical_subs"; 243 244This enables declaration of subroutines via C<my sub foo>, C<state sub foo> 245and C<our sub foo> syntax. See L<perlsub/Lexical Subroutines> for details. 246 247This feature is available from Perl 5.18 onwards. 248 249=head2 The 'postderef' and 'postderef_qq' features 250 251B<WARNING>: This feature is still experimental and the implementation may 252change in future versions of Perl. For this reason, Perl will 253warn when you use the feature, unless you have explicitly disabled the 254warning: 255 256 no warnings "experimental::postderef"; 257 258The 'postderef' feature allows the use of L<postfix dereference 259syntax|perlref/Postfix Dereference Syntax>. For example, it will make the 260following two statements equivalent: 261 262 my @x = @{ $h->{a} }; 263 my @x = $h->{a}->@*; 264 265The 'postderef_qq' feature extends this, for array and scalar dereference, to 266working inside of double-quotish interpolations. 267 268This feature is available from Perl 5.20 onwards. 269 270=head2 The 'signatures' feature 271 272B<WARNING>: This feature is still experimental and the implementation may 273change in future versions of Perl. For this reason, Perl will 274warn when you use the feature, unless you have explicitly disabled the 275warning: 276 277 no warnings "experimental::signatures"; 278 279This enables unpacking of subroutine arguments into lexical variables 280by syntax such as 281 282 sub foo ($left, $right) { 283 return $left + $right; 284 } 285 286See L<perlsub/Signatures> for details. 287 288This feature is available from Perl 5.20 onwards. 289 290=head1 FEATURE BUNDLES 291 292It's possible to load multiple features together, using 293a I<feature bundle>. The name of a feature bundle is prefixed with 294a colon, to distinguish it from an actual feature. 295 296 use feature ":5.10"; 297 298The following feature bundles are available: 299 300 bundle features included 301 --------- ----------------- 302 :default array_base 303 304 :5.10 say state switch array_base 305 306 :5.12 say state switch unicode_strings array_base 307 308 :5.14 say state switch unicode_strings array_base 309 310 :5.16 say state switch unicode_strings 311 unicode_eval evalbytes current_sub fc 312 313 :5.18 say state switch unicode_strings 314 unicode_eval evalbytes current_sub fc 315 316 :5.20 say state switch unicode_strings 317 unicode_eval evalbytes current_sub fc 318 319The C<:default> bundle represents the feature set that is enabled before 320any C<use feature> or C<no feature> declaration. 321 322Specifying sub-versions such as the C<0> in C<5.14.0> in feature bundles has 323no effect. Feature bundles are guaranteed to be the same for all sub-versions. 324 325 use feature ":5.14.0"; # same as ":5.14" 326 use feature ":5.14.1"; # same as ":5.14" 327 328=head1 IMPLICIT LOADING 329 330Instead of loading feature bundles by name, it is easier to let Perl do 331implicit loading of a feature bundle for you. 332 333There are two ways to load the C<feature> pragma implicitly: 334 335=over 4 336 337=item * 338 339By using the C<-E> switch on the Perl command-line instead of C<-e>. 340That will enable the feature bundle for that version of Perl in the 341main compilation unit (that is, the one-liner that follows C<-E>). 342 343=item * 344 345By explicitly requiring a minimum Perl version number for your program, with 346the C<use VERSION> construct. That is, 347 348 use v5.10.0; 349 350will do an implicit 351 352 no feature ':all'; 353 use feature ':5.10'; 354 355and so on. Note how the trailing sub-version 356is automatically stripped from the 357version. 358 359But to avoid portability warnings (see L<perlfunc/use>), you may prefer: 360 361 use 5.010; 362 363with the same effect. 364 365If the required version is older than Perl 5.10, the ":default" feature 366bundle is automatically loaded instead. 367 368=back 369 370=cut 371 372sub import { 373 my $class = shift; 374 375 if (!@_) { 376 croak("No features specified"); 377 } 378 379 __common(1, @_); 380} 381 382sub unimport { 383 my $class = shift; 384 385 # A bare C<no feature> should reset to the default bundle 386 if (!@_) { 387 $^H &= ~($hint_uni8bit|$hint_mask); 388 return; 389 } 390 391 __common(0, @_); 392} 393 394 395sub __common { 396 my $import = shift; 397 my $bundle_number = $^H & $hint_mask; 398 my $features = $bundle_number != $hint_mask 399 && $feature_bundle{$hint_bundles[$bundle_number >> $hint_shift]}; 400 if ($features) { 401 # Features are enabled implicitly via bundle hints. 402 # Delete any keys that may be left over from last time. 403 delete @^H{ values(%feature) }; 404 $^H |= $hint_mask; 405 for (@$features) { 406 $^H{$feature{$_}} = 1; 407 $^H |= $hint_uni8bit if $_ eq 'unicode_strings'; 408 } 409 } 410 while (@_) { 411 my $name = shift; 412 if (substr($name, 0, 1) eq ":") { 413 my $v = substr($name, 1); 414 if (!exists $feature_bundle{$v}) { 415 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/; 416 if (!exists $feature_bundle{$v}) { 417 unknown_feature_bundle(substr($name, 1)); 418 } 419 } 420 unshift @_, @{$feature_bundle{$v}}; 421 next; 422 } 423 if (!exists $feature{$name}) { 424 unknown_feature($name); 425 } 426 if ($import) { 427 $^H{$feature{$name}} = 1; 428 $^H |= $hint_uni8bit if $name eq 'unicode_strings'; 429 } else { 430 delete $^H{$feature{$name}}; 431 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings'; 432 } 433 } 434} 435 436sub unknown_feature { 437 my $feature = shift; 438 croak(sprintf('Feature "%s" is not supported by Perl %vd', 439 $feature, $^V)); 440} 441 442sub unknown_feature_bundle { 443 my $feature = shift; 444 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd', 445 $feature, $^V)); 446} 447 448sub croak { 449 require Carp; 450 Carp::croak(@_); 451} 452 4531; 454 455# ex: set ro: 456