1package experimental;
2$experimental::VERSION = '0.031';
3use strict;
4use warnings;
5use version ();
6
7BEGIN { eval { require feature } };
8use Carp qw/croak carp/;
9
10my %warnings = map { $_ => 1 } grep { /^experimental::/ } keys %warnings::Offsets;
11my %removed_warnings = map { $_ => 1 } grep { /^experimental::/ } keys %warnings::NoOp;
12my %features = map { $_ => 1 } $] > 5.015006 ? keys %feature::feature : do {
13	my @features;
14	if ($] >= 5.010) {
15		push @features, qw/switch say state/;
16		push @features, 'unicode_strings' if $] > 5.011002;
17	}
18	@features;
19};
20
21my %min_version = (
22	args_array_with_signatures => '5.20.0',
23	array_base      => '5',
24	autoderef       => '5.14.0',
25	bitwise         => '5.22.0',
26	builtin         => '5.35.7',
27	const_attr      => '5.22.0',
28	current_sub     => '5.16.0',
29	declared_refs   => '5.26.0',
30	defer           => '5.35.4',
31	evalbytes       => '5.16.0',
32	extra_paired_delimiters => '5.35.9',
33	fc              => '5.16.0',
34	for_list        => '5.35.5',
35	isa             => '5.31.7',
36	lexical_topic   => '5.10.0',
37	lexical_subs    => '5.18.0',
38	postderef       => '5.20.0',
39	postderef_qq    => '5.20.0',
40	refaliasing     => '5.22.0',
41	regex_sets      => '5.18.0',
42	say             => '5.10.0',
43	smartmatch      => '5.10.0',
44	signatures      => '5.20.0',
45	state           => '5.10.0',
46	switch          => '5.10.0',
47	try             => '5.34.0',
48	unicode_eval    => '5.16.0',
49	unicode_strings => '5.12.0',
50);
51my %removed_in_version = (
52	array_base      => '5.30.0',
53	autoderef       => '5.24.0',
54	lexical_topic   => '5.24.0',
55);
56
57$_ = version->new($_) for values %min_version;
58$_ = version->new($_) for values %removed_in_version;
59
60my %additional = (
61	postderef     => ['postderef_qq'],
62	switch        => ['smartmatch'],
63	declared_refs => ['refaliasing'],
64);
65
66sub _enable {
67	my $pragma = shift;
68	if ($warnings{"experimental::$pragma"}) {
69		warnings->unimport("experimental::$pragma");
70		feature->import($pragma) if exists $features{$pragma};
71		_enable(@{ $additional{$pragma} }) if $additional{$pragma};
72	}
73	elsif ($features{$pragma}) {
74		feature->import($pragma);
75		_enable(@{ $additional{$pragma} }) if $additional{$pragma};
76	}
77	elsif ($removed_warnings{"experimental::$pragma"}) {
78		_enable(@{ $additional{$pragma} }) if $additional{$pragma};
79	}
80	elsif (not exists $min_version{$pragma}) {
81		croak "Can't enable unknown feature $pragma";
82	}
83	elsif ($] < $min_version{$pragma}) {
84		my $stable = $min_version{$pragma}->stringify;
85		$stable =~ s/^ 5\. ([0-9]?[13579]) \. \d+ $/"5." . ($1 + 1) . ".0"/xe;
86		croak "Need perl $stable or later for feature $pragma";
87	}
88	elsif ($] >= ($removed_in_version{$pragma} || 7)) {
89		croak "Experimental feature $pragma has been removed from perl in version $removed_in_version{$pragma}";
90	}
91}
92
93sub import {
94	my ($self, @pragmas) = @_;
95
96	for my $pragma (@pragmas) {
97		_enable($pragma);
98	}
99	return;
100}
101
102sub _disable {
103	my $pragma = shift;
104	if ($warnings{"experimental::$pragma"}) {
105		warnings->import("experimental::$pragma");
106		feature->unimport($pragma) if exists $features{$pragma};
107		_disable(@{ $additional{$pragma} }) if $additional{$pragma};
108	}
109	elsif ($features{$pragma}) {
110		feature->unimport($pragma);
111		_disable(@{ $additional{$pragma} }) if $additional{$pragma};
112	}
113	elsif (not exists $min_version{$pragma}) {
114		carp "Can't disable unknown feature $pragma, ignoring";
115	}
116}
117
118sub unimport {
119	my ($self, @pragmas) = @_;
120
121	for my $pragma (@pragmas) {
122		_disable($pragma);
123	}
124	return;
125}
126
1271;
128
129#ABSTRACT: Experimental features made easy
130
131__END__
132
133=pod
134
135=encoding UTF-8
136
137=head1 NAME
138
139experimental - Experimental features made easy
140
141=head1 VERSION
142
143version 0.031
144
145=head1 SYNOPSIS
146
147 use experimental 'lexical_subs', 'signatures';
148 my sub plus_one($value) { $value + 1 }
149
150=head1 DESCRIPTION
151
152This pragma provides an easy and convenient way to enable or disable
153experimental features.
154
155Every version of perl has some number of features present but considered
156"experimental."  For much of the life of Perl 5, this was only a designation
157found in the documentation.  Starting in Perl v5.10.0, and more aggressively in
158v5.18.0, experimental features were placed behind pragmata used to enable the
159feature and disable associated warnings.
160
161The C<experimental> pragma exists to combine the required incantations into a
162single interface stable across releases of perl.  For every experimental
163feature, this should enable the feature and silence warnings for the enclosing
164lexical scope:
165
166  use experimental 'feature-name';
167
168To disable the feature and, if applicable, re-enable any warnings, use:
169
170  no experimental 'feature-name';
171
172The supported features, documented further below, are:
173
174=over 4
175
176=item * C<args_array_with_signatures> - allow C<@_> to be used in signatured subs.
177
178This is supported on perl 5.20.0 and above, but is likely to be removed in the future.
179
180=item * C<array_base> - allow the use of C<$[> to change the starting index of C<@array>.
181
182This was removed in perl 5.30.0.
183
184=item * C<autoderef> - allow push, each, keys, and other built-ins on references.
185
186This was added in perl 5.14.0 and removed in perl 5.24.0.
187
188=item * C<bitwise> - allow the new stringwise bit operators
189
190This was added in perl 5.22.0.
191
192=item * C<builtin> - allow the use of the functions in the builtin:: namespace
193
194This was added in perl 5.36.0
195
196=item * C<const_attr> - allow the :const attribute on subs
197
198This was added in perl 5.22.0.
199
200=item * C<declared_refs> - enables aliasing via assignment to references
201
202This was added in perl 5.26.0.
203
204=item * C<defer> - enables the use of defer blocks
205
206This was added in perl 5.36.0
207
208=item * C<extra_paired_delimiters> - enables the use of more paired string delimiters than the
209traditional four, S<C<< <  > >>>, S<C<( )>>, S<C<{ }>>, and S<C<[ ]>>.
210
211This was added in perl 5.36.
212
213=item * C<for_list> - allows iterating over multiple values at a time with C<for>
214
215This was added in perl 5.36.0
216
217=item * C<isa> - allow the use of the C<isa> infix operator
218
219This was added in perl 5.32.0.
220
221=item * C<lexical_topic> - allow the use of lexical C<$_> via C<my $_>.
222
223This was added in perl 5.10.0 and removed in perl 5.24.0.
224
225=item * C<lexical_subs> - allow the use of lexical subroutines.
226
227This was added in 5.18.0, and became non-experimental (and always enabled) in 5.26.0.
228
229=item * C<postderef> - allow the use of postfix dereferencing expressions
230
231This was added in perl 5.20.0, and became non-experimental (and always enabled) in 5.24.0.
232
233=item * C<postderef_qq> - allow the use of postfix dereferencing expressions inside interpolating strings
234
235This was added in perl 5.20.0, and became non-experimental (and always enabled) in 5.24.0.
236
237=item * C<re_strict> - enables strict mode in regular expressions
238
239This was added in perl 5.22.0.
240
241=item * C<refaliasing> - allow aliasing via C<\$x = \$y>
242
243This was added in perl 5.22.0.
244
245=item * C<regex_sets> - allow extended bracketed character classes in regexps
246
247This was added in perl 5.18.0.
248
249=item * C<signatures> - allow subroutine signatures (for named arguments)
250
251This was added in perl 5.20.0.
252
253=item * C<smartmatch> - allow the use of C<~~>
254
255This was added in perl 5.10.0, but it should be noted there are significant
256incompatibilities between 5.10.0 and 5.10.1.
257
258The feature is going to be deprecated in perl 5.38.0, and removed in 5.42.0.
259
260=item * C<switch> - allow the use of C<~~>, given, and when
261
262This was added in perl 5.10.0.
263
264The feature is going to be deprecated in perl 5.38.0, and removed in 5.42.0.
265
266=item * C<try> - allow the use of C<try> and C<catch>
267
268This was added in perl 5.34.0
269
270=item * C<win32_perlio> - allows the use of the :win32 IO layer.
271
272This was added on perl 5.22.0.
273
274=back
275
276=head2 Ordering matters
277
278Using this pragma to 'enable an experimental feature' is another way of saying
279that this pragma will disable the warnings which would result from using that
280feature.  Therefore, the order in which pragmas are applied is important.  In
281particular, you probably want to enable experimental features I<after> you
282enable warnings:
283
284  use warnings;
285  use experimental 'smartmatch';
286
287You also need to take care with modules that enable warnings for you.  A common
288example being Moose.  In this example, warnings for the 'smartmatch' feature are
289first turned on by the warnings pragma, off by the experimental pragma and back
290on again by the Moose module (fix is to switch the last two lines):
291
292  use warnings;
293  use experimental 'smartmatch';
294  use Moose;
295
296=head2 Disclaimer
297
298Because of the nature of the features it enables, forward compatibility can not
299be guaranteed in any way.
300
301=head1 SEE ALSO
302
303L<perlexperiment|perlexperiment> contains more information about experimental features.
304
305=head1 AUTHOR
306
307Leon Timmermans <leont@cpan.org>
308
309=head1 COPYRIGHT AND LICENSE
310
311This software is copyright (c) 2013 by Leon Timmermans.
312
313This is free software; you can redistribute it and/or modify it under
314the same terms as the Perl 5 programming language system itself.
315
316=cut
317