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

Hi Monks,
I am creating one module and using exporter module. Whole idea is not to export all methods that I have wriiten inside the module.
Want only some of the methods to export in calling programs. To achieve this I am doing like this:
BEGIN { use Exporter (); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); #set the version for version checking; uncomment to use $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw( &getTableDDL &getIndex &getMatchingTableDDL + &getConstraints); %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], # your exported package globals go here, # as well as any optionally exported functions @EXPORT_OK = qw(); $Exporter::Verbose=1 }
When I run the calling program it says like this :
Importing into P3::feed from P3::lib::Dbschema: &getTableDDL &getIndex &getMatchingTableDDL &getConstraints
does this mean that it only exports above methods, not all methods.
Is there any way to test that other subroutines has not been exported.
I removed all methods name from export array then I ran the program,
it says the Importing into P3::lib::Dbschema: does this mean it imports all methods from module ?
Thanks in advance for your valuable advice.
Additon:
i am using this in calling program like this:
my $dbh = $entity->getConnection(); # Grab a connection for general us +e my $obj = Dbschema->new($dbh); my ($val) = $obj->getMatchingTableDDLDbaPortal','Entity%'); ###this method getMatchingTableDDL is get called even if removed from +exporter array in module !!!

Replies are listed 'Best First'.
Re: Using Exporter module
by ikegami (Patriarch) on Jul 18, 2007 at 19:39 UTC

    Not exporting a sub doesn't prevent it from being called. Exporting simply allows you to do sub() instead of doing Module::sub(). Functions which aren't imported can still be called

    • using their fully qualified name (Module::sub()),
    • as a class method (Module->sub()),
    • as an object method ($o->sub()), and
    • using a code ref ($sub->())

    Perl doesn't provide a means of making a function uncallable from outside of a namespace. There are ways of making it harder to call a function, but most programmers are satisfied to signal that a function is for private use by prepending an underscore (_) to its name.

    Update: By the way, it's totally useless to export methods unless they can also be called as functions. It looks to me like you don't need Exporter at all.

      Thanks for reply.
      What is the way to stop exporting all functions in calling program ? is there any way or I am just trying to do things that is not possible.

        Exporting doesn't mean what you think it does, at least not in Perl. To export is to remove the requirement to use a fully qualified name.

        What you really want, it seems, is to make a function uncallable from outside of {something}. (A namespace? Code that originated from a certain file?) Perl doesn't provide any direct means of doing that. Perl considers this a documentation issue. If a user of your module disreguards your documentation and call a function he shouldn't, it's his own foot he's shooting.

        Many programmers prepend an underscore (_) to the name of their private subs/methods as an indication that they shouldn't be called.