List all the subroutine names in perl program
Asked Answered
S

3

5

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

Spitz answered 28/9, 2015 at 10:30 Comment(3)
Does this help you?: What's the best way to discover all subroutines a Perl module has?Assembly
See Devel::Examine::SubsConducive
Re "so likewise File module contains Basename, Path, stat". No, it doesn't There is no File module, and there is no relationship between the File::Basename and File::Path modules.Thalia
S
4

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.

Sisterly answered 28/9, 2015 at 11:10 Comment(3)
You obviously need some lunch soon. :)Farinose
FEED ME SEYMOUR! (yes, it is coming up to lunchtime).Sisterly
Thanks a lot SobriqueSpitz
F
2

My Devel::Examine::Subs module can do this, plus much more. Note that whether it's a method or function is irrelevant, it'll catch both. It works purely on subroutines as found with PPI.

use warnings;
use strict;

use Devel::Examine::Subs;

my $des = Devel::Examine::Subs->new;

my $subs = $des->module(module => 'File::Copy');

for (@$subs){
    print "$_\n";
}

Output:

_move
move
syscopy
carp
mv
_eq
_catname
cp
copy
croak

Or a file/full directory. For all Perl files in a directory (recursively), just pass the dir to file param without a file at the end of the path:

my $des = Devel::Examine::Subs->new(file => '/path/to/file.pm');

my $subs = $des->all;
Fleischman answered 28/9, 2015 at 16:16 Comment(0)
P
1

If you just want to print it use the Data::Dumper module and the following method, CGI used as an example:

use strict;
use warnings;

use CGI;
use Data::Dumper;

my $object = CGI->new();

{
    no strict 'refs';
    print "Instance METHOD IS  " . Dumper( \%{ref ($object)."::" }) ;
}

Also note, it's File::Copy, not File::copy.

Puritanical answered 28/9, 2015 at 11:1 Comment(2)
File::Copy is not object oriented and OP is asking for finding subroutines. However, to get all methods of a class (which is, granted, a package/namespace) it makes a lot more sense to use Data::Printer, which has that built in. p $object will give a nice list of all the methods and attributes it has.Farinose
I really like the look of the Data::Printer module @simbabque... I can see numerous benefits to it over others for specific troubleshooting.Fleischman

© 2022 - 2024 — McMap. All rights reserved.