Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Can a Perl module "inspect" itself and provide details about methods and parameters? -- oneliner

by Discipulus (Canon)
on Sep 22, 2019 at 16:13 UTC ( [id://11106547]=note: print w/replies, xml ) Need Help??


in reply to Can a Perl module "inspect" itself and provide details about methods and parameters?

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.
  • Comment on Re: Can a Perl module "inspect" itself and provide details about methods and parameters? -- oneliner
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106547]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found