in reply to Can a Perl module "inspect" itself and provide details about methods and parameters?
I think it'd be a futile effort to try to sort out what params each subroutine accepts because they can come in and be handled in a myriad of ways.
However, using my Devel::Examine::Subs, you can at least get info about subroutines within a file, module or directory.
Here's a code snip. In this case, I'm in a distribution directory (my Mock::Sub repository):
use warnings; use strict; use Devel::Examine::Subs; my $des = Devel::Examine::Subs->new(file => '.'); my $data = $des->all; for my $file (keys %$data){ print "$file:\n"; for my $sub (@{ $data->{$file} }){ print "\t$sub\n"; } }
Output:
lib/Mock/Sub/Child.pm: new _mock remock unmock called called_count called_with name reset return_value side_effect _check_side_effect mocked_state _no_warn DESTROY _end lib/Mock/Sub.pm: import new mock mocked_subs mocked_objects mocked_state DESTROY __end
You can even get info about each sub. This example is reading just a single file (but it does work with directories and modules, it's just a bit different to extract the data. Docs contain this info.
use warnings; use strict; use feature 'say'; use Devel::Examine::Subs; my $des = Devel::Examine::Subs->new(file => 'lib/Mock/Sub.pm'); my $subs = $des->objects; for my $sub (@$subs){ say $sub->name; say "\tstart line: ". $sub->start; say "\tend line: " . $sub->end; say "\tline count: " . $sub->line_count; # the following would list out the entire code block # for the sub. Commented out for output brevity #say "\tsub code:"; #say "\t\t$_" for @{ $sub->code }; }
Snip of output:
import start line: 14 end line: 17 line count: 4 mocked_objects start line: 74 end line: 82 line count: 9 new start line: 18 end line: 26 line count: 9 DESTROY start line: 100 end line: 101 line count: 2 __end start line: 102 end line: 102 line count: 1 mocked_state start line: 83 end line: 99 line count: 17 mocked_subs start line: 62 end line: 73 line count: 12 mock start line: 27 end line: 61
Works on in-memory modules as well. Send the module name to the file parameter. If the module is installed, we'll load it and read from there:
use warnings; use strict; use Devel::Examine::Subs; my $des = Devel::Examine::Subs->new(file => 'Logging::Simple'); my $subs = $des->all; print "$_\n" for @$subs;
Output from my Logging::Simple distribution module:
BEGIN _sub_names new level file name timestamp levels labels display print child custom_display fatal _generate_entry _levels _log_only
Note that the _end() methods are simply placeholders; they allow my editor to fold all of my POD up into the last subroutine so when I go to bottom of the current file, it doesn't take me to the end of my POD.
You could also use PPI to do the same thing. That's what my distribution uses, and added a whole slew of things around it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Can a Perl module "inspect" itself and provide details about methods and parameters?
by Br'er Rabbit (Novice) on Sep 21, 2019 at 22:19 UTC | |
by stevieb (Canon) on Sep 21, 2019 at 22:42 UTC | |
by Your Mother (Archbishop) on Sep 21, 2019 at 22:51 UTC | |
by Br'er Rabbit (Novice) on Sep 22, 2019 at 12:46 UTC |