CC: | Yanick Champoux <yanick [...] babyl.dyndns.org> |
Subject: | [PATCH] add a 'end_of_life' optional deprecation parameter |
Date: | Sat, 28 May 2011 13:55:15 -0400 |
To: | bug-Package-DeprecationManager [...] rt.cpan.org |
From: | Yanick <yanick [...] cpan.org> |
From: Yanick Champoux <yanick@babyl.dyndns.org>
In a nutshell, this patch allows to do
use Package::DeprecationManager -deprecations => {
'My::Class::foo' => '0.02',
'My::Class::frobnicate' => {
since => '0.07',
end_of_life => '1.0',
},
};
Which will generate the warning
My::Class::frobnicate has been deprecated since version 0.07 and is planned to be removed by 1.0
For now the 'end_of_life' is only a string, so a version number, a
date, or anything else can be put in there.
---
lib/Package/DeprecationManager.pm | 26 +++++++++++++++++++++++---
t/basic.t | 24 ++++++++++++++++++++++++
2 files changed, 47 insertions(+), 3 deletions(-)
diff --git a/lib/Package/DeprecationManager.pm b/lib/Package/DeprecationManager.pm
index 6494acb..5db93fd 100644
--- a/lib/Package/DeprecationManager.pm
+++ b/lib/Package/DeprecationManager.pm
@@ -63,7 +63,7 @@ sub _build_import {
sub _build_warn {
my $registry = shift;
- my $deprecated_at = shift;
+ my $deprecations = shift;
my $ignore = shift;
my %ignore = map { $_ => 1 } grep { !ref } @{ $ignore || [] };
@@ -94,7 +94,12 @@ sub _build_warn {
my $compat_version = $registry->{$package};
- my $deprecated_at = $deprecated_at->{ $args{feature} };
+ my $dep = $deprecations->{ $args{feature} };
+
+ my ( $deprecated_at, $end_of_life ) =
+ ref $dep eq 'HASH'
+ ? ( map { $dep->{$_} } qw/ since end_of_life / )
+ : ( $dep, undef );
return
if defined $compat_version
@@ -109,6 +114,8 @@ sub _build_warn {
$msg = "$args{feature} has been deprecated";
$msg .= " since version $deprecated_at"
if defined $deprecated_at;
+ $msg .= ' and is planned to be removed by ' . $end_of_life
+ if defined $end_of_life;
}
return if $warned{$package}{ $args{feature} }{$msg};
@@ -147,6 +154,10 @@ version 0.10
'My::Class::foo' => '0.02',
'My::Class::bar' => '0.05',
'feature-X' => '0.07',
+ 'My::Class::frobnicate' => {
+ since => '0.07',
+ end_of_life => '1.0',
+ },
};
sub foo {
@@ -172,6 +183,12 @@ version 0.10
}
}
+ sub frobnicate {
+ deprecated();
+
+ ...
+ }
+
package Other::Class;
use My::Class -api_version => '0.04';
@@ -186,7 +203,10 @@ This module allows you to manage a set of deprecations for one or more modules.
When you import C<Package::DeprecationManager>, you must provide a set of
C<-deprecations> as a hash ref. The keys are "feature" names, and the values
-are the version when that feature was deprecated.
+are either the version when that feature was deprecated, or a hash ref
+with the keys C<since> (when the feature was deprecated)
+and C<end_of_life> (when the feature is planned for removal, which can
+be a version number or a date).
In many cases, you can simply use the fully qualified name of a subroutine or
method as the feature name. This works for cases where the whole subroutine is
diff --git a/t/basic.t b/t/basic.t
index 1032d4a..d49256a 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -27,6 +27,13 @@ use Test::Requires {
'Foo::bar' => '0.03',
'Foo::baz' => '1.21',
'not a sub' => '1.23',
+ 'Foo::deadline' => {
+ end_of_life => 'version 9',
+ },
+ 'Foo::deadline_with_since' => {
+ since => '0.02',
+ end_of_life => 'version 9',
+ },
};
sub foo {
@@ -41,6 +48,9 @@ use Test::Requires {
deprecated();
}
+ sub deadline { deprecated(); }
+ sub deadline_with_since { deprecated(); }
+
sub quux {
if ( $_[0] > 5 ) {
deprecated(
@@ -93,6 +103,20 @@ use Test::Requires {
}
{
+ package My::EndOfLife;
+ Foo->import;
+
+ ::stderr_like { Foo::deadline() }
+ qr/Foo::deadline has been deprecated and is planned to be removed by version 9/,
+ 'end of life warning';
+
+ ::stderr_like { Foo::deadline_with_since() }
+ qr/Foo::deadline_with_since has been deprecated since .* and is planned to be removed by/,
+ 'end of life warning with since';
+
+}
+
+{
package Baz;
Foo->import( -api_version => '0.01' );
--
1.7.4.1