1b39c5158Smillertpackage Module::Load::Conditional; 2b39c5158Smillert 3b39c5158Smillertuse strict; 4b39c5158Smillert 56fb12b70Safresh1use Module::Load qw/load autoload_remote/; 6b39c5158Smillertuse Params::Check qw[check]; 7b39c5158Smillertuse Locale::Maketext::Simple Style => 'gettext'; 8b39c5158Smillert 9b39c5158Smillertuse Carp (); 10b39c5158Smillertuse File::Spec (); 11b39c5158Smillertuse FileHandle (); 12b39c5158Smillertuse version; 13b39c5158Smillert 1491f110e0Safresh1use Module::Metadata (); 1591f110e0Safresh1 16b39c5158Smillertuse constant ON_VMS => $^O eq 'VMS'; 176fb12b70Safresh1use constant ON_WIN32 => $^O eq 'MSWin32' ? 1 : 0; 186fb12b70Safresh1use constant QUOTE => do { ON_WIN32 ? q["] : q['] }; 19b39c5158Smillert 20b39c5158SmillertBEGIN { 21b39c5158Smillert use vars qw[ $VERSION @ISA $VERBOSE $CACHE @EXPORT_OK $DEPRECATED 229f11ffb7Safresh1 $FIND_VERSION $ERROR $CHECK_INC_HASH $FORCE_SAFE_INC ]; 23b39c5158Smillert use Exporter; 24b39c5158Smillert @ISA = qw[Exporter]; 25*eac174f2Safresh1 $VERSION = '0.74'; 26b39c5158Smillert $VERBOSE = 0; 27b39c5158Smillert $DEPRECATED = 0; 28b39c5158Smillert $FIND_VERSION = 1; 29b39c5158Smillert $CHECK_INC_HASH = 0; 309f11ffb7Safresh1 $FORCE_SAFE_INC = 0; 31b39c5158Smillert @EXPORT_OK = qw[check_install can_load requires]; 32b39c5158Smillert} 33b39c5158Smillert 34b39c5158Smillert=pod 35b39c5158Smillert 36b39c5158Smillert=head1 NAME 37b39c5158Smillert 38b39c5158SmillertModule::Load::Conditional - Looking up module information / loading at runtime 39b39c5158Smillert 40b39c5158Smillert=head1 SYNOPSIS 41b39c5158Smillert 42b39c5158Smillert use Module::Load::Conditional qw[can_load check_install requires]; 43b39c5158Smillert 44b39c5158Smillert 45b39c5158Smillert my $use_list = { 46b39c5158Smillert CPANPLUS => 0.05, 47b39c5158Smillert LWP => 5.60, 48b39c5158Smillert 'Test::More' => undef, 49b39c5158Smillert }; 50b39c5158Smillert 51b39c5158Smillert print can_load( modules => $use_list ) 52b39c5158Smillert ? 'all modules loaded successfully' 53b39c5158Smillert : 'failed to load required modules'; 54b39c5158Smillert 55b39c5158Smillert 56b39c5158Smillert my $rv = check_install( module => 'LWP', version => 5.60 ) 57b39c5158Smillert or print 'LWP is not installed!'; 58b39c5158Smillert 59b39c5158Smillert print 'LWP up to date' if $rv->{uptodate}; 60b39c5158Smillert print "LWP version is $rv->{version}\n"; 61b39c5158Smillert print "LWP is installed as file $rv->{file}\n"; 62b39c5158Smillert 63b39c5158Smillert 64b39c5158Smillert print "LWP requires the following modules to be installed:\n"; 65b39c5158Smillert print join "\n", requires('LWP'); 66b39c5158Smillert 67b39c5158Smillert ### allow M::L::C to peek in your %INC rather than just 68b39c5158Smillert ### scanning @INC 69b39c5158Smillert $Module::Load::Conditional::CHECK_INC_HASH = 1; 70b39c5158Smillert 71b39c5158Smillert ### reset the 'can_load' cache 72b39c5158Smillert undef $Module::Load::Conditional::CACHE; 73b39c5158Smillert 74b39c5158Smillert ### don't have Module::Load::Conditional issue warnings -- 75b39c5158Smillert ### default is '1' 76b39c5158Smillert $Module::Load::Conditional::VERBOSE = 0; 77b39c5158Smillert 78b39c5158Smillert ### The last error that happened during a call to 'can_load' 79b39c5158Smillert my $err = $Module::Load::Conditional::ERROR; 80b39c5158Smillert 81b39c5158Smillert 82b39c5158Smillert=head1 DESCRIPTION 83b39c5158Smillert 84b39c5158SmillertModule::Load::Conditional provides simple ways to query and possibly load any of 85b39c5158Smillertthe modules you have installed on your system during runtime. 86b39c5158Smillert 87b39c5158SmillertIt is able to load multiple modules at once or none at all if one of 88b39c5158Smillertthem was not able to load. It also takes care of any error checking 89b39c5158Smillertand so forth. 90b39c5158Smillert 91b39c5158Smillert=head1 Methods 92b39c5158Smillert 93898184e3Ssthen=head2 $href = check_install( module => NAME [, version => VERSION, verbose => BOOL ] ); 94b39c5158Smillert 95b39c5158SmillertC<check_install> allows you to verify if a certain module is installed 96b39c5158Smillertor not. You may call it with the following arguments: 97b39c5158Smillert 98b39c5158Smillert=over 4 99b39c5158Smillert 100b39c5158Smillert=item module 101b39c5158Smillert 102b39c5158SmillertThe name of the module you wish to verify -- this is a required key 103b39c5158Smillert 104b39c5158Smillert=item version 105b39c5158Smillert 106b39c5158SmillertThe version this module needs to be -- this is optional 107b39c5158Smillert 108b39c5158Smillert=item verbose 109b39c5158Smillert 110b39c5158SmillertWhether or not to be verbose about what it is doing -- it will default 111b39c5158Smillertto $Module::Load::Conditional::VERBOSE 112b39c5158Smillert 113b39c5158Smillert=back 114b39c5158Smillert 115b39c5158SmillertIt will return undef if it was not able to find where the module was 116b39c5158Smillertinstalled, or a hash reference with the following keys if it was able 117b39c5158Smillertto find the file: 118b39c5158Smillert 119b39c5158Smillert=over 4 120b39c5158Smillert 121b39c5158Smillert=item file 122b39c5158Smillert 123b39c5158SmillertFull path to the file that contains the module 124b39c5158Smillert 125b39c5158Smillert=item dir 126b39c5158Smillert 127b39c5158SmillertDirectory, or more exact the C<@INC> entry, where the module was 128b39c5158Smillertloaded from. 129b39c5158Smillert 130b39c5158Smillert=item version 131b39c5158Smillert 132b39c5158SmillertThe version number of the installed module - this will be C<undef> if 133b39c5158Smillertthe module had no (or unparsable) version number, or if the variable 134b39c5158SmillertC<$Module::Load::Conditional::FIND_VERSION> was set to true. 135b39c5158Smillert(See the C<GLOBAL VARIABLES> section below for details) 136b39c5158Smillert 137b39c5158Smillert=item uptodate 138b39c5158Smillert 139b39c5158SmillertA boolean value indicating whether or not the module was found to be 140b39c5158Smillertat least the version you specified. If you did not specify a version, 141b39c5158Smillertuptodate will always be true if the module was found. 142b39c5158SmillertIf no parsable version was found in the module, uptodate will also be 143b39c5158Smillerttrue, since C<check_install> had no way to verify clearly. 144b39c5158Smillert 145b39c5158SmillertSee also C<$Module::Load::Conditional::DEPRECATED>, which affects 146b39c5158Smillertthe outcome of this value. 147b39c5158Smillert 148b39c5158Smillert=back 149b39c5158Smillert 150b39c5158Smillert=cut 151b39c5158Smillert 152b39c5158Smillert### this checks if a certain module is installed already ### 153b39c5158Smillert### if it returns true, the module in question is already installed 154b39c5158Smillert### or we found the file, but couldn't open it, OR there was no version 155b39c5158Smillert### to be found in the module 156b39c5158Smillert### it will return 0 if the version in the module is LOWER then the one 157b39c5158Smillert### we are looking for, or if we couldn't find the desired module to begin with 158b39c5158Smillert### if the installed version is higher or equal to the one we want, it will return 159b39c5158Smillert### a hashref with he module name and version in it.. so 'true' as well. 160b39c5158Smillertsub check_install { 161b39c5158Smillert my %hash = @_; 162b39c5158Smillert 163b39c5158Smillert my $tmpl = { 164b39c5158Smillert version => { default => '0.0' }, 165b39c5158Smillert module => { required => 1 }, 166b39c5158Smillert verbose => { default => $VERBOSE }, 167b39c5158Smillert }; 168b39c5158Smillert 169b39c5158Smillert my $args; 170b39c5158Smillert unless( $args = check( $tmpl, \%hash, $VERBOSE ) ) { 171b39c5158Smillert warn loc( q[A problem occurred checking arguments] ) if $VERBOSE; 172b39c5158Smillert return; 173b39c5158Smillert } 174b39c5158Smillert 175b39c5158Smillert my $file = File::Spec->catfile( split /::/, $args->{module} ) . '.pm'; 176b39c5158Smillert my $file_inc = File::Spec::Unix->catfile( 177b39c5158Smillert split /::/, $args->{module} 178b39c5158Smillert ) . '.pm'; 179b39c5158Smillert 180b39c5158Smillert ### where we store the return value ### 181b39c5158Smillert my $href = { 182b39c5158Smillert file => undef, 183b39c5158Smillert version => undef, 184b39c5158Smillert uptodate => undef, 185b39c5158Smillert }; 186b39c5158Smillert 187b39c5158Smillert my $filename; 188b39c5158Smillert 189b39c5158Smillert ### check the inc hash if we're allowed to 190b39c5158Smillert if( $CHECK_INC_HASH ) { 191b39c5158Smillert $filename = $href->{'file'} = 192b39c5158Smillert $INC{ $file_inc } if defined $INC{ $file_inc }; 193b39c5158Smillert 194b39c5158Smillert ### find the version by inspecting the package 195b39c5158Smillert if( defined $filename && $FIND_VERSION ) { 196b39c5158Smillert no strict 'refs'; 197b39c5158Smillert $href->{version} = ${ "$args->{module}"."::VERSION" }; 198b39c5158Smillert } 199b39c5158Smillert } 200b39c5158Smillert 2016fb12b70Safresh1 ### we didn't find the filename yet by looking in %INC, 202b39c5158Smillert ### so scan the dirs 203b39c5158Smillert unless( $filename ) { 204b39c5158Smillert 2059f11ffb7Safresh1 local @INC = @INC[0..$#INC-1] if $FORCE_SAFE_INC && $INC[-1] eq '.'; 2069f11ffb7Safresh1 207b39c5158Smillert DIR: for my $dir ( @INC ) { 208b39c5158Smillert 209b39c5158Smillert my $fh; 210b39c5158Smillert 211b39c5158Smillert if ( ref $dir ) { 212b39c5158Smillert ### @INC hook -- we invoke it and get the filehandle back 213b39c5158Smillert ### this is actually documented behaviour as of 5.8 ;) 214b39c5158Smillert 215898184e3Ssthen my $existed_in_inc = $INC{$file_inc}; 216898184e3Ssthen 217b39c5158Smillert if (UNIVERSAL::isa($dir, 'CODE')) { 218b39c5158Smillert ($fh) = $dir->($dir, $file); 219b39c5158Smillert 220b39c5158Smillert } elsif (UNIVERSAL::isa($dir, 'ARRAY')) { 221b39c5158Smillert ($fh) = $dir->[0]->($dir, $file, @{$dir}{1..$#{$dir}}) 222b39c5158Smillert 223b39c5158Smillert } elsif (UNIVERSAL::can($dir, 'INC')) { 224b39c5158Smillert ($fh) = $dir->INC($file); 225b39c5158Smillert } 226b39c5158Smillert 227b39c5158Smillert if (!UNIVERSAL::isa($fh, 'GLOB')) { 228b39c5158Smillert warn loc(q[Cannot open file '%1': %2], $file, $!) 229b39c5158Smillert if $args->{verbose}; 230b39c5158Smillert next; 231b39c5158Smillert } 232b39c5158Smillert 233b39c5158Smillert $filename = $INC{$file_inc} || $file; 234b39c5158Smillert 235898184e3Ssthen delete $INC{$file_inc} if not $existed_in_inc; 236898184e3Ssthen 237b39c5158Smillert } else { 238b39c5158Smillert $filename = File::Spec->catfile($dir, $file); 239b39c5158Smillert next unless -e $filename; 240b39c5158Smillert 241*eac174f2Safresh1 $fh = FileHandle->new(); 242b39c5158Smillert if (!$fh->open($filename)) { 243b39c5158Smillert warn loc(q[Cannot open file '%1': %2], $file, $!) 244b39c5158Smillert if $args->{verbose}; 245b39c5158Smillert next; 246b39c5158Smillert } 247b39c5158Smillert } 248b39c5158Smillert 249b39c5158Smillert ### store the directory we found the file in 250b39c5158Smillert $href->{dir} = $dir; 251b39c5158Smillert 252b39c5158Smillert ### files need to be in unix format under vms, 253b39c5158Smillert ### or they might be loaded twice 254b39c5158Smillert $href->{file} = ON_VMS 255b39c5158Smillert ? VMS::Filespec::unixify( $filename ) 256b39c5158Smillert : $filename; 257b39c5158Smillert 25891f110e0Safresh1 ### if we don't need the version, we're done 25991f110e0Safresh1 last DIR unless $FIND_VERSION; 260b39c5158Smillert 26191f110e0Safresh1 ### otherwise, the user wants us to find the version from files 26256d68f1eSafresh1 26356d68f1eSafresh1 { 26456d68f1eSafresh1 local $SIG{__WARN__} = sub {}; 26556d68f1eSafresh1 my $ver = eval { 26691f110e0Safresh1 my $mod_info = Module::Metadata->new_from_handle( $fh, $filename ); 26756d68f1eSafresh1 $mod_info->version( $args->{module} ); 26856d68f1eSafresh1 }; 269b39c5158Smillert 270b39c5158Smillert if( defined $ver ) { 271b39c5158Smillert $href->{version} = $ver; 272b39c5158Smillert 273b39c5158Smillert last DIR; 274b39c5158Smillert } 275b39c5158Smillert } 276b39c5158Smillert } 27756d68f1eSafresh1 } 278b39c5158Smillert 279b39c5158Smillert ### if we couldn't find the file, return undef ### 280b39c5158Smillert return unless defined $href->{file}; 281b39c5158Smillert 282b39c5158Smillert ### only complain if we're expected to find a version higher than 0.0 anyway 283b39c5158Smillert if( $FIND_VERSION and not defined $href->{version} ) { 284b39c5158Smillert { ### don't warn about the 'not numeric' stuff ### 285b39c5158Smillert local $^W; 286b39c5158Smillert 287b39c5158Smillert ### if we got here, we didn't find the version 288b39c5158Smillert warn loc(q[Could not check version on '%1'], $args->{module} ) 289b39c5158Smillert if $args->{verbose} and $args->{version} > 0; 290b39c5158Smillert } 291b39c5158Smillert $href->{uptodate} = 1; 292b39c5158Smillert 293b39c5158Smillert } else { 294b39c5158Smillert ### don't warn about the 'not numeric' stuff ### 295b39c5158Smillert local $^W; 296b39c5158Smillert 297b39c5158Smillert ### use qv(), as it will deal with developer release number 298b39c5158Smillert ### ie ones containing _ as well. This addresses bug report 299b39c5158Smillert ### #29348: Version compare logic doesn't handle alphas? 300b39c5158Smillert ### 301b39c5158Smillert ### Update from JPeacock: apparently qv() and version->new 302b39c5158Smillert ### are different things, and we *must* use version->new 303b39c5158Smillert ### here, or things like #30056 might start happening 304b39c5158Smillert 305b39c5158Smillert ### We have to wrap this in an eval as version-0.82 raises 306b39c5158Smillert ### exceptions and not warnings now *sigh* 307b39c5158Smillert 308b39c5158Smillert eval { 309b39c5158Smillert 310b39c5158Smillert $href->{uptodate} = 311b39c5158Smillert version->new( $args->{version} ) <= version->new( $href->{version} ) 312b39c5158Smillert ? 1 313b39c5158Smillert : 0; 314b39c5158Smillert 315b39c5158Smillert }; 316b39c5158Smillert } 317b39c5158Smillert 31891f110e0Safresh1 if ( $DEPRECATED and "$]" >= 5.011 ) { 3199f11ffb7Safresh1 local @INC = @INC[0..$#INC-1] if $FORCE_SAFE_INC && $INC[-1] eq '.'; 320b39c5158Smillert require Module::CoreList; 321b39c5158Smillert require Config; 322b39c5158Smillert 323*eac174f2Safresh1 no warnings 'once'; 324b39c5158Smillert $href->{uptodate} = 0 if 325b39c5158Smillert exists $Module::CoreList::version{ 0+$] }{ $args->{module} } and 326b39c5158Smillert Module::CoreList::is_deprecated( $args->{module} ) and 327b8851fccSafresh1 $Config::Config{privlibexp} eq $href->{dir} 328b8851fccSafresh1 and $Config::Config{privlibexp} ne $Config::Config{sitelibexp}; 329b39c5158Smillert } 330b39c5158Smillert 331b39c5158Smillert return $href; 332b39c5158Smillert} 333b39c5158Smillert 3346fb12b70Safresh1=head2 $bool = can_load( modules => { NAME => VERSION [,NAME => VERSION] }, [verbose => BOOL, nocache => BOOL, autoload => BOOL] ) 335b39c5158Smillert 336b39c5158SmillertC<can_load> will take a list of modules, optionally with version 337b39c5158Smillertnumbers and determine if it is able to load them. If it can load *ALL* 338b39c5158Smillertof them, it will. If one or more are unloadable, none will be loaded. 339b39c5158Smillert 340b39c5158SmillertThis is particularly useful if you have More Than One Way (tm) to 341b39c5158Smillertsolve a problem in a program, and only wish to continue down a path 342b39c5158Smillertif all modules could be loaded, and not load them if they couldn't. 343b39c5158Smillert 3446fb12b70Safresh1This function uses the C<load> function or the C<autoload_remote> function 3456fb12b70Safresh1from Module::Load under the hood. 346b39c5158Smillert 347b39c5158SmillertC<can_load> takes the following arguments: 348b39c5158Smillert 349b39c5158Smillert=over 4 350b39c5158Smillert 351b39c5158Smillert=item modules 352b39c5158Smillert 353b39c5158SmillertThis is a hashref of module/version pairs. The version indicates the 354b39c5158Smillertminimum version to load. If no version is provided, any version is 355b39c5158Smillertassumed to be good enough. 356b39c5158Smillert 357b39c5158Smillert=item verbose 358b39c5158Smillert 359b39c5158SmillertThis controls whether warnings should be printed if a module failed 360b39c5158Smillertto load. 361b39c5158SmillertThe default is to use the value of $Module::Load::Conditional::VERBOSE. 362b39c5158Smillert 363b39c5158Smillert=item nocache 364b39c5158Smillert 365b39c5158SmillertC<can_load> keeps its results in a cache, so it will not load the 366b39c5158Smillertsame module twice, nor will it attempt to load a module that has 367b39c5158Smillertalready failed to load before. By default, C<can_load> will check its 368b39c5158Smillertcache, but you can override that by setting C<nocache> to true. 369b39c5158Smillert 3706fb12b70Safresh1=item autoload 3716fb12b70Safresh1 3726fb12b70Safresh1This controls whether imports the functions of a loaded modules to the caller package. The default is no importing any functions. 3736fb12b70Safresh1 3746fb12b70Safresh1See the C<autoload> function and the C<autoload_remote> function from L<Module::Load> for details. 3756fb12b70Safresh1 376b39c5158Smillert=cut 377b39c5158Smillert 378b39c5158Smillertsub can_load { 379b39c5158Smillert my %hash = @_; 380b39c5158Smillert 381b39c5158Smillert my $tmpl = { 382b39c5158Smillert modules => { default => {}, strict_type => 1 }, 383b39c5158Smillert verbose => { default => $VERBOSE }, 384b39c5158Smillert nocache => { default => 0 }, 3856fb12b70Safresh1 autoload => { default => 0 }, 386b39c5158Smillert }; 387b39c5158Smillert 388b39c5158Smillert my $args; 389b39c5158Smillert 390b39c5158Smillert unless( $args = check( $tmpl, \%hash, $VERBOSE ) ) { 391b39c5158Smillert $ERROR = loc(q[Problem validating arguments!]); 392b39c5158Smillert warn $ERROR if $VERBOSE; 393b39c5158Smillert return; 394b39c5158Smillert } 395b39c5158Smillert 396b39c5158Smillert ### layout of $CACHE: 397b39c5158Smillert ### $CACHE = { 398b39c5158Smillert ### $ module => { 399b39c5158Smillert ### usable => BOOL, 400b39c5158Smillert ### version => \d, 401b39c5158Smillert ### file => /path/to/file, 402b39c5158Smillert ### }, 403b39c5158Smillert ### }; 404b39c5158Smillert 405b39c5158Smillert $CACHE ||= {}; # in case it was undef'd 406b39c5158Smillert 407b39c5158Smillert my $error; 408b39c5158Smillert BLOCK: { 409b39c5158Smillert my $href = $args->{modules}; 410b39c5158Smillert 411b39c5158Smillert my @load; 412b39c5158Smillert for my $mod ( keys %$href ) { 413b39c5158Smillert 414b39c5158Smillert next if $CACHE->{$mod}->{usable} && !$args->{nocache}; 415b39c5158Smillert 416b39c5158Smillert ### else, check if the hash key is defined already, 417b39c5158Smillert ### meaning $mod => 0, 418b39c5158Smillert ### indicating UNSUCCESSFUL prior attempt of usage 419b39c5158Smillert 420b39c5158Smillert ### use qv(), as it will deal with developer release number 421b39c5158Smillert ### ie ones containing _ as well. This addresses bug report 422b39c5158Smillert ### #29348: Version compare logic doesn't handle alphas? 423b39c5158Smillert ### 424b39c5158Smillert ### Update from JPeacock: apparently qv() and version->new 425b39c5158Smillert ### are different things, and we *must* use version->new 426b39c5158Smillert ### here, or things like #30056 might start happening 427b39c5158Smillert if ( !$args->{nocache} 428b39c5158Smillert && defined $CACHE->{$mod}->{usable} 429b39c5158Smillert && (version->new( $CACHE->{$mod}->{version}||0 ) 430b39c5158Smillert >= version->new( $href->{$mod} ) ) 431b39c5158Smillert ) { 432b39c5158Smillert $error = loc( q[Already tried to use '%1', which was unsuccessful], $mod); 433b39c5158Smillert last BLOCK; 434b39c5158Smillert } 435b39c5158Smillert 436b39c5158Smillert my $mod_data = check_install( 437b39c5158Smillert module => $mod, 438b39c5158Smillert version => $href->{$mod} 439b39c5158Smillert ); 440b39c5158Smillert 441b39c5158Smillert if( !$mod_data or !defined $mod_data->{file} ) { 442b39c5158Smillert $error = loc(q[Could not find or check module '%1'], $mod); 443b39c5158Smillert $CACHE->{$mod}->{usable} = 0; 444b39c5158Smillert last BLOCK; 445b39c5158Smillert } 446b39c5158Smillert 447b39c5158Smillert map { 448b39c5158Smillert $CACHE->{$mod}->{$_} = $mod_data->{$_} 449b39c5158Smillert } qw[version file uptodate]; 450b39c5158Smillert 451b39c5158Smillert push @load, $mod; 452b39c5158Smillert } 453b39c5158Smillert 454b39c5158Smillert for my $mod ( @load ) { 455b39c5158Smillert 456b39c5158Smillert if ( $CACHE->{$mod}->{uptodate} ) { 457b39c5158Smillert 4589f11ffb7Safresh1 local @INC = @INC[0..$#INC-1] if $FORCE_SAFE_INC && $INC[-1] eq '.'; 4599f11ffb7Safresh1 4606fb12b70Safresh1 if ( $args->{autoload} ) { 4616fb12b70Safresh1 my $who = (caller())[0]; 4626fb12b70Safresh1 eval { autoload_remote $who, $mod }; 4636fb12b70Safresh1 } else { 464b39c5158Smillert eval { load $mod }; 4656fb12b70Safresh1 } 466b39c5158Smillert 467b39c5158Smillert ### in case anything goes wrong, log the error, the fact 468b39c5158Smillert ### we tried to use this module and return 0; 469b39c5158Smillert if( $@ ) { 470b39c5158Smillert $error = $@; 471b39c5158Smillert $CACHE->{$mod}->{usable} = 0; 472b39c5158Smillert last BLOCK; 473b39c5158Smillert } else { 474b39c5158Smillert $CACHE->{$mod}->{usable} = 1; 475b39c5158Smillert } 476b39c5158Smillert 477b39c5158Smillert ### module not found in @INC, store the result in 478b39c5158Smillert ### $CACHE and return 0 479b39c5158Smillert } else { 480b39c5158Smillert 481b39c5158Smillert $error = loc(q[Module '%1' is not uptodate!], $mod); 482b39c5158Smillert $CACHE->{$mod}->{usable} = 0; 483b39c5158Smillert last BLOCK; 484b39c5158Smillert } 485b39c5158Smillert } 486b39c5158Smillert 487b39c5158Smillert } # BLOCK 488b39c5158Smillert 489b39c5158Smillert if( defined $error ) { 490b39c5158Smillert $ERROR = $error; 491b39c5158Smillert Carp::carp( loc(q|%1 [THIS MAY BE A PROBLEM!]|,$error) ) if $args->{verbose}; 492b39c5158Smillert return; 493b39c5158Smillert } else { 494b39c5158Smillert return 1; 495b39c5158Smillert } 496b39c5158Smillert} 497b39c5158Smillert 498b39c5158Smillert=back 499b39c5158Smillert 500b39c5158Smillert=head2 @list = requires( MODULE ); 501b39c5158Smillert 502b39c5158SmillertC<requires> can tell you what other modules a particular module 503b39c5158Smillertrequires. This is particularly useful when you're intending to write 504b39c5158Smillerta module for public release and are listing its prerequisites. 505b39c5158Smillert 506b39c5158SmillertC<requires> takes but one argument: the name of a module. 507b39c5158SmillertIt will then first check if it can actually load this module, and 508b39c5158Smillertreturn undef if it can't. 509b39c5158SmillertOtherwise, it will return a list of modules and pragmas that would 510b39c5158Smillerthave been loaded on the module's behalf. 511b39c5158Smillert 512b39c5158SmillertNote: The list C<require> returns has originated from your current 513b39c5158Smillertperl and your current install. 514b39c5158Smillert 515b39c5158Smillert=cut 516b39c5158Smillert 517b39c5158Smillertsub requires { 518b39c5158Smillert my $who = shift; 519b39c5158Smillert 520b39c5158Smillert unless( check_install( module => $who ) ) { 521b39c5158Smillert warn loc(q[You do not have module '%1' installed], $who) if $VERBOSE; 522b39c5158Smillert return undef; 523b39c5158Smillert } 524b39c5158Smillert 5259f11ffb7Safresh1 local @INC = @INC[0..$#INC-1] if $FORCE_SAFE_INC && $INC[-1] eq '.'; 5269f11ffb7Safresh1 527b39c5158Smillert my $lib = join " ", map { qq["-I$_"] } @INC; 5286fb12b70Safresh1 my $oneliner = 'print(join(qq[\n],map{qq[BONG=$_]}keys(%INC)),qq[\n])'; 5296fb12b70Safresh1 my $cmd = join '', qq["$^X" $lib -M$who -e], QUOTE, $oneliner, QUOTE; 530b39c5158Smillert 531b39c5158Smillert return sort 532b39c5158Smillert grep { !/^$who$/ } 533b39c5158Smillert map { chomp; s|/|::|g; $_ } 534b39c5158Smillert grep { s|\.pm$||i; } 5356fb12b70Safresh1 map { s!^BONG\=!!; $_ } 5366fb12b70Safresh1 grep { m!^BONG\=! } 537b39c5158Smillert `$cmd`; 538b39c5158Smillert} 539b39c5158Smillert 540b39c5158Smillert1; 541b39c5158Smillert 542b39c5158Smillert__END__ 543b39c5158Smillert 544b39c5158Smillert=head1 Global Variables 545b39c5158Smillert 546b39c5158SmillertThe behaviour of Module::Load::Conditional can be altered by changing the 547b39c5158Smillertfollowing global variables: 548b39c5158Smillert 549b39c5158Smillert=head2 $Module::Load::Conditional::VERBOSE 550b39c5158Smillert 551b39c5158SmillertThis controls whether Module::Load::Conditional will issue warnings and 552b39c5158Smillertexplanations as to why certain things may have failed. If you set it 553b39c5158Smillertto 0, Module::Load::Conditional will not output any warnings. 554b39c5158SmillertThe default is 0; 555b39c5158Smillert 556b39c5158Smillert=head2 $Module::Load::Conditional::FIND_VERSION 557b39c5158Smillert 558b39c5158SmillertThis controls whether Module::Load::Conditional will try to parse 559b39c5158Smillert(and eval) the version from the module you're trying to load. 560b39c5158Smillert 561b39c5158SmillertIf you don't wish to do this, set this variable to C<false>. Understand 562b39c5158Smillertthen that version comparisons are not possible, and Module::Load::Conditional 563b39c5158Smillertcan not tell you what module version you have installed. 564b39c5158SmillertThis may be desirable from a security or performance point of view. 565b39c5158SmillertNote that C<$FIND_VERSION> code runs safely under C<taint mode>. 566b39c5158Smillert 567b39c5158SmillertThe default is 1; 568b39c5158Smillert 569b39c5158Smillert=head2 $Module::Load::Conditional::CHECK_INC_HASH 570b39c5158Smillert 571b39c5158SmillertThis controls whether C<Module::Load::Conditional> checks your 572b39c5158SmillertC<%INC> hash to see if a module is available. By default, only 573b39c5158SmillertC<@INC> is scanned to see if a module is physically on your 574898184e3Ssthenfilesystem, or available via an C<@INC-hook>. Setting this variable 575b39c5158Smillertto C<true> will trust any entries in C<%INC> and return them for 576b39c5158Smillertyou. 577b39c5158Smillert 578b39c5158SmillertThe default is 0; 579b39c5158Smillert 5809f11ffb7Safresh1=head2 $Module::Load::Conditional::FORCE_SAFE_INC 5819f11ffb7Safresh1 5829f11ffb7Safresh1This controls whether C<Module::Load::Conditional> sanitises C<@INC> 5839f11ffb7Safresh1by removing "C<.>". The current default setting is C<0>, but this 5849f11ffb7Safresh1may change in a future release. 5859f11ffb7Safresh1 586b39c5158Smillert=head2 $Module::Load::Conditional::CACHE 587b39c5158Smillert 588b39c5158SmillertThis holds the cache of the C<can_load> function. If you explicitly 589b39c5158Smillertwant to remove the current cache, you can set this variable to 590b39c5158SmillertC<undef> 591b39c5158Smillert 592b39c5158Smillert=head2 $Module::Load::Conditional::ERROR 593b39c5158Smillert 594b39c5158SmillertThis holds a string of the last error that happened during a call to 595b39c5158SmillertC<can_load>. It is useful to inspect this when C<can_load> returns 596b39c5158SmillertC<undef>. 597b39c5158Smillert 598b39c5158Smillert=head2 $Module::Load::Conditional::DEPRECATED 599b39c5158Smillert 600b39c5158SmillertThis controls whether C<Module::Load::Conditional> checks if 601b39c5158Smillerta dual-life core module has been deprecated. If this is set to 602b39c5158Smillerttrue C<check_install> will return false to C<uptodate>, if 603b39c5158Smillerta dual-life module is found to be loaded from C<$Config{privlibexp}> 604b39c5158Smillert 605b39c5158SmillertThe default is 0; 606b39c5158Smillert 607b39c5158Smillert=head1 See Also 608b39c5158Smillert 609b39c5158SmillertC<Module::Load> 610b39c5158Smillert 611b39c5158Smillert=head1 BUG REPORTS 612b39c5158Smillert 613b39c5158SmillertPlease report bugs or other issues to E<lt>bug-module-load-conditional@rt.cpan.orgE<gt>. 614b39c5158Smillert 615b39c5158Smillert=head1 AUTHOR 616b39c5158Smillert 617b39c5158SmillertThis module by Jos Boumans E<lt>kane@cpan.orgE<gt>. 618b39c5158Smillert 619b39c5158Smillert=head1 COPYRIGHT 620b39c5158Smillert 621b39c5158SmillertThis library is free software; you may redistribute and/or modify it 622b39c5158Smillertunder the same terms as Perl itself. 623b39c5158Smillert 624b39c5158Smillert=cut 625