diff options
-rw-r--r-- | MANIFEST | 1 | ||||
-rw-r--r-- | lib/feature.pm | 29 | ||||
-rw-r--r-- | pod/perlfunc.pod | 6 | ||||
-rw-r--r-- | pp_ctl.c | 13 | ||||
-rw-r--r-- | t/lib/feature/implicit | 62 |
5 files changed, 109 insertions, 2 deletions
@@ -3391,6 +3391,7 @@ t/lib/dprof/test8_t Perl code profiler tests t/lib/dprof/test8_v Perl code profiler tests t/lib/dprof/V.pm Perl code profiler tests t/lib/feature/err Tests for enabling/disabling err feature +t/lib/feature/implicit Tests for implicit loading of feature.pm t/lib/feature/nonesuch Tests for enabling/disabling nonexistent feature t/lib/feature/say Tests for enabling/disabling say feature t/lib/feature/switch Tests for enabling/disabling switch feature diff --git a/lib/feature.pm b/lib/feature.pm index b8256f4a19..7a88b154c8 100644 --- a/lib/feature.pm +++ b/lib/feature.pm @@ -18,6 +18,8 @@ my %feature_bundle = ( $feature_bundle{"5.10"} = $feature_bundle{"5.10.0"}; #$feature_bundle{"5.10"} = $feature_bundle{sprintf("%vd",$^V)}; +$feature_bundle{"5.9.5"} = $feature_bundle{"5.10.0"}; + # TODO: # - think about versioned features (use feature switch => 2) @@ -113,6 +115,33 @@ which both are equivalent to C<use feature qw(switch say err state)>. In the forthcoming 5.10.X perl releases, C<use feature ":5.10"> will be equivalent to the latest C<use feature ":5.10.X">. +=head1 IMPLICIT LOADING + +There are two ways to load the C<feature> pragma implicitly : + +=over 4 + +=item * + +By using the C<-E> switch on the command-line instead of C<-e>. It enables +all available features in the main compilation unit (that is, the one-liner.) + +=item * + +By requiring explicitly a minimal Perl version number for your program, with +the C<use VERSION> construct, and when the version is higher than or equal to +5.9.5. That is, + + use 5.9.5; + +will do an implicit + + use feature ':5.9.5'; + +and so on. + +=back + =cut sub import { diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index b9e17b9c49..67e8c1e762 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -6815,7 +6815,7 @@ except that Module I<must> be a bareword. VERSION may be either a numeric argument such as 5.006, which will be compared to C<$]>, or a literal of the form v5.6.1, which will be compared -to C<$^V> (aka $PERL_VERSION. A fatal error is produced if VERSION is +to C<$^V> (aka $PERL_VERSION). A fatal error is produced if VERSION is greater than the version of the current Perl interpreter; Perl will not attempt to parse the rest of the file. Compare with L</require>, which can do a similar check at run time. @@ -6833,6 +6833,10 @@ This is often useful if you need to check the current Perl version before C<use>ing library modules that have changed in incompatible ways from older versions of Perl. (We try not to do this more than we have to.) +If the specified perl version is greater than or equal to 5.9.5, C<use +VERSION> will also load the C<feature> pragma and enable all features +available in the requested version. See L<feature>. + The C<BEGIN> forces the C<require> and C<import> to happen at compile time. The C<require> makes sure the module is loaded into memory if it hasn't been yet. The C<import> is not a builtin--it's just an ordinary static method @@ -3102,7 +3102,18 @@ PP(pp_require) SVfARG(vnormal(sv)), SVfARG(vnormal(PL_patchlevel))); } - RETPUSHYES; + /* If we request a version >= 5.9.5, load feature.pm with the + * feature bundle that corresponds to the required version. + * We do this only with use, not require. */ + if (PL_compcv && vcmp(sv, sv_2mortal(upg_version(newSVnv(5.009005)))) >= 0) { + SV *const importsv = vnormal(sv); + *SvPVX_mutable(importsv) = ':'; + ENTER; + Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL); + LEAVE; + } + + RETPUSHYES; } name = SvPV_const(sv, len); if (!(name && len > 0 && *name)) diff --git a/t/lib/feature/implicit b/t/lib/feature/implicit new file mode 100644 index 0000000000..0632770401 --- /dev/null +++ b/t/lib/feature/implicit @@ -0,0 +1,62 @@ +Check implicit loading of features with use VERSION. + +__END__ +# Standard feature bundle +use feature ":5.10"; +say "Hello", "world"; +EXPECT +Helloworld +######## +# VERSION requirement, dotted notation +use 5.9.5; +say "Hello", "world"; +EXPECT +Helloworld +######## +# VERSION requirement, v-dotted notation +use v5.9.5; +say "Hello", "world"; +EXPECT +Helloworld +######## +# VERSION requirement, decimal notation +use 5.009005; +say defined $INC{"feature.pm"} ? "Helloworld" : "Good bye"; +EXPECT +Helloworld +######## +# VERSION requirement, doesn't load anything for < 5.9.5 +use 5.8.8; +print "<".$INC{"feature.pm"}.">\n"; +EXPECT +<> +######## +# VERSION requirement, doesn't load anything with require +require 5.9.5; +print "<".$INC{"feature.pm"}.">\n"; +EXPECT +<> +######## +# VERSION requirement in eval {} +eval { + use 5.9.5; + say "Hello", "world"; +} +EXPECT +Helloworld +######## +# VERSION requirement in eval "" +eval q{ + use 5.9.5; + say "Hello", "world"; +} +EXPECT +Helloworld +######## +# VERSION requirement in BEGIN +BEGIN { + use 5.9.5; + say "Hello", "world"; +} +EXPECT +Helloworld |