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