Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: One module to use them all (proxy moudle that exports subs of other modules)

by pryrt (Abbot)
on Sep 03, 2022 at 19:15 UTC ( [id://11146668]=note: print w/replies, xml ) Need Help??


in reply to One module to use them all (proxy moudle that exports subs of other modules)

My take, with one of the sub-modules being Exporter-based, the second using its own import(), and the One Module still being able to figure out how to bind them together and tell the world what to do:

11146642-tldr.pl

#!perl use 5.012; # //, strict, say use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use Mod11146642::All; # Mod11146642::One uses Exporter # Mod11146642::Two uses manual import() # but both work through Mod11146642::All oneFunction(); twoFunction();

Mod11146642/All.pm

package Mod11146642::All 1.00; use 5.012; # //, strict, say use warnings; use Exporter 5.47 qw(import); my %colons; my @inherited; BEGIN { $colons{$_} = $_ for keys %::Mod11146642::All::; } use Mod11146642::One; use Mod11146642::Two; BEGIN { for (sort keys %::Mod11146642::All::) { next if /^__ANON__$/; # ignore anonymous functions next if exists $colons{$_}; # ones that were in the namesp +ace before weren't inherited push @inherited, $_; # if we're here, we inherited +this } # local $" = ","; warn "inherited (@inherited)\n"; # un-comment + this line if you want to debug the ineritance check } our @EXPORT = @inherited; 1;

Mod11146642/One.pm

package Mod11146642::One 1.00; use 5.012; # //, strict, say use warnings; use Exporter 5.47 qw(import); our @EXPORT = qw(oneFunction); sub oneFunction { local $" = ","; printf STDERR "Called %s(@_)\n", (caller(0))[3]; } 1;

Mod11146642/Two.pm

package Mod11146642::Two 2.00; use 5.012; # //, strict, say use warnings; sub twoFunction { local $" = ","; printf STDERR "Called %s(@_)\n", (caller(0))[3]; } sub import { my ($pkg) = @_; my $callpkg = caller(0); no strict 'refs'; my $exportfunction = $callpkg . '::twoFunction'; *{$exportfunction} = \&twoFunction; } 1;

Replies are listed 'Best First'.
Re^2: One module to use them all (proxy moudle that exports subs of other modules)
by BillKSmith (Monsignor) on Sep 04, 2022 at 20:46 UTC
    The OP has existing modules which do not have an import function because they do not have use EXPORTER;. Would it not be easier to add that line than a custom import function? Adding *twoFunction =\&Mod11146642::All::twoFunction; to the new module Mod11146642::All should do the same thing for this application and not affect anything else.

    Long UPDATE to Clarify my point

    Nataraj has posted the following update to One module to use them all (proxy moudle that exports subs of other modules).
    Update: Partially solved here. This solution (Re: One module to use them all (proxy moudle that exports subs of other modules)) works for modules that uses Export for exporting. For other modules I added desired subs to export list explicitly. This did most of the trick.
    From this I assume that most functions ‘work’ (i.e. The application tldr.pl can access them with unqualified names) . Later, he tells us that all the exceptions are in third party (‘not really mine’) modules which do not have use EXPORTER. It is reasonable to assume that these modules do not have an appropriate ‘import’ function. At least three solutions have been proposed. Any one of them would ‘work’ . None of them are exactly what the OP was hoping for.
    • Manually ‘import’ the function name(s) into the proxy module.
    • Edit the module to add use EXPORT
    • Edit the module to add a custom import function.

    The second and third option are probably not available for third party modules. The first option has the advantage that all extra code is added to the ‘proxy’ module ‘ModAll’. This method fails to meet the OP's expectations only because the proxy module must be manually updated every time the library of third party functions is reorganized.

    Demo of ‘manual’ method

    Modification of Re: One module to use them all (proxy moudle that exports subs of other modules)

    The following files are unchanged

    • tldr.pl
    • Mod1.pm
    • Mod2.pm

    EXPORTER has been removed from Mod3.pm to simulate a 'third party' module.

    package Mod3; use warnings; use strict; #use Exporter 'import'; #our @EXPORT = qw( m3 ); sub m3 { 'm3' } __PACKAGE__

    Manual ‘import’ added to ModAll.pm

    package ModAll; use warnings; use strict; use Mod1; use Mod2; use Mod3; use Exporter 'import'; # Supply function name ‘m3’ manually (not in any EXPORT list) #our @EXPORT = (@Mod1::EXPORT, @Mod2::EXPORT, @Mod3::EXPORT); our @EXPORT = (@Mod1::EXPORT, @Mod2::EXPORT, ‘m3’); *m3 = \&Mod3::m3 # hand code result of missing import; __PACKAGE__
    Bill
      Would it not be easier to add that line than a custom import function?

      That assumes that you know all the functions exported by all the sub-modules before designing the All.pm module. The OP specifically contradicted this assumption: "their export list is not fixed and changes from time to time".

      So, ::All needs to be designed in such a way that if ::Two v2.00 exports just twoFunction() , but ::Two v2.01 exports frobnicate() as well, ::All can still work without having to be updated.

      > The OP has existing modules which do not have an import function because they do not have use EXPORTER;.

      Sorry, this doesn't make sense.

      You can have a sub import function without using the customized one provided from the module Exporter °

      And without import no way to export.

      Hence if the OP wanted to call those module-subs fully qualified anyway (because they are not exported) there wouldn't be any problem. He could just require the modules, and call the subs in their native package.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      °) have a look at the provided Two.pm "using its own import()"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-24 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found