File Coverage

lib/Package/Constants.pm
Criterion Covered Total %
statement 32 32 100.0
branch 6 8 75.0
condition 3 3 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 49 51 96.0


line stmt bran cond sub pod time code
1             package Package::Constants;
2              
3 1     1   1271 use if $] >= 5.019006, 'deprecate';
  1         8  
  1         5  
4              
5 1     1   1021 use 5.006;
  1         3  
  1         27  
6 1     1   12 use strict;
  1         1  
  1         23  
7 1     1   3 use warnings;
  1         1  
  1         88  
8              
9             our $VERSION = '0.06';
10             our $DEBUG = 0;
11              
12             =head1 NAME
13              
14             Package::Constants - List all constants declared in a package
15              
16             =head1 SYNOPSIS
17              
18             use Package::Constants;
19              
20             ### list the names of all constants in a given package;
21             @const = Package::Constants->list( __PACKAGE__ );
22             @const = Package::Constants->list( 'main' );
23              
24             ### enable debugging output
25             $Package::Constants::DEBUG = 1;
26              
27             =head1 DESCRIPTION
28              
29             C lists all the constants defined in a certain
30             package. This can be useful for, among others, setting up an
31             autogenerated C<@EXPORT/@EXPORT_OK> for a Constants.pm file.
32              
33             =head1 CLASS METHODS
34              
35             =head2 @const = Package::Constants->list( PACKAGE_NAME );
36              
37             Lists the names of all the constants defined in the provided package.
38              
39             =cut
40              
41             sub list {
42 1     1 1 696 my $class = shift;
43 1         2 my $pkg = shift;
44 1 50       4 return unless defined $pkg; # some joker might use '0' as a pkg...
45              
46 1         5 _debug("Inspecting package '$pkg'");
47              
48 1         1 my @rv;
49 1     1   3 { no strict 'refs';
  1         2  
  1         174  
  1         1  
50 1         2 my $stash = $pkg . '::';
51              
52 1         10 for my $name (sort keys %$stash ) {
53              
54 8         11 _debug( " Checking stash entry '$name'" );
55              
56             ### is it a subentry?
57 8         21 my $sub = $pkg->can( $name );
58 8 100       14 next unless defined $sub;
59              
60 5         9 _debug( " '$name' is a coderef" );
61              
62 5 100 100     18 next unless defined prototype($sub) and
63             not length prototype($sub);
64              
65 3         7 _debug( " '$name' is a constant" );
66 3         4 push @rv, $name;
67             }
68             }
69              
70 1         4 return sort @rv;
71             }
72              
73             =head1 GLOBAL VARIABLES
74              
75             =head2 $Package::Constants::DEBUG
76              
77             When set to true, prints out debug information to STDERR about the
78             package it is inspecting. Helps to identify issues when the results
79             are not as you expect.
80              
81             Defaults to false.
82              
83             =cut
84              
85 17 50   17   24 sub _debug { warn "@_\n" if $DEBUG; }
86              
87             1;
88              
89             =head1 SEE ALSO
90              
91             L - get a list of all the public functions defined in a package.
92              
93             =head1 BUG REPORTS
94              
95             Please report bugs or other issues to Ebug-package-constants@rt.cpan.org.
96              
97             =head1 AUTHOR
98              
99             This module by Jos Boumans Ekane@cpan.orgE.
100              
101             =head1 COPYRIGHT
102              
103             This library is free software; you may redistribute and/or modify it
104             under the same terms as Perl itself.
105              
106             =cut
107              
108             # Local variables:
109             # c-indentation-style: bsd
110             # c-basic-offset: 4
111             # indent-tabs-mode: nil
112             # End:
113             # vim: expandtab shiftwidth=4: