Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Exporter Module

by anbutechie (Sexton)
on Mar 11, 2009 at 09:51 UTC ( [id://749827]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
Lets take,
Module.pm is
require Exporter; @ISA = qw(Exporter); @EXPORT =qw\fun1 fun2\; @EXPORT_OK = qw\fun3\; @EXPORT_FAIL= qw\fun4\; sub fun1{print "\nfun1";} sub fun2{print "\nfun2";} sub fun3{print "\nfun3";} sub fun4{print "\nfun4";}
Test_Exporter.pl is
use Module; fun1; fun2; fun3; fun4;
Output is
fun1
fun2
fun3
fun4
All the functions are called and executed without any trouble
My question is: What's the use of these statements in module
require Exporter; @ISA = qw(Exporter); @EXPORT =qw\fun1 fun2\; @EXPORT_OK = qw\fun3\; @EXPORT_FAIL= qw\fun4\;
Regards,
Anbarasu

Replies are listed 'Best First'.
Re: Exporter Module
by ELISHEVA (Prior) on Mar 11, 2009 at 09:55 UTC

    @EXPORT, @EXPORT_OK, and @EXPORT_FAIL are described in some detail in the perl documentation: see exporter.

    @ISA is used to store the super classes of a class. For more information, on @ISA see perltoot.

    Briefly, here's how the two fit together:

    • @EXPORT stores the list of subroutines that are exported automatically whenever a user includes the module using use. Exporting lets you use subroutines without the full package qualification: so instead of Toys::Weeble::wobble(...) you can just use wobble(...). However, generally it is not a good idea to export things automatically. It can cause clashes with the names of subroutines in any code that uses your module.
    • @EXPORT_OK stores the list of subroutines that can be optionally exported. Those who want to omit the package qualifier from the subroutine name can request the functions to be exported, by adding parameters to the use statement, like this: use Toys::Weeble qw(wobble wontFallDown);
    • @EXPORT_FAIL are things you don't want exported. If a module tries to export any subroutine in that list, an error will be generated.

    The actual exporting is done by a subroutine named import(...). It processes @EXPORT etc. It also takes the parameters to use and processes them. There is a default definition for it, so unless you want to do something special, you can pretty much forget about it and let the default subroutine do its thing. To get access to the default definition, you need to make your module a subclass of Exporter, hence the @ISA statement.

    Best, beth

    Update added explanation of how @ISA and @EXPORT etc work together.

Re: Exporter Module
by Corion (Patriarch) on Mar 11, 2009 at 09:56 UTC

    As soon as you start using different namespaces through the package keyword, you will want to make function names available in other packages. Exporter is a common way to make function names available in other packages.

Re: Exporter Module
by shmem (Chancellor) on Mar 11, 2009 at 13:48 UTC

    Your Module.pm doesn't set up a separate namespace from which functions may be imported by another package. All your functions are compiled into the caller's namespace:

    Extending your Module.pm functions slightly

    require Exporter; @ISA = qw(Exporter); @EXPORT =qw\fun1 fun2\; @EXPORT_OK = qw\fun3\; @EXPORT_FAIL= qw\fun4\; sub fun1{print "\nfun1 in package " . __PACKAGE__ } sub fun2{print "\nfun2 in package " . __PACKAGE__ } sub fun3{print "\nfun3 in package " . __PACKAGE__ } sub fun4{print "\nfun4 in package " . __PACKAGE__ }

    the script

    use Module; fun1; fun2; fun3; fun4;

    outputs

    fun1 in package main fun2 in package main fun3 in package main fun4 in package main

    Nothing exported, nothing imported. All is in the default package 'main'.

    Use a package statement in your Module.pm:

    package Module; require Exporter; @ISA = qw(Exporter); ...

    Running the script again yields

    fun1 in package Module fun2 in package Module
Re: Exporter Module
by leslie (Pilgrim) on Mar 11, 2009 at 13:19 UTC

    The Exporter module implements an import method which allows a module to export functions and variables to its users' namespaces. Many modules use Exporter rather than implementing their own import method because Exporter provides a highly flexible interface, with an implementation optimised for the common case. Perl automatically calls the import method when processing a use statement for a module. Modules and use are documented in perlfunc and perlmod. Understanding the concept of modules and how the use statement operates is important to understanding the Exporter.

    The arrays @EXPORT and @EXPORT_OK in a module hold lists of symbols that are going to be exported into the users name space by default, or which they can request to be exported, respectively. The symbols can represent functions, scalars, arrays, hashes, or typeglobs. The symbols must be given by full name with the exception that the ampersand in front of a function is optional,
    Eg:
    @EXPORT = qw(afunc $scalar @array); # afunc is a function
    @EXPORT_OK = qw(afunc $scalar @array); # afunc is a function

    Exports pollute the namespace of the module user. If you must export try to use @EXPORT_OK in preference to @EXPORT and avoid short or common symbol names to reduce the risk of name clashes.

    The names of any symbols that cannot be exported should be listed in the @EXPORT_FAIL array. If a module attempts to import any of these symbols the Exporter will give the module an opportunity to handle the situation before generating an error. The Exporter will call an export_fail method with a list of the failed symbols.
    Eg:
    @failed_symbols = $module_name->export_fail(@failed_symbols);

    If the export_fail method returns an empty list then no error is recorded and all the requested symbols are exported. If the returned list is not empty then an error is generated for each symbol and the export fails. The Exporter provides a default export_fail method which simply returns the list unchanged.

    Uses for the export_fail method include giving better error messages for some symbols and performing lazy architectural checks (put more symbols into @EXPORT_FAIL by default and then take them out if someone actually tries to use them and an expensive check shows that they are usable on that platform).

Re: Exporter Module
by jethro (Monsignor) on Mar 11, 2009 at 13:30 UTC

    What is missing from your module code is the package statement. As you have it now, you just include module.pm into the same namespace as the main program, which makes all functions trivially accessible

    Add a "package Module;" as the first line of your Module.pm and it should work as expected.

Re: Exporter Module
by targetsmart (Curate) on Mar 11, 2009 at 10:24 UTC
    According to the code you shown above
    use Module; fun1; fun2; fun3; fun4;
    perl will say error on fun3, that 'undefined subroutine called', because it is coded in EXPORT_OK, so how you have got the output?
    fun1 fun2 fun3 fun4

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

Log In?
Username:
Password:

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

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

    No recent polls found