perlperl-module

List all the subroutine names in perl program


I am using more modules in my perl program. example:

use File::copy;

so likewise File module contains Basename, Path, stat and etc.. i want to list all the subroutine(function) names which is in File Package module.

In python has dir(modulename) It list all the function that used in that module.... example: #!/usr/bin/python

# Import built-in module math
import math

content = dir(math)

print content

Like python tell any code for in perl


Solution

  • If you want to look at the contents of a namespace in perl, you can use %modulename::.

    For main that's either %main:: or %::.

    E.g.:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    sub fish {};
    sub carrot {};
    
    print "Stuff defined in Dumper:\n"; 
    print Dumper \%Data::Dumper::;
    
    print "Stuff defined:\n";
    print Dumper \%::;
    

    That covers a load of stuff though - including pragmas. But you can check for e.g. subroutines by simply testing it for being a code reference.

    foreach my $thing ( keys %:: ) {
       if ( defined &$thing ) { 
            print "sub $thing\n";
       }
    }
    

    And with reference to the above sample, this prints:

    sub Dumper
    sub carrot
    sub fish
    

    So with reference to your original question:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    use File::Copy;
    
    print "File::Copy has subs of:\n";
    foreach my $thing ( keys %File::Copy:: ) {
       if ( defined &$thing ) { 
            print "sub $thing\n";
       }
    }
    

    Unfortunately you can't do the same thing with the whole File:: namespace, because there's a whole bunch of different modules that could be installed/loaded, but might not be.

    You'd have to use e.g. CPAN to check that -

    perl -MCPAN -e shell
    i /^File::/
    

    Which will list you around 717 modules that are grouped into the File:: tree.

    You could look this up on CPAN. Or if you're just after the core modules, then some variant of using Module::CoreList might do what you want.

    Something like this:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Module::CoreList;
    
    foreach my $module ( Module::CoreList->find_modules(qr/^File::/) ) {
        if ( eval { require $module =~ s|::|/|gr . ".pm" } ) {
            print "Module: $module contains\n";
            my $key_str = "\%$module\:\:";
    
            my %stuff = eval $key_str;
            foreach my $thing ( sort keys %stuff ) {
                my $full_sub_path = "$module::$thing";
                if ( eval {"defined &$full_sub_path"} ) {
                    if ( defined &$thing ) {
                        print "$thing <- $full_sub_path imported by default\n";
                    }
                    else {
                        print "\t$full_sub_path might be loadable\n";
                    }
                }
            }
        }
        else {
            print "Module: $module couldn't be loaded\n";
        }
    }
    

    It's a bit messy because you have to eval various bits of it to test if a module is in fact present and loadable at runtime. Oddly enough, File::Spec::VMS wasn't present on my Win32 system. Can't think why.... :).

    Should note - just because you could import a sub from a module (that isn't exported by default) doesn't make it a good idea. By convention, any sub prefixed with an _ is not supposed to be used externally, etc.