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.


In reply to Re: Can a Perl module "inspect" itself and provide details about methods and parameters? by stevieb
in thread Can a Perl module "inspect" itself and provide details about methods and parameters? by Br'er Rabbit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.