xref: /openbsd-src/gnu/usr.bin/perl/cpan/experimental/lib/experimental.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
16fb12b70Safresh1package experimental;
2*3d61058aSafresh1$experimental::VERSION = '0.032';
36fb12b70Safresh1use strict;
46fb12b70Safresh1use warnings;
5b8851fccSafresh1use version ();
66fb12b70Safresh1
79f11ffb7Safresh1BEGIN { eval { require feature } };
86fb12b70Safresh1use Carp qw/croak carp/;
96fb12b70Safresh1
106fb12b70Safresh1my %warnings = map { $_ => 1 } grep { /^experimental::/ } keys %warnings::Offsets;
11e0680481Safresh1my %removed_warnings = map { $_ => 1 } grep { /^experimental::/ } keys %warnings::NoOp;
12b8851fccSafresh1my %features = map { $_ => 1 } $] > 5.015006 ? keys %feature::feature : do {
13b8851fccSafresh1	my @features;
14b8851fccSafresh1	if ($] >= 5.010) {
15b8851fccSafresh1		push @features, qw/switch say state/;
16b8851fccSafresh1		push @features, 'unicode_strings' if $] > 5.011002;
17b8851fccSafresh1	}
18b8851fccSafresh1	@features;
19b8851fccSafresh1};
206fb12b70Safresh1
216fb12b70Safresh1my %min_version = (
22eac174f2Safresh1	args_array_with_signatures => '5.20.0',
23b8851fccSafresh1	array_base      => '5',
24b8851fccSafresh1	autoderef       => '5.14.0',
25b8851fccSafresh1	bitwise         => '5.22.0',
26eac174f2Safresh1	builtin         => '5.35.7',
279f11ffb7Safresh1	const_attr      => '5.22.0',
28b8851fccSafresh1	current_sub     => '5.16.0',
29b46d8ef2Safresh1	declared_refs   => '5.26.0',
30eac174f2Safresh1	defer           => '5.35.4',
31b8851fccSafresh1	evalbytes       => '5.16.0',
32e0680481Safresh1	extra_paired_delimiters => '5.35.9',
33b8851fccSafresh1	fc              => '5.16.0',
34eac174f2Safresh1	for_list        => '5.35.5',
35eac174f2Safresh1	isa             => '5.31.7',
36b8851fccSafresh1	lexical_topic   => '5.10.0',
37b8851fccSafresh1	lexical_subs    => '5.18.0',
38b8851fccSafresh1	postderef       => '5.20.0',
39b8851fccSafresh1	postderef_qq    => '5.20.0',
40b8851fccSafresh1	refaliasing     => '5.22.0',
41b8851fccSafresh1	regex_sets      => '5.18.0',
42b8851fccSafresh1	say             => '5.10.0',
43b8851fccSafresh1	smartmatch      => '5.10.0',
44b8851fccSafresh1	signatures      => '5.20.0',
45b8851fccSafresh1	state           => '5.10.0',
46b8851fccSafresh1	switch          => '5.10.0',
47eac174f2Safresh1	try             => '5.34.0',
48b8851fccSafresh1	unicode_eval    => '5.16.0',
49b8851fccSafresh1	unicode_strings => '5.12.0',
506fb12b70Safresh1);
51eac174f2Safresh1my %removed_in_version = (
52eac174f2Safresh1	array_base      => '5.30.0',
53eac174f2Safresh1	autoderef       => '5.24.0',
54eac174f2Safresh1	lexical_topic   => '5.24.0',
55b8851fccSafresh1);
56b8851fccSafresh1
57b8851fccSafresh1$_ = version->new($_) for values %min_version;
58eac174f2Safresh1$_ = version->new($_) for values %removed_in_version;
596fb12b70Safresh1
606fb12b70Safresh1my %additional = (
616fb12b70Safresh1	postderef     => ['postderef_qq'],
626fb12b70Safresh1	switch        => ['smartmatch'],
63b46d8ef2Safresh1	declared_refs => ['refaliasing'],
646fb12b70Safresh1);
656fb12b70Safresh1
666fb12b70Safresh1sub _enable {
676fb12b70Safresh1	my $pragma = shift;
686fb12b70Safresh1	if ($warnings{"experimental::$pragma"}) {
696fb12b70Safresh1		warnings->unimport("experimental::$pragma");
706fb12b70Safresh1		feature->import($pragma) if exists $features{$pragma};
716fb12b70Safresh1		_enable(@{ $additional{$pragma} }) if $additional{$pragma};
726fb12b70Safresh1	}
736fb12b70Safresh1	elsif ($features{$pragma}) {
746fb12b70Safresh1		feature->import($pragma);
756fb12b70Safresh1		_enable(@{ $additional{$pragma} }) if $additional{$pragma};
766fb12b70Safresh1	}
77e0680481Safresh1	elsif ($removed_warnings{"experimental::$pragma"}) {
78e0680481Safresh1		_enable(@{ $additional{$pragma} }) if $additional{$pragma};
79e0680481Safresh1	}
806fb12b70Safresh1	elsif (not exists $min_version{$pragma}) {
816fb12b70Safresh1		croak "Can't enable unknown feature $pragma";
826fb12b70Safresh1	}
83b8851fccSafresh1	elsif ($] < $min_version{$pragma}) {
84eac174f2Safresh1		my $stable = $min_version{$pragma}->stringify;
85eac174f2Safresh1		$stable =~ s/^ 5\. ([0-9]?[13579]) \. \d+ $/"5." . ($1 + 1) . ".0"/xe;
86b8851fccSafresh1		croak "Need perl $stable or later for feature $pragma";
87b8851fccSafresh1	}
88eac174f2Safresh1	elsif ($] >= ($removed_in_version{$pragma} || 7)) {
89eac174f2Safresh1		croak "Experimental feature $pragma has been removed from perl in version $removed_in_version{$pragma}";
906fb12b70Safresh1	}
916fb12b70Safresh1}
926fb12b70Safresh1
936fb12b70Safresh1sub import {
946fb12b70Safresh1	my ($self, @pragmas) = @_;
956fb12b70Safresh1
966fb12b70Safresh1	for my $pragma (@pragmas) {
976fb12b70Safresh1		_enable($pragma);
986fb12b70Safresh1	}
996fb12b70Safresh1	return;
1006fb12b70Safresh1}
1016fb12b70Safresh1
1026fb12b70Safresh1sub _disable {
1036fb12b70Safresh1	my $pragma = shift;
1046fb12b70Safresh1	if ($warnings{"experimental::$pragma"}) {
1056fb12b70Safresh1		warnings->import("experimental::$pragma");
1066fb12b70Safresh1		feature->unimport($pragma) if exists $features{$pragma};
1076fb12b70Safresh1		_disable(@{ $additional{$pragma} }) if $additional{$pragma};
1086fb12b70Safresh1	}
1096fb12b70Safresh1	elsif ($features{$pragma}) {
1106fb12b70Safresh1		feature->unimport($pragma);
1116fb12b70Safresh1		_disable(@{ $additional{$pragma} }) if $additional{$pragma};
1126fb12b70Safresh1	}
1136fb12b70Safresh1	elsif (not exists $min_version{$pragma}) {
1146fb12b70Safresh1		carp "Can't disable unknown feature $pragma, ignoring";
1156fb12b70Safresh1	}
1166fb12b70Safresh1}
1176fb12b70Safresh1
1186fb12b70Safresh1sub unimport {
1196fb12b70Safresh1	my ($self, @pragmas) = @_;
1206fb12b70Safresh1
1216fb12b70Safresh1	for my $pragma (@pragmas) {
1226fb12b70Safresh1		_disable($pragma);
1236fb12b70Safresh1	}
1246fb12b70Safresh1	return;
1256fb12b70Safresh1}
1266fb12b70Safresh1
1276fb12b70Safresh11;
1286fb12b70Safresh1
1296fb12b70Safresh1#ABSTRACT: Experimental features made easy
1306fb12b70Safresh1
1316fb12b70Safresh1__END__
1326fb12b70Safresh1
1336fb12b70Safresh1=pod
1346fb12b70Safresh1
1356fb12b70Safresh1=encoding UTF-8
1366fb12b70Safresh1
1376fb12b70Safresh1=head1 NAME
1386fb12b70Safresh1
1396fb12b70Safresh1experimental - Experimental features made easy
1406fb12b70Safresh1
1416fb12b70Safresh1=head1 VERSION
1426fb12b70Safresh1
143*3d61058aSafresh1version 0.032
1446fb12b70Safresh1
1456fb12b70Safresh1=head1 SYNOPSIS
1466fb12b70Safresh1
147eac174f2Safresh1 use experimental 'lexical_subs', 'signatures';
148eac174f2Safresh1 my sub plus_one($value) { $value + 1 }
1496fb12b70Safresh1
1506fb12b70Safresh1=head1 DESCRIPTION
1516fb12b70Safresh1
1526fb12b70Safresh1This pragma provides an easy and convenient way to enable or disable
1536fb12b70Safresh1experimental features.
1546fb12b70Safresh1
1556fb12b70Safresh1Every version of perl has some number of features present but considered
1566fb12b70Safresh1"experimental."  For much of the life of Perl 5, this was only a designation
1576fb12b70Safresh1found in the documentation.  Starting in Perl v5.10.0, and more aggressively in
1586fb12b70Safresh1v5.18.0, experimental features were placed behind pragmata used to enable the
1596fb12b70Safresh1feature and disable associated warnings.
1606fb12b70Safresh1
1616fb12b70Safresh1The C<experimental> pragma exists to combine the required incantations into a
1626fb12b70Safresh1single interface stable across releases of perl.  For every experimental
1636fb12b70Safresh1feature, this should enable the feature and silence warnings for the enclosing
1646fb12b70Safresh1lexical scope:
1656fb12b70Safresh1
1666fb12b70Safresh1  use experimental 'feature-name';
1676fb12b70Safresh1
1686fb12b70Safresh1To disable the feature and, if applicable, re-enable any warnings, use:
1696fb12b70Safresh1
1706fb12b70Safresh1  no experimental 'feature-name';
1716fb12b70Safresh1
1726fb12b70Safresh1The supported features, documented further below, are:
1736fb12b70Safresh1
1749f11ffb7Safresh1=over 4
1759f11ffb7Safresh1
176eac174f2Safresh1=item * C<args_array_with_signatures> - allow C<@_> to be used in signatured subs.
177eac174f2Safresh1
178eac174f2Safresh1This is supported on perl 5.20.0 and above, but is likely to be removed in the future.
179eac174f2Safresh1
1809f11ffb7Safresh1=item * C<array_base> - allow the use of C<$[> to change the starting index of C<@array>.
1819f11ffb7Safresh1
182eac174f2Safresh1This was removed in perl 5.30.0.
1839f11ffb7Safresh1
1849f11ffb7Safresh1=item * C<autoderef> - allow push, each, keys, and other built-ins on references.
1859f11ffb7Safresh1
186eac174f2Safresh1This was added in perl 5.14.0 and removed in perl 5.24.0.
1879f11ffb7Safresh1
1889f11ffb7Safresh1=item * C<bitwise> - allow the new stringwise bit operators
1899f11ffb7Safresh1
1909f11ffb7Safresh1This was added in perl 5.22.0.
1919f11ffb7Safresh1
192eac174f2Safresh1=item * C<builtin> - allow the use of the functions in the builtin:: namespace
193eac174f2Safresh1
194eac174f2Safresh1This was added in perl 5.36.0
195eac174f2Safresh1
1969f11ffb7Safresh1=item * C<const_attr> - allow the :const attribute on subs
1979f11ffb7Safresh1
1989f11ffb7Safresh1This was added in perl 5.22.0.
1999f11ffb7Safresh1
200eac174f2Safresh1=item * C<declared_refs> - enables aliasing via assignment to references
201eac174f2Safresh1
202eac174f2Safresh1This was added in perl 5.26.0.
203eac174f2Safresh1
204eac174f2Safresh1=item * C<defer> - enables the use of defer blocks
205eac174f2Safresh1
206eac174f2Safresh1This was added in perl 5.36.0
207eac174f2Safresh1
208e0680481Safresh1=item * C<extra_paired_delimiters> - enables the use of more paired string delimiters than the
209e0680481Safresh1traditional four, S<C<< <  > >>>, S<C<( )>>, S<C<{ }>>, and S<C<[ ]>>.
210e0680481Safresh1
211e0680481Safresh1This was added in perl 5.36.
212e0680481Safresh1
213eac174f2Safresh1=item * C<for_list> - allows iterating over multiple values at a time with C<for>
214eac174f2Safresh1
215eac174f2Safresh1This was added in perl 5.36.0
216eac174f2Safresh1
217eac174f2Safresh1=item * C<isa> - allow the use of the C<isa> infix operator
218eac174f2Safresh1
219eac174f2Safresh1This was added in perl 5.32.0.
220eac174f2Safresh1
2219f11ffb7Safresh1=item * C<lexical_topic> - allow the use of lexical C<$_> via C<my $_>.
2229f11ffb7Safresh1
223eac174f2Safresh1This was added in perl 5.10.0 and removed in perl 5.24.0.
2249f11ffb7Safresh1
2259f11ffb7Safresh1=item * C<lexical_subs> - allow the use of lexical subroutines.
2269f11ffb7Safresh1
227e0680481Safresh1This was added in 5.18.0, and became non-experimental (and always enabled) in 5.26.0.
2289f11ffb7Safresh1
229eac174f2Safresh1=item * C<postderef> - allow the use of postfix dereferencing expressions
2309f11ffb7Safresh1
231eac174f2Safresh1This was added in perl 5.20.0, and became non-experimental (and always enabled) in 5.24.0.
232eac174f2Safresh1
233eac174f2Safresh1=item * C<postderef_qq> - allow the use of postfix dereferencing expressions inside interpolating strings
234eac174f2Safresh1
235eac174f2Safresh1This was added in perl 5.20.0, and became non-experimental (and always enabled) in 5.24.0.
2369f11ffb7Safresh1
2379f11ffb7Safresh1=item * C<re_strict> - enables strict mode in regular expressions
2389f11ffb7Safresh1
2399f11ffb7Safresh1This was added in perl 5.22.0.
2409f11ffb7Safresh1
2419f11ffb7Safresh1=item * C<refaliasing> - allow aliasing via C<\$x = \$y>
2429f11ffb7Safresh1
2439f11ffb7Safresh1This was added in perl 5.22.0.
2449f11ffb7Safresh1
2459f11ffb7Safresh1=item * C<regex_sets> - allow extended bracketed character classes in regexps
2469f11ffb7Safresh1
2479f11ffb7Safresh1This was added in perl 5.18.0.
2489f11ffb7Safresh1
2499f11ffb7Safresh1=item * C<signatures> - allow subroutine signatures (for named arguments)
2509f11ffb7Safresh1
2519f11ffb7Safresh1This was added in perl 5.20.0.
2529f11ffb7Safresh1
2539f11ffb7Safresh1=item * C<smartmatch> - allow the use of C<~~>
2549f11ffb7Safresh1
2559f11ffb7Safresh1This was added in perl 5.10.0, but it should be noted there are significant
2569f11ffb7Safresh1incompatibilities between 5.10.0 and 5.10.1.
2579f11ffb7Safresh1
258e0680481Safresh1The feature is going to be deprecated in perl 5.38.0, and removed in 5.42.0.
259e0680481Safresh1
2609f11ffb7Safresh1=item * C<switch> - allow the use of C<~~>, given, and when
2619f11ffb7Safresh1
2629f11ffb7Safresh1This was added in perl 5.10.0.
2639f11ffb7Safresh1
264e0680481Safresh1The feature is going to be deprecated in perl 5.38.0, and removed in 5.42.0.
265e0680481Safresh1
266eac174f2Safresh1=item * C<try> - allow the use of C<try> and C<catch>
267eac174f2Safresh1
268eac174f2Safresh1This was added in perl 5.34.0
269eac174f2Safresh1
2709f11ffb7Safresh1=item * C<win32_perlio> - allows the use of the :win32 IO layer.
2719f11ffb7Safresh1
2729f11ffb7Safresh1This was added on perl 5.22.0.
2739f11ffb7Safresh1
2749f11ffb7Safresh1=back
275b8851fccSafresh1
276b8851fccSafresh1=head2 Ordering matters
277b8851fccSafresh1
278b8851fccSafresh1Using this pragma to 'enable an experimental feature' is another way of saying
279b8851fccSafresh1that this pragma will disable the warnings which would result from using that
280b8851fccSafresh1feature.  Therefore, the order in which pragmas are applied is important.  In
281b8851fccSafresh1particular, you probably want to enable experimental features I<after> you
282b8851fccSafresh1enable warnings:
283b8851fccSafresh1
284b8851fccSafresh1  use warnings;
285b8851fccSafresh1  use experimental 'smartmatch';
286b8851fccSafresh1
287b8851fccSafresh1You also need to take care with modules that enable warnings for you.  A common
288b8851fccSafresh1example being Moose.  In this example, warnings for the 'smartmatch' feature are
289b8851fccSafresh1first turned on by the warnings pragma, off by the experimental pragma and back
290b8851fccSafresh1on again by the Moose module (fix is to switch the last two lines):
291b8851fccSafresh1
292b8851fccSafresh1  use warnings;
293b8851fccSafresh1  use experimental 'smartmatch';
294b8851fccSafresh1  use Moose;
2956fb12b70Safresh1
2966fb12b70Safresh1=head2 Disclaimer
2976fb12b70Safresh1
2986fb12b70Safresh1Because of the nature of the features it enables, forward compatibility can not
2996fb12b70Safresh1be guaranteed in any way.
3006fb12b70Safresh1
3019f11ffb7Safresh1=head1 SEE ALSO
3029f11ffb7Safresh1
303eac174f2Safresh1L<perlexperiment|perlexperiment> contains more information about experimental features.
3049f11ffb7Safresh1
3056fb12b70Safresh1=head1 AUTHOR
3066fb12b70Safresh1
3076fb12b70Safresh1Leon Timmermans <leont@cpan.org>
3086fb12b70Safresh1
3096fb12b70Safresh1=head1 COPYRIGHT AND LICENSE
3106fb12b70Safresh1
3116fb12b70Safresh1This software is copyright (c) 2013 by Leon Timmermans.
3126fb12b70Safresh1
3136fb12b70Safresh1This is free software; you can redistribute it and/or modify it under
3146fb12b70Safresh1the same terms as the Perl 5 programming language system itself.
3156fb12b70Safresh1
3166fb12b70Safresh1=cut
317