Hello
Br'er Rabbit,
> Can a Perl module "inspect" itself and provide details about methods..
Yes! You can parse a perl document with PPI two oneliners just to get started:
# a bare package provided as string
perl -MPPI -e "print $_->content for @{ PPI::Document->new(\'package M
+yPack{ sub new{ bless{}, shift} }')->find('PPI::Statement::Sub') }"
#output
sub new{ bless{}, shift}
# inspecting Data::Dump module (getting its path from %INC)
perl -MPPI -MData::Dump -e "print $_->content for @{ PPI::Document->ne
+w( $INC{'Data/Dump.pm'} )->find('PPI::Statement::Sub') }; "
#output
sub dump
{
local %seen;
local %refcnt;
local %require;
local @fixup;
...
You can go further trying to extract arguments received (good luck ;) and maybe you have to learn what PPI PDOM Tree is:
perl -MPPI -MPPI::Dumper -e "PPI::Dumper->new( PPI::Document->new(\'pa
+ckage MyPack{ sub new{my class shift; return bless{},$class} }') )->
+print"
PPI::Document
PPI::Statement::Package
PPI::Token::Word 'package'
PPI::Token::Whitespace ' '
PPI::Token::Word 'MyPack'
PPI::Structure::Block { ... }
PPI::Token::Whitespace ' '
PPI::Statement::Sub
PPI::Token::Word 'sub'
PPI::Token::Whitespace ' '
PPI::Token::Word 'new'
PPI::Structure::Block { ... }
PPI::Statement::Variable
PPI::Token::Word 'my'
PPI::Token::Whitespace ' '
PPI::Token::Word 'class'
PPI::Token::Whitespace ' '
PPI::Token::Word 'shift'
PPI::Token::Structure ';'
PPI::Token::Whitespace ' '
PPI::Statement::Break
PPI::Token::Word 'return'
PPI::Token::Whitespace ' '
PPI::Token::Word 'bless'
PPI::Structure::Constructor { ... }
PPI::Token::Operator ','
PPI::Token::Symbol '$class'
PPI::Token::Whitespace ' '
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.