Added is_core(), which returns true if the module was/is in core
authorNeil Bowers <neil@bowers.com>
Wed, 18 Sep 2013 19:47:53 +0000 (20:47 +0100)
committerChris 'BinGOs' Williams <chris@bingosnet.co.uk>
Thu, 19 Sep 2013 10:09:26 +0000 (11:09 +0100)
Default to checking against $^V, but you can optionally specify
the perl release, and can also optionally specify a minimum
version of the module.

Signed-off-by: Chris 'BinGOs' Williams <chris@bingosnet.co.uk>
MANIFEST
dist/Module-CoreList/Changes
dist/Module-CoreList/MANIFEST
dist/Module-CoreList/lib/Module/CoreList.pm
dist/Module-CoreList/lib/Module/CoreList.pod
dist/Module-CoreList/t/is_core.t [new file with mode: 0644]
pod/perldelta.pod

index 66cadf1..a0e1c4a 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -3357,6 +3357,7 @@ dist/Module-CoreList/README                       Module::CoreList
 dist/Module-CoreList/t/corelist.t              Module::CoreList tests
 dist/Module-CoreList/t/deprecated.t            Module::CoreList tests
 dist/Module-CoreList/t/find_modules.t          Module::CoreList tests
+dist/Module-CoreList/t/is_core.t               Module::CoreList tests
 dist/Module-CoreList/t/pod.t                   Module::CoreList tests
 dist/Module-CoreList/t/utils.t                 Module::CoreList tests
 dist/Net-Ping/Changes                  Net::Ping
index fecb700..1c7272a 100644 (file)
@@ -1,6 +1,9 @@
 2.99
   - fixed Module::Build core deprecation
   - changes_between now has the same API as all other functions
+  - added is_core() which returns true if a module is/was core
+    in a specific version of Perl. Can optionally specify minimum
+    version of the module.
 
 2.98 Wed Aug 21 2013
   - Prepared for v5.19.4
index ec9ea02..9b2b202 100644 (file)
@@ -12,6 +12,7 @@ META.yml
 t/corelist.t
 t/deprecated.t
 t/find_modules.t
+t/is_core.t
 t/pod.t
 t/utils.t
 META.json                                Module JSON meta-data (added by MakeMaker)
index b182c85..7e18c93 100644 (file)
@@ -3,7 +3,7 @@ use strict;
 use vars qw/$VERSION %released %version %families %upstream
            %bug_tracker %deprecated/;
 use Module::CoreList::TieHashDelta;
-$VERSION = '2.98';
+$VERSION = '2.99';
 
 my $dumpinc = 0;
 sub import {
@@ -8651,6 +8651,46 @@ my %delta = (
     },
 );
 
