File Coverage

blib/lib/Module/Loaded.pm
Criterion Covered Total %
statement 35 37 94.5
branch 8 14 57.1
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 55 63 87.3


line stmt bran cond sub pod time code
1             package Module::Loaded;
2              
3 1     1   926 use strict;
  1         2  
  1         69  
4 1     1   6 use Carp qw[carp];
  1         2  
  1         66  
5              
6 1     1   15 BEGIN { use base 'Exporter';
  1         2  
  1         102  
7 1     1   5 use vars qw[@EXPORT $VERSION];
  1         1  
  1         68  
8              
9 1     1   1 $VERSION = '0.08';
10 1         395 @EXPORT = qw[mark_as_loaded mark_as_unloaded is_loaded];
11             }
12              
13             =head1 NAME
14              
15             Module::Loaded - mark modules as loaded or unloaded
16              
17             =head1 SYNOPSIS
18              
19             use Module::Loaded;
20              
21             $bool = mark_as_loaded('Foo'); # Foo.pm is now marked as loaded
22             $loc = is_loaded('Foo'); # location of Foo.pm set to the
23             # loaders location
24             eval "require 'Foo'"; # is now a no-op
25              
26             $bool = mark_as_unloaded('Foo'); # Foo.pm no longer marked as loaded
27             eval "require 'Foo'"; # Will try to find Foo.pm in @INC
28              
29             =head1 DESCRIPTION
30              
31             When testing applications, often you find yourself needing to provide
32             functionality in your test environment that would usually be provided
33             by external modules. Rather than munging the C<%INC> by hand to mark
34             these external modules as loaded, so they are not attempted to be loaded
35             by perl, this module offers you a very simple way to mark modules as
36             loaded and/or unloaded.
37              
38             =head1 FUNCTIONS
39              
40             =head2 $bool = mark_as_loaded( PACKAGE );
41              
42             Marks the package as loaded to perl. C can be a bareword or
43             string.
44              
45             If the module is already loaded, C will carp about
46             this and tell you from where the C has been loaded already.
47              
48             =cut
49              
50             sub mark_as_loaded (*) {
51 1     1 1 2 my $pm = shift;
52 1 50       2 my $file = __PACKAGE__->_pm_to_file( $pm ) or return;
53 1         4 my $who = [caller]->[1];
54              
55 1         3 my $where = is_loaded( $pm );
56 1 50       4 if ( defined $where ) {
57 0         0 carp "'$pm' already marked as loaded ('$where')";
58              
59             } else {
60 1         2 $INC{$file} = $who;
61             }
62              
63 1         5 return 1;
64             }
65              
66             =head2 $bool = mark_as_unloaded( PACKAGE );
67              
68             Marks the package as unloaded to perl, which is the exact opposite
69             of C. C can be a bareword or string.
70              
71             If the module is already unloaded, C will carp about
72             this and tell you the C has been unloaded already.
73              
74             =cut
75              
76             sub mark_as_unloaded (*) {
77 2     2 1 713 my $pm = shift;
78 2 50       7 my $file = __PACKAGE__->_pm_to_file( $pm ) or return;
79              
80 2 50       4 unless( defined is_loaded( $pm ) ) {
81 0         0 carp "'$pm' already marked as unloaded";
82              
83             } else {
84 2         6 delete $INC{ $file };
85             }
86              
87 2         10 return 1;
88             }
89              
90             =head2 $loc = is_loaded( PACKAGE );
91              
92             C tells you if C has been marked as loaded yet.
93             C can be a bareword or string.
94              
95             It returns falls if C has not been loaded yet and the location
96             from where it is said to be loaded on success.
97              
98             =cut
99              
100             sub is_loaded (*) {
101 8     8 1 3471 my $pm = shift;
102 8 50       19 my $file = __PACKAGE__->_pm_to_file( $pm ) or return;
103              
104 8 100       42 return $INC{$file} if exists $INC{$file};
105              
106 3         12 return;
107             }
108              
109              
110             sub _pm_to_file {
111 11     11   17 my $pkg = shift;
112 11 50       22 my $pm = shift or return;
113              
114 11         32 my $file = join '/', split '::', $pm;
115 11         17 $file .= '.pm';
116              
117 11         34 return $file;
118             }
119              
120             =head1 BUG REPORTS
121              
122             Please report bugs or other issues to Ebug-module-loaded@rt.cpan.org.
123              
124             =head1 AUTHOR
125              
126             This module by Jos Boumans Ekane@cpan.orgE.
127              
128             =head1 COPYRIGHT
129              
130             This library is free software; you may redistribute and/or modify it
131             under the same terms as Perl itself.
132              
133             =cut
134              
135             # Local variables:
136             # c-indentation-style: bsd
137             # c-basic-offset: 4
138             # indent-tabs-mode: nil
139             # End:
140             # vim: expandtab shiftwidth=4:
141              
142             1;