1 package Module::Loaded;
6 BEGIN { use base 'Exporter';
7 use vars qw[@EXPORT $VERSION];
10 @EXPORT = qw[mark_as_loaded mark_as_unloaded is_loaded];
15 Module::Loaded - mark modules as loaded or unloaded
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
24 eval "require 'Foo'"; # is now a no-op
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
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.
40 =head2 $bool = mark_as_loaded( PACKAGE );
42 Marks the package as loaded to perl. C<PACKAGE> can be a bareword or
45 If the module is already loaded, C<mark_as_loaded> will carp about
46 this and tell you from where the C<PACKAGE> has been loaded already.
50 sub mark_as_loaded (*) {
52 my $file = __PACKAGE__->_pm_to_file( $pm ) or return;
53 my $who = [caller]->[1];
55 my $where = is_loaded( $pm );
56 if ( defined $where ) {
57 carp "'$pm' already marked as loaded ('$where')";
66 =head2 $bool = mark_as_unloaded( PACKAGE );
68 Marks the package as unloaded to perl, which is the exact opposite
69 of C<mark_as_loaded>. C<PACKAGE> can be a bareword or string.
71 If the module is already unloaded, C<mark_as_unloaded> will carp about
72 this and tell you the C<PACKAGE> has been unloaded already.
76 sub mark_as_unloaded (*) {
78 my $file = __PACKAGE__->_pm_to_file( $pm ) or return;
80 unless( defined is_loaded( $pm ) ) {
81 carp "'$pm' already marked as unloaded";
90 =head2 $loc = is_loaded( PACKAGE );
92 C<is_loaded> tells you if C<PACKAGE> has been marked as loaded yet.
93 C<PACKAGE> can be a bareword or string.
95 It returns falls if C<PACKAGE> has not been loaded yet and the location
96 from where it is said to be loaded on success.
102 my $file = __PACKAGE__->_pm_to_file( $pm ) or return;
104 return $INC{$file} if exists $INC{$file};
112 my $pm = shift or return;
114 my $file = join '/', split '::', $pm;
122 Please report bugs or other issues to E<lt>bug-module-loaded@rt.cpan.org<gt>.
126 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
130 This library is free software; you may redistribute and/or modify it
131 under the same terms as Perl itself.
136 # c-indentation-style: bsd
138 # indent-tabs-mode: nil
140 # vim: expandtab shiftwidth=4: