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