How to get a listing of module functions

Discussion in 'Perl Misc' started by Web Surfer, Jan 29, 2004.

  1. Web Surfer

    Web Surfer Guest

    [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 ?
    Web Surfer, Jan 29, 2004
    #1
    1. Advertising

  2. Web Surfer

    Ben Morrow Guest

    Web Surfer <> wrote:
    > [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

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
    Ben Morrow, Jan 29, 2004
    #2
    1. Advertising

  3. Web Surfer <> writes:

    > [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\\
    Brian McCauley, Jan 29, 2004
    #3
  4. On Thu, 29 Jan 2004 11:13:00 -0600, Web Surfer wrote:

    <snip>
    > 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.
    James Willmore, Jan 30, 2004
    #4
  5. Web Surfer

    Ben Morrow Guest

    wrote:
    > On Thu, 29 Jan 2004 11:13:00 -0600, Web Surfer wrote:
    >
    > <snip>
    > > 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.


    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

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
    Ben Morrow, Jan 30, 2004
    #5
  6. Also sprach Ben Morrow:

    > 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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Jan 30, 2004
    #6
  7. On Fri, 30 Jan 2004 09:56:24 +0000, Ben Morrow wrote:


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

    <snip>

    >> 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.


    <snip>

    >> Or, you just use a method call like ...
    >>
    >> Foo::method();
    >> Bar::method();

    >
    > 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"
    James Willmore, Jan 30, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Sparks

    Listing functions in a file IN ORDER

    Ian Sparks, Jun 29, 2004, in forum: Python
    Replies:
    6
    Views:
    289
    Scott David Daniels
    Jul 2, 2004
  2. ohms377
    Replies:
    3
    Views:
    298
    Cameron Laird
    May 11, 2005
  3. Replies:
    1
    Views:
    667
    Tim Roberts
    Sep 9, 2006
  4. Replies:
    0
    Views:
    286
  5. NickGB
    Replies:
    0
    Views:
    260
    NickGB
    May 7, 2010
Loading...

Share This Page