Skip to content

Instantly share code, notes, and snippets.

@toddr
Last active June 30, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toddr/0c280d3a4f5951776cd0d1e6d49dba11 to your computer and use it in GitHub Desktop.
Save toddr/0c280d3a4f5951776cd0d1e6d49dba11 to your computer and use it in GitHub Desktop.
This module enables all features and sets strict and warnings on by default with experimental warnings off. WARNING: this is not the definitive list of what will be in perl 7 but provides a way to test what might break in perl 7 but on 5.30.0
package compat::perl5;
# This helps hint to perl 7+ what level of compatibility this code has with future versions of perl.
# use compat::perl5 should be the first thing in your code. Especially before use strict, warnings, v5.XXX, or feature.
BEGIN {
$] <= 8 or die("This code is incompatible with Perl 8. Please see XXX for more information.");
if ( $] > 6 ) {
warn_quietly_once("This code is being run using Perl $]. It should be updated or may break in Perl 8. See YYY for more information.");
}
}
sub import {
# no warnings;
${^WARNING_BITS} = undef;
# perl -e'my $h; BEGIN { $h = $^H } printf("\$^H = 0x%08X\n", $h); '
$^H = 0x0;
%^H = ();
}
1;
package compat::perl7;
# use compat::perl7 enables perl 5 code to function in a perl 7-ish way as much as possible compared to the version you are running.
# it also is a hint to both tools and the compiler what the level of compatibility is with future versions of the language.
BEGIN {
# This code is a proof of concept provided against 5.30. In order for this code to work on other versions of perl
# we would need to generate it via p7.pm.PL as part of shipping it to CPAN.
$] >= 5.030 && $] < 5.031 or die("Perl 5.30 is required to use this module.");
}
sub import {
# use warnings; no warnings qw/experimental/;
# perl -e'use warnings; no warnings qw/experimental/; my $w; BEGIN {$w = ${^WARNING_BITS} } print unpack("H*", $w) . "\n"'
${^WARNING_BITS} = pack( "H*", "55555555555555555555555515000440050454" );
# use strict; use utf8;
# perl -MData::Dumper -e'my $h; use strict; use utf8; use feature (qw/bitwise current_sub declared_refs evalbytes fc postderef_qq refaliasing say signatures state switch unicode_eval unicode_strings/); BEGIN { $h = $^H } printf("\$^H = 0x%08X\n", $h); print Dumper \%^H; '
$^H = 0x1C820FE2;
%^H = (
'feature___SUB__' => 1,
'feature_bitwise' => 1,
'feature_evalbytes' => 1,
'feature_fc' => 1,
'feature_myref' => 1,
'feature_postderef_qq' => 1,
'feature_refaliasing' => 1,
'feature_say' => 1,
'feature_signatures' => 1,
'feature_state' => 1,
'feature_switch' => 1,
'feature_unicode' => 1,
'feature_unieval' => 1
);
}
1;
@toddr
Copy link
Author

toddr commented Jun 25, 2020

note older versions don't understand newer "use v5.44" and get very angry if you use them.

@briandfoy
Copy link

Will this ever be non-secret? I was thinking about doing something similar.

@toddr
Copy link
Author

toddr commented Jun 30, 2020

I don't think I realized it was secret.

@toddr
Copy link
Author

toddr commented Jun 30, 2020

fixed.

@atoomic
Copy link

atoomic commented Jun 30, 2020

note that https://github.com/Perl/perl5/blob/core-p7/regen/pX.pl provides a way to generate these two files

you can view the generated files here:

We should close this gist and move the discussion to the repo itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment