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

Dear monks I have a question where when you say

use Employee ##where Employee is a class defined in a Employee.pm without any export.

Note use by definition is:

BEGIN { require Employee; Employee->import(); }

Now my question is since "import" is not defined wouldn't it cause a compilation error. But use Employee does load properly without any problems. Sorry , correct me if my understanding on the "use" is wrong.

update (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: Loading class
by Paladin (Vicar) on Mar 07, 2003 at 05:54 UTC
    Quoted from the docs on use:
    The "import" is not a builtin--it's just an ordinary static method call into the "Module" package to tell the module to import the list of features back into the current package. The module can implement its "import" method any way it likes, though most modules just choose to derive their "import" method via inheritance from the "Exporter" class that is defined in the "Exporter" module. See Exporter. If no "import" method can be found then the call is skipped.
Re: Loading class
by nite_man (Deacon) on Mar 07, 2003 at 07:50 UTC
    You can include your class into another module of class with using a statement use:
    use Employee;
    or
    use Employee qw( method1 method2 ... methodN );
    This is equal following:
    BEGIN { require "Employee.pm"; import Employee; }
    or
    BEGIN { require "Employee.pm"; import Employee qw( method1 method +2 ... methodN ); }
    Good like.
    *-*-*-*-*-*-* { firstname => 'Michael', quote => 'Mice were crying and stinging but went on nibbling the + cactus', }
Re: Loading class
by jasonk (Parson) on Mar 07, 2003 at 12:54 UTC

    It works because use acts more like this:

    BEGIN { require Employee; Employee->import() if Employee->can("import"); }

    If your module doesn't have an import, or doesn't inherit from something that does, then the call is skipped.

      Perl seems to do some magic with import method call. Even if you don't define it always work:
      $ perl -e 'XXX->xxx' Can't locate object method "xxx" via package "XXX" (perhaps you forgot + to load "XXX"?) at -e line 1. $ perl -e 'XXX->import' # no error

      --
      Ilya Martynov, ilya@iponweb.net
      CTO IPonWEB (UK) Ltd
      Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
      Personal website - http://martynov.org

        Perl seems to do some magic with import method call
        Indeedey, perl does some groovy AUTOLOAD magic at the source level. See. import() magic for more info on the matter.
        HTH

        _________
        broquaint

Re: Loading class
by arturo (Vicar) on Mar 07, 2003 at 14:53 UTC

    Just a note on general OO technique; normally, there's no need for a module that defines a class to export any symbols because you want your objects to be encapsulated: the functionality an object makes available to the programs that use it is typically expected to be accessed through an instance of the object, or, in the case of class methods, by explicity referencing the class method through its fully qualified name. If you find yourself wanting to export a lot of functions or other symbols from a module that defines a class, then it might be time to rethink your design.

    If not P, what? Q maybe?
    "Sidney Morgenbesser"