Revert change 28322, which makes ithreads panic
[p5sagit/p5-mst-13.2.git] / lib / feature.pm
CommitLineData
0d863452 1package feature;
2
712d05cf 3our $VERSION = '1.01';
0d863452 4
5# (feature name) => (internal name, used in %^H)
6my %feature = (
7b9ef140 7 switch => 'feature_switch',
8 "~~" => "feature_~~",
9 say => "feature_say",
bc9b29db 10 err => "feature_err",
712d05cf 11 state => "feature_state",
bc9b29db 12);
13
14my %feature_bundle = (
712d05cf 15 "5.10" => [qw(switch ~~ say err state)],
0d863452 16);
17
18
19# Here are some notes that probably shouldn't be in the public
20# documentation, but which it's useful to have somewhere.
21#
22# One side-effect of the change is that C<prototype("CORE::continue")>
23# no longer throws the error C<Can't find an opnumber for "continue">.
24# One of the tests in t/op/cproto.t had to be changed to accommodate
25# this, but it really shouldn't affect real-world code.
26#
27# TODO:
28# - sort out the smartmatch semantics
29# - think about versioned features (use switch => 2)
30#
31# -- Robin 2005-12
32
33=head1 NAME
34
35feature - Perl pragma to enable new syntactic features
36
37=head1 SYNOPSIS
38
bc9b29db 39 use feature qw(switch say);
0d863452 40 given ($foo) {
bc9b29db 41 when (1) { say "\$foo == 1" }
42 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
43 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
44 when ($_ > 100) { say "\$foo > 100" }
45 default { say "None of the above" }
0d863452 46 }
47
48=head1 DESCRIPTION
49
50It is usually impossible to add new syntax to Perl without breaking
51some existing programs. This pragma provides a way to minimize that
52risk. New syntactic constructs can be enabled by C<use feature 'foo'>,
53and will be parsed only when the appropriate feature pragma is in
54scope.
55
56=head2 The 'switch' feature
57
58C<use feature 'switch'> tells the compiler to enable the Perl 6
59given/when construct from here to the end of the enclosing BLOCK.
60
61See L<perlsyn/"Switch statements"> for details.
62
63=head2 The '~~' feature
64
65C<use feature '~~'> tells the compiler to enable the Perl 6
66smart match C<~~> operator from here to the end of the enclosing BLOCK.
67
68See L<perlsyn/"Smart Matching in Detail"> for details.
69
70=head2 The 'say' feature
71
72C<use feature 'say'> tells the compiler to enable the Perl 6
73C<say> function from here to the end of the enclosing BLOCK.
74
75See L<perlfunc/say> for details.
76
bc9b29db 77=head2 the 'err' feature
78
79C<use feature 'err'> tells the compiler to enable the C<err>
80operator from here to the end of the enclosing BLOCK.
81
82C<err> is a low-precedence variant of the C<//> operator:
83see C<perlop> for details.
84
712d05cf 85=head2 the 'state' feature
86
87C<use feature 'state'> tells the compiler to enable C<state>
88variables from here to the end of the enclosing BLOCK.
89
bc9b29db 90=head1 FEATURE BUNDLES
91
92It's possible to load a whole slew of features in one go, using
93a I<feature bundle>. The name of a feature bundle is prefixed with
94a colon, to distinguish it from an actual feature. At present, the
95only feature bundle is C<use feature ":5.10">, which is equivalent
712d05cf 96to C<use feature qw(switch ~~ say err state)>.
bc9b29db 97
0d863452 98=cut
99
100sub import {
0d863452 101 my $class = shift;
102 if (@_ == 0) {
103 require Carp;
104 Carp->import("croak");
105 croak("No features specified");
106 }
107 while (@_) {
108 my $name = shift(@_);
bc9b29db 109 if ($name =~ /^:(.*)/) {
110 if (!exists $feature_bundle{$1}) {
111 require Carp;
112 Carp->import("croak");
113 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
114 $1, $^V));
115 }
116 unshift @_, @{$feature_bundle{$1}};
117 next;
118 }
0d863452 119 if (!exists $feature{$name}) {
120 require Carp;
121 Carp->import("croak");
122 croak(sprintf('Feature "%s" is not supported by Perl %vd',
123 $name, $^V));
124 }
125 $^H{$feature{$name}} = 1;
126 }
127}
128
129sub unimport {
130 my $class = shift;
131
132 # A bare C<no feature> should disable *all* features
bc9b29db 133 if (!@_) {
134 delete @^H{ values(%feature) };
135 return;
136 }
137
138 while (@_) {
139 my $name = shift;
140 if ($name =~ /^:(.*)/) {
141 if (!exists $feature_bundle{$1}) {
142 require Carp;
143 Carp->import("croak");
144 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
145 $1, $^V));
146 }
147 unshift @_, @{$feature_bundle{$1}};
148 next;
149 }
0d863452 150 if (!exists($feature{$name})) {
151 require Carp;
152 Carp->import("croak");
153 croak(sprintf('Feature "%s" is not supported by Perl %vd',
154 $name, $^V));
155 }
156 else {
157 delete $^H{$feature{$name}};
158 }
159 }
0d863452 160}
161
1621;