Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. package Modulino::Load;
  2. # ABSTRACT: Load modulinos into their own namespaces
  3.  
  4. use strict;
  5. use warnings;
  6.  
  7. use Module::Runtime 'is_module_name';
  8. use Carp 'croak';
  9.  
  10. # We heard you like legacy code
  11. # so we put legacy code on your legacy code
  12. # so you can rake technical debt while raking technical debt.
  13.  
  14. # Is this too evil?
  15.  
  16. sub import {
  17. my ( undef, @paths ) = @_;
  18.  
  19. my ($caller) = caller;
  20.  
  21. for my $path (@paths) {
  22. croak 'Imported path does not exist: ' . $path unless -e $path;
  23.  
  24. # Convert path into pseudo-package name
  25. # ( no extension, change - into _, and with :: as separators )
  26. my $package = $path;
  27. $package =~ s/\..*$//;
  28. $package =~ y/-/_/;
  29. $package = join '::', split m{/}, $package;
  30.  
  31. croak 'Could not generate module name from path: ' . $package
  32. unless is_module_name($package);
  33.  
  34. # Evil code is evil
  35. eval qq{ package $package { require("$path") }; };
  36. }
  37. }
  38.  
  39. 1;
  40.  
  41. __END__
  42.  
  43. #!/usr/bin/env perl
  44.  
  45. use strict;
  46. use warnings;
  47.  
  48. # Load multiple modulinos without polluting your own namespace
  49. use Modulino::Load qw(
  50. some/modulino.pl
  51. another/modulino.pl
  52. yet-another/crazy/modulino.pl
  53. );
  54.  
  55. main() unless caller;
  56.  
  57. sub main {
  58. some::modulino::run();
  59. yet_another::crazy::modulino::foo();
  60. another::modulino::main();
  61. yet_another::crazy::modulino::bar();
  62. exit;
  63. };
  64.  
  65. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement