Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How do I check all the variables, subroutines etc in a Perl Module?

by Anonymous Monk
on Aug 30, 2020 at 21:57 UTC ( [id://11121210]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Respected Monks

Is there some kind of a construct that sort of lets me "peek" inside a Module so that I can see what all variables, functions and other stuff provided in a Module?

Just as an oversimplified example, when I came across the %ENV Hash, I knew I could do this to see what all it offers.

use strict; use warnings; use diagnostics; while ( my($key, $value) = each %ENV) { print "Key: $key\tValue: $value\n"; }

This prints out everything that the %ENV holds. I know Modules could be far more deeply nested that this, but, hope the example would suffice to demonstrate what I am looking for. I understand that reading the Module documentation will give me a good overview and something that should be done anyways (RTFM), but, just want to know if something like this exists.

  • Comment on How do I check all the variables, subroutines etc in a Perl Module?
  • Download Code

Replies are listed 'Best First'.
Re: How do I check all the variables, subroutines etc in a Perl Module?
by tobyink (Canon) on Aug 30, 2020 at 23:59 UTC
    use strict; use warnings; use diagnostics; use HTTP::Tiny; while ( my($key, $value) = each %HTTP::Tiny::) { print "Key: $key\tValue: $value\n"; }

    (This was my 3000th writeup on PerlMonks.)

Re: How do I check all the variables, subroutines etc in a Perl Module?
by BillKSmith (Monsignor) on Aug 31, 2020 at 00:09 UTC
    Lexical variables are not accessible (and may not even exist) outside their scope. Package variables are stored in a symbol table. The symbol table for module (package) is a hash. The keys are the names you seek. The values are references to typeglobs. It is necessary to dereference the typeglob to find out what the name is used for. The details are beyond my experience. The book "Programming Perl" has a lengthy section name "Symbol Tables". It appears to be what you need.
    Bill
      > (and may not even exist)

      For clarification:

      As soon as a sub is compiled, it's lexical vars will "exist". They'll just be undefined outside their scope.

      DB<23> sub foo { my $bar =42 } DB<24> use PadWalker qw/peek_sub/; DB<25> x peek_sub(\&foo) 0 HASH(0x31ffa38) '$bar' => SCALAR(0x31ff828) -> undef DB<26>

      Peeking into file-scoped lexicals might be harder tho.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: How do I check all the variables, subroutines etc in a Perl Module?
by swl (Parson) on Aug 31, 2020 at 03:26 UTC

    Devel::SymDump has a decent interface for most of what you need.

    Lexical variables are harder, as noted by BillKSmith in 11121213. You can get some details using PadWalker. However, that requires your code to be called by the sub, which means you are usually the person writing it, and should therefore know what the variables are.

    That said, lexical variables are not provided by a module so are probably out of scope for the question anyway.

Re: How do I check all the variables, subroutines etc in a Perl Module?
by Fletch (Bishop) on Aug 31, 2020 at 13:38 UTC

    Not that the how hasn't been answered, but just a side related comment if you're doing this for anything other than maybe pedagogical reasons:

    Unless you're trying to modify the module for some reason you probably shouldn't go monkeying around in a random module's internals and introduce dependencies on things that aren't specified in its documentation. What's documented is the public API and you can (mostly, usually :) count on that interface not changing, or if it does that any changes will (probably) be noted therein the subsequent versions.

    If you go and tie your code to something which isn't documented you're just asking for trouble because you may then be dependent on something the author considers a trivial implementation detail. When they change it out from under you without making any note of it your code's going to be broked and it's going to take effort to figure out why (e.g. you upgrade Foo::Blorp and the module tests all passed, but your code now can't access $Foo::Blorp::vreemflitzel any more).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thank you for the clarity.
Re: How do I check all the variables, subroutines etc in a Perl Module?
by LanX (Saint) on Aug 31, 2020 at 08:00 UTC
Re: How do I check all the variables, subroutines etc in a Perl Module?
by stevieb (Canon) on Aug 31, 2020 at 23:02 UTC
Re: How do I check all the variables, subroutines etc in a Perl Module?
by Anonymous Monk on Aug 31, 2020 at 05:31 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found