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