File Coverage

blib/lib/Devel/Constants.pm
Criterion Covered Total %
statement 58 58 100.0
branch 21 28 75.0
condition 6 9 66.6
subroutine 12 12 100.0
pod 2 2 100.0
total 99 109 90.8


line stmt bran cond sub pod time code
1             package Devel::Constants;
2              
3 2     2   24742 use 5.006;
  2         7  
  2         97  
4 2     2   12 use strict;
  2         4  
  2         93  
5 2     2   12 use warnings;
  2         18  
  2         71  
6 2     2   11 use vars qw( $VERSION %EXPORT_OK );
  2         7  
  2         184  
7              
8             $VERSION = '1.03';
9              
10             %EXPORT_OK =
11             (
12             flag_to_names => \&flag_to_names,
13             to_name => \&to_name,
14             );
15              
16 2     2   9 use constant;
  2         3  
  2         46  
17 2     2   2172 use subs 'oldimport';
  2         42  
  2         11  
18              
19             {
20 2     2   86 no warnings;
  2         4  
  2         446  
21             *oldimport = \&constant::import;
22             *constant::import = \&_newimport;
23             }
24              
25             my %constants;
26              
27             sub import
28             {
29 7     7   3482 my $class = shift;
30              
31 7         14 my $pkg = my $import = caller();
32 7         13 my $flagholder = {};
33              
34 7         9 my @exports;
35              
36 7         25 while ( my $arg = shift )
37             {
38 9 100       46 if ( ref($arg) eq 'HASH' )
    100          
    100          
    50          
39             {
40 2         8 $flagholder = $arg;
41             }
42             elsif ( $arg eq 'package' )
43             {
44 1 50       6 $pkg = shift if @_;
45             }
46             elsif ( $arg eq 'import' )
47             {
48 1 50       5 $import = shift if @_;
49             }
50             elsif ( exists $EXPORT_OK{$arg} )
51             {
52 5 100 100     25 my $name = (@_ and ! exists $EXPORT_OK{ $_[0] }) ? shift : $arg;
53 5         22 push @exports, [ $name, $EXPORT_OK{$arg} ];
54             }
55             }
56              
57 7         23 $constants{$pkg} = $flagholder;
58              
59 2     2   11 no strict 'refs';
  2         4  
  2         686  
60 7         26 for my $export (@exports)
61             {
62 5         8 *{ $import . "::$export->[0]" } = $export->[1];
  5         43  
63             }
64              
65             }
66              
67             sub _newimport
68             {
69 36     36   19306 my ( $class, @args ) = @_;
70 36         55 my $pkg = caller();
71              
72 36 100       931 if ( defined $constants{$pkg} )
73             {
74 10         20 while (@args)
75             {
76 10         25 my ( $name, $val ) = splice( @args, 0, 2 );
77 10 50       22 last unless $val;
78 10         40 $constants{$pkg}{$val} = $name;
79             }
80             }
81              
82 36         3631 goto &oldimport;
83             }
84              
85             sub flag_to_names
86             {
87 3     3 1 2000 my ( $val, $pkg ) = @_;
88 3   66     22 $pkg ||= caller();
89              
90 3 50       15 return unless my $constants = $constants{$pkg};
91              
92 3         5 my @flags;
93 3         15 for my $flag ( keys %$constants )
94             {
95 12 100       47 push @flags, $constants->{$flag} if $val & $flag;
96             }
97 3 100       24 return wantarray() ? @flags : join( ' ', @flags );
98             }
99              
100             sub to_name
101             {
102 1     1 1 939 my ( $val, $pkg ) = @_;
103 1   33     9 $pkg ||= caller();
104              
105 1 50       5 return unless my $constants = $constants{$pkg};
106 1 50       12 return $constants->{$val} if exists $constants->{$val};
107             }
108              
109             1;
110              
111             __END__