feature.pm 15 KB

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