From: Rafael Garcia-Suarez Date: Sat, 3 Mar 2007 09:29:37 +0000 (+0000) Subject: Make use VERSION also load feature.pm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7dfde25db661bada3e1f19c61513f0bac481ca05;p=p5sagit%2Fp5-mst-13.2.git Make use VERSION also load feature.pm p4raw-id: //depot/perl@30446 --- diff --git a/MANIFEST b/MANIFEST index b70220b..02a5fb5 100644 --- a/MANIFEST +++ b/MANIFEST @@ -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 b8256f4..7a88b15 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. In the forthcoming 5.10.X perl releases, C will be equivalent to the latest C. +=head1 IMPLICIT LOADING + +There are two ways to load the C 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 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 b9e17b9..67e8c1e 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -6815,7 +6815,7 @@ except that Module I 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, 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 Cing 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 will also load the C pragma and enable all features +available in the requested version. See L. + The C forces the C and C to happen at compile time. The C makes sure the module is loaded into memory if it hasn't been yet. The C is not a builtin--it's just an ordinary static method diff --git a/pp_ctl.c b/pp_ctl.c index 3aecb2d..c9a7f58 100644 --- a/pp_ctl.c +++ b/pp_ctl.c @@ -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 0000000..0632770 --- /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