xref: /openbsd-src/gnu/usr.bin/perl/lib/deprecate.pm (revision f3efcd0145415b7d44d9da97e0ad5c21b186ac61)
1b39c5158Smillertpackage deprecate;
2b39c5158Smillertuse strict;
3b39c5158Smillertuse warnings;
4*f3efcd01Safresh1our $VERSION = 0.04;
5b39c5158Smillert
6b39c5158Smillert# our %Config can ignore %Config::Config, e.g. for testing
7b39c5158Smillertour %Config;
8b39c5158Smillertunless (%Config) { require Config; *Config = \%Config::Config; }
9b39c5158Smillert
10898184e3Ssthen# This isn't a public API. It's internal to code maintained by the perl-porters
11898184e3Ssthen# If you would like it to be a public API, please send a patch with
12898184e3Ssthen# documentation and tests. Until then, it may change without warning.
13898184e3Ssthensub __loaded_from_core {
14898184e3Ssthen    my ($package, $file, $expect_leaf) = @_;
15b39c5158Smillert
16b39c5158Smillert    foreach my $pair ([qw(sitearchexp archlibexp)],
17b39c5158Smillert		      [qw(sitelibexp privlibexp)]) {
18b39c5158Smillert	my ($site, $priv) = @Config{@$pair};
19b39c5158Smillert	if ($^O eq 'VMS') {
20b39c5158Smillert	    for my $d ($site, $priv) { $d = VMS::Filespec::unixify($d) };
21b39c5158Smillert	}
22b39c5158Smillert	# Just in case anyone managed to configure with trailing /s
23b39c5158Smillert	s!/*$!!g foreach $site, $priv;
24b39c5158Smillert
25b39c5158Smillert	next if $site eq $priv;
26b39c5158Smillert	if (uc("$priv/$expect_leaf") eq uc($file)) {
27898184e3Ssthen	    return 1;
28898184e3Ssthen	}
29898184e3Ssthen    }
30898184e3Ssthen    return 0;
31898184e3Ssthen}
32898184e3Ssthen
33898184e3Ssthensub import {
34898184e3Ssthen    my ($package, $file) = caller;
35898184e3Ssthen
36898184e3Ssthen    my $expect_leaf = "$package.pm";
37898184e3Ssthen    $expect_leaf =~ s!::!/!g;
38898184e3Ssthen
39898184e3Ssthen    if (__loaded_from_core($package, $file, $expect_leaf)) {
40b39c5158Smillert	my $call_depth=1;
41b39c5158Smillert	my @caller;
42b39c5158Smillert	while (@caller = caller $call_depth++) {
43b39c5158Smillert	    last if $caller[7]			# use/require
44b39c5158Smillert		and $caller[6] eq $expect_leaf;	# the package file
45b39c5158Smillert	}
46b39c5158Smillert	unless (@caller) {
47b39c5158Smillert	    require Carp;
48b39c5158Smillert	    Carp::cluck(<<"EOM");
49b39c5158SmillertCan't find use/require $expect_leaf in caller stack
50b39c5158SmillertEOM
51898184e3Ssthen	    return;
52b39c5158Smillert	}
53b39c5158Smillert
54b39c5158Smillert	# This is fragile, because it
55b39c5158Smillert	# is directly poking in the internals of warnings.pm
56b39c5158Smillert	my ($call_file, $call_line, $callers_bitmask) = @caller[1,2,9];
57b39c5158Smillert
58b39c5158Smillert	if (defined $callers_bitmask
59b39c5158Smillert	    && (vec($callers_bitmask, $warnings::Offsets{deprecated}, 1)
60b39c5158Smillert		|| vec($callers_bitmask, $warnings::Offsets{all}, 1))) {
61b39c5158Smillert	    warn <<"EOM";
62b39c5158Smillert$package will be removed from the Perl core distribution in the next major release. Please install it from CPAN. It is being used at $call_file, line $call_line.
63b39c5158SmillertEOM
64b39c5158Smillert	}
65b39c5158Smillert    }
66b39c5158Smillert}
67b39c5158Smillert
68b39c5158Smillert1;
69b39c5158Smillert
70b39c5158Smillert__END__
71b39c5158Smillert
72b39c5158Smillert=head1 NAME
73b39c5158Smillert
74*f3efcd01Safresh1deprecate - Perl pragma for deprecating the inclusion of a module in core
75b39c5158Smillert
76b39c5158Smillert=head1 SYNOPSIS
77b39c5158Smillert
78*f3efcd01Safresh1    use deprecate;  # warn about future absence if loaded from core
79b39c5158Smillert
80b39c5158Smillert
81b39c5158Smillert=head1 DESCRIPTION
82b39c5158Smillert
83*f3efcd01Safresh1This pragma simplifies the maintenance of dual-life modules that will no longer
84*f3efcd01Safresh1be included in the Perl core in a future Perl release, but are still included
85*f3efcd01Safresh1currently.
86b39c5158Smillert
87*f3efcd01Safresh1The purpose of the pragma is to alert users to the status of such a module by
88*f3efcd01Safresh1issuing a warning that encourages them to install the module from CPAN, so that
89*f3efcd01Safresh1a future upgrade to a perl which omits the module will not break their code.
90b39c5158Smillert
91*f3efcd01Safresh1This warning will only be issued if the module was loaded from a core library
92*f3efcd01Safresh1directory, which allows the C<use deprecate> line to be included in the CPAN
93*f3efcd01Safresh1version of the module. Because the pragma remains silent when the module is run
94*f3efcd01Safresh1from a non-core library directory, the pragma call does not need to be patched
95*f3efcd01Safresh1into or out of either the core or CPAN version of the module. The exact same
96*f3efcd01Safresh1code can be shipped for either purpose.
97*f3efcd01Safresh1
98*f3efcd01Safresh1=head2 Important Caveat
99*f3efcd01Safresh1
100*f3efcd01Safresh1Note that when a module installs from CPAN to a core library directory rather
101*f3efcd01Safresh1than the site library directories, the user gains no protection from having
102*f3efcd01Safresh1installed it.
103*f3efcd01Safresh1
104*f3efcd01Safresh1At the same time, this pragma cannot detect when such a module has installed
105*f3efcd01Safresh1from CPAN to the core library, and so it would endlessly and uselessly exhort
106*f3efcd01Safresh1the user to upgrade.
107*f3efcd01Safresh1
108*f3efcd01Safresh1Therefore modules that can install from CPAN to the core library must make sure
109*f3efcd01Safresh1not to call this pragma when they have done so. Generally this means that the
110*f3efcd01Safresh1exact logic from the installer must be mirrored inside the module. E.g.:
111*f3efcd01Safresh1
112*f3efcd01Safresh1    # Makefile.PL
113*f3efcd01Safresh1    WriteMakefile(
114*f3efcd01Safresh1        # ...
115*f3efcd01Safresh1        INSTALLDIRS => ( "$]" >= 5.011 ? 'site' : 'perl' ),
116*f3efcd01Safresh1    );
117*f3efcd01Safresh1
118*f3efcd01Safresh1    # lib/Foo/Bar.pm
119*f3efcd01Safresh1    use if "$]" >= 5.011, 'deprecate';
120*f3efcd01Safresh1
121*f3efcd01Safresh1(The above example shows the most important case of this: when the target is
122*f3efcd01Safresh1a Perl older than 5.12 (where the core library directories take precedence over
123*f3efcd01Safresh1the site library directories) and the module being installed was included in
124*f3efcd01Safresh1core in that Perl version. Under those circumstances, an upgrade of the module
125*f3efcd01Safresh1from CPAN is only possible by installing to the core library.)
126*f3efcd01Safresh1
127*f3efcd01Safresh1
128*f3efcd01Safresh1=head1 EXPORT
129b39c5158Smillert
130b39c5158SmillertNone by default.  The only method is C<import>, called by C<use deprecate;>.
131b39c5158Smillert
132b39c5158Smillert
133b39c5158Smillert=head1 SEE ALSO
134b39c5158Smillert
135b39c5158SmillertFirst example to C<use deprecate;> was L<Switch>.
136b39c5158Smillert
137b39c5158Smillert
138b39c5158Smillert=head1 AUTHOR
139b39c5158Smillert
140b39c5158SmillertOriginal version by Nicholas Clark
141b39c5158Smillert
142b39c5158Smillert
143b39c5158Smillert=head1 COPYRIGHT AND LICENSE
144b39c5158Smillert
145898184e3SsthenCopyright (C) 2009, 2011
146b39c5158Smillert
147b39c5158SmillertThis library is free software; you can redistribute it and/or modify
148b39c5158Smillertit under the same terms as Perl itself, either Perl version 5.10.0 or,
149b39c5158Smillertat your option, any later version of Perl 5 you may have available.
150b39c5158Smillert
151b39c5158Smillert
152b39c5158Smillert=cut
153