File Coverage

blib/lib/Sub/Deprecated.pm
Criterion Covered Total %
statement 34 37 91.8
branch 4 6 66.6
condition 3 6 50.0
subroutine 8 9 88.8
pod 0 1 0.0
total 49 59 83.0


line stmt bran cond sub pod time code
1             package Sub::Deprecated;
2              
3 5     5   118107 use 5.008005;
  5         19  
  5         192  
4 5     5   41 use strict;
  5         12  
  5         142  
5 5     5   34 use warnings;
  5         8  
  5         213  
6              
7             our $VERSION = "0.04";
8              
9 5     5   8592 use Attribute::Handlers;
  5         39752  
  5         86  
10              
11             sub Deprecated : ATTR(CODE,BEGIN) {
12 16     16 0 7473 my ($package, $symbol, $referent, $attr, $data, $phase, $file, $line) = @_;
13              
14             my $die = sub {
15 0     0   0 die sprintf('%s at %s line %d.', $_[0], $file, $line);
16 16         55 };
17              
18 16 50 33     183 if (! defined $data || scalar(@$data) > 2) {
19 0         0 $die->('ERROR: Invalid use of Deprecated(version, [message]) attribute.');
20             }
21              
22 16 50       61 if ($symbol eq 'ANON') {
23 0         0 $die->('ERROR: Deprecated attribute does not work on anonymous subroutines.');
24             }
25              
26 16         21 my $name = *{$symbol}{NAME};
  16         31  
27 16         21 my ($deprecated_version, $message) = @{$data};
  16         118  
28              
29 5     5   820 no warnings 'redefine';
  5         12  
  5         653  
30 16         85 *{$symbol} = sub {
31 9     9   21086 my $warning = sprintf(
32             'WARNING: %s::%s deprecated as of %s.',
33             $package, $name, $deprecated_version,
34             );
35 9 100 66     55 if (defined $message && $message) {
36 3         8 chomp $message;
37 3         11 $warning .= ' ' . $message;
38             }
39 9         79 warn $warning, "\n";
40 9         74 goto &$referent;
41 16         69 };
42 5     5   24 }
  5         8  
  5         26  
43              
44             1;
45              
46             __END__