+sub is_core
+{
+    my $module = shift;
+    $module = shift if eval { $module->isa(__PACKAGE__) } && @_ > 0 && defined($_[0]) && $_[0] =~ /^\w/;
+    my ($module_version, $perl_version);
+
+    $module_version = shift if @_ > 0;
+    $perl_version   = @_ > 0 ? shift : $^V;
+
+    my $first_release = first_release($module);
+
+    return 0 if !defined($first_release) || $first_release > $perl_version;
+
+    my $final_release = removed_from($module);
+
+    return 0 if defined($final_release) && $perl_version > $final_release;
+
+    # If a minimum version of the module was specified:
+    # Step through all perl release numbers ($prn)
+    # in order, so we can find what version of the module
+    # was included in the specified version of perl.
+    # On the way if we pass the required module version, we can
+    # short-circuit and return true
+    if (defined($module_version)) {
+        RELEASE:
+        foreach my $prn (sort keys %delta) {
+            next RELEASE if $prn <= $first_release;
+            last RELEASE if $prn > $perl_version;
+            next unless defined(my $next_module_version
+                                   = $delta{$prn}->{changed}->{$module});
+            return 1 if $next_module_version >= $module_version;
+        }
+        return 0;
+    }
+
+    return 1 if !defined($final_release);
+
+    return $perl_version <= $final_release;
+}
+
 for my $version (sort { $a <=> $b } keys %delta) {
     my $data = $delta{$version};
 
index 72fc5e3..55f46d9 100644 (file)
@@ -12,6 +12,10 @@ Module::CoreList - what modules shipped with versions of perl
  print Module::CoreList->first_release_by_date('File::Spec'); # prints 5.005
  print Module::CoreList->first_release('File::Spec', 0.82);   # prints 5.006001
 
+ if (Module::CoreList::is_core('File::Spec')) {
+   print "File::Spec is a core module\n";
+ }
+
  print join ', ', Module::CoreList->find_modules(qr/Data/);
     # prints 'Data::Dumper'
  print join ', ', Module::CoreList->find_modules(qr/test::h.*::.*s/i, 5.008008);
@@ -71,6 +75,19 @@ you may provide a list of perl versions to limit the regex search.
 Takes a perl version as an argument. Returns that perl version if it exists or C<undef>
 otherwise.
 
+=item C<is_core( MODULE, [ MODULE_VERSION, [ PERL_VERSION ] ] )>
+
+Available in version 2.99 and above.
+
+Returns true if MODULE was bundled with the specified version of Perl.
+You can optionally specify a minimum version of the module,
+and can also specify a version of Perl.
+If a version of Perl isn't specified,
+C<is_core()> will use the version of Perl that is running (ie C<$^V>).
+
+If you want to specify the version of Perl, but don't care about
+the version of the module, pass C<undef> for the module version:
+
 =item C<is_deprecated( MODULE, PERL_VERSION )>
 
 Available in version 2.22 and above.
diff --git a/dist/Module-CoreList/t/is_core.t b/dist/Module-CoreList/t/is_core.t
new file mode 100644 (file)
index 0000000..a145315
--- /dev/null
@@ -0,0 +1,52 @@
+#!perl -w
+use strict;
+use Module::CoreList;
+use Test::More tests => 23;
+
+BEGIN { require_ok('Module::CoreList'); }
+
+ok(!Module::CoreList::is_core('Module::Path'), 'Module::Path has never been in core');
+ok(!Module::CoreList::is_core('Module::Path', undef, '5.016003'), 'Module::Path has never been in core');
+ok(!Module::CoreList::is_core('Module::Path', undef), 'Module::Path has never been in core');
+
+# List::Util::PP was added in 5.010001 and removed in 5.017001
+ok(!Module::CoreList::is_core('List::Util::PP', undef, '5.002'), 'List::Util::PP was added in 5.10.1 so not in core in 5.002');
+ok(Module::CoreList::is_core('List::Util::PP', undef, '5.016003'), 'List::Util::PP was in core in 5.16.3');
+ok(!Module::CoreList::is_core('List::Util::PP', undef, '5.018001'), 'List::Util::PP was removed in 5.17.1 so not in core in 5.18.1');
+
+# Carp has always been a core module
+ok(Module::CoreList::is_core('Carp', undef, '5'), 'Carp was a core module in first release of perl 5');
+ok(Module::CoreList::is_core('Carp', undef, '5.019004'), 'Carp was still a core module in 5.19.4');
+ok(Module::CoreList::is_core('Carp'), "Carp should be a core module whatever version of perl you're running");
+
+ok(Module::CoreList::is_core('attributes', undef, '5.00503') == 0, "attributes weren't in 5.00503");
+ok(Module::CoreList::is_core('attributes', undef, '5.006001') == 1, "attributes were in 5.6.1");
+ok(Module::CoreList::is_core('Pod::Plainer', undef, '5.012001') == 1, "Pod::Plainer was core in 5.12.1");
+ok(Module::CoreList::is_core('Pod::Plainer', undef, '5.016003') == 0, "Pod::Plainer was removed in 5.13.1");
+
+# history of module 'encoding' in core
+#   version 1.00 included in 5.007003
+#   version 1.35 included in 5.008
+#   version 1.47 included in 5.008001
+#   version 1.48 included in 5.008003
+#   version 2.00 included in 5.008005
+#   version 2.01 included in 5.008006
+#   version 2.02 included in 5.008008
+#   version 2.6_01 included in 5.008009
+#   version 2.04 included in 5.009004
+#   version 2.06 included in 5.009005
+#   version 2.6_01 included in 5.010001
+#   version 2.12 included in 5.019001
+
+ok(!Module::CoreList::is_core('encoding', undef, '5'), "encoding wasn't in core in first release of perl 5");
+ok(!Module::CoreList::is_core('encoding', '1.00', '5'), "encoding 1.00 wasn't in core in first release of perl 5");
+ok(!Module::CoreList::is_core('encoding', '1.35', '5.007003'), "encoding 1.35 wasn't yet in core in perl 5.007003");
+ok(Module::CoreList::is_core('encoding', '1.35', '5.008'), "encoding 1.35 was first included in perl 5.008");
+ok(Module::CoreList::is_core('encoding', '1.35', '5.009004'), "encoding 2.04 (>1.35) was included in 5.009004");
+ok(Module::CoreList::is_core('encoding', '2.01', '5.008007'), "encoding 2.01 was first in core in perl 5.008006, so was core in 5.8.7");
+ok(Module::CoreList->is_core('encoding', '2.01', '5.008007'), "encoding 2.01 was first in core in perl 5.008006, so was core in 5.8.7");
+
+# Module::CoreList (2.17) was first included in 5.008009
+ok(!Module::CoreList::is_core('Module::CoreList', undef, '5.007003'), "Module::CoreList wasn't core in perl 5.7.3");
+ok(!Module::CoreList->is_core('Module::CoreList', undef, '5.007003'), "Module::CoreList wasn't core in perl 5.7.3 (class method)");
+
index 9cdf700..217fa83 100644 (file)
@@ -489,10 +489,15 @@ by using SSize_t instead of I32 for array indices.
 
 =item *
 
-L<Module::CoreList> has been upgraded from version 2.97 to 2.98.
+L<Module::CoreList> has been upgraded from version 2.97 to 2.99.
 
 The list of Perl versions covered has been updated.
 
+A function C<is_core()> has been added, which returns true if the
+specified module was bundled with Perl. Optionally you can specify
+a minimum version of the module, and the specific version of Perl
+you're interested in (defaults to C<$^V>, the running version of Perl).
+
 =item *
 
 L<Module::Load::Conditional> has been upgraded from version 0.54 to 0.58.