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

Hello Monks, I am writing a module and would like to export its functions. Looking at some docs it seems relatively straight forward, but I always seems to get an error and cannot seem to figure out why.
Undefined subroutine &main::foo called at h line 10.
Both the module and script are in the same directory.

Module = HELPER.pm
package Helper; use strict; use warnings; use Exporter qw(import); our @ISA = qw(Exporter); our @EXPORT = qw(foo); sub foo { print "foo\n"; } sub bar { print "bar\n"; } 1;
Calling Script test.pl
#!/usr/bin/perl use strict; use warnings; use HELPER; print "In Main\n"; foo();
It does work if I prefix foo with the package name Helper::foo, but I was hoping to avoid that.

any help would be appreciated -- thanks

Replies are listed 'Best First'.
Re: Export function from module
by davido (Cardinal) on Apr 30, 2013 at 02:48 UTC

    The first line of HELPER.pm says: package Helper.

    The 5th line of test.pl is use HELPER;

    Though your filesystem may or may not be case-sensitive, package names are identifiers, and Perl's identifiers are case sensitive. You should be consistent in your use of case. I recommend naming the module Helper.pm, the package, Helper, and then "use Helper;". But if you prefer all caps, make sure your package name is all caps too.


    Dave

      Thanks for the reply Dave... That did the trick, but now I am confused. I though the use HELPER, simply imports packages from a file. Then by my exporting the functions I wish to be available, I simply need to call the function and the functions exported in the package are available to the calling script. What part am I missing? thanks

        use HELPER;: Perl looks for a file named HELPER.pm from among the paths listed in @INC, and it is found. But use HELPER; is the same as saying:

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

        The problem is that second part; you have inherited Exporter into a package named Helper. So when Perl goes looking for HELPER->import() it doesn't find anything. And what does exist, Helper->import() isn't being called, because you're using HELPER, not Helper.

        You may as well be saying:

        BEGIN { require HELPER; Goofy->import(); }

        Additonally, the fully qualified name of foo() isn't HELPER::foo, it's Helper::foo.


        Dave

        Hi AM,
        I want to believe the Anonymous Monk is the OP iang.
        If so, what other errors are you getting?
        Dave have pointed out that explicitly, that the package you used is Helper not HELPER.
        Hope, your module is also saved as Helper.pm not HELPER.pm.

        That been said, don't forget you only "exported" the foo subroutine.
        However, if you still wanted the bar subroutine, then use Helper::bar() in your script.

        UPDATE: Oouch!! Davido, got that answered before me !!! Why am i this slow typing..Ah...
        Nice one, faster fingers.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me