This is a not-yet-(and perhaps never)-ready-for-primetime idea after looking at this idea for providing a way to export functions (and perhaps other symbols) to an aliased name, except that I wanted to try to create a way to do that with a plain, already existing module which exports in a "normal" way (update: but not necessarily using Exporter). This is not meant to be production code, it is just starting to play with the idea, and so far only exports functions. Toward the end of making this, I did see Exporter::VA, which is fine, but that module requires that the exporting modules use the Exporter module, which not all modules do (such as TheDamian's), or what if a module exports a code reference which does not already exist as a symbol in the module, etc., so there still may be some purpose to this code :-)
Example:
#!/usr/bin/perl
use strict;
use warnings;
use Alias::Exporter (
"Test::Foo" => { bar => 'foo' },
"Test::Bar" => { bar => 'baz' },
);
foo();
baz();
# file Test/Foo.pm
package Test::Foo;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(bar);
sub bar {
print "Test::Foo::bar!\n";
}
1;
# file Test/Bar.pm
package Test::Bar;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(bar);
sub bar {
print "Test::Bar::bar!\n";
}
1;
# the guts: warning - ugly code ahead
package Alias::Exporter;
use strict;
use warnings;
sub import {
my $class = shift;
my %modules = @_;
my $package = caller();
for my $module ( keys %modules ) {
my %imports = %{$modules{$module}};
my @import_list = keys %imports;
eval qq|
use $module ( \@import_list );
for my \$var ( \@import_list ) {
no strict 'refs';
*{ \$package . "::\$imports{\$var}" } = \\&{\$var};
}
|;
die $@ if $@;
}
}
1;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|