4

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

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • 2
    Does this help you?: [What's the best way to discover all subroutines a Perl module has?](http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has) – Eirik Birkeland Sep 28 '15 at 10:42
  • 1
    See [`Devel::Examine::Subs`](https://metacpan.org/pod/Devel::Examine::Subs) – Håkon Hægland Sep 28 '15 at 12:18
  • 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. – ikegami Sep 28 '15 at 15:36

3 Answers3

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.

Sobrique
  • 52,974
  • 7
  • 60
  • 101
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;
stevieb
  • 9,065
  • 3
  • 26
  • 36
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.

Dr.Avalanche
  • 1,944
  • 2
  • 28
  • 37
  • 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](http://p3rl.org/Data::Printer), which has that built in. `p $object` will give a nice list of all the methods and attributes it has. – simbabque Sep 28 '15 at 11:18
  • 1
    I really like the look of the `Data::Printer` module @simbabque... I can see numerous benefits to it over others for specific troubleshooting. – stevieb Sep 28 '15 at 20:21