Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Perl: Dynamically loading modules and accessing the exported contents
- package modules::Test;
- use strict;
- use Exporter;
- use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
- $VERSION = 1.00;
- @ISA = qw(Exporter);
- @EXPORT = (*TestSubSomeUnknownName);
- @EXPORT_OK = qw(&TestSubSomeUnknownName);
- %EXPORT_TAGS = ( ALL => [qw(&TestSubSomeUnknownName)]
- );
- sub TestSubSomeUnknownName
- {
- # return a hash reference
- }
- use Module::Load;
- my $package = "modules::Test";
- my $subr = "TestSubSomeUnknownName";
- load $package;
- # Call the subroutine
- my $hashref = $package->$subr;
- die 'Auto-import sub was not named "TestSubSomeUnknownName"'
- unless $package->can( 'TestSubSomeUnknownName' )
- ;
- $package->TestSubSomeUnknownName();
- use My::Module qw(subroutines to be imported);
- #! /usr/bin/env perl
- use strict;
- use warnings;
- use feature qw(say);
- while (my ($var, $type) = each %Foo::) {
- if (defined &$type) {
- say "$var is a subroutine";
- }
- else {
- say "$var is defined as something or another";
- }
- }
- package Foo;
- our %bar = (foo => 1, bar => 2);
- our @foo = qw(foo bar);
- our $fubar = "barfu";
- sub barfu {
- print "FOO!";
- return "FOO!";
- }
- barfu is a subroutine
- fubar is defined as something or another
- bar is defined as something or another
- foo is defined as something or another
- #! /usr/bin/env perl
- use strict;
- use warnings;
- use feature qw(say);
- use Data::Dumper;
- while (my ($var, $type) = each %Foo::) {
- print "$var";
- # foreach my $ref_type (qw(SCALAR GLOB ARRAY HASH CODE REF LVALUE FORMAT IO VSTRING Regexp)) {
- foreach my $ref_type (qw(ARRAY HASH CODE REF LVALUE FORMAT IO VSTRING Regexp)) {
- if (defined *$type{$ref_type}) {
- say qq("$var" is a type $ref_type);
- }
- }
- }
- package Foo;
- our %bar = (foo => 1, bar => 2);
- our @foo = qw(foo bar);
- our $fubar = "barfu";
- sub barfu {
- print "FOO!";
- return "FOO!";
- }
- my $package = 'modules::Test';
- load $package;
- my $exports;
- eval {
- no strict;
- $exports = @{"$package::EXPORT"};
- };
- # Call the subroutine
- my $subr = @{$exports}[0];
- my $hash = $package->$subr if($package->can($subr));
Add Comment
Please, Sign In to add comment