vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Package

package MYCALC; use Exporter; our @EXPORT = (); our @ISA = qw(Exporter); our @EXPORT_OK = qw(addition multi); our %EXPORT_TAGS = (DEFAULT => [qw(&addition)],Both => [qw(&addition & +multi)]); sub addition { return $_[0] + $_[1]; } sub multi { return $_[0] * $_[1]; } 1;

Program:

use strict; use warnings; my @list = qw (2 2); use Module qw(:DEFAULT); print addition(@list),"\n";

Above coding is my module MYCALC and the program which using this module, I have not exported any function using @EXPORT, but I have used the DEFAULT in %EXPORT_TAGS with the function addition, when I call this function from the main it says the error as,

Undefined subroutine &main::addition called at Module.pl line 25.

Why the addition function not getting called?

Replies are listed 'Best First'.
Re: Module problem
by shmem (Chancellor) on Mar 09, 2009 at 09:30 UTC
    I have not exported any function using @EXPORT, but I have used the DEFAULT in %EXPORT_TAG

    The :DEFAULT tag applies to @EXPORT. From the Exporter pod:

    Advanced features
    Specialised Import Lists If any of the entries in an import list begins with !, : or / then the list is treated as a series of specifica- tions which either add to or delete from the list of names to import. They are processed left to right. Specifica- tions are in the form:
    [!]name This name only [!]:DEFAULT All names in @EXPORT [!]:tag All names in $EXPORT_TAGS{tag} anonymous li +st [!]/pattern/ All names in @EXPORT and @EXPORT_OK which m +atch
    A leading ! indicates that matching names should be deleted from the list of names to import. If the first specification is a deletion it is treated as though preceded by :DEFAULT. If you just want to import extra names in addition to the default set you will still need to include :DEFAULT explicitly.

Re: Module problem
by Bloodnok (Vicar) on Mar 09, 2009 at 11:34 UTC
    ree, the key thing to note in shmems reply is :DEFAULT All names in @EXPORT - since @EXPORT is empty in your code, I'm not be at all surprised you hit the the problem you did.

    A user level that continues to overstate my experience :-))
Re: Module problem
by boom (Scribe) on Mar 10, 2009 at 03:31 UTC

    The DEFAULT tag name is special and is automatically set in our modules %EXPORT_TAGS hash like this DEFAULT=>\@EXPORT.

Re: Module problem
by ig (Vicar) on Mar 10, 2009 at 08:30 UTC
    but I have used the DEFAULT in %EXPORT_TAGS with the function addition...

    To clarify the previous answers slightly: the tag :DEFAULT is special. The import function does not look at $EXPORT_TAGS{DEFAULT} for this tag, so it doesn't matter that you have set this element in %EXPORT_TAGS.

    If you change DEFAULT to MYTAG (or anything other than DEFAULT) in your module and program, then your addition subroutine is imported into your main package.