File Coverage

blib/lib/Devel/Constants.pm
Criterion Covered Total %
statement 57 57 100.0
branch 21 28 75.0
condition 6 9 66.6
subroutine 12 12 100.0
pod 2 2 100.0
total 98 108 90.7


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