Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by choroba (Cardinal)
on Sep 02, 2022 at 21:00 UTC ( [id://11146644]=note: print w/replies, xml ) Need Help??


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

tldr.pl
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use ModAll; say for m1(), m2(), m3();

Mod1.pm

package Mod1; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw( m1 ); sub m1 { 'm1' } __PACKAGE__

Mod2.pm

package Mod2; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw( m2 ); sub m2 { 'm2' } __PACKAGE__

Mod3.pm

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

ModAll.pm

package ModAll; use warnings; use strict; use Mod1; use Mod2; use Mod3; use Exporter 'import'; our @EXPORT = (@Mod1::EXPORT, @Mod2::EXPORT, @Mod3::EXPORT); __PACKAGE__

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: One module to use them all (proxy moudle that exports subs of other modules)
by nataraj (Sexton) on Sep 02, 2022 at 21:15 UTC

    So easy! But this worked for me!

    Thank you for your answer

Re^2: One module to use them all (proxy moudle that exports subs of other modules)
by nataraj (Sexton) on Sep 02, 2022 at 21:28 UTC

    Oups... Sorry... not that easy... Some of MyMod* modules (that are not really mine) does not use Exporter. And this breaks everything :-(

      Based on choroba's ++reply, here's a (partial?) solution (?) that may be closer to what you want.

      tldr.pl:

      # from choroba pm#11146644 (modified) use warnings; use strict; use ModAll; print "'$_' \n" for Foo::bar(qw(some stuff)), m1(), m2('whatever'), m3 +;
      ModAll.pm:
      # from choroba pm#11146644 (modified) package ModAll; use warnings; use strict; # use Data::Dump qw(dd); # for debug use constant USE_ALL => qw(Mod1 Mod2 Mod3 Foo); eval "use $_" for USE_ALL; use Exporter 'import'; our @EXPORT = map eval, map "\@${_}::EXPORT", USE_ALL ; __PACKAGE__
      Mod1.pm:
      package Mod1; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw( m1 ); sub m1 { "m1(@_)" } __PACKAGE__
      Mod2.pm:
      package Mod2; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw( m2 ); sub m2 { "m2(@_)" } __PACKAGE__
      Mod3.pm:
      package Mod3; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw( m3 ); sub m3 { "m3(@_)" } __PACKAGE__
      Foo.pm (forgot this previously):
      # Foo.pm package Foo; use strict; use warnings; sub bar { sprintf "hiya from -%s-(@_) invoked from '%s'", (caller 0)[3, 0]; } 1;
      Invocation:
      Win8 Strawberry 5.8.9.5 (32) Fri 09/02/2022 19:51:48 C:\@Work\Perl\monks\nataraj >perl tldr.pl 'hiya from -Foo::bar-(some stuff) invoked from 'main'' 'm1()' 'm2(whatever)' 'm3()'
      (Caution: There may be some important phasing considerations in my version of ModAll.pm that I'm overlooking!)

      Update: Forgot to list the Foo.pm module. It's listed elsewhere, but I've added it here for completeness.


      Give a man a fish:  <%-{-{-{-<

      Some of MyMod* modules ... does not use Exporter.

      So what do they use?

      Is it possible that some of the modules do not export at all, i.e., the fully-qualified name of a subroutine or package-global variable defined in the module must be used to invoke the subroutine/variable? E.g.:
      File Foo.pm:

      # Foo.pm package Foo; use strict; use warnings; sub bar { printf "hiya from -%s-(@_) invoked from '%s' \n", (caller 0)[3, 0] +; } 1;
      Invocation:
      Win8 Strawberry 5.8.9.5 (32) Fri 09/02/2022 18:25:19 C:\@Work\Perl\monks\nataraj >perl -wMstrict -le "use Foo; Foo::bar(qw(some stuff)); print 'ok'" hiya from -Foo::bar-(some stuff) invoked from 'main' ok
      This situation can be addressed, but we need these details.


      Give a man a fish:  <%-{-{-{-<

      And this breaks everything :-(

      I don't see how the absence of Exporter in any of those modules would cause breakage.
      Can you provide more details ?

      Cheers,
      Rob
        I don't see how the absence of Exporter in any of those modules would cause breakage. Can you provide more details ?

        Because the solutions that assume Exporter specifically assume the existance of @EXPORT in every one of the sub-modules. My Two.pm does not contain @EXPORT, and thus none of Two.pm's functions would be exported to the tldr script if my ::All had our @EXPORT = (@Mod11146642::One::EXPORT, @Mod11146642::Two::EXPORT); instead of the our @EXPORT = @inherited; and the stash-diff that populated @inherited. If I used the EXPORT-assuming line, ignoring @inherited, I would get

        C:\usr\local\share\PassThru\perl\perlmonks > 11146642-tldr.pl Called Mod11146642::One::oneFunction() Undefined subroutine &main::twoFunction called at C:\usr\local\share\P +assThru\perl\perlmonks\11146642-tldr.pl line 14.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-20 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found