This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Resort MANIFEST for 95318fb6
[perl5.git] / cpan / experimental / lib / experimental.pm
CommitLineData
de84ff2b 1package experimental;
af9caa01 2$experimental::VERSION = '0.014';
de84ff2b
RS
3use strict;
4use warnings;
edd0583e 5use version ();
de84ff2b
RS
6
7use feature ();
8use Carp qw/croak carp/;
9
10my %warnings = map { $_ => 1 } grep { /^experimental::/ } keys %warnings::Offsets;
f8c3bb9c
CBW
11my %features = map { $_ => 1 } $] > 5.015006 ? keys %feature::feature : do {
12 my @features;
13 if ($] >= 5.010) {
14 push @features, qw/switch say state/;
15 push @features, 'unicode_strings' if $] > 5.011002;
16 }
17 @features;
18};
de84ff2b
RS
19
20my %min_version = (
f8c3bb9c
CBW
21 array_base => '5',
22 autoderef => '5.14.0',
af9caa01 23 bitwise => '5.22.0',
f8c3bb9c
CBW
24 current_sub => '5.16.0',
25 evalbytes => '5.16.0',
26 fc => '5.16.0',
27 lexical_topic => '5.10.0',
28 lexical_subs => '5.18.0',
29 postderef => '5.20.0',
30 postderef_qq => '5.20.0',
af9caa01 31 refaliasing => '5.22.0',
f8c3bb9c
CBW
32 regex_sets => '5.18.0',
33 say => '5.10.0',
34 smartmatch => '5.10.0',
35 signatures => '5.20.0',
36 state => '5.10.0',
37 switch => '5.10.0',
38 unicode_eval => '5.16.0',
39 unicode_strings => '5.12.0',
de84ff2b 40);
f8c3bb9c 41$_ = version->new($_) for values %min_version;
de84ff2b
RS
42
43my %additional = (
44 postderef => ['postderef_qq'],
45 switch => ['smartmatch'],
46);
47
48sub _enable {
49 my $pragma = shift;
50 if ($warnings{"experimental::$pragma"}) {
51 warnings->unimport("experimental::$pragma");
52 feature->import($pragma) if exists $features{$pragma};
53 _enable(@{ $additional{$pragma} }) if $additional{$pragma};
54 }
55 elsif ($features{$pragma}) {
56 feature->import($pragma);
57 _enable(@{ $additional{$pragma} }) if $additional{$pragma};
58 }
59 elsif (not exists $min_version{$pragma}) {
60 croak "Can't enable unknown feature $pragma";
61 }
62 elsif ($min_version{$pragma} > $]) {
9c71c2c5
CBW
63 my $stable = $min_version{$pragma};
64 if ($stable->{version}[1] % 2) {
65 $stable = version->new(
66 "5.".($stable->{version}[1]+1).'.0'
67 );
68 }
69 croak "Need perl $stable or later for feature $pragma";
de84ff2b
RS
70 }
71}
72
73sub import {
74 my ($self, @pragmas) = @_;
75
76 for my $pragma (@pragmas) {
77 _enable($pragma);
78 }
79 return;
80}
81
82sub _disable {
83 my $pragma = shift;
84 if ($warnings{"experimental::$pragma"}) {
85 warnings->import("experimental::$pragma");
86 feature->unimport($pragma) if exists $features{$pragma};
87 _disable(@{ $additional{$pragma} }) if $additional{$pragma};
88 }
89 elsif ($features{$pragma}) {
90 feature->unimport($pragma);
91 _disable(@{ $additional{$pragma} }) if $additional{$pragma};
92 }
93 elsif (not exists $min_version{$pragma}) {
94 carp "Can't disable unknown feature $pragma, ignoring";
95 }
96}
97
98sub unimport {
99 my ($self, @pragmas) = @_;
100
101 for my $pragma (@pragmas) {
102 _disable($pragma);
103 }
104 return;
105}
106
1071;
108
109#ABSTRACT: Experimental features made easy
110
111__END__
112
113=pod
114
115=encoding UTF-8
116
117=head1 NAME
118
119experimental - Experimental features made easy
120
121=head1 VERSION
122
af9caa01 123version 0.014
de84ff2b
RS
124
125=head1 SYNOPSIS
126
127 use experimental 'lexical_subs', 'smartmatch';
128 my sub foo { $_[0] ~~ 1 }
129
130=head1 DESCRIPTION
131
132This pragma provides an easy and convenient way to enable or disable
133experimental features.
134
135Every version of perl has some number of features present but considered
136"experimental." For much of the life of Perl 5, this was only a designation
137found in the documentation. Starting in Perl v5.10.0, and more aggressively in
138v5.18.0, experimental features were placed behind pragmata used to enable the
139feature and disable associated warnings.
140
141The C<experimental> pragma exists to combine the required incantations into a
142single interface stable across releases of perl. For every experimental
143feature, this should enable the feature and silence warnings for the enclosing
144lexical scope:
145
146 use experimental 'feature-name';
147
148To disable the feature and, if applicable, re-enable any warnings, use:
149
150 no experimental 'feature-name';
151
152The supported features, documented further below, are:
153
154 array_base - allow the use of $[ to change the starting index of @array
155 autoderef - allow push, each, keys, and other built-ins on references
156 lexical_topic - allow the use of lexical $_ via "my $_"
157 postderef - allow the use of postfix dereferencing expressions, including
158 in interpolating strings
e87ace2e 159 refaliasing - allow aliasing via \$x = \$y
de84ff2b
RS
160 regex_sets - allow extended bracketed character classes in regexps
161 signatures - allow subroutine signatures (for named arguments)
edd0583e
CBW
162 smartmatch - allow the use of ~~
163 switch - allow the use of ~~, given, and when
de84ff2b 164
4177cbce
CBW
165=head2 Ordering matters
166
167Using this pragma to 'enable an experimental feature' is another way of saying
168that this pragma will disable the warnings which would result from using that
169feature. Therefore, the order in which pragmas are applied is important. In
170particular, you probably want to enable experimental features I<after> you
171enable warnings:
172
173 use warnings;
174 use experimental 'smartmatch';
175
176You also need to take care with modules that enable warnings for you. A common
177example being Moose. In this example, warnings for the 'smartmatch' feature are
178first turned on by the warnings pragma, off by the experimental pragma and back
179on again by the Moose module (fix is to switch the last two lines):
180
181 use warnings;
182 use experimental 'smartmatch';
183 use Moose;
184
de84ff2b
RS
185=head2 Disclaimer
186
187Because of the nature of the features it enables, forward compatibility can not
188be guaranteed in any way.
189
190=head1 AUTHOR
191
192Leon Timmermans <leont@cpan.org>
193
194=head1 COPYRIGHT AND LICENSE
195
196This software is copyright (c) 2013 by Leon Timmermans.
197
198This is free software; you can redistribute it and/or modify it under
199the same terms as the Perl 5 programming language system itself.
200
201=cut