home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / require.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-14  |  2.9 KB  |  121 lines

  1. package UNIVERSAL::require;
  2. $UNIVERSAL::require::VERSION = '0.03';
  3.  
  4. # We do this because UNIVERSAL.pm uses CORE::require().  We're going
  5. # to put our own require() into UNIVERSAL and that makes an ambiguity.
  6. # So we load it up beforehand to avoid that.
  7. BEGIN { require UNIVERSAL }
  8.  
  9. package UNIVERSAL;
  10.  
  11. use strict;
  12.  
  13.  
  14. =head1 NAME
  15.  
  16.   UNIVERSAL::require - require() modules from a variable
  17.  
  18. =head1 SYNOPSIS
  19.  
  20.   # This only needs to be said once in your program.
  21.   require UNIVERSAL::require;
  22.  
  23.   # Same as "require Some::Module;"
  24.   Some::Module->require;
  25.  
  26.   # Ditto
  27.   my $module = 'Some::Module';
  28.   $module->require;
  29.  
  30. =head1 DESCRIPTION
  31.  
  32. If you've ever had to do this...
  33.  
  34.     eval "require $module";
  35.  
  36. to get around the bareword caveats on require(), this module is for
  37. you.  It creates a universal require() class method that will work
  38. with every Perl module.  So instead of doing some arcane eval() work,
  39. you can do this:
  40.  
  41.     $module->require;
  42.  
  43. And C<use Some::Module> can be done dynamically like so:
  44.  
  45.     BEGIN {
  46.         $module->require;
  47.         $module->import;
  48.     }
  49.  
  50. It doesn't save you much typing, but it'll make alot more sense to
  51. someone who's not a ninth level Perl acolyte.
  52.  
  53. =head1 Methods
  54.  
  55. =over 4
  56.  
  57. =item B<require>
  58.  
  59.   my $return_val = $module->require;
  60.   my $return_val = $module->require($version);
  61.  
  62. This works exactly like Perl's require, except without the bareword
  63. restriction, and it doesn't die.  Since require() is placed in the
  64. UNIVERSAL namespace, it will work on B<any> module.  You just have to
  65. use UNIVERSAL::require somewhere in your code.
  66.  
  67. Should the module require fail, or not be a high enough $version, it
  68. will simply return false and B<not die>.  The error will be in
  69. $UNIVERSAL::require::ERROR.
  70.  
  71. =back
  72.  
  73. =head1 AUTHOR
  74.  
  75. Michael G Schwern <schwern@pobox.com>
  76.  
  77.  
  78. =head1 SEE ALSO
  79.  
  80. L<UNIVERSAL::exports>, L<perlfunc/require>,
  81. http://dev.perl.org/rfc/253.pod
  82.  
  83. =cut
  84.  
  85.  
  86. sub require {
  87.     my($module, $want_version) = @_;
  88.  
  89.     $UNIVERSAL::require::ERROR = '';
  90.  
  91.     die("UNIVERSAL::require() can only be run as a class method")
  92.       if ref $module; 
  93.  
  94.     die("UNIVERSAL::require() takes no or one arguments") if @_ > 2;
  95.  
  96.     # Load the module.
  97.     my $return = eval "CORE::require $module";
  98.  
  99.     # Check for module load failure.
  100.     if( $@ ) {
  101.         $@ =~ s/ at .*?\n$//;
  102.         $UNIVERSAL::require::ERROR = sprintf "$@ at %s line %d.\n", 
  103.                                             (caller)[1,2];
  104.         return 0;
  105.     }
  106.  
  107.     # Module version check.  We can't just call UNIVERSAL->VERSION
  108.     # and let it die because the file and line numbers will be wrong.
  109.     if( @_ == 2 and !eval { $module->VERSION($want_version); 1 } ) {
  110.         $@ =~ s/ at .*?\n$//;
  111.         $UNIVERSAL::require::ERROR = sprintf "$@ at %s line %d\n", 
  112.                                              (caller)[1,2];
  113.         return 0;
  114.     }
  115.  
  116.     return $return;
  117. }
  118.  
  119.  
  120. 1;
  121.