Commit | Line | Data |
---|---|---|
de84ff2b | 1 | package experimental; |
edd0583e | 2 | $experimental::VERSION = '0.008'; |
de84ff2b RS |
3 | use strict; |
4 | use warnings; | |
edd0583e | 5 | use version (); |
de84ff2b RS |
6 | |
7 | use feature (); | |
8 | use Carp qw/croak carp/; | |
9 | ||
10 | my %warnings = map { $_ => 1 } grep { /^experimental::/ } keys %warnings::Offsets; | |
11 | my %features = map { $_ => 1 } keys %feature::feature; | |
12 | ||
13 | my %min_version = ( | |
edd0583e CBW |
14 | array_base => version->new('5'), |
15 | autoderef => version->new('5.14.0'), | |
16 | lexical_topic => version->new('5.10.0'), | |
17 | regex_sets => version->new('5.18.0'), | |
18 | smartmatch => version->new('5.10.1'), | |
19 | signatures => version->new('5.20.0'), | |
de84ff2b RS |
20 | ); |
21 | ||
22 | my %additional = ( | |
23 | postderef => ['postderef_qq'], | |
24 | switch => ['smartmatch'], | |
25 | ); | |
26 | ||
27 | sub _enable { | |
28 | my $pragma = shift; | |
29 | if ($warnings{"experimental::$pragma"}) { | |
30 | warnings->unimport("experimental::$pragma"); | |
31 | feature->import($pragma) if exists $features{$pragma}; | |
32 | _enable(@{ $additional{$pragma} }) if $additional{$pragma}; | |
33 | } | |
34 | elsif ($features{$pragma}) { | |
35 | feature->import($pragma); | |
36 | _enable(@{ $additional{$pragma} }) if $additional{$pragma}; | |
37 | } | |
38 | elsif (not exists $min_version{$pragma}) { | |
39 | croak "Can't enable unknown feature $pragma"; | |
40 | } | |
41 | elsif ($min_version{$pragma} > $]) { | |
edd0583e | 42 | croak "Need perl $min_version{$pragma} or later for feature $pragma"; |
de84ff2b RS |
43 | } |
44 | } | |
45 | ||
46 | sub import { | |
47 | my ($self, @pragmas) = @_; | |
48 | ||
49 | for my $pragma (@pragmas) { | |
50 | _enable($pragma); | |
51 | } | |
52 | return; | |
53 | } | |
54 | ||
55 | sub _disable { | |
56 | my $pragma = shift; | |
57 | if ($warnings{"experimental::$pragma"}) { | |
58 | warnings->import("experimental::$pragma"); | |
59 | feature->unimport($pragma) if exists $features{$pragma}; | |
60 | _disable(@{ $additional{$pragma} }) if $additional{$pragma}; | |
61 | } | |
62 | elsif ($features{$pragma}) { | |
63 | feature->unimport($pragma); | |
64 | _disable(@{ $additional{$pragma} }) if $additional{$pragma}; | |
65 | } | |
66 | elsif (not exists $min_version{$pragma}) { | |
67 | carp "Can't disable unknown feature $pragma, ignoring"; | |
68 | } | |
69 | } | |
70 | ||
71 | sub unimport { | |
72 | my ($self, @pragmas) = @_; | |
73 | ||
74 | for my $pragma (@pragmas) { | |
75 | _disable($pragma); | |
76 | } | |
77 | return; | |
78 | } | |
79 | ||
80 | 1; | |
81 | ||
82 | #ABSTRACT: Experimental features made easy | |
83 | ||
84 | __END__ | |
85 | ||
86 | =pod | |
87 | ||
88 | =encoding UTF-8 | |
89 | ||
90 | =head1 NAME | |
91 | ||
92 | experimental - Experimental features made easy | |
93 | ||
94 | =head1 VERSION | |
95 | ||
edd0583e | 96 | version 0.008 |
de84ff2b RS |
97 | |
98 | =head1 SYNOPSIS | |
99 | ||
100 | use experimental 'lexical_subs', 'smartmatch'; | |
101 | my sub foo { $_[0] ~~ 1 } | |
102 | ||
103 | =head1 DESCRIPTION | |
104 | ||
105 | This pragma provides an easy and convenient way to enable or disable | |
106 | experimental features. | |
107 | ||
108 | Every version of perl has some number of features present but considered | |
109 | "experimental." For much of the life of Perl 5, this was only a designation | |
110 | found in the documentation. Starting in Perl v5.10.0, and more aggressively in | |
111 | v5.18.0, experimental features were placed behind pragmata used to enable the | |
112 | feature and disable associated warnings. | |
113 | ||
114 | The C<experimental> pragma exists to combine the required incantations into a | |
115 | single interface stable across releases of perl. For every experimental | |
116 | feature, this should enable the feature and silence warnings for the enclosing | |
117 | lexical scope: | |
118 | ||
119 | use experimental 'feature-name'; | |
120 | ||
121 | To disable the feature and, if applicable, re-enable any warnings, use: | |
122 | ||
123 | no experimental 'feature-name'; | |
124 | ||
125 | The supported features, documented further below, are: | |
126 | ||
127 | array_base - allow the use of $[ to change the starting index of @array | |
128 | autoderef - allow push, each, keys, and other built-ins on references | |
129 | lexical_topic - allow the use of lexical $_ via "my $_" | |
130 | postderef - allow the use of postfix dereferencing expressions, including | |
131 | in interpolating strings | |
132 | regex_sets - allow extended bracketed character classes in regexps | |
133 | signatures - allow subroutine signatures (for named arguments) | |
edd0583e CBW |
134 | smartmatch - allow the use of ~~ |
135 | switch - allow the use of ~~, given, and when | |
de84ff2b RS |
136 | |
137 | =head2 Disclaimer | |
138 | ||
139 | Because of the nature of the features it enables, forward compatibility can not | |
140 | be guaranteed in any way. | |
141 | ||
142 | =head1 AUTHOR | |
143 | ||
144 | Leon Timmermans <leont@cpan.org> | |
145 | ||
146 | =head1 COPYRIGHT AND LICENSE | |
147 | ||
148 | This software is copyright (c) 2013 by Leon Timmermans. | |
149 | ||
150 | This is free software; you can redistribute it and/or modify it under | |
151 | the same terms as the Perl 5 programming language system itself. | |
152 | ||
153 | =cut |