1 package Package::DeprecationManager;
7 use Params::Util qw( _HASH );
15 'You must provide a hash reference -deprecations parameter when importing Package::DeprecationManager'
16 unless $args{-deprecations} && _HASH( $args{-deprecations} );
20 my $import = _build_import( \%registry );
21 my $warn = _build_warn( \%registry, $args{-deprecations} );
23 my $caller = caller();
25 Sub::Install::install_sub(
33 Sub::Install::install_sub(
51 $args{-api_version} ||= delete $args{-compatible};
53 $registry->{ caller() } = $args{-api_version}
54 if $args{-api_version};
62 my $deprecated_at = shift;
67 my ( $package, undef, undef, $sub ) = caller(1);
69 my $compat_version = $registry->{$package};
71 my $deprecated_at = $deprecated_at->{$sub};
74 if defined $compat_version
75 && defined $deprecated_at
76 && $compat_version lt $deprecated_at;
78 return if $warned{$package}{$sub};
81 my $msg = "$sub has been deprecated";
82 $msg .= " since version $deprecated_at"
83 if defined $deprecated_at;
88 $warned{$package}{$sub} = 1;
96 # ABSTRACT: Manage deprecation warnings for your distribution
106 use Package::DeprecationManager
108 'My::Class::foo' => '0.02',
109 'My::Class::bar' => '0.05',
113 deprecated( 'Do not call foo!' );
124 package Other::Class;
126 use My::Class -api_version => '0.04';
128 My::Class->new()->foo(); # warns
129 My::Class->new()->bar(); # does not warn
130 My::Class->new()->far(); # does not warn again
134 This module allows you to manage a set of deprecations for one or more modules.
136 When you import C<Package::DeprecationManager>, you must provide a set of
137 C<-deprecations> as a hash ref. The keys are fully qualified sub/method names,
138 and the values are the version when that subroutine was deprecated.
140 As part of the import process, C<Package::DeprecationManager> will export two
141 subroutines into its caller. It proves an C<import()> sub for the caller and a
144 The C<import()> sub allows callers of I<your> class to specify an C<-api_version>
145 parameter. If this is supplied, then deprecation warnings are only issued for
146 deprecations for api versions earlier than the one specified.
148 You must call C<deprecated()> sub in each deprecated subroutine. When called,
149 it will issue a warning using C<Carp::cluck()>. If you do not pass an explicit
150 warning message, one will be generated for you.
152 Deprecation warnings are only issued once for a given package, regardless of
153 how many times the deprecated sub/method is called.