in reply to Re^3: Exporter. Correct way to override import?
in thread Exporter. Correct way to override import?

I had no time to answer earlier, but I fully agree with afoken.

Unfortunately do the perldocs often reflect only an archaic Perl.

It's no wonder if beginners become confused and frustrated.

For instance we keep telling them to use strict and declare variables while a big part of the docs isn't strict at all.

I consider this a real problem, because it's damaging the reputation of Perl.

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

  • Comment on Re^4: Exporter. Correct way to override import? (archaic perldoc)

Replies are listed 'Best First'.
Re^5: Exporter. Correct way to override import? (archaic perldoc)
by ikegami (Patriarch) on May 07, 2018 at 00:32 UTC

    Unfortunately do the perldocs often reflect only an archaic Perl.

    Again, not so. Exporter includes

    package YourModule; use Exporter 'import'; # gives you Exporter's import() method directly @EXPORT_OK = qw(munge frobnicate); # symbols to export on request

      The Exporter POD also includes (in the Good Practices section no less):

      When using Exporter with the standard strict and warnings pragmas, the our keyword is needed to declare the package variables @EXPORT_OK, @EXPORT, @ISA, etc.

      our @ISA = qw(Exporter); our @EXPORT_OK = qw(munge frobnicate);

        So what? Even if it is presented as an option, you should still not pick the version that makes no sense (unless you need compatibility with pre-5.8.3 builds of Perl).

Re^5: Exporter. Correct way to override import? (archaic perldoc)
by Veltro (Hermit) on May 06, 2018 at 21:34 UTC

    Well, I tried to do it without the ISA. But with the serious hack that I am trying to make as an experiment I can't get around just using export_to_level. export_to_level is not exported by Exporter by the way. The goto breaks the import method that I made because of its recursive nature and so I fully depend on export_to_level. I have not been able to figure out yet how to use the function without the ISA. Also Exporter->export_to_level( 1, @_ ) ; does not seem to work for me. Errors are of the kind: <Function Name> is not exported by the Exporter module.

      Please explain:
      • What is your goal?
      • What do you want to do in your own import()?
      • Why do you need Exporter if you want to write your own import()?

      ->export_to_level can't be "exported", since it is a method. (expects the obj/class as first argument). In order to work you need to require or use exporter first.

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

        Oh, I was just trying to abuse the Exporter to to create a flexible function importer over packages with the ability to be able to redefine the function on a higher level

        Let's say I have a package structure (where I make sure that the sub packages have a 'use parent' statement)

        Parent->ChildX ->ChildY->GrandchildYX ->GrandchildYY

        Inside of these packages I want have a bunch of functions with the same name:

        Parent.function1 (function1 defined in Parent) ChildX.function1 (function1 imported from Parent) ChildX.function2 (function2 defined in ChildX) ChildY.function1 (function1 defined in ChildY) ChildY.function2 (function2 defined in ChildY) GrandchildYX.function1 (function1 imported from ChildY) GrandchildYX.function2 (function2 defined in GrandchildYX)
        Any program that I use
        use Parent qw(function1) # function 1 from Parent or use ChildX qw(function1 function2) # function1 from Parent, function2 +from ChildX or use ChildY qw(function1) # function1 from ChildY or use GrandchildYX qw(function1 function2) # function1 from ChildY, func +tion2 from GrandchildYX

        The experiment/challenge was to see if I can pull this off by writing ONE import function in Parent

        The code that I wrote works now I think, but it relies on the method export_to_level which I basically abuse and call static

        If you want I can share the code (not now though it is late), but I have to warn you it is an ugly piece