Commit | Line | Data |
---|---|---|
0d863452 RH |
1 | package feature; |
2 | ||
3 | our $VERSION = '1.00'; | |
4 | $feature::hint_bits = 0x04020000; # HINT_LOCALIZE_HH | HINT_HH_FOR_EVAL | |
5 | ||
6 | # (feature name) => (internal name, used in %^H) | |
7 | my %feature = ( | |
7b9ef140 RH |
8 | switch => 'feature_switch', |
9 | "~~" => "feature_~~", | |
10 | say => "feature_say", | |
0d863452 RH |
11 | ); |
12 | ||
13 | ||
14 | # Here are some notes that probably shouldn't be in the public | |
15 | # documentation, but which it's useful to have somewhere. | |
16 | # | |
17 | # One side-effect of the change is that C<prototype("CORE::continue")> | |
18 | # no longer throws the error C<Can't find an opnumber for "continue">. | |
19 | # One of the tests in t/op/cproto.t had to be changed to accommodate | |
20 | # this, but it really shouldn't affect real-world code. | |
21 | # | |
22 | # TODO: | |
23 | # - sort out the smartmatch semantics | |
24 | # - think about versioned features (use switch => 2) | |
25 | # | |
26 | # -- Robin 2005-12 | |
27 | ||
28 | =head1 NAME | |
29 | ||
30 | feature - Perl pragma to enable new syntactic features | |
31 | ||
32 | =head1 SYNOPSIS | |
33 | ||
34 | use feature 'switch'; | |
35 | given ($foo) { | |
36 | when (1) { print "\$foo == 1\n" } | |
37 | when ([2,3]) { print "\$foo == 2 || \$foo == 3\n" } | |
38 | when (/^a[bc]d$/) { print "\$foo eq 'abd' || \$foo eq 'acd'\n" } | |
39 | when ($_ > 100) { print "\$foo > 100\n" } | |
40 | default { print "None of the above\n" } | |
41 | } | |
42 | ||
43 | =head1 DESCRIPTION | |
44 | ||
45 | It is usually impossible to add new syntax to Perl without breaking | |
46 | some existing programs. This pragma provides a way to minimize that | |
47 | risk. New syntactic constructs can be enabled by C<use feature 'foo'>, | |
48 | and will be parsed only when the appropriate feature pragma is in | |
49 | scope. | |
50 | ||
51 | =head2 The 'switch' feature | |
52 | ||
53 | C<use feature 'switch'> tells the compiler to enable the Perl 6 | |
54 | given/when construct from here to the end of the enclosing BLOCK. | |
55 | ||
56 | See L<perlsyn/"Switch statements"> for details. | |
57 | ||
58 | =head2 The '~~' feature | |
59 | ||
60 | C<use feature '~~'> tells the compiler to enable the Perl 6 | |
61 | smart match C<~~> operator from here to the end of the enclosing BLOCK. | |
62 | ||
63 | See L<perlsyn/"Smart Matching in Detail"> for details. | |
64 | ||
65 | =head2 The 'say' feature | |
66 | ||
67 | C<use feature 'say'> tells the compiler to enable the Perl 6 | |
68 | C<say> function from here to the end of the enclosing BLOCK. | |
69 | ||
70 | See L<perlfunc/say> for details. | |
71 | ||
72 | =cut | |
73 | ||
74 | sub import { | |
75 | $^H |= $feature::hint_bits; # Need this or %^H won't work | |
76 | ||
77 | my $class = shift; | |
78 | if (@_ == 0) { | |
79 | require Carp; | |
80 | Carp->import("croak"); | |
81 | croak("No features specified"); | |
82 | } | |
83 | while (@_) { | |
84 | my $name = shift(@_); | |
85 | if (!exists $feature{$name}) { | |
86 | require Carp; | |
87 | Carp->import("croak"); | |
88 | croak(sprintf('Feature "%s" is not supported by Perl %vd', | |
89 | $name, $^V)); | |
90 | } | |
91 | $^H{$feature{$name}} = 1; | |
92 | } | |
93 | } | |
94 | ||
95 | sub unimport { | |
96 | my $class = shift; | |
97 | ||
98 | # A bare C<no feature> should disable *all* features | |
99 | for my $name (@_) { | |
100 | if (!exists($feature{$name})) { | |
101 | require Carp; | |
102 | Carp->import("croak"); | |
103 | croak(sprintf('Feature "%s" is not supported by Perl %vd', | |
104 | $name, $^V)); | |
105 | } | |
106 | else { | |
107 | delete $^H{$feature{$name}}; | |
108 | } | |
109 | } | |
110 | ||
111 | if(!@_) { | |
112 | delete @^H{ values(%feature) }; | |
113 | } | |
114 | } | |
115 | ||
116 | 1; |