Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Calling a subroutine located in a module

by Anonymous Monk
on Nov 02, 2001 at 04:06 UTC ( #122724=perlquestion: print w/replies, xml ) Need Help??

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

OK, I am stumped. What's new!

I am working on a small module(my first but not last) of my own brewing. And I have generated a test script to test the module. I have a which calls a module which is exporting a subroutine OpenNotes. The problem is as follows.
"my $notesreturn = OpenNotes($var1, $var2);"

This fails for

Undefined subroutine &main::OpenNotes called at C:\My \ line 31.

Now if I remove the "my $notesreturn =" And just have OpenNotes($var1,$var2);

I don't get an error until latter in the script when $notesreturn is needed.

Anyone have a good ideas.
  • Comment on Calling a subroutine located in a module

Replies are listed 'Best First'.
Re: Calling a subroutine located in a module
by runrig (Abbot) on Nov 02, 2001 at 04:10 UTC
    I don't suppose you Use strict and warnings (in the script and the module)?? I assume that because otherwise you would immediately get a compile time error about the undeclared variable (or is that the error you're talking about??). The subroutine error is a run-time error.

    Might need to see more code, it doesn't sound like the function is being exported. Is the function in the @EXPORT or @EXPORT_OK list in the module? Is Exporter used in the module? Do you say 'use Module qw(function);' in the script if its in EXPORT_OK, or just 'use Module;' if its in @EXPORT? Is Exporter in the @ISA list in the module?

    Update: The 'undeclared' error I was talking about is about when you said "I tried removing the my $opendir", and you said that you get an error about it being used later...

    Anyway, as others have said, did you forget the package declaration in the module ('package NotesOLE;' if that's the name of the module, and the filename is '')? Is the module 'NotesOLE'? If not, you need to use it in the main script.

      I use strict and warnings on both, is there a problem with that? I am not sure what undeclared variable you are talking about? $var1 and $var2 are declared earlier in the script and $openreturn is declared inline.


      Module NOTES

      use strict;

      use warnings;

      use Win32::OLE;

      use Win32::OLE::Variant;

      use Date::Manip;

      use base 'Exporter';

      use vars qw($VERSION @EXPORT @EXPORT_OK);

      ($VERSION) = ' $Revision: 0.1 $ ' =~ /\$Revision:\s+(^\s+)/;

      @EXPORT = qw( OpenNotes CloseNotes FetchNotesDoc Search FTSearch ); # Exported by Default

      @EXPORT_OK = qw(); # Exported if Reference

      rest of code


      Test Script

      use strict;

      use warnings;

      use Win32::OLE;

      use Win32::OLE::Variant;

      use NotesOLE;

      Bunch of variable stuff

      my $openreturn = OpenNotes($dbname,$server);

      Rest of code.


      It is my understanding that if you use base 'Exporter'; you don't have to declare @ISA in vars, I tried both and had no success!

      Tried Export(..) with use Module and Export_ok() with use Module qw(sub);

      Thanks for the response.

      </BODY> </HTML>
        From the code you posted, it looks like you may have forgotten the package declaration in your module. You should add a package NotesOLE; at the top of your module.

        Curiously, although the missing package declaration would prevent perl from calling the module's import method, it should also cause all of the module's methods to be declared in package main in the first place. So, there may be something else going on to cause the Undefined subroutine &main::OpenNotes called ... error. Do you have another module which loads NotesOLE before does?

        Anyway, put in the package declaration and see what happens.

        Hmm. Maybe just a copy-and-paste thing, but in the first part you say your module is named "Notes", but in the sample of it says "use NotesOLE". Check for typos in the package and file names (although strict should have caught things like that). Otherwise I don't see anything obviously wrong with your code.


        May be it is out of the thread but I must say a word.

        Be carefull with base pragma, it will only require your base modules, not use them! So import subroutines of the base modules will be ignored.

        A spot from documentation:

        ----------- package Baz; use base qw(Foo Bar);
        Roughly similar in effect to
        BEGIN { require Foo; require Bar; push @ISA, qw(Foo Bar); } ----------

        An example:

        ---------- ### File package A; sub import { warn "A imported"; } ---------- ### File package X; use base qw(A); sub import { warn "X imported"; } ---------- ### File package Y; use A; use base qw(A); sub import { warn "Y imported"; } ----------- ### File package Z; use A; @ISA = qw(A); sub import { warn "Z imported"; } ----------

        And now:

        bash$ perl -e 'use X' import X at line 7. bash$ perl -e 'use Y' import A at line 5. import Y at line 8. bash$ perl -e 'use Z' import A at line 5. import Z at line 9.

        You can see base module A is not imported in the first case.

        -- brother ab
Re: Calling a subroutine located in a module
by Rich36 (Chaplain) on Nov 02, 2001 at 04:18 UTC
    If you're not working with a subroutine within the package that you are in (in this case "main"), you have to define which package the subroutine is in (i.e. the module).
    my $notesreturn = &ModuleName::OpenNotes($var1, $var2);"

    ... where "ModuleName" is the name of the module that you've created. Also, make sure that your module is loaded up correctly in @INC.

    There's more than one way to screw it up...

      Tried this and it didn't help. The test script is in the same directory as the module and it caught some errors in the module when it first loaded. So I think that the module is loading fine.
Re: Calling a subroutine located in a module
by Anonymous Monk on Nov 02, 2001 at 09:38 UTC
    Thank you all for your help!!!

    Thanks to Chipmunk, I discovered the problem. The original module is a sub-module i.e. DB::NotesOLE So the package command consisted of "package DB::NotesOLE" The test script is in the same directory as the module itself and was calling the module directly, so I changed the package to "package NotesOLE" and everything worked well. I will have to check that use DB::NotesOLE works in the test script and all will be fine.

    One again thanks I really appreciate all the support.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2021-12-01 18:10 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (13 votes). Check out past polls.