use module; run subroutine 
Author Message
 use module; run subroutine

Hello,

I have a module /project/Archive/PassServer/Generic.pm defined
as follows:
#------------------------------------
package Generic;
use strict;


$VERSION = 0.01;


sub hello()
{
    print "This is a Perl module.\n";

Quote:
}

1;
#------------------------------------

I can run this module from the directory /project/Archive/PassServer/
using the program hello.pl, which is defined like this:
#------------------------------------
#!/usr/bin/perl
use Generic;
hello();
#------------------------------------

The output is ``This is a Perl module.'' which means that the module
is correct.

I have another program, /project/archive.pl within which i'm trying to
load the same module, Archive/PassServer/Generic.pm, and run hello()
by issuing:
#------------------------------------

Archive::PassServer::Generic::hello();
#------------------------------------

The output is:
Undefined subroutine &Archive::PassServer::Generic::hello called at ./archive.pl line 125, <STDIN> chunk 3.

Can anyone please suggest how I should load the module
so that I can execute the subroutine defined in it (ie. hello()) ?

Thank you for your time.



Mon, 17 May 2004 19:35:25 GMT  
 use module; run subroutine

  AB> I have a module /project/Archive/PassServer/Generic.pm defined
  AB> as follows:
  AB> #------------------------------------
  AB> package Generic;
  AB> use strict;


  AB> $VERSION = 0.01;

  AB> require Exporter;

simpler to just say:

        use base 'Exporter' ;

and then you can lose those vars in use vars as well.



  AB> Archive::PassServer::Generic::hello();

that is not the package name in the module. module file locations and
package name spaces are not related in any way (except that a use
command will call import in that package).

the correct name of that sub is Generic::hello();

so either change the call or the package name to be the fully qualified
name.

uri

--

-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Mon, 17 May 2004 19:50:48 GMT  
 use module; run subroutine

Quote:


>   AB> eval qw/use Archive::PassServer::Generic/ or <...snip...>
>   AB> Archive::PassServer::Generic::hello();

> that is not the package name in the module. module file locations and
> package name spaces are not related in any way (except that a use
> command will call import in that package).

> the correct name of that sub is Generic::hello();

> so either change the call or the package name to be the fully qualified
> name.

I changed the package name to ``Archive::PassServer::Generic'' and
thereafter the call to subroutine Archive::PassServer::Generic::hello()
worked.  So thanks for your help with that.  But another problem emerged.

I have other modules that define similar subroutines of the same name.

So for example, what I need to be able to do is something like this:

    use Archive::PassServer::Generic;
    use Archive::FileServer::Generic;
    use Archive::MailServer::Generic;

    Archive::PassServer::Generic::hello();  # print "Hello PassServer.\n";
    Archive::FileServer::Generic::hello();  # print "Hello FileServer.\n";
    Archive::MailServer::Generic::hello();  # print "Hello MailServer.\n";

I eventually get the expected output:

Hello PassServer.
Hello FileServer.
Hello MailServer.

but first, the following is what I see after `perl -w archive.pl`

Subroutine hello redefined at /usr/libdata/perl/5.00503/Exporter.pm line 157
        Exporter::export('Archive::FileServer::Generic', 'main') called at /usr/libdata/perl/5.00503/Exporter.pm line 182
        Exporter::import('Archive::FileServer::Generic') called at ./archive.pl line 124
        main::BEGIN() called at /usr/libdata/perl/5.00503/Carp.pm line 124
        eval {...} called at /usr/libdata/perl/5.00503/Carp.pm line 124
Subroutine hello redefined at /usr/libdata/perl/5.00503/Exporter.pm line 157
        Exporter::export('Archive::MailServer::Generic', 'main') called at /usr/libdata/perl/5.00503/Exporter.pm line 182
        Exporter::import('Archive::MailServer::Generic') called at ./archive.pl line 125
        main::BEGIN() called at Archive/MailServer/Generic.pm line 125
        eval {...} called at Archive/MailServer/Generic.pm line 125

I realize that in some sense there I'm redefining the subroutine hello(),
but I figure that since I'm referring to the module names and hello()
subroutines using what seems like absolute notation, I don't understand
why there is a conflict.

I have no idea how to resolve this error message, so if you can give
me some help I would greatly appreciate it.



Mon, 17 May 2004 21:07:09 GMT  
 use module; run subroutine

  AB> I changed the package name to ``Archive::PassServer::Generic'' and
  AB> thereafter the call to subroutine Archive::PassServer::Generic::hello()
  AB> worked.  So thanks for your help with that.  But another problem emerged.

  AB> I have other modules that define similar subroutines of the same name.

so don't do that! :)

  AB> So for example, what I need to be able to do is something like this:

  AB>     use Archive::PassServer::Generic;
  AB>     use Archive::FileServer::Generic;
  AB>     use Archive::MailServer::Generic;

  AB>     Archive::PassServer::Generic::hello();  # print "Hello PassServer.\n";
  AB>     Archive::FileServer::Generic::hello();  # print "Hello FileServer.\n";
  AB>     Archive::MailServer::Generic::hello();  # print "Hello MailServer.\n";

  AB> I eventually get the expected output:

  AB> Hello PassServer.
  AB> Hello FileServer.
  AB> Hello MailServer.

  AB> Subroutine hello redefined at /usr/libdata/perl/5.00503/Exporter.pm line 157
  AB>         Exporter::export('Archive::FileServer::Generic', 'main') called at /usr/libdata/perl/5.00503/Exporter.pm line 182

  AB> I realize that in some sense there I'm redefining the subroutine hello(),
  AB> but I figure that since I'm referring to the module names and hello()
  AB> subroutines using what seems like absolute notation, I don't understand
  AB> why there is a conflict.


explicitly import it in a use call, or explicitly don't import anything
by passing in a null list to use:

use Archive::MailServer::Generic () ;

you have set up the conflict so you have to figure out a way to stop
it. perl can't figure out which one you want so it warns you about the
redefines. either control your exporting or importing or don't do any
and call the functions by their fully qualified names. another solution
is to go OO with those modules and then you can have methods in
different classes with the same names. and with OO you don't
export/import anything. given the brief name space snippets above, OO
might be a good solution for you.

uri

--

-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Mon, 17 May 2004 21:33:36 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Own module: how to export constants/subroutines of other modules

2. Sending function name to subroutine then run...

3. running a subroutine as an independent process?

4. running a subroutine as a different user

5. Running subroutines from arrays?

6. Run subroutines from another CGI file?

7. Running out of memory in recursive subroutine

8. Tk::Radiobutton invoke doesn't seem to run callback subroutine

9. Using Image::Size.pm on system where I can't install modules using make

10. Redefined subroutines using mod_perl

11. Using eval() for subroutine unwind

12. performance gain by using subroutine prototypes?

 

 
Powered by phpBB® Forum Software