in reply to import() magic

it creates an import method in package Foo

Not really. While *Foo::import is defined &Foo::import isn't.

Whenever a symbol is seen an entry in the symbol table is created. For instance, if you do
use Data::Dumper; print Dumper(\%Foo::); exit; Foo::bar(); @Foo::baz;
you see that both *Foo::bar and *Foo::baz exists in %Foo::.

Cheers,
-Anomo

Replies are listed 'Best First'.
Re2: import() magic
by ehdonhon (Curate) on May 29, 2002 at 22:13 UTC

    If the function is not being created, then how is it possible to call the function without crashing? This does not crash, for example:

    $x = bless {}, 'Foo'; $x->import( 'whatever' );

      If you read the docs carefully, you'll see that even though import is not a built-in, it is has 'magic' treatment.

      Ovid@OT-D1 ~ $ perl -e '$x=bless{}, "Foo";$x->import;print "Defined" if defined &Fo +o::import' Ovid@OT-D1 ~ $ perl -e 'print "Defined" if defined &UNIVERSAL::isa' Defined

      As you can see from the above snippet, even though we call an import method, none is actually defined in %Foo::. I threw in the %UNIVERSAL:: test so you can see how that test works. Here's proof that it is "special". Note that the call to the non-existent method &Foo::import is successful, but the second call to a non-existent method is not.

      $ perl -e '$x=bless{}, "Foo";$x->import;$x->no_way' Can't locate object method "no_way" via package "Foo" (perhaps you for +got to load "Foo"?) at -e line 1.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.