How to get a listing of module functions

W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

I know that you can get a listing of all the modules that are installed
in your perl installation by doing something like this :

#!/usr/bin/perl -w

use ExtUtils::Installed;

my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();

$total = @modules;
print "$total modules detected\n\n";
print join("\n",@modules),"\n";

exit 0;


But, given this list of modules how can you determine the functions that
are "defined" in any one paticular module ?
 
B

Ben Morrow

Web Surfer said:
[This followup was posted to comp.lang.perl.misc]

I know that you can get a listing of all the modules that are installed
in your perl installation by doing something like this :

#!/usr/bin/perl -w

use ExtUtils::Installed;

my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();

$total = @modules;
print "$total modules detected\n\n";
print join("\n",@modules),"\n";

exit 0;
Unnecessary.

But, given this list of modules how can you determine the functions that
are "defined" in any one paticular module ?

One way is to check the symbol table:

my $module = "Carp";

eval "require $module" or die $@;
{
no strict 'refs';
for (keys %{"${module}::"}) {
print if exists &{"${module}::$_"};
}
}

This won't find functions that are defined in some different package
from the module name.

Ben
 
B

Brian McCauley

Web Surfer said:
[This followup was posted to comp.lang.perl.misc]

I know that you can get a listing of all the modules that are installed
in your perl installation by doing something like this :

#!/usr/bin/perl -w

use ExtUtils::Installed;

my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();

$total = @modules;
print "$total modules detected\n\n";
print join("\n",@modules),"\n";

exit 0;


But, given this list of modules how can you determine the functions that
are "defined" in any one paticular module ?

You look in the documentation. Why do you think that you could want
to know what functions are defined without knowing what they do?

Are you sure you meant to say "functions" not "packages"?

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

James Willmore

On Thu, 29 Jan 2004 11:13:00 -0600, Web Surfer wrote:

But, given this list of modules how can you determine the functions that
are "defined" in any one paticular module ?

Why? Are you looking for something in particular?

After creating a reference to an object, the reference applies to *only*
that module. So, if you have two modules that have a method that's the
same name, only one will be called.

For example:

use Foo;
use Bar;

my $foo = new Foo;
my $bar = new Bar;

$foo->method(); #calls to Foo::method
$bar->method(); #calls to Bar::method

This *may* not apply if you import the methods into your namespace. In
most cases, AFAIK, Perl will let you know if there's a problem. Or, you
just use a method call like ...

Foo::method();
Bar::method();

But I doubt there are too many situations in which you'll have an issue or
have a need to call methods in this manner.

I said all this because I get the impression that you are trying to avoid
using a method name that's already in use by another module.

If that's not the case, the best bet on seeing what methods are available
in a module is to RTM on the module.

Of course, you *could* fashion a script to read each files of the module
(as listed in the packing list) and search for each instance of 'sub' :)
But, that will take a long time and, IMHO, it doesn't have any real payoff
in the end.

HTH and please, someone correct me if I'm wrong.

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
When God endowed human beings with brains, He did not intend to
guarantee them.
 
B

Ben Morrow

On Thu, 29 Jan 2004 11:13:00 -0600, Web Surfer wrote:



Why? Are you looking for something in particular?

After creating a reference to an object, the reference applies to *only*
that module. So, if you have two modules that have a method that's the
same name, only one will be called.

module != package != class.

Modules are not necessarily OO.
For example:

use Foo;
use Bar;

my $foo = new Foo;
my $bar = new Bar;

$foo->method(); #calls to Foo::method
$bar->method(); #calls to Bar::method

This *may* not apply if you import the methods into your namespace.

You cannot import a method. You import a sub into your namespace.
In most cases, AFAIK, Perl will let you know if there's a problem.

Nope. You will get the last to be imported.
Or, you just use a method call like ...

Foo::method();
Bar::method();

This is not a method call, it is a sub call.
If that's not the case, the best bet on seeing what methods are available
in a module is to RTM on the module.

Yes.

Ben
 
T

Tassilo v. Parseval

Also sprach Ben Morrow:
(e-mail address removed) wrote:
After creating a reference to an object, the reference applies to *only*
that module. So, if you have two modules that have a method that's the
same name, only one will be called.
[...]
Or, you just use a method call like ...

Foo::method();
Bar::method();

This is not a method call, it is a sub call.

I haven't followed this thread very closely, but maybe it's a good time
to mention that also methods can be package qualified:

ethan@ethan:~$ perl -l
package p1;
sub test { print __PACKAGE__ }
package p2;
sub test { print __PACKAGE__ }
package main;
my $o = bless {} => "p1";
$o->test;
$o->p2::test;
__END__
p1
p2

As I said, I am not sure how useful this is in the context of this
thread.

Tassilo
 
J

James Willmore

module != package != class.

Modules are not necessarily OO.


You cannot import a method. You import a sub into your namespace.


This is not a method call, it is a sub call.

Okay ... I'll go back and read some more.

I get the impression from your post I'm not alone in mixing terms.

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Mustgo, n.: Any item of food that has been sitting in the
refrigerator so long it has become a science project. --
Sniglets, "Rich Hall & Friends"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top