Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.1 KiB

  1. package autouse;
  2. #use strict; # debugging only
  3. use 5.003_90; # ->can, for my $var
  4. $autouse::VERSION = '1.02';
  5. $autouse::DEBUG ||= 0;
  6. sub vet_import ($);
  7. sub croak {
  8. require Carp;
  9. Carp::croak(@_);
  10. }
  11. sub import {
  12. my $class = @_ ? shift : 'autouse';
  13. croak "usage: use $class MODULE [,SUBS...]" unless @_;
  14. my $module = shift;
  15. (my $pm = $module) =~ s{::}{/}g;
  16. $pm .= '.pm';
  17. if (exists $INC{$pm}) {
  18. vet_import $module;
  19. local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
  20. # $Exporter::Verbose = 1;
  21. return $module->import(map { (my $f = $_) =~ s/\(.*?\)$//; $f } @_);
  22. }
  23. # It is not loaded: need to do real work.
  24. my $callpkg = caller(0);
  25. print "autouse called from $callpkg\n" if $autouse::DEBUG;
  26. my $index;
  27. for my $f (@_) {
  28. my $proto;
  29. $proto = $1 if (my $func = $f) =~ s/\((.*)\)$//;
  30. my $closure_import_func = $func; # Full name
  31. my $closure_func = $func; # Name inside package
  32. my $index = index($func, '::');
  33. if ($index == -1) {
  34. $closure_import_func = "${callpkg}::$func";
  35. } else {
  36. $closure_func = substr $func, $index + 2;
  37. croak "autouse into different package attempted"
  38. unless substr($func, 0, $index) eq $module;
  39. }
  40. my $load_sub = sub {
  41. unless ($INC{$pm}) {
  42. eval {require $pm};
  43. die if $@;
  44. vet_import $module;
  45. }
  46. *$closure_import_func = \&{"${module}::$closure_func"};
  47. print "autousing $module; "
  48. ."imported $closure_func as $closure_import_func\n"
  49. if $autouse::DEBUG;
  50. goto &$closure_import_func;
  51. };
  52. if (defined $proto) {
  53. *$closure_import_func = eval "sub ($proto) { &\$load_sub }";
  54. } else {
  55. *$closure_import_func = $load_sub;
  56. }
  57. }
  58. }
  59. sub vet_import ($) {
  60. my $module = shift;
  61. if (my $import = $module->can('import')) {
  62. croak "autoused module has unique import() method"
  63. unless defined(&Exporter::import)
  64. && $import == \&Exporter::import;
  65. }
  66. }
  67. 1;
  68. __END__
  69. =head1 NAME
  70. autouse - postpone load of modules until a function is used
  71. =head1 SYNOPSIS
  72. use autouse 'Carp' => qw(carp croak);
  73. carp "this carp was predeclared and autoused ";
  74. =head1 DESCRIPTION
  75. If the module C<Module> is already loaded, then the declaration
  76. use autouse 'Module' => qw(func1 func2($;$) Module::func3);
  77. is equivalent to
  78. use Module qw(func1 func2);
  79. if C<Module> defines func2() with prototype C<($;$)>, and func1() and
  80. func3() have no prototypes. (At least if C<Module> uses C<Exporter>'s
  81. C<import>, otherwise it is a fatal error.)
  82. If the module C<Module> is not loaded yet, then the above declaration
  83. declares functions func1() and func2() in the current package, and
  84. declares a function Module::func3(). When these functions are called,
  85. they load the package C<Module> if needed, and substitute themselves
  86. with the correct definitions.
  87. =head1 WARNING
  88. Using C<autouse> will move important steps of your program's execution
  89. from compile time to runtime. This can
  90. =over
  91. =item *
  92. Break the execution of your program if the module you C<autouse>d has
  93. some initialization which it expects to be done early.
  94. =item *
  95. hide bugs in your code since important checks (like correctness of
  96. prototypes) is moved from compile time to runtime. In particular, if
  97. the prototype you specified on C<autouse> line is wrong, you will not
  98. find it out until the corresponding function is executed. This will be
  99. very unfortunate for functions which are not always called (note that
  100. for such functions C<autouse>ing gives biggest win, for a workaround
  101. see below).
  102. =back
  103. To alleviate the second problem (partially) it is advised to write
  104. your scripts like this:
  105. use Module;
  106. use autouse Module => qw(carp($) croak(&$));
  107. carp "this carp was predeclared and autoused ";
  108. The first line ensures that the errors in your argument specification
  109. are found early. When you ship your application you should comment
  110. out the first line, since it makes the second one useless.
  111. =head1 AUTHOR
  112. Ilya Zakharevich (ilya@math.ohio-state.edu)
  113. =head1 SEE ALSO
  114. perl(1).
  115. =cut