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