Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Trouble Exporting a Function

by Rich36 (Chaplain)
on Mar 13, 2003 at 10:42 UTC ( #242715=perlquestion: print w/replies, xml ) Need Help??

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

NOTE: Author determined that this was not actually a problem. Please see this node

I'm working on a module for a project that I'm involved in - nothing big, just a few common subroutines that need to be called by a variety of applications. The problem that I'm having is that I can't seem to export the subroutines to be used by the application. In the the code 'modtest.pl' below, it picks up the version number out of the module without any problem. However, when I try to access the subroutine testMod(), I get an error message stating

Undefined subroutine &main::testMod called at C:\path\path\modtest.pl line
	17 (#1)
    (F) The subroutine indicated hasn't been defined, or if it was, it has
    since been undefined.

I've checked perlmod and a couple sites here, but with no luck. It looks like it should work, but doesn't. I've tried with and without the begin blocks - 'require Exporter' vs. 'use Exporter'... still no luck. I'm running Perl 5.6.1 on Win32. Any help, suggestions, or information would be very much appreciated.


«Rich36»
#!/usr/bin/perl # modtest.pl ####################################################### # Setup variables and packages ####################################################### use strict; use warnings; use diagnostics; use lib './lib'; use CART::Validate; ####################################################### # MAIN ####################################################### print qq(The module version is $CART::Validate::VERSION\n); my $testing = testMod(); print qq(The return value from testMod is $testing\n); exit;
# CART::Validate package CART::Validate; BEGIN { use Exporter; our( @ISA, @EXPORT, @EXPORT_OK, $VERSION ); @ISA = qw( Exporter ); @EXPORT = qw( &testMod ); @EXPORT_OK = qw(); $VERSION = 1.00; } sub testMod { return "HELLO!"; } 1;

Replies are listed 'Best First'.
Re: Trouble Exporting a Function
by broquaint (Abbot) on Mar 13, 2003 at 10:51 UTC
    If that's the actual file then you need to change the use CART::Validate line to CART::Validate->import() e.g
    use strict; use warnings; use diagnostics; use lib './lib'; ## was: use CART::Validate CART::Validate->import(); print qq(The module version is $CART::Validate::VERSION\n); my $testing = testMod(); print qq(The return value from testMod is $testing\n); exit; # CART::Validate package CART::Validate; BEGIN { use Exporter; our( @ISA, @EXPORT, @EXPORT_OK, $VERSION ); @ISA = qw( Exporter ); @EXPORT = qw( &testMod ); @EXPORT_OK = qw(); $VERSION = 1.00; } sub testMod { return "HELLO!"; } 1; __output__ The module version is 1 The return value from testMod is HELLO!
    However you should really put everything below (and including) the line package CART::Validate into a file of it's own (i.e Cart/Validate.pm in one of the paths in @INC) and then your use statement will work fine.
    HTH

    _________
    broquaint

      I'm sorry, but that's not related to the error he gets (and neither is his snippet).


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        I'm sorry, but that's not related to the error he gets (and neither is his snippet).
        His error is
        Undefined subroutine &main::testMod called at C:\path\path\modtest.pl +line
        His problem is that testMod() is not being imported into main. Calling import() explicitly does this when CART::Validate is in the same file. Now if that is module code is separated into a file of it's own then it'll work as expected when useed() as the import call did the job the first time thanks to Exporter code.

        Or have I totally misread the question, the error and the code presented?
        HTH

        _________
        broquaint

      However you should really put everything below (and including) the line package CART::Validate into a file of it's own (i.e Cart/Validate.pm in one of the paths in @INC) and then your use statement will work fine.

      That's actually what I've done and what's causing me problems. The code for CART::Validate is in a separate file. Sorry for the confusion.


      «Rich36»
Re: Trouble Exporting a Function (I'm an idiot)
by Rich36 (Chaplain) on Mar 13, 2003 at 13:42 UTC

    Ugh. I just realized that the reason that it wasn't working is that I had the wrong version of the module up in my editor. While I was frantically making changes to the module trying to get it to work, I was changing the one under source code control instead of the one in the development area - the one in the development area had a different name for the sub.

    I am REALLY sorry for those who wasted their time answering my question.

    Would someone be so kind as to remove this whole thread and the evidence of my idiocy?

    (I guess the only comfort that I can take from this is that I learned about the import() function)
    «Rich36»

Re: Trouble Exporting a Function
by PodMaster (Abbot) on Mar 13, 2003 at 11:34 UTC
    broquaint brings up an interesting point (in a /msg), is there a CART/Validate.pm in your @INC? Why are you inlining the module then? (if you really are inlining it). Try adding `print $INC{'CART/Validate.pm'};' and if you get a value, edit that file ... also, see what the use function is all about (`perldoc -f use')


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      I'm sorry about that - I guess I should have been more explicit. Cart::Validate is a different file. I've got two files:

      /path/path/modtest.pl
      /path/path/lib/CART/Validate.pm
      

      modtest.pl is pathed correctly to CART::Validate (using "use lib './lib';") to the module since I can get the version number out. Thanks for taking the time to help with this.


      «Rich36»

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-04-13 00:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?