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