daily pastebin goal
78%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 52 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. Perl: Dynamically loading modules and accessing the exported contents
  2. package modules::Test;
  3.  
  4. use strict;
  5. use Exporter;
  6. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.  
  8. $VERSION     = 1.00;
  9. @ISA         = qw(Exporter);
  10. @EXPORT      = (*TestSubSomeUnknownName);
  11. @EXPORT_OK   = qw(&TestSubSomeUnknownName);
  12. %EXPORT_TAGS = ( ALL => [qw(&TestSubSomeUnknownName)]
  13.                );
  14.  
  15.  
  16. sub TestSubSomeUnknownName
  17. {
  18.     # return a hash reference
  19. }
  20.    
  21. use Module::Load;
  22.  
  23. my $package = "modules::Test";
  24. my $subr = "TestSubSomeUnknownName";
  25.  
  26. load $package;
  27. # Call the subroutine
  28. my $hashref = $package->$subr;
  29.    
  30. die 'Auto-import sub was not named "TestSubSomeUnknownName"'
  31.     unless  $package->can( 'TestSubSomeUnknownName' )
  32.     ;
  33. $package->TestSubSomeUnknownName();
  34.    
  35. use My::Module qw(subroutines to be imported);
  36.    
  37. #! /usr/bin/env perl
  38. use strict;
  39. use warnings;
  40. use feature qw(say);
  41.  
  42. while (my ($var, $type) = each %Foo::) {
  43.     if (defined &$type) {
  44.         say "$var is a subroutine";
  45.     }
  46.     else {
  47.         say "$var is defined as something or another";
  48.     }
  49. }
  50.  
  51. package Foo;
  52.  
  53. our %bar = (foo => 1, bar => 2);
  54. our @foo = qw(foo bar);
  55. our $fubar = "barfu";
  56.  
  57. sub barfu {
  58.     print "FOO!";
  59.     return "FOO!";
  60. }
  61.    
  62. barfu is a subroutine
  63. fubar is defined as something or another
  64. bar is defined as something or another
  65. foo is defined as something or another
  66.    
  67. #! /usr/bin/env perl
  68. use strict;
  69. use warnings;
  70. use feature qw(say);
  71. use Data::Dumper;
  72.  
  73. while (my ($var, $type) = each %Foo::) {
  74.     print "$var";
  75. #   foreach my $ref_type (qw(SCALAR GLOB ARRAY HASH CODE REF LVALUE FORMAT IO VSTRING Regexp)) {
  76.     foreach my $ref_type (qw(ARRAY HASH CODE REF LVALUE FORMAT IO VSTRING Regexp)) {
  77.         if (defined *$type{$ref_type}) {
  78.             say qq("$var" is a type $ref_type);
  79.         }
  80.     }
  81. }
  82.  
  83. package Foo;
  84.  
  85. our %bar = (foo => 1, bar => 2);
  86. our @foo = qw(foo bar);
  87. our $fubar = "barfu";
  88.  
  89. sub barfu {
  90.     print "FOO!";
  91.     return "FOO!";
  92. }
  93.    
  94. my $package = 'modules::Test';
  95. load $package;
  96. my $exports;
  97. eval {
  98.     no strict;
  99.     $exports = @{"$package::EXPORT"};
  100. };
  101. # Call the subroutine
  102. my $subr = @{$exports}[0];
  103. my $hash = $package->$subr if($package->can($subr));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top