This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update experimental to CPAN version 0.008
[perl5.git] / cpan / experimental / lib / experimental.pm
CommitLineData
de84ff2b 1package experimental;
edd0583e 2$experimental::VERSION = '0.008';
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;
11my %features = map { $_ => 1 } keys %feature::feature;
12
13my %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
22my %additional = (
23 postderef => ['postderef_qq'],
24 switch => ['smartmatch'],
25);
26
27sub _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
46sub import {
47 my ($self, @pragmas) = @_;
48
49 for my $pragma (@pragmas) {
50 _enable($pragma);
51 }
52 return;
53}
54
55sub _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
71sub unimport {
72 my ($self, @pragmas) = @_;
73
74 for my $pragma (@pragmas) {
75 _disable($pragma);
76 }
77 return;
78}
79
801;
81
82#ABSTRACT: Experimental features made easy
83
84__END__
85
86=pod
87
88=encoding UTF-8
89
90=head1 NAME
91
92experimental - Experimental features made easy
93
94=head1 VERSION
95
edd0583e 96version 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
105This pragma provides an easy and convenient way to enable or disable
106experimental features.
107
108Every 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
110found in the documentation. Starting in Perl v5.10.0, and more aggressively in
111v5.18.0, experimental features were placed behind pragmata used to enable the
112feature and disable associated warnings.
113
114The C<experimental> pragma exists to combine the required incantations into a
115single interface stable across releases of perl. For every experimental
116feature, this should enable the feature and silence warnings for the enclosing
117lexical scope:
118
119 use experimental 'feature-name';
120
121To disable the feature and, if applicable, re-enable any warnings, use:
122
123 no experimental 'feature-name';
124
125The 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
139Because of the nature of the features it enables, forward compatibility can not
140be guaranteed in any way.
141
142=head1 AUTHOR
143
144Leon Timmermans <leont@cpan.org>
145
146=head1 COPYRIGHT AND LICENSE
147
148This software is copyright (c) 2013 by Leon Timmermans.
149
150This is free software; you can redistribute it and/or modify it under
151the same terms as the Perl 5 programming language system itself.
152
153=cut