experimentals - Experimental features made even easier
This document describes experimentals version 0.017
use experimentals; # All experimental features for this Perl version are now enabled { no experimentals; # No experimental features enabled in this scope }
use experimental is a life-saver under modern Perls, but if you want to be truly modern Perl hacker you need something like:
use experimental
use v5.22; use experimental qw( fc postderef current_sub say regex_sets unicode_eval state array_base postderef_qq switch smartmatch lexical_subs bitwise signatures lexical_topic evalbytes refaliasing unicode_strings autoderef );
which is uncomfortably verbose.
This module reduces that to:
use v5.22; use experimentals;
You load the module and it enables all the Perl 5.10+ features that are available under whatever version of Perl you are using. It also silences the "...is experimental" warnings on those features that are still considered experimental (as listed in perlexperiment)
If you specify:
no experimentals;
then all "experimental" features are disabled (i.e. their warnings are re-enabled). However, non-experimental features (such as say, state, or __SUB__) are unaffected by no experimentals.
say
state
__SUB__
no experimentals
This module works seamlessly with experimental.pm (because they both wrap the same underlying pragmas).
experimental.pm
So you can turn on every modern feature, except one or two you don't trust, like so:
use experimentals; no experimental 'lexical_topic', 'smartmatch', 'array_base';
Likewise, in some inner scope you can lexically disable all experimental features, except the few you actually need, with:
no experimentals; use experimental 'signatures', 'refaliasing';
Another annoyance with experimental warnings is that several new features of Perl were subsequently retconned to "experimental" status in later versions of Perl.
For example, from Perl 5.10 to 5.16 the use of smartmatching (either via an explicit ~~, or implicitly in a given/when) did not generate an "experimental" warning. From 5.18 onwards, it does.
~~
given
when
Similarly, Perl 5.14 added the ability to pass an array reference as the first argument of push. But in 5.20, this feature was retconned to "experimental" status, and started generating a warning. In Perl 5.24 the feature was removed entirely, and now generates a compile-time error.
push
This means that, when porting existing code to run under Perl 5.18 or later, you may start getting spurious warnings if that code contains any of the various retconned experimental features.
The experimentals module can assist with porting older code to newer Perls, via the -report option.
experimentals
-report
For example, if you are porting code from 5.14 to Perl 5.22, you could put the following at the start of your file:
use experimentals -report;
and then run the code under Perl 5.22.
With the -report flag, experimentals will list every use of any feature that would generate an "experimentals" warning under the version of Perl with which you compile the code.
So, for example, the following code:
use experimentals -report; my $_ = 'A1'; my $aref = []; given (readline) { when (1) { say 'okay'; } when (0) { say fc $_ ~~ //; } default { push $aref, 1 | 2; } }
produces no output at all under Perl 5.14 or 5.16.
But under Perl 5.18, it reports:
old_code.pl line 7: Use of my $_ old_code.pl line 10: Given old_code.pl line 11: When old_code.pl line 12: When old_code.pl line 12: Smartmatch
whilst under Perl 5.22, it reports:
old_code.pl line 7: Use of my $_ old_code.pl line 10: Given old_code.pl line 11: When old_code.pl line 12: When old_code.pl line 12: Smartmatch old_code.pl line 13: The bitwise feature old_code.pl line 13: Push on reference
Note that, when use experimentals -report is specified all other non-fatal compile-time warnings are suppressed, and the code itself is only compiled, not executed.
use experimentals -report
Fatal errors cannot be suppressed, however, so under Perl 5.24 the report would look like:
old_code.pl line 10: Given old_code.pl line 11: When old_code.pl line 12: When old_code.pl line 12: Smartmatch old_code.pl line 13: The bitwise feature Can't use global $_ in "my" at old_code.pl line 7, near "my $_ " Experimental push on scalar is now forbidden at old_code.pl line 13, near "2;" Execution of old_code.pl aborted due to compilation errors.
Note too that the module is lexically scoped, so it cannot report problems inside an eval $STRING call...unless the use experimentals -report itself is inside the string as well. In that case, obviously, the code will be executed, since the eval is performed at run-time.
eval $STRING
eval
If you are using the Vim editor, you can add the following code:
nmap er :call Experimental_Report()<CR> function! Experimental_Report () normal 1GOuse experimentals -report; setlocal makeprg=perl\ % errorformat=%f\ line\ %l:%m make set makeprg< errorformat< normal 1Gdd`` redraw cc endfunction
to your .vimrc to create a Normal-mode mapping that runs the current buffer under:
#! /usr/bin/env perl use experimentals -report
and then initializes your "quickfix" list with the resulting compatibility report.
For details of using quickfix mode in Vim, see:
:help quickfix
None. (That's the point. ;-)
This module requires no configuration files or environment variables.
None.
None reported.
No bugs have been reported.
Please report any bugs or feature requests to bug-experimentals@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-experimentals@rt.cpan.org
Damian Conway <DCONWAY@CPAN.org>
<DCONWAY@CPAN.org>
Copyright (c) 2015, Damian Conway <DCONWAY@CPAN.org>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install experimentals, copy and paste the appropriate command in to your terminal.
cpanm
cpanm experimentals
CPAN shell
perl -MCPAN -e shell install experimentals
For more information on module installation, please visit the detailed CPAN module installation guide.