Athanasius has asked for the wisdom of the Perl Monks concerning the following question:

According to the documentation for import:

There is no builtin import function. It is just an ordinary method (subroutine) defined (or inherited) by modules that wish to export names to another module. The use function calls the import method for the package used.

However, when I create an empty module (file “MyMod.pm”):

package MyMod; 1;

and print out its symbol table (file “1712_SoPW.pl”):

use strict; use warnings; use Data::Dump; use MyMod; dd \%MyMod::;

the output shows that an import function is already present:

16:10 >perl 1712_SoPW.pl { import => *MyMod::import } 16:10 >

Where does it come from? Is the documentation incorrect, or am I simply misreading it?

Thanks,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re: Default import function
by salva (Canon) on Oct 24, 2016 at 06:51 UTC
    the output shows that an import function is already present

    Actually, it shows that an entry for import has been added to the symbol table.

    use 5.10; use Devel::Peek; use foo; say join " ", keys %{"foo::"}; Dump ${"foo::"}{import}' __END__ # Output: import SV = PVGV(0x55c979ee96a0) at 0x55c979e89f30 REFCNT = 1 FLAGS = (MULTI) NAME = "import" NAMELEN = 6 GvSTASH = 0x55c979ef40e0 "foo" FLAGS = 0x2 GP = 0x55c979f017e0 SV = 0x0 REFCNT = 1 IO = 0x0 FORM = 0x0 AV = 0x0 HV = 0x0 CV = 0x0 CVGEN = 0x6 GPFLAGS = 0x0 () LINE = 1 FILE = "-e" EGV = 0x55c979e89f30 "import"
    You can see that the function slot is empty where it says CV = 0x0
Re: Default import function
by choroba (Cardinal) on Oct 24, 2016 at 06:28 UTC
    That's what use does. Compare the output of the following commands:
    ~ $ perl -lwe '{package My; sub x {}} $INC{"My.pm"}=1; +print for keys %{"My::"}' x ~ $ perl -lwe '{package My; sub x {}} $INC{"My.pm"}=1; eval "use My"; +print for keys %{"My::"}' import x

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Hello choroba,

      That's what use does.

      Yes, according to the documentation for use, it calls the import function:

      It is exactly equivalent to
      BEGIN { require Module; Module->import( LIST ); }
      except that Module must be a bareword.

      But how does use call a function that hasn’t been defined anywhere? Are you saying that use itself creates an import function for the target module? (And if so, what are the contents of that function?)

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        I haven't checked the source, but it seems so, don't you think? The created sub seems empty:
        ~ $ perl -lwe '{package My; sub x {}} $INC{"My.pm"}=1; eval "use My"; +use Data::Dumper; $Data::Dumper::Deparse = 1; print Dumper \&$_ for v +alues %{"My::"}' $VAR1 = sub { package My; }; $VAR1 = sub ;;

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Default import function
by dave_the_m (Monsignor) on Oct 24, 2016 at 13:23 UTC
    The "import" method is special-cased. For Foo->import(), when the import method is looked up and not found, a special value (&PL_sv_yes) is returned for the lookup, rather than croaking as would happen for any other method name. Then the subroutine-calling code (pp_entersub) is special-cased to return immediately if the CV passed to it is &PL_sv_yes.

    This is an implementation detail and is subject to change. But it doesn't create an import() sub, nor does it rely on the existence of UNIVERSAL::import.

    Dave.

      Whenever there's a thread like this and the answer is shared by someone who definitively knows, I murmur a chant of gratitude: thank gods that there's someone who does know.

      Whenever someone lifts the lid a crack on the guts of perl, I am truly overwhelmed with gratitude for all the builders and tenders over the years who have made it possible for me to earn a living doing what I love.

      The way forward always starts with a minimal test.
Re: Default import function
by Athanasius (Cardinal) on Oct 25, 2016 at 04:07 UTC

    Thanks to all who replied!

    choroba: Data::Dumper::Deparse looks useful, thanks. I wasn’t aware of it, as I usually use Data::Dump, which doesn’t seem to have an equivalent function (it always prints sub { ... }).

    Anonymous Monk: I thought of UNIVERSAL, but as others have said, that’s not the source of the import in question. As I understand it, UNIVERSAL is inherited by every class, meaning it applies only where there is a blessed reference. In my minimal example, MyMod is a module but not a class.

    salva: Thanks for the source code extract! I think this special case should be documented in the entry for import. Also, good point about distinguishing between import being defined and merely being entered in the symbol table. (But I wonder: since, as you have shown, import is special-cased so that invoking it when it’s not defined is passed over in silence, why is it necessary or useful to add it to the symbol table at all in this case?)

    dave_the_m: Thanks for the explanation!

    1nickt: “I murmur a chant of gratitude” Amen!

    Cheers,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      But I wonder: since, as you have shown, import is special-cased so that invoking it when it’s not defined is passed over in silence, why is it necessary or useful to add it to the symbol table at all in this case?

      That's probably just an implementation detail.

      It is common for symbol table entries to be created whenever the name is referenced. For instance, the following code...

      sub foo { bar(); return $baz }
      adds entries for foo, bar and baz.
      $obj->non_existent_method;
      dies but after creating an entry for non_existent_method.